Skip to content

Commit

Permalink
Support updating bot principal but not name (#7253)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Jan 20, 2025
1 parent 69e0ea5 commit 756f66a
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 34 deletions.
1 change: 1 addition & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion backend/canisters/local_user_index/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pub struct BotRegistered {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct BotUpdated {
pub user_id: UserId,
pub name: Option<String>,
pub commands: Option<Vec<SlashCommandSchema>>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, commands: Option<Vec<SlashCommandSchema>>) {
pub fn update(&mut self, user_id: UserId, commands: Option<Vec<SlashCommandSchema>>) {
self.bots.entry(user_id).and_modify(|bot| {
if let Some(name) = name {
bot.name = name;
}
if let Some(commands) = commands {
bot.commands = commands;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,16 @@ 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;
};

type UpdateBotResponse = variant {
Success;
NameInvalid;
NameAlreadyExists;
PrincipalInvalid;
PrincipalAlreadyUsed;
AvatarInvalid;
EndpointInvalid;
BotNotFound;
Expand Down
9 changes: 5 additions & 4 deletions backend/canisters/user_index/api/src/updates/update_bot.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -8,7 +8,8 @@ use types::{BotDefinition, OptionUpdate, UserId};
pub struct Args {
pub bot_id: UserId,
pub owner: Option<UserId>,
pub name: Option<String>,
#[ts(as = "Option::<ts_export::TSBytes>")]
pub principal: Option<Principal>,
#[ts(as = "types::OptionUpdateString")]
pub avatar: OptionUpdate<String>, // Image as a data URL
pub endpoint: Option<String>,
Expand All @@ -19,8 +20,8 @@ pub struct Args {
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
NameInvalid,
NameAlreadyExists,
PrincipalInvalid,
PrincipalAlreadyUsed,
AvatarInvalid,
EndpointInvalid,
BotNotFound,
Expand Down
24 changes: 9 additions & 15 deletions backend/canisters/user_index/impl/src/updates/update_bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions backend/integration_tests/src/bot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions backend/integration_tests/src/client/user_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ pub mod happy_path {
user_index_canister_id: CanisterId,
caller: Principal,
bot_id: UserId,
bot_principal: Option<Principal>,
owner: Option<UserId>,
name: Option<String>,
endpoint: Option<String>,
definition: Option<BotDefinition>,
) {
Expand All @@ -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,
Expand Down

0 comments on commit 756f66a

Please sign in to comment.