From 84725d0fcd076a864f9331579b6545a3caa34fdf Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Mon, 22 Jan 2024 11:17:50 +0000 Subject: [PATCH] Notify community canisters when a user is unsuspended (#5227) --- backend/canisters/user_index/CHANGELOG.md | 4 ++ .../impl/src/updates/unsuspend_user.rs | 22 +++++-- .../src/suspend_user_tests.rs | 60 +++++++++++++++++-- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index 3237c73d18..dfc93fee2d 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -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 diff --git a/backend/canisters/user_index/impl/src/updates/unsuspend_user.rs b/backend/canisters/user_index/impl/src/updates/unsuspend_user.rs index b60eb28917..b26f96ca8a 100644 --- a/backend/canisters/user_index/impl/src/updates/unsuspend_user.rs +++ b/backend/canisters/user_index/impl/src/updates/unsuspend_user.rs @@ -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")] @@ -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, state: &mut RuntimeState) { +fn commit(user_id: UserId, groups: Vec, communities: Vec, state: &mut RuntimeState) { let now = state.env.now(); for group in groups { state.data.timer_jobs.enqueue_job( @@ -41,5 +41,19 @@ fn commit(user_id: UserId, groups: Vec, 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); } diff --git a/backend/integration_tests/src/suspend_user_tests.rs b/backend/integration_tests/src/suspend_user_tests.rs index b6fd346cb7..81c93cb277 100644 --- a/backend/integration_tests/src/suspend_user_tests.rs +++ b/backend/integration_tests/src/suspend_user_tests.rs @@ -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; @@ -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, @@ -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(), @@ -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, @@ -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(), @@ -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] @@ -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);