Skip to content

Commit

Permalink
Record user who added bot to community/group (#7035)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Dec 11, 2024
1 parent 5fe3268 commit cc8f7b5
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 23 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions backend/canisters/group/impl/src/queries/selected_initial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions backend/libraries/group_chat_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ impl GroupChatCore {
result.bots_added_or_updated.push(BotGroupDetails {
user_id,
permissions: bot.permissions.clone(),
added_by: bot.added_by,
});
}
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
43 changes: 28 additions & 15 deletions backend/libraries/group_community_common/src/bots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId, BotGroupConfig>,
bots: BTreeMap<UserId, BotInternal>,
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 {
Expand All @@ -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<Item = (&UserId, &BotGroupConfig)> {
pub fn iter(&self) -> impl Iterator<Item = (&UserId, &BotInternal)> {
self.bots.iter()
}

Expand Down Expand Up @@ -93,3 +100,9 @@ pub enum BotUpdate {
Removed = 2,
Updated = 3,
}

#[derive(Serialize, Deserialize)]
pub struct BotInternal {
pub added_by: UserId,
pub permissions: SlashCommandPermissions,
}
1 change: 1 addition & 0 deletions backend/libraries/types/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -2176,5 +2176,6 @@ type BotGroupConfig = record {

type BotGroupDetails = record {
user_id : UserId;
added_by : UserId;
permissions : SlashCommandPermissions;
}
1 change: 1 addition & 0 deletions backend/libraries/types/src/bots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down

0 comments on commit cc8f7b5

Please sign in to comment.