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

Implement register_bot including bot schema #6928

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use airdrop_bot_canister::set_avatar::*;
use canister_tracing_macros::trace;
use ic_cdk::update;
use types::{CanisterId, Timestamped};
use utils::document_validation::validate_avatar;
use utils::document::validate_avatar;

#[update(guard = "caller_is_admin")]
#[trace]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use group_chat_core::GroupChatCore;
use rand::Rng;
use types::{MultiUserChat, UserType};
use url::Url;
use utils::document_validation::validate_avatar;
use utils::document::validate_avatar;
use utils::text_validation::{
validate_description, validate_group_name, validate_rules, NameValidationError, RulesValidationError,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use types::{
CommunityPermissionsChanged, CommunityVisibilityChanged, Document, GroupDescriptionChanged, GroupNameChanged,
GroupRulesChanged, OptionUpdate, OptionalCommunityPermissions, PrimaryLanguageChanged, Timestamped, UserId,
};
use utils::document_validation::{validate_avatar, validate_banner};
use utils::document::{validate_avatar, validate_banner};
use utils::text_validation::{
validate_community_name, validate_description, validate_rules, NameValidationError, RulesValidationError,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async fn c2c_handle_bot_messages(
let user_type = match verify_user(local_user_index_canister_id, user_id).await {
Some(UserType::Bot) => UserType::Bot,
Some(UserType::OcControlledBot) => UserType::OcControlledBot,
Some(UserType::BotV2) => UserType::BotV2,
_ => panic!("This request is not from a bot registered with OpenChat"),
};
(user_id, user_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::HashSet;
use tracing::error;
use types::{CanisterId, CommunityId};
use user_canister::create_community::{Response::*, *};
use utils::document_validation::{validate_avatar, validate_banner};
use utils::document::{validate_avatar, validate_banner};
use utils::text_validation::{
validate_community_name, validate_description, validate_group_name, validate_rules, NameValidationError,
RulesValidationError,
Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/user/impl/src/updates/create_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use group_index_canister::c2c_create_group;
use tracing::error;
use types::{CanisterId, ChatId};
use user_canister::create_group::{Response::*, *};
use utils::document_validation::validate_avatar;
use utils::document::validate_avatar;
use utils::text_validation::{
validate_description, validate_group_name, validate_rules, NameValidationError, RulesValidationError,
};
Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/user/impl/src/updates/set_avatar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use canister_api_macros::update;
use canister_tracing_macros::trace;
use types::{Achievement, CanisterId, Timestamped};
use user_canister::set_avatar::*;
use utils::document_validation::validate_avatar;
use utils::document::validate_avatar;

#[update(guard = "caller_is_owner", msgpack = true)]
#[trace]
Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Implement `register_bot` including bot schema ([#6928](https://github.com/open-chat-labs/open-chat/pull/6928))

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

### Changed
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type PublicKeyResponse = variant {

type CheckUsernameArgs = record {
username : text;
is_bot : bool;
};

type CheckUsernameResponse = variant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ts_export::ts_export;
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub username: String,
pub is_bot: bool,
}

#[ts_export(user_index, check_username)]
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/src/queries/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use types::{CurrentUserSummary, TimestampMillis, UserId, UserSummaryV2};
pub struct Args {
pub user_groups: Vec<UserGroup>,
pub users_suspended_since: Option<TimestampMillis>,
//pub bots_updated_since: Option<TimestampMillis>,
}

#[ts_export(user_index, users)]
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod mark_local_user_index_full;
pub mod mark_suspected_bot;
pub mod modclub_callback;
pub mod pay_for_diamond_membership;
pub mod register_bot;
pub mod register_external_achievement;
pub mod remove_platform_moderator;
pub mod remove_platform_operator;
Expand Down
45 changes: 45 additions & 0 deletions backend/canisters/user_index/api/src/updates/register_bot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use candid::{CandidType, Principal};
use human_readable::{HumanReadablePrincipal, ToHumanReadable};
use serde::{Deserialize, Serialize};
use types::{SlashCommandSchema, UserId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub principal: Principal,
pub owner: UserId,
pub name: String,
pub avatar: Option<String>, // Image as a data URL
pub endpoint: String,
pub description: String,
pub commands: Vec<SlashCommandSchema>,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
}

#[derive(Serialize)]
pub struct HumanReadableArgs {
principal: HumanReadablePrincipal,
owner: HumanReadablePrincipal,
name: String,
endpoint: String,
description: String,
commands: Vec<SlashCommandSchema>,
}

impl ToHumanReadable for Args {
type Target = HumanReadableArgs;

fn to_human_readable(&self) -> Self::Target {
HumanReadableArgs {
principal: self.principal.into(),
owner: Principal::from(self.owner).into(),
name: self.name.clone(),
endpoint: self.endpoint.clone(),
description: self.endpoint.clone(),
commands: self.commands.clone(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use candid::{CandidType, Principal};
use human_readable::{HumanReadablePrincipal, ToHumanReadable};
use serde::{Deserialize, Serialize};
use ts_export::ts_export;
use types::{CanisterId, TimestampMillis, UserId};

#[ts_export(user_index, pay_for_diamond_membership)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub id: u32,
Expand All @@ -18,7 +16,6 @@ pub struct Args {
pub max_awards: u32,
}

#[ts_export(user_index, pay_for_diamond_membership)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ testing = { path = "../../../libraries/testing" }
time = { workspace = true }
tracing = { workspace = true }
types = { path = "../../../libraries/types" }
url = { workspace = true }
user_canister = { path = "../../user/api" }
user_canister_c2c_client = { path = "../../user/c2c_client" }
user_index_canister = { path = "../api" }
Expand Down
8 changes: 4 additions & 4 deletions backend/canisters/user_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::time::Duration;
use types::{
BotConfig, BuildVersion, CanisterId, ChatId, ChildCanisterWasms, Cryptocurrency, Cycles, DiamondMembershipFees,
Milliseconds, TimestampMillis, Timestamped, UserId, UserType,
BuildVersion, CanisterId, ChatId, ChildCanisterWasms, Cryptocurrency, Cycles, DiamondMembershipFees, Milliseconds,
TimestampMillis, Timestamped, UserId, UserType,
};
use user_index_canister::ChildCanisterType;
use utils::canister::{CanistersRequiringUpgrade, FailedUpgradeCount};
Expand Down Expand Up @@ -463,7 +463,7 @@ impl Data {
now,
None,
UserType::OcControlledBot,
Some(BotConfig::default()),
None,
);

// Register the AirdropBot
Expand All @@ -475,7 +475,7 @@ impl Data {
now,
None,
UserType::OcControlledBot,
Some(BotConfig::default()),
None,
);

data
Expand Down
14 changes: 4 additions & 10 deletions backend/canisters/user_index/impl/src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use types::{
is_default, BotConfig, CyclesTopUp, CyclesTopUpInternal, PhoneNumber, RegistrationFee, SuspensionAction,
SuspensionDuration, TimestampMillis, UniquePersonProof, UserId, UserSummary, UserSummaryStable, UserSummaryV2,
UserSummaryVolatile, UserType,
is_default, CyclesTopUp, CyclesTopUpInternal, PhoneNumber, RegistrationFee, SuspensionAction, SuspensionDuration,
TimestampMillis, UniquePersonProof, UserId, UserSummary, UserSummaryStable, UserSummaryV2, UserSummaryVolatile, UserType,
};
use utils::time::MonthKey;

Expand Down Expand Up @@ -40,8 +39,6 @@ pub struct User {
pub referred_by: Option<UserId>,
#[serde(rename = "ut", default, skip_serializing_if = "is_default")]
pub user_type: UserType,
#[serde(rename = "bc", default, skip_serializing_if = "Option::is_none")]
pub bot_config: Option<BotConfig>,
#[serde(rename = "sd", default, skip_serializing_if = "Option::is_none")]
pub suspension_details: Option<SuspensionDetails>,
#[serde(
Expand Down Expand Up @@ -88,7 +85,7 @@ impl User {
now: TimestampMillis,
referred_by: Option<UserId>,
user_type: UserType,
bot_config: Option<BotConfig>,
avatar_id: Option<u128>,
) -> User {
let display_name_upper = display_name.as_ref().map(|dn| dn.to_uppercase());

Expand All @@ -101,13 +98,12 @@ impl User {
date_created: now,
date_updated: now,
cycle_top_ups: Vec::new(),
avatar_id: None,
avatar_id,
registration_fee: None,
account_billing: AccountBilling::default(),
phone_status: PhoneStatus::None,
referred_by,
user_type,
bot_config,
suspension_details: None,
diamond_membership_details: DiamondMembershipDetailsInternal::default(),
moderation_flags_enabled: 0,
Expand Down Expand Up @@ -156,7 +152,6 @@ impl User {
suspended: self.suspension_details.is_some(),
diamond_membership_status: self.diamond_membership_details.status(now),
is_unique_person: self.unique_person_proof.is_some(),
bot_config: self.bot_config.clone(),
}
}

Expand Down Expand Up @@ -246,7 +241,6 @@ impl Default for User {
phone_status: PhoneStatus::None,
referred_by: None,
user_type: UserType::User,
bot_config: None,
suspension_details: None,
diamond_membership_details: DiamondMembershipDetailsInternal::default(),
moderation_flags_enabled: 0,
Expand Down
Loading
Loading