Skip to content

Commit

Permalink
Set user type correctly for OC controlled bots (#6494)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 1, 2024
1 parent e37f30c commit 9cd595b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Return owned values from `EventsMap` in prep for switch to stable memory ([#6469](https://github.com/open-chat-labs/open-chat/pull/6469))
- Add serde default attribute in preparation for skipping serialization if default ([#6475](https://github.com/open-chat-labs/open-chat/pull/6475))

### Fixed

- Set user type correctly for OC controlled bots ([#6494](https://github.com/open-chat-labs/open-chat/pull/6494))

## [[2.0.1357](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1357-user)] - 2024-09-20

### Added
Expand Down
13 changes: 13 additions & 0 deletions backend/canisters/user/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::{mutate_state, Data};
use candid::Principal;
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use chat_events::OPENCHAT_BOT_USER_ID;
use ic_cdk::post_upgrade;
use stable_memory::get_reader;
use tracing::info;
Expand All @@ -23,9 +25,20 @@ fn post_upgrade(args: Args) {

info!(version = %args.wasm_version, "Post-upgrade complete");

let oc_controlled_bots = [
OPENCHAT_BOT_USER_ID, // OC Bot
Principal::from_text("62rh2-kiaaa-aaaaf-bmy5q-cai").unwrap().into(), // AirdropBot
Principal::from_text("iywa7-ayaaa-aaaaf-aemga-cai").unwrap().into(), // ProposalsBot
];

mutate_state(|state| {
for chat in state.data.direct_chats.iter_mut() {
chat.events.populate_search_index();
}

state
.data
.direct_chats
.set_user_type_for_oc_controlled_bots(&oc_controlled_bots);
});
}
7 changes: 4 additions & 3 deletions backend/canisters/user/impl/src/model/direct_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ pub struct DirectChat {
pub read_by_them_up_to: Timestamped<Option<MessageIndex>>,
pub notifications_muted: Timestamped<bool>,
pub archived: Timestamped<bool>,
pub is_bot: bool,
#[serde(default)]
pub user_type: UserType,
pub unconfirmed: Vec<SendMessageArgs>,
}

impl DirectChat {
pub fn set_user_as_ic_controlled_bot(&mut self) {
self.user_type = UserType::OcControlledBot;
}

pub fn new(
them: UserId,
user_type: UserType,
Expand All @@ -42,7 +44,6 @@ impl DirectChat {
read_by_them_up_to: Timestamped::new(None, now),
notifications_muted: Timestamped::new(false, now),
archived: Timestamped::new(false, now),
is_bot: user_type.is_bot(),
user_type,
unconfirmed: Vec::new(),
}
Expand Down
8 changes: 8 additions & 0 deletions backend/canisters/user/impl/src/model/direct_chats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ pub struct DirectChats {
}

impl DirectChats {
pub fn set_user_type_for_oc_controlled_bots(&mut self, oc_controlled_bots: &[UserId]) {
for user_id in oc_controlled_bots {
if let Some(chat) = self.direct_chats.get_mut(&ChatId::from(*user_id)) {
chat.set_user_as_ic_controlled_bot();
}
}
}

pub fn get(&self, chat_id: &ChatId) -> Option<&DirectChat> {
self.direct_chats.get(chat_id)
}
Expand Down

0 comments on commit 9cd595b

Please sign in to comment.