Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow/unfollow thread backend #4431

Merged
merged 7 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Implement tipping messages ([#4420](https://github.com/open-chat-labs/open-chat/pull/4420))
- Implement notifications for message tips ([#4427](https://github.com/open-chat-labs/open-chat/pull/4427))
- Implement follow/unfollow thread ([#4431](https://github.com/open-chat-labs/open-chat/pull/4431))

### Changed

Expand Down
34 changes: 34 additions & 0 deletions backend/canisters/community/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,38 @@ type UpdateUserGroupResponse = variant {
UserSuspended;
};

type FollowThreadArgs = record {
channel_id : ChannelId;
thread_root_message_index : MessageIndex;
};

type FollowThreadResponse = variant {
Success;
AlreadyFollowing;
ThreadNotFound;
ChannelNotFound;
UserNotInChannel;
UserNotInCommunity;
UserSuspended;
CommunityFrozen;
};

type UnfollowThreadArgs = record {
channel_id : ChannelId;
thread_root_message_index : MessageIndex;
};

type UnfollowThreadResponse = variant {
Success;
NotFollowing;
ThreadNotFound;
ChannelNotFound;
UserNotInChannel;
UserNotInCommunity;
UserSuspended;
CommunityFrozen;
};

service : {
channel_summary : (ChannelSummaryArgs) -> (ChannelSummaryResponse) query;
channel_summary_updates : (ChannelSummaryUpdatesArgs) -> (ChannelSummaryUpdatesResponse) query;
Expand Down Expand Up @@ -944,4 +976,6 @@ service : {
update_channel : (UpdateChannelArgs) -> (UpdateChannelResponse);
update_community : (UpdateCommunityArgs) -> (UpdateCommunityResponse);
update_user_group : (UpdateUserGroupArgs) -> (UpdateUserGroupResponse);
follow_thread : (FollowThreadArgs) -> (FollowThreadResponse);
unfollow_thread : (UnfollowThreadArgs) -> (UnfollowThreadResponse);
};
2 changes: 2 additions & 0 deletions backend/canisters/community/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn main() {
generate_candid_method!(community, disable_invite_code, update);
generate_candid_method!(community, edit_message, update);
generate_candid_method!(community, enable_invite_code, update);
generate_candid_method!(community, follow_thread, update);
generate_candid_method!(community, import_group, update);
generate_candid_method!(community, leave_channel, update);
generate_candid_method!(community, pin_message, update);
Expand All @@ -53,6 +54,7 @@ fn main() {
generate_candid_method!(community, toggle_mute_notifications, update);
generate_candid_method!(community, unblock_user, update);
generate_candid_method!(community, undelete_messages, update);
generate_candid_method!(community, unfollow_thread, update);
generate_candid_method!(community, unpin_message, update);
generate_candid_method!(community, update_channel, update);
generate_candid_method!(community, update_community, update);
Expand Down
21 changes: 21 additions & 0 deletions backend/canisters/community/api/src/updates/follow_thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{ChannelId, MessageIndex};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub channel_id: ChannelId,
pub thread_root_message_index: MessageIndex,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
AlreadyFollowing,
ThreadNotFound,
ChannelNotFound,
UserNotInChannel,
UserNotInCommunity,
UserSuspended,
CommunityFrozen,
}
2 changes: 2 additions & 0 deletions backend/canisters/community/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod delete_user_groups;
pub mod disable_invite_code;
pub mod edit_message;
pub mod enable_invite_code;
pub mod follow_thread;
pub mod import_group;
pub mod leave_channel;
pub mod pin_message;
Expand All @@ -41,6 +42,7 @@ pub mod set_member_display_name;
pub mod toggle_mute_notifications;
pub mod unblock_user;
pub mod undelete_messages;
pub mod unfollow_thread;
pub mod unpin_message;
pub mod update_channel;
pub mod update_community;
Expand Down
21 changes: 21 additions & 0 deletions backend/canisters/community/api/src/updates/unfollow_thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{ChannelId, MessageIndex};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub channel_id: ChannelId,
pub thread_root_message_index: MessageIndex,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
NotFollowing,
ThreadNotFound,
ChannelNotFound,
UserNotInCommunity,
UserNotInChannel,
UserSuspended,
CommunityFrozen,
}
43 changes: 43 additions & 0 deletions backend/canisters/community/impl/src/updates/follow_thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::{activity_notifications::handle_activity_notification, mutate_state, run_regular_jobs, RuntimeState};
use canister_tracing_macros::trace;
use community_canister::follow_thread::{Response::*, *};
use group_chat_core::FollowThreadResult;
use ic_cdk_macros::update;

#[update]
#[trace]
fn follow_thread(args: Args) -> Response {
run_regular_jobs();

mutate_state(|state| follow_thread_impl(args, state))
}

fn follow_thread_impl(args: Args, state: &mut RuntimeState) -> Response {
if state.data.is_frozen() {
return CommunityFrozen;
}

let caller = state.env.caller();
let now = state.env.now();

let user_id = match state.data.members.get(caller) {
Some(member) if member.suspended.value => return UserSuspended,
Some(member) => member.user_id,
None => return UserNotInCommunity,
};

if let Some(channel) = state.data.channels.get_mut(&args.channel_id) {
match channel.chat.follow_thread(user_id, args.thread_root_message_index, now) {
FollowThreadResult::Success => {
handle_activity_notification(state);
megrogan marked this conversation as resolved.
Show resolved Hide resolved
Success
}
FollowThreadResult::AlreadyFollowing => AlreadyFollowing,
FollowThreadResult::ThreadNotFound => ThreadNotFound,
FollowThreadResult::UserNotInGroup => UserNotInChannel,
FollowThreadResult::UserSuspended => UserSuspended,
}
} else {
ChannelNotFound
}
}
2 changes: 2 additions & 0 deletions backend/canisters/community/impl/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod delete_user_groups;
pub mod disable_invite_code;
pub mod edit_message;
pub mod enable_invite_code;
pub mod follow_thread;
pub mod import_group;
pub mod leave_channel;
pub mod pin_message;
Expand All @@ -37,6 +38,7 @@ pub mod set_member_display_name;
pub mod toggle_mute_notifications;
pub mod unblock_user;
pub mod undelete_messages;
pub mod unfollow_thread;
pub mod update_channel;
pub mod update_community;
pub mod update_user_group;
Expand Down
43 changes: 43 additions & 0 deletions backend/canisters/community/impl/src/updates/unfollow_thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::{activity_notifications::handle_activity_notification, mutate_state, run_regular_jobs, RuntimeState};
use canister_tracing_macros::trace;
use community_canister::unfollow_thread::{Response::*, *};
use group_chat_core::UnfollowThreadResult;
use ic_cdk_macros::update;

#[update]
#[trace]
fn unfollow_thread(args: Args) -> Response {
run_regular_jobs();

mutate_state(|state| unfollow_thread_impl(args, state))
}

fn unfollow_thread_impl(args: Args, state: &mut RuntimeState) -> Response {
if state.data.is_frozen() {
return CommunityFrozen;
}

let caller = state.env.caller();
let now = state.env.now();

let user_id = match state.data.members.get(caller) {
Some(member) if member.suspended.value => return UserSuspended,
Some(member) => member.user_id,
None => return UserNotInCommunity,
};

if let Some(channel) = state.data.channels.get_mut(&args.channel_id) {
match channel.chat.unfollow_thread(user_id, args.thread_root_message_index, now) {
UnfollowThreadResult::Success => {
handle_activity_notification(state);
megrogan marked this conversation as resolved.
Show resolved Hide resolved
Success
}
UnfollowThreadResult::NotFollowing => NotFollowing,
UnfollowThreadResult::ThreadNotFound => ThreadNotFound,
UnfollowThreadResult::UserNotInGroup => UserNotInChannel,
UnfollowThreadResult::UserSuspended => UserSuspended,
}
} else {
ChannelNotFound
}
}
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Implement tipping messages ([#4420](https://github.com/open-chat-labs/open-chat/pull/4420))
- Implement notifications for message tips ([#4427](https://github.com/open-chat-labs/open-chat/pull/4427))
- Implement follow/unfollow thread ([#4431](https://github.com/open-chat-labs/open-chat/pull/4431))

### Changed

Expand Down
30 changes: 30 additions & 0 deletions backend/canisters/group/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,34 @@ type ResetInviteCodeResponse = variant {
ChatFrozen;
};

type FollowThreadArgs = record {
hpeebles marked this conversation as resolved.
Show resolved Hide resolved
channel_id : ChannelId;
thread_root_message_index : MessageIndex;
};

type FollowThreadResponse = variant {
Success;
AlreadyFollowing;
ThreadNotFound;
UserNotInGroup;
UserSuspended;
GroupFrozen;
};

type UnfollowThreadArgs = record {
channel_id : ChannelId;
thread_root_message_index : MessageIndex;
};

type UnfollowThreadResponse = variant {
Success;
NotFollowing;
ThreadNotFound;
UserNotInGroup;
UserSuspended;
GroupFrozen;
};

service : {
// Owner only
convert_into_community : (ConvertIntoCommunityArgs) -> (ConvertIntoCommunityResponse);
Expand Down Expand Up @@ -593,6 +621,8 @@ service : {
claim_prize : (ClaimPrizeArgs) -> (ClaimPrizeResponse);
decline_invitation : (EmptyArgs) -> (DeclineInvitationResponse);
toggle_mute_notifications : (ToggleMuteNotificationsArgs) -> (ToggleMuteNotificationsResponse);
follow_thread : (FollowThreadArgs) -> (FollowThreadResponse);
unfollow_thread : (UnfollowThreadArgs) -> (UnfollowThreadResponse);

summary : (SummaryArgs) -> (SummaryResponse) query;
summary_updates : (SummaryUpdatesArgs) -> (SummaryUpdatesResponse) query;
Expand Down
2 changes: 2 additions & 0 deletions backend/canisters/group/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() {
generate_candid_method!(group, disable_invite_code, update);
generate_candid_method!(group, edit_message_v2, update);
generate_candid_method!(group, enable_invite_code, update);
generate_candid_method!(group, follow_thread, update);
generate_candid_method!(group, pin_message_v2, update);
generate_candid_method!(group, register_poll_vote, update);
generate_candid_method!(group, register_proposal_vote, update);
Expand All @@ -39,6 +40,7 @@ fn main() {
generate_candid_method!(group, toggle_mute_notifications, update);
generate_candid_method!(group, unblock_user, update);
generate_candid_method!(group, undelete_messages, update);
generate_candid_method!(group, unfollow_thread, update);
generate_candid_method!(group, unpin_message, update);
generate_candid_method!(group, update_group_v2, update);

Expand Down
19 changes: 19 additions & 0 deletions backend/canisters/group/api/src/updates/follow_thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{ChannelId, MessageIndex};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub channel_id: ChannelId,
pub thread_root_message_index: MessageIndex,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
AlreadyFollowing,
ThreadNotFound,
UserNotInGroup,
UserSuspended,
GroupFrozen,
}
2 changes: 2 additions & 0 deletions backend/canisters/group/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod delete_messages;
pub mod disable_invite_code;
pub mod edit_message_v2;
pub mod enable_invite_code;
pub mod follow_thread;
pub mod pin_message_v2;
pub mod register_poll_vote;
pub mod register_proposal_vote;
Expand All @@ -34,5 +35,6 @@ pub mod send_message_v2;
pub mod toggle_mute_notifications;
pub mod unblock_user;
pub mod undelete_messages;
pub mod unfollow_thread;
pub mod unpin_message;
pub mod update_group_v2;
19 changes: 19 additions & 0 deletions backend/canisters/group/api/src/updates/unfollow_thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{ChannelId, MessageIndex};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub channel_id: ChannelId,
pub thread_root_message_index: MessageIndex,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
NotFollowing,
ThreadNotFound,
UserNotInGroup,
UserSuspended,
GroupFrozen,
}
1 change: 1 addition & 0 deletions backend/canisters/group/impl/src/new_joiner_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn send_reward_transferred_message(user_id: UserId, transfer: nns::CompletedCryp
transfer: CryptoTransaction::Completed(CompletedCryptoTransaction::NNS(transfer)),
caption: None,
}),
mentioned: Vec::new(),
replies_to: None,
forwarded: false,
correlation_id: 0,
Expand Down
Loading
Loading