diff --git a/backend/canisters/local_user_index/CHANGELOG.md b/backend/canisters/local_user_index/CHANGELOG.md index f739307255..bbc1775c6b 100644 --- a/backend/canisters/local_user_index/CHANGELOG.md +++ b/backend/canisters/local_user_index/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Reduce message Ids to 64 bits down from 128 bits ([#7232](https://github.com/open-chat-labs/open-chat/pull/7232)) - Reduce channel Ids to 32 bits down from 128 bits ([#7233](https://github.com/open-chat-labs/open-chat/pull/7233)) - Sync platform moderators/operators to LocalUserIndexes ([#7248](https://github.com/open-chat-labs/open-chat/pull/7248)) +- Support updating bot principal but not name ([#7250](https://github.com/open-chat-labs/open-chat/pull/7250)) ## [[2.0.1567](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1567-local_user_index)] - 2025-01-14 diff --git a/backend/canisters/local_user_index/api/src/lib.rs b/backend/canisters/local_user_index/api/src/lib.rs index 9cdd9c1f8b..1393318680 100644 --- a/backend/canisters/local_user_index/api/src/lib.rs +++ b/backend/canisters/local_user_index/api/src/lib.rs @@ -93,7 +93,6 @@ pub struct BotRegistered { #[derive(Serialize, Deserialize, Clone, Debug)] pub struct BotUpdated { pub user_id: UserId, - pub name: Option, pub commands: Option>, } diff --git a/backend/canisters/local_user_index/impl/src/model/bots_map.rs b/backend/canisters/local_user_index/impl/src/model/bots_map.rs index 25eb9c961e..5c99803b02 100644 --- a/backend/canisters/local_user_index/impl/src/model/bots_map.rs +++ b/backend/canisters/local_user_index/impl/src/model/bots_map.rs @@ -32,11 +32,8 @@ impl BotsMap { self.principal_to_user_id.insert(user_principal, user_id); } - pub fn update(&mut self, user_id: UserId, name: Option, commands: Option>) { + pub fn update(&mut self, user_id: UserId, commands: Option>) { self.bots.entry(user_id).and_modify(|bot| { - if let Some(name) = name { - bot.name = name; - } if let Some(commands) = commands { bot.commands = commands; } diff --git a/backend/canisters/local_user_index/impl/src/updates/c2c_notify_user_index_events.rs b/backend/canisters/local_user_index/impl/src/updates/c2c_notify_user_index_events.rs index c01307ceab..358fda8fc8 100644 --- a/backend/canisters/local_user_index/impl/src/updates/c2c_notify_user_index_events.rs +++ b/backend/canisters/local_user_index/impl/src/updates/c2c_notify_user_index_events.rs @@ -78,7 +78,7 @@ fn handle_event(event: UserIndexEvent, state: &mut RuntimeState) { state.data.bots.add(ev.user_principal, ev.user_id, ev.name, ev.commands); } UserIndexEvent::BotUpdated(ev) => { - state.data.bots.update(ev.user_id, ev.name, ev.commands); + state.data.bots.update(ev.user_id, ev.commands); } UserIndexEvent::PlatformOperatorStatusChanged(ev) => { state diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index df9d1e43ff..67f78baa52 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Reduce message Ids to 64 bits down from 128 bits ([#7232](https://github.com/open-chat-labs/open-chat/pull/7232)) - Reduce channel Ids to 32 bits down from 128 bits ([#7233](https://github.com/open-chat-labs/open-chat/pull/7233)) - Sync platform moderators/operators to LocalUserIndexes ([#7248](https://github.com/open-chat-labs/open-chat/pull/7248)) +- Support updating bot principal but not name ([#7250](https://github.com/open-chat-labs/open-chat/pull/7250)) ### Fixed diff --git a/backend/canisters/user_index/api/can.did b/backend/canisters/user_index/api/can.did index ed53040419..a6dca9c3b1 100644 --- a/backend/canisters/user_index/api/can.did +++ b/backend/canisters/user_index/api/can.did @@ -171,7 +171,7 @@ type RegisterBotResponse = variant { type UpdateBotArgs = record { bot_id : UserId; owner : opt UserId; - name : opt text; + "principal" : opt principal; avatar : TextUpdate; // Image as a data URL endpoint : opt text; definition : opt BotDefinition; @@ -179,8 +179,8 @@ type UpdateBotArgs = record { type UpdateBotResponse = variant { Success; - NameInvalid; - NameAlreadyExists; + PrincipalInvalid; + PrincipalAlreadyUsed; AvatarInvalid; EndpointInvalid; BotNotFound; diff --git a/backend/canisters/user_index/api/src/updates/update_bot.rs b/backend/canisters/user_index/api/src/updates/update_bot.rs index 76771d8156..13af8e01f9 100644 --- a/backend/canisters/user_index/api/src/updates/update_bot.rs +++ b/backend/canisters/user_index/api/src/updates/update_bot.rs @@ -1,4 +1,4 @@ -use candid::CandidType; +use candid::{CandidType, Principal}; use serde::{Deserialize, Serialize}; use ts_export::ts_export; use types::{BotDefinition, OptionUpdate, UserId}; @@ -8,7 +8,8 @@ use types::{BotDefinition, OptionUpdate, UserId}; pub struct Args { pub bot_id: UserId, pub owner: Option, - pub name: Option, + #[ts(as = "Option::")] + pub principal: Option, #[ts(as = "types::OptionUpdateString")] pub avatar: OptionUpdate, // Image as a data URL pub endpoint: Option, @@ -19,8 +20,8 @@ pub struct Args { #[derive(CandidType, Serialize, Deserialize, Debug)] pub enum Response { Success, - NameInvalid, - NameAlreadyExists, + PrincipalInvalid, + PrincipalAlreadyUsed, AvatarInvalid, EndpointInvalid, BotNotFound, diff --git a/backend/canisters/user_index/impl/src/updates/update_bot.rs b/backend/canisters/user_index/impl/src/updates/update_bot.rs index 19ad681663..19c34884c5 100644 --- a/backend/canisters/user_index/impl/src/updates/update_bot.rs +++ b/backend/canisters/user_index/impl/src/updates/update_bot.rs @@ -9,7 +9,7 @@ use local_user_index_canister::{BotUpdated, UserIndexEvent}; use types::OptionUpdate; use url::Url; use user_index_canister::update_bot::{Response::*, *}; -use utils::{document::try_parse_data_url, text_validation::validate_username}; +use utils::document::try_parse_data_url; #[update(candid = true, msgpack = true)] #[trace] @@ -33,9 +33,8 @@ fn update_bot_impl(args: Args, state: &mut RuntimeState) -> Response { let mut bot = bot.clone(); let mut user = user.clone(); - if let Some(name) = args.name.as_ref() { - bot.name = name.clone(); - user.username = name.clone(); + if let Some(principal) = args.principal { + user.principal = principal; } if let Some(owner_id) = args.owner { @@ -67,17 +66,16 @@ fn update_bot_impl(args: Args, state: &mut RuntimeState) -> Response { bot.last_updated = now; - match state.data.users.update(user, now, true, Some(bot)) { + match state.data.users.update(user, now, false, Some(bot)) { UpdateUserResult::Success => (), - UpdateUserResult::UsernameTaken => return NameAlreadyExists, + UpdateUserResult::UsernameTaken => unreachable!(), UpdateUserResult::UserNotFound => return BotNotFound, - UpdateUserResult::PrincipalTaken => unreachable!(), + UpdateUserResult::PrincipalTaken => return PrincipalAlreadyUsed, } state.push_event_to_all_local_user_indexes( UserIndexEvent::BotUpdated(BotUpdated { user_id: args.bot_id, - name: args.name.clone(), commands: args.definition.map(|d| d.commands.clone()), }), None, @@ -89,13 +87,9 @@ fn update_bot_impl(args: Args, state: &mut RuntimeState) -> Response { } fn validate(args: &Args, state: &RuntimeState) -> Result<(), Response> { - if let Some(name) = args.name.as_ref() { - if validate_username(name).is_err() { - return Err(NameInvalid); - } - - if state.data.users.does_username_exist(name, true) { - return Err(NameAlreadyExists); + if let Some(principal) = args.principal { + if principal == Principal::anonymous() { + return Err(PrincipalInvalid); } } diff --git a/backend/integration_tests/src/bot_tests.rs b/backend/integration_tests/src/bot_tests.rs index 7bdb4a845f..bfc8138038 100644 --- a/backend/integration_tests/src/bot_tests.rs +++ b/backend/integration_tests/src/bot_tests.rs @@ -206,16 +206,16 @@ fn e2e_bot_test() { assert!(message.bot_context.is_some()); assert!(message.bot_context.as_ref().unwrap().finalised); - // Update the bot name - let new_bot_name = random_string(); + // Update the bot endpoint + let new_endpoint = "https://123.bot.xyz/".to_string(); client::user_index::happy_path::update_bot( env, canister_ids.user_index, user.principal, bot.id, None, - Some(new_bot_name.clone()), None, + Some(new_endpoint.clone()), None, ); @@ -225,7 +225,7 @@ fn e2e_bot_test() { assert_eq!(response.added_or_updated.len(), 1); let bot = &response.added_or_updated[0]; - assert_eq!(bot.name, new_bot_name); + assert_eq!(bot.endpoint, new_endpoint); } fn init_test_data(env: &mut PocketIc, canister_ids: &CanisterIds, controller: Principal) -> TestData { diff --git a/backend/integration_tests/src/client/user_index.rs b/backend/integration_tests/src/client/user_index.rs index 928ce388a0..4bb47a4b1e 100644 --- a/backend/integration_tests/src/client/user_index.rs +++ b/backend/integration_tests/src/client/user_index.rs @@ -280,8 +280,8 @@ pub mod happy_path { user_index_canister_id: CanisterId, caller: Principal, bot_id: UserId, + bot_principal: Option, owner: Option, - name: Option, endpoint: Option, definition: Option, ) { @@ -292,7 +292,7 @@ pub mod happy_path { &user_index_canister::update_bot::Args { bot_id, owner, - name, + principal: bot_principal, avatar: OptionUpdate::NoChange, endpoint, definition,