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

Add is_oc_controlled_bot to GlobalUser #6115

Merged
merged 3 commits into from
Jul 25, 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
4 changes: 4 additions & 0 deletions backend/canisters/local_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

- Add `is_oc_controlled_bot` to `GlobalUser` ([#6115](https://github.com/open-chat-labs/open-chat/pull/6115))

### Removed

- Remove `Invited` gate ([#6112](https://github.com/open-chat-labs/open-chat/pull/6112))
Expand Down
7 changes: 7 additions & 0 deletions backend/canisters/local_user_index/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ pub struct UserRegistered {
pub user_id: UserId,
pub user_principal: Principal,
pub username: String,
#[serde(default)]
pub is_bot: bool,
#[serde(default)]
pub is_oc_controlled_bot: bool,
pub referred_by: Option<UserId>,
}

Expand Down Expand Up @@ -161,10 +164,14 @@ pub struct DeleteUser {
pub struct GlobalUser {
pub user_id: UserId,
pub principal: Principal,
#[serde(default)]
pub is_bot: bool,
#[serde(default)]
pub is_platform_moderator: bool,
pub diamond_membership_expires_at: Option<TimestampMillis>,
pub unique_person_proof: Option<UniquePersonProof>,
#[serde(default)]
pub is_oc_controlled_bot: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ pub struct GlobalUserMap {
unique_person_proofs: HashMap<UserId, UniquePersonProof>,
platform_moderators: HashSet<UserId>,
bots: HashSet<UserId>,
#[serde(default)]
oc_controlled_bot_users: HashSet<UserId>,
diamond_membership_expiry_dates: HashMap<UserId, TimestampMillis>,
}

impl GlobalUserMap {
pub fn add(&mut self, principal: Principal, user_id: UserId, is_bot: bool) {
pub fn add(&mut self, principal: Principal, user_id: UserId, is_bot: bool, is_oc_controlled_bot: bool) {
self.user_id_to_principal.insert(user_id, principal);
self.principal_to_user_id.insert(principal, user_id);

if is_bot {
self.bots.insert(user_id);

if is_oc_controlled_bot {
self.oc_controlled_bot_users.insert(user_id);
}
}
}

Expand Down Expand Up @@ -95,13 +101,16 @@ impl GlobalUserMap {
}

fn hydrate_user(&self, user_id: UserId, principal: Principal) -> GlobalUser {
let is_bot = self.bots.contains(&user_id);

GlobalUser {
user_id,
principal,
is_bot: self.bots.contains(&user_id),
is_bot,
is_platform_moderator: self.platform_moderators.contains(&user_id),
diamond_membership_expires_at: self.diamond_membership_expiry_dates.get(&user_id).copied(),
unique_person_proof: self.unique_person_proofs.get(&user_id).cloned(),
is_oc_controlled_bot: is_bot && self.oc_controlled_bot_users.contains(&user_id),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ fn handle_event(event: Event, state: &mut RuntimeState) {
);
}
Event::UserRegistered(ev) => {
state.data.global_users.add(ev.user_principal, ev.user_id, ev.is_bot);
state
.data
.global_users
.add(ev.user_principal, ev.user_id, ev.is_bot, ev.is_oc_controlled_bot);

if let Some(referred_by) = ev.referred_by {
if state.data.local_users.get(&referred_by).is_some() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn commit(
) {
let now = state.env.now();
state.data.local_users.add(user_id, principal, wasm_version, now);
state.data.global_users.add(principal, user_id, false);
state.data.global_users.add(principal, user_id, false, false);

state.push_event_to_user_index(UserIndexEvent::UserRegistered(Box::new(UserRegistered {
principal,
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]

### Changed

- Set `is_oc_controlled_bot` to true when registering the ProposalsBot ([#6115](https://github.com/open-chat-labs/open-chat/pull/6115))

## [[2.0.1255](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1255-user_index)] - 2024-07-25

### Added
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ impl Data {
0,
None,
true,
true,
);

data
Expand Down
5 changes: 5 additions & 0 deletions backend/canisters/user_index/impl/src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct User {
pub referred_by: Option<UserId>,
#[serde(rename = "ib", default, skip_serializing_if = "is_default")]
pub is_bot: bool,
#[serde(rename = "ocb", default, skip_serializing_if = "is_default")]
pub is_oc_controlled_bot: bool,
#[serde(rename = "sd", default, skip_serializing_if = "Option::is_none")]
pub suspension_details: Option<SuspensionDetails>,
#[serde(
Expand Down Expand Up @@ -102,6 +104,7 @@ impl User {
now: TimestampMillis,
referred_by: Option<UserId>,
is_bot: bool,
is_oc_controlled_bot: bool,
) -> User {
#[allow(deprecated)]
User {
Expand All @@ -119,6 +122,7 @@ impl User {
phone_status: PhoneStatus::None,
referred_by,
is_bot,
is_oc_controlled_bot,
suspension_details: None,
diamond_membership_details: DiamondMembershipDetailsInternal::default(),
moderation_flags_enabled: 0,
Expand Down Expand Up @@ -242,6 +246,7 @@ impl Default for User {
phone_status: PhoneStatus::None,
referred_by: None,
is_bot: false,
is_oc_controlled_bot: false,
suspension_details: None,
diamond_membership_details: DiamondMembershipDetailsInternal::default(),
moderation_flags_enabled: 0,
Expand Down
12 changes: 7 additions & 5 deletions backend/canisters/user_index/impl/src/model/user_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ impl UserMap {
now: TimestampMillis,
referred_by: Option<UserId>,
is_bot: bool,
is_oc_controlled_bot: bool,
) {
self.username_to_user_id.insert(&username, user_id);
self.principal_to_user_id.insert(principal, user_id);

let user = User::new(principal, user_id, username, now, referred_by, is_bot);
let user = User::new(principal, user_id, username, now, referred_by, is_bot, is_oc_controlled_bot);
self.users.insert(user_id, user);

if let Some(ref_by) = referred_by {
Expand Down Expand Up @@ -380,6 +381,7 @@ impl UserMap {
user.date_created,
None,
false,
false,
);
self.update(user, date_created, false);
}
Expand Down Expand Up @@ -451,9 +453,9 @@ mod tests {
let user_id2: UserId = Principal::from_slice(&[3, 2]).into();
let user_id3: UserId = Principal::from_slice(&[3, 3]).into();

user_map.register(principal1, user_id1, username1.clone(), 1, None, false);
user_map.register(principal2, user_id2, username2.clone(), 2, None, false);
user_map.register(principal3, user_id3, username3.clone(), 3, None, false);
user_map.register(principal1, user_id1, username1.clone(), 1, None, false, false);
user_map.register(principal2, user_id2, username2.clone(), 2, None, false, false);
user_map.register(principal3, user_id3, username3.clone(), 3, None, false, false);

let principal_to_user_id: Vec<_> = user_map
.principal_to_user_id
Expand Down Expand Up @@ -490,7 +492,7 @@ mod tests {

let user_id = Principal::from_slice(&[1, 1]).into();

user_map.register(principal, user_id, username1, 1, None, false);
user_map.register(principal, user_id, username1, 1, None, false, false);

if let Some(original) = user_map.get_by_principal(&principal) {
let mut updated = original.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn prepare(args: &Args, state: &RuntimeState) -> Result<PrepareResult, Response>

fn commit(canister_id: CanisterId, wasm_version: BuildVersion, state: &mut RuntimeState) -> Response {
if state.data.local_index_map.add_index(canister_id, wasm_version) {
// We need to initialize the new local user index with all of the existing users
// We need to initialize the new local user index with all the existing users
for user in state.data.users.iter() {
state.data.user_index_event_sync_queue.push(
canister_id,
Expand All @@ -78,6 +78,7 @@ fn commit(canister_id: CanisterId, wasm_version: BuildVersion, state: &mut Runti
user_principal: user.principal,
username: user.username.clone(),
is_bot: user.is_bot,
is_oc_controlled_bot: user.is_oc_controlled_bot,
referred_by: user.referred_by,
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn process_new_user(
state
.data
.users
.register(caller, user_id, username.clone(), now, referred_by, false);
.register(caller, user_id, username.clone(), now, referred_by, false, false);

state.data.local_index_map.add_user(local_user_index_canister_id, user_id);

Expand All @@ -140,6 +140,7 @@ fn process_new_user(
user_principal: caller,
username: username.clone(),
is_bot: false,
is_oc_controlled_bot: false,
referred_by,
}),
Some(local_user_index_canister_id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ fn c2c_register_bot_impl(args: Args, state: &mut RuntimeState) -> Response {
state
.data
.users
.register(caller, user_id, args.username.clone(), now, None, true);
.register(caller, user_id, args.username.clone(), now, None, true, false);

state.push_event_to_all_local_user_indexes(
Event::UserRegistered(UserRegistered {
user_id,
user_principal: caller,
username: args.username,
is_bot: true,
is_oc_controlled_bot: false,
referred_by: None,
}),
None,
Expand Down
Loading