From a76414ca1895a8df32cd30581257d7596e4d0fd8 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Wed, 17 Jul 2024 14:24:07 +0100 Subject: [PATCH] Allow UserIndex to send Group/Channel messages as the OpenChat Bot (#6048) --- backend/canisters/community/CHANGELOG.md | 1 + .../canisters/community/impl/src/updates/send_message.rs | 9 ++++++++- backend/canisters/group/CHANGELOG.md | 1 + backend/canisters/group/impl/src/updates/send_message.rs | 8 +++++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/canisters/community/CHANGELOG.md b/backend/canisters/community/CHANGELOG.md index d4652050d0..9702bee2f8 100644 --- a/backend/canisters/community/CHANGELOG.md +++ b/backend/canisters/community/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Support gates with multiple verifiable credentials ([#6029](https://github.com/open-chat-labs/open-chat/pull/6029)) +- Allow UserIndex to send Group/Channel messages as the OpenChat Bot ([#6048](https://github.com/open-chat-labs/open-chat/pull/6048)) ## [[2.0.1235](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1235-community)] - 2024-07-09 diff --git a/backend/canisters/community/impl/src/updates/send_message.rs b/backend/canisters/community/impl/src/updates/send_message.rs index ff53a2e571..370e11696b 100644 --- a/backend/canisters/community/impl/src/updates/send_message.rs +++ b/backend/canisters/community/impl/src/updates/send_message.rs @@ -5,6 +5,7 @@ use crate::timer_job_types::{DeleteFileReferencesJob, EndPollJob, MarkP2PSwapExp use crate::{mutate_state, run_regular_jobs, Data, RuntimeState}; use canister_api_macros::update; use canister_tracing_macros::trace; +use chat_events::OPENCHAT_BOT_USER_ID; use community_canister::c2c_send_message::{Args as C2CArgs, Response as C2CResponse}; use community_canister::send_message::{Response::*, *}; use group_chat_core::SendMessageResult; @@ -95,7 +96,7 @@ fn c2c_send_message_impl(args: C2CArgs, state: &mut RuntimeState) -> C2CResponse }; // Bots can't call this c2c endpoint since it skips the validation - if is_bot && user_id != state.data.proposals_bot_user_id { + if is_bot && user_id != state.data.proposals_bot_user_id && user_id != OPENCHAT_BOT_USER_ID { return NotAuthorized; } @@ -174,6 +175,12 @@ fn validate_caller(community_rules_accepted: Option, state: &mut Runtim display_name: member.display_name().value.clone(), }) } + } else if caller == state.data.user_index_canister_id { + Ok(Caller { + user_id: OPENCHAT_BOT_USER_ID, + is_bot: true, + display_name: None, + }) } else { Err(UserNotInCommunity) } diff --git a/backend/canisters/group/CHANGELOG.md b/backend/canisters/group/CHANGELOG.md index 9ac051ded1..d9c6d80577 100644 --- a/backend/canisters/group/CHANGELOG.md +++ b/backend/canisters/group/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Support gates with multiple verifiable credentials ([#6029](https://github.com/open-chat-labs/open-chat/pull/6029)) +- Allow UserIndex to send Group/Channel messages as the OpenChat Bot ([#6048](https://github.com/open-chat-labs/open-chat/pull/6048)) ## [[2.0.1234](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1234-group)] - 2024-07-09 diff --git a/backend/canisters/group/impl/src/updates/send_message.rs b/backend/canisters/group/impl/src/updates/send_message.rs index b120d370e7..ae766429b3 100644 --- a/backend/canisters/group/impl/src/updates/send_message.rs +++ b/backend/canisters/group/impl/src/updates/send_message.rs @@ -3,6 +3,7 @@ use crate::timer_job_types::{DeleteFileReferencesJob, EndPollJob, MarkP2PSwapExp use crate::{mutate_state, run_regular_jobs, Data, RuntimeState, TimerJob}; use canister_api_macros::update; use canister_tracing_macros::trace; +use chat_events::OPENCHAT_BOT_USER_ID; use group_canister::c2c_send_message::{Args as C2CArgs, Response as C2CResponse}; use group_canister::send_message_v2::{Response::*, *}; use group_chat_core::SendMessageResult; @@ -69,7 +70,7 @@ fn c2c_send_message_impl(args: C2CArgs, state: &mut RuntimeState) -> C2CResponse match validate_caller(state) { Ok(Caller { user_id, is_bot }) => { // Bots can't call this c2c endpoint since it skips the validation - if is_bot && user_id != state.data.proposals_bot_user_id { + if is_bot && user_id != state.data.proposals_bot_user_id && user_id != OPENCHAT_BOT_USER_ID { return NotAuthorized; } @@ -125,6 +126,11 @@ fn validate_caller(state: &RuntimeState) -> Result { is_bot: member.is_bot, }) } + } else if caller == state.data.user_index_canister_id { + Ok(Caller { + user_id: OPENCHAT_BOT_USER_ID, + is_bot: true, + }) } else { Err(CallerNotInGroup) }