Skip to content

Commit

Permalink
Add is_oc_controlled_bot to GlobalUser
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jul 25, 2024
1 parent d849203 commit dff84fc
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 8 deletions.
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
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
3 changes: 2 additions & 1 deletion 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
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

0 comments on commit dff84fc

Please sign in to comment.