Skip to content

Commit

Permalink
Add the BotCommand access token type (#6830)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 15, 2024
1 parent 6eca4f2 commit 61334e6
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 17 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Reduce size of metrics in memory ([#6765](https://github.com/open-chat-labs/open-chat/pull/6765))
- Run cycles check (+ other background tasks) when executing timer jobs ([#6815](https://github.com/open-chat-labs/open-chat/pull/6815))
- Add cycles balance check to more timer jobs ([#6822](https://github.com/open-chat-labs/open-chat/pull/6822))
- Add the `BotCommand` access token type ([#6830](https://github.com/open-chat-labs/open-chat/pull/6830))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn c2c_can_issue_access_token_for_channel_impl(args: Args, state: &RuntimeState)
AccessTokenType::StartVideoCallV2(vc) => {
can_start_video_call(member, state.data.is_public, vc.call_type, &channel.chat)
}
AccessTokenType::JoinVideoCall | AccessTokenType::MarkVideoCallAsEnded => true,
AccessTokenType::JoinVideoCall | AccessTokenType::MarkVideoCallAsEnded | AccessTokenType::BotCommand(_) => true,
}
}

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 @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Add cycles balance check to more timer jobs ([#6822](https://github.com/open-chat-labs/open-chat/pull/6822))
- Add the `BotCommand` access token type ([#6830](https://github.com/open-chat-labs/open-chat/pull/6830))

## [[2.0.1453](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1453-group)] - 2024-11-14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn c2c_can_issue_access_token_impl(args: Args, state: &RuntimeState) -> bool {

match args.access_type {
AccessTokenType::StartVideoCallV2(vc) => can_start_video_call(member, vc.call_type, &state.data.chat),
AccessTokenType::JoinVideoCall | AccessTokenType::MarkVideoCallAsEnded => true,
AccessTokenType::JoinVideoCall | AccessTokenType::MarkVideoCallAsEnded | AccessTokenType::BotCommand(_) => true,
}
}

Expand Down
6 changes: 5 additions & 1 deletion backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Changed

- Add `BotApiCanister` to the canister state ([#6828](https://github.com/open-chat-labs/open-chat/pull/6828))
- Add the `BotCommand` access token type ([#6830](https://github.com/open-chat-labs/open-chat/pull/6830))

## [[2.0.1454](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1454-local_user_index)] - 2024-11-14

### Changed

- Top up canisters which have fewer than `MIN_CYCLES_BALANCE` cycles ([#6819](https://github.com/open-chat-labs/open-chat/pull/6819))
- Reduce top up amount to 0.02T ([#6821](https://github.com/open-chat-labs/open-chat/pull/6821))
- Add `BotApiCanister` to the canister state ([#6828](https://github.com/open-chat-labs/open-chat/pull/6828))

## [[2.0.1447](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1447-local_user_index)] - 2024-11-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use local_user_index_canister::access_token::{Response::*, *};
use rand::rngs::StdRng;
use rand::SeedableRng;
use serde::Serialize;
use types::{AccessTokenType, ChannelId, Chat, ChatId, CommunityId, JoinOrEndVideoCallClaims, StartVideoCallClaims, UserId};
use types::{
AccessTokenType, BotCommandClaims, ChannelId, Chat, ChatId, CommunityId, JoinOrEndVideoCallClaims, StartVideoCallClaims,
UserId,
};

#[query(composite = true, guard = "caller_is_openchat_user", candid = true, msgpack = true)]
#[trace]
Expand Down Expand Up @@ -53,6 +56,18 @@ async fn access_token(args: Args) -> Response {
};
build_token(args.token_type, custom_claims, state)
}
AccessTokenType::BotCommand(bc) => {
let bot_api_gateway = state.data.internet_identity_canister_id;
let custom_claims = BotCommandClaims {
user_id: bc.user_id,
bot: bc.bot,
thread_root_message_index: bc.thread_root_message_index,
message_id: bc.message_id,
bot_api_gateway,
reply_url: format!("https://{bot_api_gateway}.icp0.io/call"),
};
build_token(args.token_type, custom_claims, state)
}
})
}

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/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/).
- Run cycles check (+ other background tasks) when executing timer jobs ([#6815](https://github.com/open-chat-labs/open-chat/pull/6815))
- Add cycles balance check to more timer jobs ([#6822](https://github.com/open-chat-labs/open-chat/pull/6822))
- Avoid iterating all events when migrating private replies after group import ([#6827](https://github.com/open-chat-labs/open-chat/pull/6827))
- Add the `BotCommand` access token type ([#6830](https://github.com/open-chat-labs/open-chat/pull/6830))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ fn c2c_can_issue_access_token_impl(args: Args, state: &RuntimeState) -> bool {
}

match args.access_type {
AccessTokenType::StartVideoCallV2(_) | AccessTokenType::JoinVideoCall | AccessTokenType::MarkVideoCallAsEnded => true,
AccessTokenType::StartVideoCallV2(_)
| AccessTokenType::JoinVideoCall
| AccessTokenType::MarkVideoCallAsEnded
| AccessTokenType::BotCommand(_) => true,
}
}
13 changes: 12 additions & 1 deletion backend/libraries/types/src/access_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::VideoCallType;
use crate::{MessageId, MessageIndex, UserId, VideoCallType};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use ts_export::ts_export;
Expand All @@ -9,6 +9,7 @@ pub enum AccessTokenType {
StartVideoCallV2(VideoCallAccessTokenArgs),
JoinVideoCall,
MarkVideoCallAsEnded,
BotCommand(BotCommandArgs),
}

#[ts_export]
Expand All @@ -17,12 +18,22 @@ pub struct VideoCallAccessTokenArgs {
pub call_type: VideoCallType,
}

#[ts_export]
#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub struct BotCommandArgs {
pub user_id: UserId,
pub bot: UserId,
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
}

impl AccessTokenType {
pub fn type_name(&self) -> &str {
match self {
AccessTokenType::StartVideoCallV2(_) => "StartVideoCall",
AccessTokenType::JoinVideoCall => "JoinVideoCall",
AccessTokenType::MarkVideoCallAsEnded => "MarkVideoCallAsEnded",
AccessTokenType::BotCommand(_) => "BotCommand",
}
}
}
12 changes: 11 additions & 1 deletion backend/libraries/types/src/claims.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Chat, UserId, VideoCallType};
use crate::{CanisterId, Chat, MessageId, MessageIndex, UserId, VideoCallType};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -31,3 +31,13 @@ pub struct StartVideoCallClaims {
pub call_type: VideoCallType,
pub is_diamond: bool,
}

#[derive(Serialize, Deserialize)]
pub struct BotCommandClaims {
pub user_id: UserId,
pub bot: UserId,
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub bot_api_gateway: CanisterId,
pub reply_url: String,
}
29 changes: 20 additions & 9 deletions frontend/openchat-agent/src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4208,15 +4208,6 @@ export const OptionUpdateOptionalMessagePermissions = Type.Union(
{ default: "NoChange" },
);

export type AccessTokenType = Static<typeof AccessTokenType>;
export const AccessTokenType = Type.Union([
Type.Object({
StartVideoCallV2: VideoCallAccessTokenArgs,
}),
Type.Literal("JoinVideoCall"),
Type.Literal("MarkVideoCallAsEnded"),
]);

export type PendingCryptoTransactionICRC2 = Static<typeof PendingCryptoTransactionICRC2>;
export const PendingCryptoTransactionICRC2 = Type.Object({
ledger: TSBytes,
Expand Down Expand Up @@ -4526,6 +4517,14 @@ export const DiamondMembershipDetails = Type.Object({
subscription: DiamondMembershipSubscription,
});

export type BotCommandArgs = Static<typeof BotCommandArgs>;
export const BotCommandArgs = Type.Object({
user_id: UserId,
bot: UserId,
thread_root_message_index: Type.Optional(Type.Union([MessageIndex, Type.Undefined()])),
message_id: MessageId,
});

export type MemberLeft = Static<typeof MemberLeft>;
export const MemberLeft = Type.Object({
user_id: UserId,
Expand Down Expand Up @@ -6074,6 +6073,18 @@ export const GroupFrozen = Type.Object({
reason: Type.Optional(Type.Union([Type.String(), Type.Undefined()])),
});

export type AccessTokenType = Static<typeof AccessTokenType>;
export const AccessTokenType = Type.Union([
Type.Object({
StartVideoCallV2: VideoCallAccessTokenArgs,
}),
Type.Literal("JoinVideoCall"),
Type.Literal("MarkVideoCallAsEnded"),
Type.Object({
BotCommand: BotCommandArgs,
}),
]);

export type FailedCryptoTransactionICRC2 = Static<typeof FailedCryptoTransactionICRC2>;
export const FailedCryptoTransactionICRC2 = Type.Object({
ledger: TSBytes,
Expand Down
3 changes: 2 additions & 1 deletion tsBindings/shared/AccessTokenType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { BotCommandArgs } from "./BotCommandArgs";
import type { VideoCallAccessTokenArgs } from "./VideoCallAccessTokenArgs";

export type AccessTokenType = { "StartVideoCallV2": VideoCallAccessTokenArgs } | "JoinVideoCall" | "MarkVideoCallAsEnded";
export type AccessTokenType = { "StartVideoCallV2": VideoCallAccessTokenArgs } | "JoinVideoCall" | "MarkVideoCallAsEnded" | { "BotCommand": BotCommandArgs };
6 changes: 6 additions & 0 deletions tsBindings/shared/BotCommandArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { MessageId } from "./MessageId";
import type { MessageIndex } from "./MessageIndex";
import type { UserId } from "./UserId";

export type BotCommandArgs = { user_id: UserId, bot: UserId, thread_root_message_index?: MessageIndex | undefined, message_id: MessageId, };
2 changes: 2 additions & 0 deletions tsBindings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ export type ThreadSummary = { participant_ids: Array<UserId>, followed_by_me: bo
export type OptionUpdateDocument = "NoChange" | "SetToNone" | { "SetToSome": Document };
export type SuspensionDetails = { reason: string, action: SuspensionAction, suspended_by: UserId, };
export type DiamondMembershipDetails = { expires_at: bigint, pay_in_chat: boolean, subscription: DiamondMembershipSubscription, };
export type BotCommandArgs = { user_id: UserId, bot: UserId, thread_root_message_index?: MessageIndex | undefined, message_id: MessageId, };
export type MemberLeft = { user_id: UserId, };
export type UserGroupDetails = { user_group_id: number, name: string, members: Array<UserId>, };
export type GroupIndexRecommendedGroupsArgs = { count: number, exclusions: Array<ChatId>, };
Expand Down Expand Up @@ -643,6 +644,7 @@ export type Tips = Array<[TSBytes, Array<[UserId, bigint]>]>;
export type CallParticipant = { user_id: UserId, joined: bigint, };
export type PermissionsChanged = { old_permissions_v2: GroupPermissions, new_permissions_v2: GroupPermissions, changed_by: UserId, };
export type GroupFrozen = { frozen_by: UserId, reason?: string | undefined, };
export type AccessTokenType = { "StartVideoCallV2": VideoCallAccessTokenArgs } | "JoinVideoCall" | "MarkVideoCallAsEnded" | { "BotCommand": BotCommandArgs };
export type FailedCryptoTransactionICRC2 = { ledger: TSBytes, token: Cryptocurrency, amount: bigint, fee: bigint, spender: UserId, from: CryptoAccountICRC1, to: CryptoAccountICRC1, memo?: TSBytes | undefined, created: bigint, error_message: string, };
export type Proposal = { "NNS": NnsProposal } | { "SNS": SnsProposal };
export type UsersInvited = { user_ids: Array<UserId>, invited_by: UserId, };
Expand Down

0 comments on commit 61334e6

Please sign in to comment.