Skip to content

Commit

Permalink
Notify community canisters when a user is unsuspended (#5227)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jan 22, 2024
1 parent ccf58af commit 84725d0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
4 changes: 4 additions & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Fixed

- Notify community canisters when a user is unsuspended ([#5227](https://github.com/open-chat-labs/open-chat/pull/5227))

## [[2.0.1007](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1007-user_index)] - 2024-01-16

### Added
Expand Down
22 changes: 18 additions & 4 deletions backend/canisters/user_index/impl/src/updates/unsuspend_user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::guards::caller_is_platform_moderator;
use crate::timer_job_types::{SetUserSuspendedInGroup, TimerJob};
use crate::timer_job_types::{SetUserSuspendedInCommunity, SetUserSuspendedInGroup, TimerJob};
use crate::{mutate_state, read_state, RuntimeState};
use canister_tracing_macros::trace;
use ic_cdk_macros::update;
use types::{ChatId, UserId};
use types::{ChatId, CommunityId, UserId};
use user_index_canister::unsuspend_user::{Response::*, *};

#[update(guard = "caller_is_platform_moderator")]
Expand All @@ -20,14 +20,14 @@ pub(crate) async fn unsuspend_user_impl(user_id: UserId) -> Response {
let c2c_args = user_canister::c2c_set_user_suspended::Args { suspended: false };
match user_canister_c2c_client::c2c_set_user_suspended(user_id.into(), &c2c_args).await {
Ok(user_canister::c2c_set_user_suspended::Response::Success(result)) => {
mutate_state(|state| commit(user_id, result.groups, state));
mutate_state(|state| commit(user_id, result.groups, result.communities, state));
Success
}
Err(error) => InternalError(format!("{error:?}")),
}
}

fn commit(user_id: UserId, groups: Vec<ChatId>, state: &mut RuntimeState) {
fn commit(user_id: UserId, groups: Vec<ChatId>, communities: Vec<CommunityId>, state: &mut RuntimeState) {
let now = state.env.now();
for group in groups {
state.data.timer_jobs.enqueue_job(
Expand All @@ -41,5 +41,19 @@ fn commit(user_id: UserId, groups: Vec<ChatId>, state: &mut RuntimeState) {
now,
);
}

for community in communities {
state.data.timer_jobs.enqueue_job(
TimerJob::SetUserSuspendedInCommunity(SetUserSuspendedInCommunity {
user_id,
community,
suspended: false,
attempt: 0,
}),
now,
now,
);
}

state.data.users.unsuspend_user(user_id, now);
}
60 changes: 55 additions & 5 deletions backend/integration_tests/src/suspend_user_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::env::ENV;
use crate::rng::random_message_id;
use crate::rng::{random_message_id, random_string};
use crate::utils::now_millis;
use crate::{client, CanisterIds, TestEnv, User};
use candid::Principal;
Expand All @@ -26,7 +26,9 @@ fn suspend_user() {
platform_moderator,
} = init_test_data(env, canister_ids, *controller);

let group = client::user::happy_path::create_group(env, &user1, "SUSPEND_USER_TEST", false, false);
let group_id = client::user::happy_path::create_group(env, &user1, &random_string(), false, false);
let community_id = client::user::happy_path::create_community(env, &user1, &random_string(), false, vec![random_string()]);
let channel_id = client::community::happy_path::create_channel(env, user1.principal, community_id, true, random_string());

client::user_index::suspend_user(
env,
Expand Down Expand Up @@ -67,7 +69,7 @@ fn suspend_user() {
let group_message_response1 = client::group::send_message_v2(
env,
user1.principal,
group.into(),
group_id.into(),
&group_canister::send_message_v2::Args {
thread_root_message_index: None,
message_id: random_message_id(),
Expand All @@ -87,6 +89,30 @@ fn suspend_user() {
group_canister::send_message_v2::Response::UserSuspended
));

let community_message_response1 = client::community::send_message(
env,
user1.principal,
community_id.into(),
&community_canister::send_message::Args {
channel_id,
thread_root_message_index: None,
message_id: random_message_id(),
sender_name: user1.username(),
sender_display_name: None,
content: MessageContentInitial::Text(TextContent { text: "123".to_string() }),
replies_to: None,
mentioned: Vec::new(),
forwarding: false,
community_rules_accepted: None,
channel_rules_accepted: None,
message_filter_failed: None,
},
);
assert!(matches!(
community_message_response1,
community_canister::send_message::Response::UserSuspended
));

client::user_index::unsuspend_user(
env,
platform_moderator.principal,
Expand Down Expand Up @@ -122,7 +148,7 @@ fn suspend_user() {
let group_message_response2 = client::group::send_message_v2(
env,
user1.principal,
group.into(),
group_id.into(),
&group_canister::send_message_v2::Args {
thread_root_message_index: None,
message_id: random_message_id(),
Expand All @@ -141,6 +167,30 @@ fn suspend_user() {
group_message_response2,
group_canister::send_message_v2::Response::Success(_)
));

let community_message_response2 = client::community::send_message(
env,
user1.principal,
community_id.into(),
&community_canister::send_message::Args {
channel_id,
thread_root_message_index: None,
message_id: random_message_id(),
sender_name: user1.username(),
sender_display_name: None,
content: MessageContentInitial::Text(TextContent { text: "123".to_string() }),
replies_to: None,
mentioned: Vec::new(),
forwarding: false,
community_rules_accepted: None,
channel_rules_accepted: None,
message_filter_failed: None,
},
);
assert!(matches!(
community_message_response2,
community_canister::send_message::Response::Success(_)
));
}

#[test]
Expand Down Expand Up @@ -257,7 +307,7 @@ fn suspended_users_returned_from_user_index_users() {
}

fn init_test_data(env: &mut PocketIc, canister_ids: &CanisterIds, controller: Principal) -> TestData {
let user1 = client::local_user_index::happy_path::register_user(env, canister_ids.local_user_index);
let user1 = client::register_diamond_user(env, canister_ids, controller);
let user2 = client::local_user_index::happy_path::register_user(env, canister_ids.local_user_index);
let platform_moderator = client::local_user_index::happy_path::register_user(env, canister_ids.local_user_index);

Expand Down

0 comments on commit 84725d0

Please sign in to comment.