Skip to content

Commit

Permalink
Allow UserIndex to send Group/Channel messages as the OpenChat Bot (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jul 17, 2024
1 parent bd95f67 commit a76414c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion backend/canisters/community/impl/src/updates/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -174,6 +175,12 @@ fn validate_caller(community_rules_accepted: Option<Version>, 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)
}
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion backend/canisters/group/impl/src/updates/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -125,6 +126,11 @@ fn validate_caller(state: &RuntimeState) -> Result<Caller, Response> {
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)
}
Expand Down

0 comments on commit a76414c

Please sign in to comment.