Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smoke test for bots + general bot fixes #7031

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ impl Data {
self.invited_users.last_updated(),
self.events.latest_event_timestamp(),
self.members.last_updated(),
self.bots.last_updated(),
]
.into_iter()
.max()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn c2c_handle_bot_action_impl(args: Args, state: &mut RuntimeState) -> Response
MessageContent::Giphy(giphy_content) => MessageContentInitial::Giphy(giphy_content),
};

send_message_impl(
match send_message_impl(
send_message::Args {
channel_id,
thread_root_message_index: args.thread_root_message_index,
Expand All @@ -61,11 +61,12 @@ fn c2c_handle_bot_action_impl(args: Args, state: &mut RuntimeState) -> Response
},
Some(args.bot.user_id.into()),
state,
);
) {
send_message::Response::Success(_) => Ok(()),
response => Err(HandleBotActionsError::Other(format!("{response:?}"))),
}
}
}

Ok(())
}

fn is_bot_permitted_to_execute_command(args: &Args, state: &RuntimeState) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub(crate) fn send_message_impl(args: Args, caller_override: Option<Principal>,
args.forwarding,
args.channel_rules_accepted,
args.message_filter_failed.is_some(),
state.data.proposals_bot_user_id,
args.block_level_markdown,
&mut state.data.event_store_client,
now,
Expand Down Expand Up @@ -107,6 +106,7 @@ fn c2c_send_message_impl(args: C2CArgs, state: &mut RuntimeState) -> C2CResponse

let result = channel.chat.send_message(
user_id,
user_type,
args.thread_root_message_index,
args.message_id,
args.content,
Expand All @@ -115,7 +115,6 @@ fn c2c_send_message_impl(args: C2CArgs, state: &mut RuntimeState) -> C2CResponse
args.forwarding,
args.channel_rules_accepted,
args.message_filter_failed.is_some(),
state.data.proposals_bot_user_id,
args.block_level_markdown,
&mut state.data.event_store_client,
now,
Expand Down Expand Up @@ -187,6 +186,12 @@ fn validate_caller(
display_name: member.display_name().value.clone(),
})
}
} else if state.data.bots.get(&caller.into()).is_some() {
Ok(Caller {
user_id: caller.into(),
user_type: UserType::BotV2,
display_name: None,
})
} else if caller == state.data.user_index_canister_id {
Ok(Caller {
user_id: OPENCHAT_BOT_USER_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use community_canister::start_video_call::{Response::*, *};
use constants::HOUR_IN_MS;
use group_chat_core::SendMessageResult;
use ic_cdk::update;
use types::{ChannelMessageNotification, Notification, UserId, VideoCallPresence, VideoCallType};
use types::{ChannelMessageNotification, Notification, UserId, UserType, VideoCallPresence, VideoCallType};

#[update(guard = "caller_is_video_call_operator")]
#[trace]
Expand Down Expand Up @@ -39,6 +39,7 @@ fn start_video_call_impl(args: Args, state: &mut RuntimeState) -> Response {

let result = match channel.chat.send_message(
sender,
UserType::User,
None,
args.message_id,
MessageContentInternal::VideoCall(VideoCallContentInternal {
Expand All @@ -60,7 +61,6 @@ fn start_video_call_impl(args: Args, state: &mut RuntimeState) -> Response {
false,
None,
false,
state.data.proposals_bot_user_id,
false,
&mut state.data.event_store_client,
now,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn c2c_handle_bot_action_impl(args: Args, state: &mut RuntimeState) -> Response
MessageContent::Giphy(giphy_content) => MessageContentInitial::Giphy(giphy_content),
};

send_message_impl(
match send_message_impl(
send_message_v2::Args {
thread_root_message_index: args.thread_root_message_index,
message_id: args.message_id,
Expand All @@ -57,11 +57,12 @@ fn c2c_handle_bot_action_impl(args: Args, state: &mut RuntimeState) -> Response
},
Some(args.bot.user_id.into()),
state,
);
) {
send_message_v2::Response::Success(_) => Ok(()),
response => Err(HandleBotActionsError::Other(format!("{response:?}"))),
}
}
}

Ok(())
}

fn is_bot_permitted_to_execute_command(args: &Args, state: &RuntimeState) -> bool {
Expand Down
8 changes: 6 additions & 2 deletions backend/canisters/group/impl/src/updates/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub(crate) fn send_message_impl(args: Args, caller_override: Option<Principal>,
args.forwarding,
args.rules_accepted,
args.message_filter_failed.is_some(),
state.data.proposals_bot_user_id,
args.block_level_markdown,
&mut state.data.event_store_client,
now,
Expand Down Expand Up @@ -80,6 +79,7 @@ fn c2c_send_message_impl(args: C2CArgs, state: &mut RuntimeState) -> C2CResponse
let mentioned: Vec<_> = args.mentioned.iter().map(|u| u.user_id).collect();
let result = state.data.chat.send_message(
user_id,
user_type,
args.thread_root_message_index,
args.message_id,
args.content,
Expand All @@ -88,7 +88,6 @@ fn c2c_send_message_impl(args: C2CArgs, state: &mut RuntimeState) -> C2CResponse
args.forwarding,
args.rules_accepted,
args.message_filter_failed.is_some(),
state.data.proposals_bot_user_id,
args.block_level_markdown,
&mut state.data.event_store_client,
now,
Expand Down Expand Up @@ -129,6 +128,11 @@ fn validate_caller(caller_override: Option<Principal>, state: &RuntimeState) ->
user_type: member.user_type(),
})
}
} else if state.data.chat.bots.get(&caller.into()).is_some() {
Ok(Caller {
user_id: caller.into(),
user_type: UserType::BotV2,
})
} else if caller == state.data.user_index_canister_id {
Ok(Caller {
user_id: OPENCHAT_BOT_USER_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use constants::HOUR_IN_MS;
use group_canister::start_video_call::{Response::*, *};
use group_chat_core::SendMessageResult;
use ic_cdk::update;
use types::{GroupMessageNotification, Notification, VideoCallPresence, VideoCallType};
use types::{GroupMessageNotification, Notification, UserType, VideoCallPresence, VideoCallType};

#[update(guard = "caller_is_video_call_operator")]
#[trace]
Expand All @@ -35,6 +35,7 @@ fn start_video_call_impl(args: Args, state: &mut RuntimeState) -> Response {

let result = match state.data.chat.send_message(
sender,
UserType::User,
None,
args.message_id,
MessageContentInternal::VideoCall(VideoCallContentInternal {
Expand All @@ -56,7 +57,6 @@ fn start_video_call_impl(args: Args, state: &mut RuntimeState) -> Response {
false,
None,
false,
state.data.proposals_bot_user_id,
false,
&mut state.data.event_store_client,
now,
Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Ensure bot has permission to execute given action ([#7014](https://github.com/open-chat-labs/open-chat/pull/7014))
- Switch to using `PrincipalToStableMemoryMap` ([#7023](https://github.com/open-chat-labs/open-chat/pull/7023))

### Fixes

- Fixes to `access_token` and `execute_bot_command` ([#7031](https://github.com/open-chat-labs/open-chat/pull/7031))

## [[2.0.1495](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1495-local_user_index)] - 2024-12-04

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn accept_if_valid(state: &RuntimeState) {
| "join_community"
| "join_group"
| "report_message_v2" => state.is_caller_openchat_user(),
"register_user" => true,
"register_user" | "execute_bot_command" => true,
_ => false,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn prepare(args: &Args, state: &RuntimeState) -> Result<PrepareResult, Response>
let Some(permissions) = state
.data
.bots
.get(&cmd.user_id)
.get(&cmd.bot)
.and_then(|b| b.commands.iter().find(|c| c.name == cmd.command_name))
.map(|c| c.permissions.clone())
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use canister_api_macros::update;
use canister_client::generate_c2c_call;
use ic_cdk::update;
use jwt::{verify_jwt, Claims};
use local_user_index_canister::execute_bot_command::*;
use types::c2c_handle_bot_action;
Expand All @@ -9,7 +9,7 @@ use types::User;
use crate::read_state;
use crate::RuntimeState;

#[update]
#[update(candid = true, msgpack = true)]
async fn execute_bot_command(args: Args) -> Response {
let c2c_args = match read_state(|state| validate(args, state)) {
Ok(c2c_args) => c2c_args,
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 @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed

- Fix to allow `register_bot` to be called in test mode ([#7015](https://github.com/open-chat-labs/open-chat/pull/7015))
- Fix `register_bot` ([#7031](https://github.com/open-chat-labs/open-chat/pull/7031))

## [[2.0.1469](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1469-user_index)] - 2024-11-25

Expand Down
11 changes: 4 additions & 7 deletions backend/canisters/user_index/impl/src/updates/register_bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use candid::Principal;
use canister_api_macros::{proposal, update};
use canister_tracing_macros::trace;
use event_store_producer::EventBuilder;
use local_user_index_canister::{Event, UserRegistered};
use local_user_index_canister::{BotRegistered, Event};
use rand::RngCore;
use tracing::error;
use types::{UserId, UserType};
Expand Down Expand Up @@ -83,18 +83,15 @@ fn register_bot_impl(args: Args, state: &mut RuntimeState) {
);

state.push_event_to_all_local_user_indexes(
Event::UserRegistered(UserRegistered {
Event::BotRegistered(BotRegistered {
user_id,
user_principal: args.principal,
username: args.name,
user_type: UserType::BotV2,
referred_by: None,
name: args.name.clone(),
commands: args.commands.clone(),
}),
None,
);

// TODO: Propogate bot to each local Bot API Gateway

state.data.event_store_client.push(
EventBuilder::new("user_registered", now)
.with_user(user_id.to_string(), true)
Expand Down
Loading
Loading