diff --git a/backend/canisters/community/CHANGELOG.md b/backend/canisters/community/CHANGELOG.md index 78cdecab70..2742a35bad 100644 --- a/backend/canisters/community/CHANGELOG.md +++ b/backend/canisters/community/CHANGELOG.md @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Merge member Ids set with channel links map ([#7027](https://github.com/open-chat-labs/open-chat/pull/7027)) - Implement new lightweight search index for searching messages ([#7029](https://github.com/open-chat-labs/open-chat/pull/7029)) - Make `MessageId` comparisons use their 64bit representation ([#7030](https://github.com/open-chat-labs/open-chat/pull/7030)) +- Record user who added bot to community/group ([#7035](https://github.com/open-chat-labs/open-chat/pull/7035)) ### Removed diff --git a/backend/canisters/community/impl/src/lib.rs b/backend/canisters/community/impl/src/lib.rs index 7e1738ea53..0c2fa92059 100644 --- a/backend/canisters/community/impl/src/lib.rs +++ b/backend/canisters/community/impl/src/lib.rs @@ -776,7 +776,7 @@ impl Data { } pub fn add_bot(&mut self, owner_id: UserId, user_id: UserId, bot_config: BotGroupConfig, now: TimestampMillis) -> bool { - if !self.bots.add(user_id, bot_config, now) { + if !self.bots.add(user_id, owner_id, bot_config.permissions, now) { return false; } @@ -795,7 +795,7 @@ impl Data { } pub fn update_bot(&mut self, owner_id: UserId, user_id: UserId, bot_config: BotGroupConfig, now: TimestampMillis) -> bool { - if !self.bots.update(user_id, bot_config, now) { + if !self.bots.update(user_id, bot_config.permissions, now) { return false; } diff --git a/backend/canisters/community/impl/src/queries/selected_initial.rs b/backend/canisters/community/impl/src/queries/selected_initial.rs index 84911420ad..9449493d6d 100644 --- a/backend/canisters/community/impl/src/queries/selected_initial.rs +++ b/backend/canisters/community/impl/src/queries/selected_initial.rs @@ -49,9 +49,10 @@ fn selected_initial_impl(args: Args, state: &RuntimeState) -> Response { let bots = data .bots .iter() - .map(|(user_id, config)| BotGroupDetails { + .map(|(user_id, bot)| BotGroupDetails { user_id: *user_id, - permissions: config.permissions.clone(), + permissions: bot.permissions.clone(), + added_by: bot.added_by, }) .collect(); diff --git a/backend/canisters/community/impl/src/queries/selected_updates.rs b/backend/canisters/community/impl/src/queries/selected_updates.rs index 65b89f4311..29e5c5f453 100644 --- a/backend/canisters/community/impl/src/queries/selected_updates.rs +++ b/backend/canisters/community/impl/src/queries/selected_updates.rs @@ -98,6 +98,7 @@ fn selected_updates_impl(args: Args, state: &RuntimeState) -> Response { result.bots_added_or_updated.push(BotGroupDetails { user_id, permissions: bot.permissions.clone(), + added_by: bot.added_by, }); } } diff --git a/backend/canisters/group/CHANGELOG.md b/backend/canisters/group/CHANGELOG.md index 45a188366f..dd5d5dcfeb 100644 --- a/backend/canisters/group/CHANGELOG.md +++ b/backend/canisters/group/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Switch to using `PrincipalToStableMemoryMap` ([#7023](https://github.com/open-chat-labs/open-chat/pull/7023)) - Implement new lightweight search index for searching messages ([#7029](https://github.com/open-chat-labs/open-chat/pull/7029)) - Make `MessageId` comparisons use their 64bit representation ([#7030](https://github.com/open-chat-labs/open-chat/pull/7030)) +- Record user who added bot to community/group ([#7035](https://github.com/open-chat-labs/open-chat/pull/7035)) ## [[2.0.1501](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1501-group)] - 2024-12-06 diff --git a/backend/canisters/group/impl/src/queries/selected_initial.rs b/backend/canisters/group/impl/src/queries/selected_initial.rs index 189e23782c..3e949890b4 100644 --- a/backend/canisters/group/impl/src/queries/selected_initial.rs +++ b/backend/canisters/group/impl/src/queries/selected_initial.rs @@ -37,9 +37,10 @@ fn selected_initial_impl(state: &RuntimeState) -> Response { let bots = chat .bots .iter() - .map(|(user_id, config)| BotGroupDetails { + .map(|(user_id, bot)| BotGroupDetails { user_id: *user_id, - permissions: config.permissions.clone(), + permissions: bot.permissions.clone(), + added_by: bot.added_by, }) .collect(); diff --git a/backend/libraries/group_chat_core/src/lib.rs b/backend/libraries/group_chat_core/src/lib.rs index ed71cd996e..a4e23f80db 100644 --- a/backend/libraries/group_chat_core/src/lib.rs +++ b/backend/libraries/group_chat_core/src/lib.rs @@ -354,6 +354,7 @@ impl GroupChatCore { result.bots_added_or_updated.push(BotGroupDetails { user_id, permissions: bot.permissions.clone(), + added_by: bot.added_by, }); } } @@ -1825,7 +1826,7 @@ impl GroupChatCore { } pub fn add_bot(&mut self, owner_id: UserId, user_id: UserId, config: BotGroupConfig, now: TimestampMillis) -> bool { - if !self.bots.add(user_id, config, now) { + if !self.bots.add(user_id, owner_id, config.permissions, now) { return false; } @@ -1842,7 +1843,7 @@ impl GroupChatCore { } pub fn update_bot(&mut self, owner_id: UserId, user_id: UserId, config: BotGroupConfig, now: TimestampMillis) -> bool { - if !self.bots.update(user_id, config, now) { + if !self.bots.update(user_id, config.permissions, now) { return false; } diff --git a/backend/libraries/group_community_common/src/bots.rs b/backend/libraries/group_community_common/src/bots.rs index fdabd3a7ef..7c0d7d2a67 100644 --- a/backend/libraries/group_community_common/src/bots.rs +++ b/backend/libraries/group_community_common/src/bots.rs @@ -2,37 +2,44 @@ use candid::Principal; use constants::calculate_summary_updates_data_removal_cutoff; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; -use std::collections::{BTreeMap, BTreeSet}; -use types::{BotGroupConfig, TimestampMillis, UserId}; +use std::collections::{btree_map::Entry, BTreeMap, BTreeSet}; +use types::{SlashCommandPermissions, TimestampMillis, UserId}; #[derive(Serialize, Deserialize, Default)] pub struct GroupBots { - bots: BTreeMap, + bots: BTreeMap, updates: BTreeSet<(TimestampMillis, UserId, BotUpdate)>, latest_update_removed: TimestampMillis, } impl GroupBots { - pub fn add(&mut self, user_id: UserId, bot_config: BotGroupConfig, now: TimestampMillis) -> bool { + pub fn add( + &mut self, + user_id: UserId, + added_by: UserId, + permissions: SlashCommandPermissions, + now: TimestampMillis, + ) -> bool { if self.bots.contains_key(&user_id) { return false; } - self.bots.insert(user_id, bot_config); + self.bots.insert(user_id, BotInternal { added_by, permissions }); self.prune_then_insert_member_update(user_id, BotUpdate::Added, now); true } - pub fn update(&mut self, user_id: UserId, bot_config: BotGroupConfig, now: TimestampMillis) -> bool { - if !self.bots.contains_key(&user_id) { - return false; + pub fn update(&mut self, user_id: UserId, permissions: SlashCommandPermissions, now: TimestampMillis) -> bool { + match self.bots.entry(user_id) { + Entry::Vacant(_) => false, + Entry::Occupied(mut o) => { + let bot = o.get_mut(); + bot.permissions = permissions; + self.prune_then_insert_member_update(user_id, BotUpdate::Updated, now); + true + } } - - self.bots.insert(user_id, bot_config); - self.prune_then_insert_member_update(user_id, BotUpdate::Updated, now); - - true } pub fn remove(&mut self, user_id: UserId, now: TimestampMillis) -> bool { @@ -45,11 +52,11 @@ impl GroupBots { removed } - pub fn get(&self, user_id: &UserId) -> Option<&BotGroupConfig> { + pub fn get(&self, user_id: &UserId) -> Option<&BotInternal> { self.bots.get(user_id) } - pub fn iter(&self) -> impl Iterator { + pub fn iter(&self) -> impl Iterator { self.bots.iter() } @@ -93,3 +100,9 @@ pub enum BotUpdate { Removed = 2, Updated = 3, } + +#[derive(Serialize, Deserialize)] +pub struct BotInternal { + pub added_by: UserId, + pub permissions: SlashCommandPermissions, +} diff --git a/backend/libraries/types/can.did b/backend/libraries/types/can.did index 1e287af8c6..fcc156280e 100644 --- a/backend/libraries/types/can.did +++ b/backend/libraries/types/can.did @@ -2176,5 +2176,6 @@ type BotGroupConfig = record { type BotGroupDetails = record { user_id : UserId; + added_by : UserId; permissions : SlashCommandPermissions; } diff --git a/backend/libraries/types/src/bots.rs b/backend/libraries/types/src/bots.rs index ab8aeeec88..41afaeed14 100644 --- a/backend/libraries/types/src/bots.rs +++ b/backend/libraries/types/src/bots.rs @@ -105,6 +105,7 @@ slash_command_option_choice!(SlashCommandOptionChoiceU16, u16); #[derive(CandidType, Serialize, Deserialize, Debug)] pub struct BotGroupDetails { pub user_id: UserId, + pub added_by: UserId, pub permissions: SlashCommandPermissions, }