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

Use UserType rather than is_bot and is_oc_controlled_bot #6116

Merged
merged 5 commits into from
Jul 26, 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
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Handle transfer fee changing in either direction ([#6064](https://github.com/open-chat-labs/open-chat/pull/6064))
- Bypass gates if user is invited ([#6110](https://github.com/open-chat-labs/open-chat/pull/6110))
- Return `is_invited` when previewing community/channel ([#6113](https://github.com/open-chat-labs/open-chat/pull/6113))
- Use `UserType` rather than `is_bot` and `is_oc_controlled_bot` ([#6116](https://github.com/open-chat-labs/open-chat/pull/6116))

### Fixed

Expand Down
6 changes: 5 additions & 1 deletion backend/canisters/community/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
use types::{AccessGate, BuildVersion, CanisterId, CommunityPermissions, Document, Milliseconds, Rules, SourceGroup, UserId};
use types::{
AccessGate, BuildVersion, CanisterId, CommunityPermissions, Document, Milliseconds, Rules, SourceGroup, UserId, UserType,
};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
Expand All @@ -14,6 +16,8 @@ pub struct Args {
pub primary_language: String,
pub created_by_principal: Principal,
pub created_by_user_id: UserId,
#[serde(default)]
pub created_by_user_type: UserType,
pub mark_active_duration: Milliseconds,
pub user_index_canister_id: CanisterId,
pub local_user_index_canister_id: CanisterId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
use types::{
ChannelId, CommunityCanisterChannelSummary, CommunityCanisterCommunitySummary, GateCheckFailedReason, TimestampMillis,
UniquePersonProof, UserId, VerifiedCredentialGateArgs,
UniquePersonProof, UserId, UserType, VerifiedCredentialGateArgs,
};

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand All @@ -13,6 +13,8 @@ pub struct Args {
pub invite_code: Option<u64>,
pub is_platform_moderator: bool,
pub is_bot: bool,
#[serde(default)]
pub user_type: UserType,
pub diamond_membership_expires_at: Option<TimestampMillis>,
pub verified_credential_args: Option<VerifiedCredentialGateArgs>,
pub unique_person_proof: Option<UniquePersonProof>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
use types::{
CommunityCanisterCommunitySummary, GateCheckFailedReason, TimestampMillis, UniquePersonProof, UserId,
CommunityCanisterCommunitySummary, GateCheckFailedReason, TimestampMillis, UniquePersonProof, UserId, UserType,
VerifiedCredentialGateArgs,
};

Expand All @@ -12,6 +12,8 @@ pub struct Args {
pub invite_code: Option<u64>,
pub is_platform_moderator: bool,
pub is_bot: bool,
#[serde(default)]
pub user_type: UserType,
pub diamond_membership_expires_at: Option<TimestampMillis>,
pub verified_credential_args: Option<VerifiedCredentialGateArgs>,
pub unique_person_proof: Option<UniquePersonProof>,
Expand Down
8 changes: 4 additions & 4 deletions backend/canisters/community/impl/src/jobs/import_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::cell::Cell;
use std::collections::HashMap;
use std::time::Duration;
use tracing::{info, trace};
use types::{ChannelId, ChannelLatestMessageIndex, Chat, ChatId, Empty, UserId, UsersBlocked};
use types::{ChannelId, ChannelLatestMessageIndex, Chat, ChatId, Empty, UserId, UserType, UsersBlocked};
use utils::consts::OPENCHAT_BOT_USER_ID;

const PAGE_SIZE: u32 = 19 * 102 * 1024; // Roughly 1.9MB (1.9 * 1024 * 1024)
Expand Down Expand Up @@ -166,12 +166,12 @@ pub(crate) async fn process_channel_members(group_id: ChatId, channel_id: Channe

let (members_to_add_to_community, local_user_index_canister_id) = mutate_state(|state| {
let channel = state.data.channels.get(&channel_id).unwrap();
let mut to_add: HashMap<UserId, bool> = HashMap::new();
for (user_id, is_bot) in channel.chat.members.iter().map(|m| (m.user_id, m.is_bot)) {
let mut to_add: HashMap<UserId, UserType> = HashMap::new();
for (user_id, user_type) in channel.chat.members.iter().map(|m| (m.user_id, m.user_type)) {
if let Some(member) = state.data.members.get_by_user_id_mut(&user_id) {
member.channels.insert(channel_id);
} else {
to_add.insert(user_id, is_bot);
to_add.insert(user_id, user_type);
}
}

Expand Down
12 changes: 10 additions & 2 deletions backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::time::Duration;
use types::{
AccessGate, Achievement, BuildVersion, CanisterId, ChatMetrics, CommunityCanisterCommunitySummary, CommunityMembership,
CommunityPermissions, CommunityRole, Cryptocurrency, Cycles, Document, Empty, FrozenGroupInfo, Milliseconds, Notification,
PaymentGate, Rules, TimestampMillis, Timestamped, UserId,
PaymentGate, Rules, TimestampMillis, Timestamped, UserId, UserType,
};
use types::{CommunityId, SNS_FEE_SHARE_PERCENT};
use user_canister::c2c_notify_achievement;
Expand Down Expand Up @@ -341,6 +341,7 @@ impl Data {
community_id: CommunityId,
created_by_principal: Principal,
created_by_user_id: UserId,
created_by_user_type: UserType,
is_public: bool,
name: String,
description: String,
Expand Down Expand Up @@ -370,13 +371,20 @@ impl Data {
let channels = Channels::new(
community_id,
created_by_user_id,
created_by_user_type,
default_channels,
default_channel_rules,
is_public,
rng,
now,
);
let members = CommunityMembers::new(created_by_principal, created_by_user_id, channels.public_channel_ids(), now);
let members = CommunityMembers::new(
created_by_principal,
created_by_user_id,
created_by_user_type,
channels.public_channel_ids(),
now,
);
let events = CommunityEvents::new(name.clone(), description.clone(), created_by_user_id, now);

Data {
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/community/impl/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn init(args: Args) {
env.canister_id().into(),
args.created_by_principal,
args.created_by_user_id,
args.created_by_user_type,
args.is_public,
args.name,
args.description,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::jobs::import_groups::finalize_group_import;
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::{read_state, Data};
use crate::{mutate_state, read_state, Data};
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use community_canister::post_upgrade::Args;
use ic_cdk::post_upgrade;
use instruction_counts_log::InstructionCountFunctionId;
use stable_memory::get_reader;
use tracing::info;
use utils::consts::OPENCHAT_BOT_USER_ID;

#[post_upgrade]
#[trace]
Expand All @@ -31,6 +32,12 @@ fn post_upgrade(args: Args) {

info!(version = %args.wasm_version, "Post-upgrade complete");

mutate_state(|state| {
let oc_controlled_bot_users = vec![OPENCHAT_BOT_USER_ID, state.data.proposals_bot_user_id];
state.data.members.set_user_types(&oc_controlled_bot_users);
state.data.channels.set_user_types(&oc_controlled_bot_users);
});

read_state(|state| {
let now = state.env.now();
state
Expand Down
14 changes: 12 additions & 2 deletions backend/canisters/community/impl/src/model/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::HashMap;
use types::{
ChannelId, ChannelMatch, CommunityCanisterChannelSummary, CommunityCanisterChannelSummaryUpdates, CommunityId,
GroupMembership, GroupMembershipUpdates, GroupPermissionRole, GroupPermissions, MultiUserChat, Rules, TimestampMillis,
Timestamped, UserId, MAX_THREADS_IN_SUMMARY,
Timestamped, UserId, UserType, MAX_THREADS_IN_SUMMARY,
};

use super::members::CommunityMembers;
Expand All @@ -28,9 +28,17 @@ pub struct Channel {
}

impl Channels {
pub fn set_user_types(&mut self, oc_controlled_bots: &[UserId]) {
for channel in self.channels.values_mut() {
channel.chat.set_user_types(oc_controlled_bots);
}
}

#[allow(clippy::too_many_arguments)]
pub fn new(
community_id: CommunityId,
created_by: UserId,
created_by_user_type: UserType,
default_channels: Vec<String>,
default_channel_rules: Option<Rules>,
is_community_public: bool,
Expand All @@ -48,6 +56,7 @@ impl Channels {
community_id,
name,
created_by,
created_by_user_type,
default_channel_rules.clone(),
is_community_public,
rng.gen(),
Expand Down Expand Up @@ -182,6 +191,7 @@ impl Channel {
community_id: CommunityId,
name: String,
created_by: UserId,
created_by_user_type: UserType,
channel_rules: Option<Rules>,
is_community_public: bool,
anonymized_id: u128,
Expand All @@ -207,7 +217,7 @@ impl Channel {
permissions,
None,
None,
false,
created_by_user_type,
anonymized_id,
now,
),
Expand Down
22 changes: 18 additions & 4 deletions backend/canisters/community/impl/src/model/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use rand::RngCore;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::Entry::Vacant;
use std::collections::{HashMap, HashSet};
use types::{ChannelId, CommunityMember, CommunityPermissions, CommunityRole, TimestampMillis, Timestamped, UserId, Version};
use types::{
ChannelId, CommunityMember, CommunityPermissions, CommunityRole, TimestampMillis, Timestamped, UserId, UserType, Version,
};

const MAX_MEMBERS_PER_COMMUNITY: u32 = 100_000;

Expand All @@ -21,9 +23,17 @@ pub struct CommunityMembers {
}

impl CommunityMembers {
pub fn set_user_types(&mut self, oc_controlled_bot_users: &[UserId]) {
for (user_id, member) in self.members.iter_mut().filter(|(_, m)| m.is_bot) {
member.user_type =
if oc_controlled_bot_users.contains(user_id) { UserType::OcControlledBot } else { UserType::Bot };
}
}

pub fn new(
creator_principal: Principal,
creator_user_id: UserId,
creator_user_type: UserType,
public_channels: Vec<ChannelId>,
now: TimestampMillis,
) -> CommunityMembers {
Expand All @@ -35,7 +45,8 @@ impl CommunityMembers {
channels: public_channels.into_iter().collect(),
channels_removed: Vec::new(),
rules_accepted: Some(Timestamped::new(Version::zero(), now)),
is_bot: false,
is_bot: creator_user_type.is_bot(),
user_type: creator_user_type,
display_name: Timestamped::default(),
};

Expand All @@ -50,7 +61,7 @@ impl CommunityMembers {
}
}

pub fn add(&mut self, user_id: UserId, principal: Principal, is_bot: bool, now: TimestampMillis) -> AddResult {
pub fn add(&mut self, user_id: UserId, principal: Principal, user_type: UserType, now: TimestampMillis) -> AddResult {
if self.blocked.contains(&user_id) {
AddResult::Blocked
} else {
Expand All @@ -64,7 +75,8 @@ impl CommunityMembers {
channels: HashSet::new(),
channels_removed: Vec::new(),
rules_accepted: None,
is_bot,
is_bot: user_type.is_bot(),
user_type,
display_name: Timestamped::default(),
};
e.insert(member.clone());
Expand Down Expand Up @@ -342,6 +354,8 @@ pub struct CommunityMemberInternal {
pub channels_removed: Vec<Timestamped<ChannelId>>,
pub rules_accepted: Option<Timestamped<Version>>,
pub is_bot: bool,
#[serde(default)]
pub user_type: UserType,
display_name: Timestamped<Option<String>>,
}

Expand Down
Loading
Loading