Skip to content

Commit

Permalink
chore: improve message API
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Sep 1, 2024
1 parent 33dbbab commit 4713f33
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/ic_message/ic_message.did
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ service : (opt ChainArgs) -> {
icrc3_get_blocks : (vec GetBlocksRequest) -> (GetBlocksResult) query;
icrc3_get_tip_certificate : () -> (opt ICRC3DataCertificate) query;
icrc3_supported_block_types : () -> (vec SupportedBlockType) query;
register_username : (text) -> (Result_3);
register_username : (text, opt text) -> (Result_3);
save_channel_kek : (ChannelKEKInput) -> (Result);
search_username : (text) -> (Result_6) query;
update_my_image : (text) -> (Result);
Expand Down
16 changes: 14 additions & 2 deletions src/ic_message/src/api_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ic_message_types::{
use crate::{is_authenticated, store, types};

#[ic_cdk::update(guard = "is_authenticated")]
async fn register_username(username: String) -> Result<UserInfo, String> {
async fn register_username(username: String, name: Option<String>) -> Result<UserInfo, String> {
if username.len() > types::MAX_USER_SIZE {
Err("username is too long".to_string())?;
}
Expand All @@ -16,9 +16,21 @@ async fn register_username(username: String) -> Result<UserInfo, String> {
}
validate_key(&username.to_ascii_lowercase())?;

if let Some(ref name) = name {
if name.is_empty() {
Err("name is empty".to_string())?;
}
if name.len() > types::MAX_USER_NAME_SIZE {
Err("name is too long".to_string())?;
}
if name != name.trim() {
Err("name has leading or trailing spaces".to_string())?;
}
}

let caller = ic_cdk::caller();
let now_ms = ic_cdk::api::time() / MILLISECONDS;
store::user::register_username(caller, username, now_ms).await
store::user::register_username(caller, username.clone(), name.unwrap_or(username), now_ms).await
}

#[ic_cdk::update(guard = "is_authenticated")]
Expand Down
3 changes: 2 additions & 1 deletion src/ic_message/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ pub mod user {
pub async fn register_username(
caller: Principal,
username: String,
name: String,
now_ms: u64,
) -> Result<UserInfo, String> {
let (cose_canister, profile_canister, price) = state::with(|s| {
Expand Down Expand Up @@ -366,7 +367,7 @@ pub mod user {
}
None => {
let user = User {
name: username.clone(),
name: name,
image: "".to_string(),
profile_canister,
cose_canister: Some(cose_canister),
Expand Down
4 changes: 2 additions & 2 deletions src/ic_message_channel/ic_message_channel.did
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type QueryStats = record {
};
type Result = variant { Ok : AddMessageOutput; Err : text };
type Result_1 = variant { Ok; Err : text };
type Result_10 = variant { Ok : nat64; Err : text };
type Result_10 = variant { Ok : record { nat64; opt Message }; Err : text };
type Result_2 = variant { Ok : ChannelInfo; Err : text };
type Result_3 = variant { Ok : vec ChannelBasicInfo; Err : text };
type Result_4 = variant { Ok : CanisterStatusResponse; Err : text };
Expand Down Expand Up @@ -150,7 +150,7 @@ service : (opt ChainArgs) -> {
my_channels_latest : () -> (Result_9) query;
quit_channel : (UpdateMySettingInput, bool) -> (Result_1);
remove_member : (UpdateChannelMemberInput) -> (Result_1);
update_channel : (UpdateChannelInput) -> (Result_10);
update_channel : (UpdateChannelInput) -> (Result_6);
update_manager : (UpdateChannelMemberInput) -> (Result_10);
update_member : (UpdateChannelMemberInput) -> (Result_10);
update_my_setting : (UpdateMySettingInput) -> (Result_1);
Expand Down
63 changes: 37 additions & 26 deletions src/ic_message_channel/src/api_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::hash_map::Entry;
use crate::{is_authenticated, store, types};

#[ic_cdk::update(guard = "is_authenticated")]
fn update_channel(input: types::UpdateChannelInput) -> Result<u64, String> {
fn update_channel(input: types::UpdateChannelInput) -> Result<types::Message, String> {
input.validate()?;

let caller = ic_cdk::caller();
Expand All @@ -23,18 +23,19 @@ fn update_channel(input: types::UpdateChannelInput) -> Result<u64, String> {
c.latest_message_at += 1;
c.latest_message_by = caller;

store::channel::add_sys_message(
Ok(store::channel::add_sys_message(
caller,
now_ms,
store::MessageId(input.id, c.latest_message_at),
types::SYS_MSG_CHANNEL_UPDATE_INFO.to_string(),
);
Ok(c.updated_at)
))
})
}

#[ic_cdk::update(guard = "is_authenticated")]
fn update_manager(input: types::UpdateChannelMemberInput) -> Result<u64, String> {
fn update_manager(
input: types::UpdateChannelMemberInput,
) -> Result<(u64, Option<types::Message>), String> {
input.validate()?;

let caller = ic_cdk::caller();
Expand Down Expand Up @@ -74,23 +75,29 @@ fn update_manager(input: types::UpdateChannelMemberInput) -> Result<u64, String>
Err("too many channels".to_string())?;
}

store::channel::add_sys_message(
caller,
Ok((
now_ms,
store::MessageId(input.id, c.latest_message_at),
format!(
"{}: {}",
types::SYS_MSG_CHANNEL_ADD_MANAGER,
input.member.to_text()
),
);
Some(store::channel::add_sys_message(
caller,
now_ms,
store::MessageId(input.id, c.latest_message_at),
format!(
"{}: {}",
types::SYS_MSG_CHANNEL_ADD_MANAGER,
input.member.to_text()
),
)),
))
} else {
Ok((now_ms, None))
}
Ok(c.updated_at)
})
}

#[ic_cdk::update(guard = "is_authenticated")]
fn update_member(input: types::UpdateChannelMemberInput) -> Result<u64, String> {
fn update_member(
input: types::UpdateChannelMemberInput,
) -> Result<(u64, Option<types::Message>), String> {
input.validate()?;

let caller = ic_cdk::caller();
Expand Down Expand Up @@ -122,18 +129,22 @@ fn update_member(input: types::UpdateChannelMemberInput) -> Result<u64, String>
if !store::state::user_add_channel(input.member, input.id, c.latest_message_at) {
Err("too many channels".to_string())?;
}
store::channel::add_sys_message(
caller,
Ok((
now_ms,
store::MessageId(input.id, c.latest_message_at),
format!(
"{}: {}",
types::SYS_MSG_CHANNEL_ADD_MEMBER,
input.member.to_text()
),
);
Some(store::channel::add_sys_message(
caller,
now_ms,
store::MessageId(input.id, c.latest_message_at),
format!(
"{}: {}",
types::SYS_MSG_CHANNEL_ADD_MEMBER,
input.member.to_text()
),
)),
))
} else {
Ok((now_ms, None))
}
Ok(c.updated_at)
})
}

Expand Down
10 changes: 8 additions & 2 deletions src/ic_message_channel/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,12 @@ pub mod channel {
})
}

pub fn add_sys_message(caller: Principal, now_ms: u64, mid: MessageId, message: String) {
pub fn add_sys_message(
caller: Principal,
now_ms: u64,
mid: MessageId,
message: String,
) -> types::Message {
if mid.1 == u32::MAX {
ic_cdk::trap("message id overflow");
}
Expand All @@ -366,10 +371,11 @@ pub mod channel {
created_by: caller,
payload: to_cbor_bytes(&message).into(),
};

let info = message.clone().into_info(mid.0, mid.1);
MESSAGE_STORE.with(|r| {
r.borrow_mut().insert(mid, message);
});
info
}

pub fn create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ChannelECDHInput = record {
type ChannelInfo = record {
id : nat32;
dek : blob;
gas : nat64;
updated_at : nat64;
members : vec principal;
managers : vec principal;
Expand All @@ -33,6 +34,7 @@ type ChannelInfo = record {
created_by : principal;
canister : principal;
image : text;
message_start : nat32;
latest_message_at : nat32;
latest_message_by : principal;
my_setting : ChannelSetting;
Expand Down Expand Up @@ -113,8 +115,10 @@ type StateInfo = record {
profile_canisters : vec principal;
names_total : nat64;
transfer_out_total : nat;
next_block_height : nat64;
users_total : nat64;
price : Price;
next_block_phash : blob;
cose_canisters : vec principal;
incoming_total : nat;
channel_canisters : vec principal;
Expand All @@ -134,7 +138,6 @@ type UserInfo = record {
username : opt text;
cose_canister : opt principal;
name : text;
paid : nat64;
image : text;
profile_canister : principal;
};
Expand All @@ -154,7 +157,7 @@ service : (opt ChainArgs) -> {
icrc3_get_blocks : (vec GetBlocksRequest) -> (GetBlocksResult) query;
icrc3_get_tip_certificate : () -> (opt ICRC3DataCertificate) query;
icrc3_supported_block_types : () -> (vec SupportedBlockType) query;
register_username : (text) -> (Result_3);
register_username : (text, opt text) -> (Result_3);
save_channel_kek : (ChannelKEKInput) -> (Result);
search_username : (text) -> (Result_6) query;
update_my_image : (text) -> (Result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ChannelECDHInput {
export interface ChannelInfo {
'id' : number,
'dek' : Uint8Array | number[],
'gas' : bigint,
'updated_at' : bigint,
'members' : Array<Principal>,
'managers' : Array<Principal>,
Expand All @@ -42,6 +43,7 @@ export interface ChannelInfo {
'created_by' : Principal,
'canister' : Principal,
'image' : string,
'message_start' : number,
'latest_message_at' : number,
'latest_message_by' : Principal,
'my_setting' : ChannelSetting,
Expand Down Expand Up @@ -135,8 +137,10 @@ export interface StateInfo {
'profile_canisters' : Array<Principal>,
'names_total' : bigint,
'transfer_out_total' : bigint,
'next_block_height' : bigint,
'users_total' : bigint,
'price' : Price,
'next_block_phash' : Uint8Array | number[],
'cose_canisters' : Array<Principal>,
'incoming_total' : bigint,
'channel_canisters' : Array<Principal>,
Expand All @@ -159,7 +163,6 @@ export interface UserInfo {
'username' : [] | [string],
'cose_canister' : [] | [Principal],
'name' : string,
'paid' : bigint,
'image' : string,
'profile_canister' : Principal,
}
Expand All @@ -182,7 +185,7 @@ export interface _SERVICE {
'icrc3_get_blocks' : ActorMethod<[Array<GetBlocksRequest>], GetBlocksResult>,
'icrc3_get_tip_certificate' : ActorMethod<[], [] | [ICRC3DataCertificate]>,
'icrc3_supported_block_types' : ActorMethod<[], Array<SupportedBlockType>>,
'register_username' : ActorMethod<[string], Result_3>,
'register_username' : ActorMethod<[string, [] | [string]], Result_3>,
'save_channel_kek' : ActorMethod<[ChannelKEKInput], Result>,
'search_username' : ActorMethod<[string], Result_6>,
'update_my_image' : ActorMethod<[string], Result>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const idlFactory = ({ IDL }) => {
'username' : IDL.Opt(IDL.Text),
'cose_canister' : IDL.Opt(IDL.Principal),
'name' : IDL.Text,
'paid' : IDL.Nat64,
'image' : IDL.Text,
'profile_canister' : IDL.Principal,
});
Expand Down Expand Up @@ -57,6 +56,7 @@ export const idlFactory = ({ IDL }) => {
const ChannelInfo = IDL.Record({
'id' : IDL.Nat32,
'dek' : IDL.Vec(IDL.Nat8),
'gas' : IDL.Nat64,
'updated_at' : IDL.Nat64,
'members' : IDL.Vec(IDL.Principal),
'managers' : IDL.Vec(IDL.Principal),
Expand All @@ -67,6 +67,7 @@ export const idlFactory = ({ IDL }) => {
'created_by' : IDL.Principal,
'canister' : IDL.Principal,
'image' : IDL.Text,
'message_start' : IDL.Nat32,
'latest_message_at' : IDL.Nat32,
'latest_message_by' : IDL.Principal,
'my_setting' : ChannelSetting,
Expand Down Expand Up @@ -125,8 +126,10 @@ export const idlFactory = ({ IDL }) => {
'profile_canisters' : IDL.Vec(IDL.Principal),
'names_total' : IDL.Nat64,
'transfer_out_total' : IDL.Nat,
'next_block_height' : IDL.Nat64,
'users_total' : IDL.Nat64,
'price' : Price,
'next_block_phash' : IDL.Vec(IDL.Nat8),
'cose_canisters' : IDL.Vec(IDL.Principal),
'incoming_total' : IDL.Nat,
'channel_canisters' : IDL.Vec(IDL.Principal),
Expand Down Expand Up @@ -222,7 +225,11 @@ export const idlFactory = ({ IDL }) => {
[IDL.Vec(SupportedBlockType)],
['query'],
),
'register_username' : IDL.Func([IDL.Text], [Result_3], []),
'register_username' : IDL.Func(
[IDL.Text, IDL.Opt(IDL.Text)],
[Result_3],
[],
),
'save_channel_kek' : IDL.Func([ChannelKEKInput], [Result], []),
'search_username' : IDL.Func([IDL.Text], [Result_6], ['query']),
'update_my_image' : IDL.Func([IDL.Text], [Result], []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type CanisterStatusType = variant { stopped; stopping; running };
type ChainArgs = variant { Upgrade : UpgradeArgs; Init : InitArgs };
type ChannelBasicInfo = record {
id : nat32;
gas : nat64;
updated_at : nat64;
name : text;
paid : nat64;
Expand All @@ -38,6 +39,7 @@ type ChannelECDHInput = record {
type ChannelInfo = record {
id : nat32;
dek : blob;
gas : nat64;
updated_at : nat64;
members : vec principal;
managers : vec principal;
Expand All @@ -48,6 +50,7 @@ type ChannelInfo = record {
created_by : principal;
canister : principal;
image : text;
message_start : nat32;
latest_message_at : nat32;
latest_message_by : principal;
my_setting : ChannelSetting;
Expand Down Expand Up @@ -96,7 +99,7 @@ type QueryStats = record {
};
type Result = variant { Ok : AddMessageOutput; Err : text };
type Result_1 = variant { Ok; Err : text };
type Result_10 = variant { Ok : nat64; Err : text };
type Result_10 = variant { Ok : record { nat64; opt Message }; Err : text };
type Result_2 = variant { Ok : ChannelInfo; Err : text };
type Result_3 = variant { Ok : vec ChannelBasicInfo; Err : text };
type Result_4 = variant { Ok : CanisterStatusResponse; Err : text };
Expand All @@ -107,8 +110,10 @@ type Result_8 = variant { Ok : vec Message; Err : text };
type Result_9 = variant { Ok : vec record { nat32; nat32 }; Err : text };
type StateInfo = record {
channel_id : nat32;
incoming_gas : nat;
managers : vec principal;
name : text;
burned_gas : nat;
channels_total : nat64;
messages_total : nat64;
};
Expand Down Expand Up @@ -145,7 +150,7 @@ service : (opt ChainArgs) -> {
my_channels_latest : () -> (Result_9) query;
quit_channel : (UpdateMySettingInput, bool) -> (Result_1);
remove_member : (UpdateChannelMemberInput) -> (Result_1);
update_channel : (UpdateChannelInput) -> (Result_10);
update_channel : (UpdateChannelInput) -> (Result_6);
update_manager : (UpdateChannelMemberInput) -> (Result_10);
update_member : (UpdateChannelMemberInput) -> (Result_10);
update_my_setting : (UpdateMySettingInput) -> (Result_1);
Expand Down
Loading

0 comments on commit 4713f33

Please sign in to comment.