Skip to content

Commit

Permalink
Remove references to deleted users (#6241)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Aug 15, 2024
1 parent 9594bda commit 2c7f9ae
Show file tree
Hide file tree
Showing 27 changed files with 254 additions and 38 deletions.
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.

1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Support next batch of achievements ([#6230](https://github.com/open-chat-labs/open-chat/pull/6230))
- Remove references to deleted users ([#6241](https://github.com/open-chat-labs/open-chat/pull/6241))

## [[2.0.1287](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1287-community)] - 2024-08-13

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::BuildVersion;
use std::collections::HashSet;
use types::{BuildVersion, UserId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub deleted_users: HashSet<UserId>,
pub wasm_version: BuildVersion,
}
24 changes: 23 additions & 1 deletion backend/canisters/community/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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;
Expand Down Expand Up @@ -37,4 +37,26 @@ fn post_upgrade(args: Args) {
.data
.record_instructions_count(InstructionCountFunctionId::PostUpgrade, now)
});

mutate_state(|state| {
let now = state.env.now();
let cutoff = 1709251200000; // 1st March 2024
for channel in state.data.channels.iter_mut() {
channel.chat.process_deleted_users(&args.deleted_users, now);
}
let mut invites_to_remove = Vec::new();
for (user_id, invitation) in state.data.invited_users.iter() {
if invitation.timestamp < cutoff && args.deleted_users.contains(user_id) {
invites_to_remove.push(*user_id);
}
}
for invited in invites_to_remove {
state.data.invited_users.remove(&invited, now);
}
for blocked in state.data.members.blocked() {
if args.deleted_users.contains(&blocked) {
state.data.members.unblock(&blocked);
}
}
});
}
4 changes: 4 additions & 0 deletions backend/canisters/community/impl/src/model/invited_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ impl InvitedUsers {
pub fn len(&self) -> usize {
self.users.len()
}

pub fn iter(&self) -> impl Iterator<Item = (&UserId, &UserInvitation)> {
self.users.iter()
}
}
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Support next batch of achievements ([#6230](https://github.com/open-chat-labs/open-chat/pull/6230))
- Remove references to deleted users ([#6241](https://github.com/open-chat-labs/open-chat/pull/6241))

## [[2.0.1273](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1273-group)] - 2024-07-31

Expand Down
4 changes: 3 additions & 1 deletion backend/canisters/group/api/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::BuildVersion;
use std::collections::HashSet;
use types::{BuildVersion, UserId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub wasm_version: BuildVersion,
pub deleted_users: HashSet<UserId>,
}
4 changes: 3 additions & 1 deletion backend/canisters/group/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 group_canister::post_upgrade::Args;
Expand Down Expand Up @@ -30,4 +30,6 @@ fn post_upgrade(args: Args) {
.data
.record_instructions_count(InstructionCountFunctionId::PostUpgrade, now)
});

mutate_state(|state| state.data.chat.process_deleted_users(&args.deleted_users, state.env.now()));
}
4 changes: 4 additions & 0 deletions backend/canisters/local_group_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Changed

- Remove references to deleted users ([#6241](https://github.com/open-chat-labs/open-chat/pull/6241))

## [[2.0.1271](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1271-local_group_index)] - 2024-07-31

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::UserId;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub user_ids: Vec<UserId>,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
}
1 change: 1 addition & 0 deletions backend/canisters/local_group_index/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod c2c_set_community_upgrade_concurrency;
pub mod c2c_set_group_upgrade_concurrency;
pub mod c2c_set_max_concurrent_community_upgrades;
pub mod c2c_set_max_concurrent_group_upgrades;
pub mod c2c_sync_deleted_users;
pub mod c2c_trigger_upgrade;
pub mod c2c_upgrade_community_canister_wasm;
pub mod c2c_upgrade_group_canister_wasm;
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/local_group_index/c2c_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ generate_c2c_call!(c2c_set_community_upgrade_concurrency);
generate_c2c_call!(c2c_set_group_upgrade_concurrency);
generate_c2c_call!(c2c_set_max_concurrent_community_upgrades);
generate_c2c_call!(c2c_set_max_concurrent_group_upgrades);
generate_c2c_call!(c2c_sync_deleted_users);
generate_c2c_call!(c2c_trigger_upgrade);
generate_c2c_call!(c2c_upgrade_community_canister_wasm);
generate_c2c_call!(c2c_upgrade_group_canister_wasm);
8 changes: 8 additions & 0 deletions backend/canisters/local_group_index/impl/src/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ pub fn caller_is_group_index_canister() -> Result<(), String> {
}
}

pub fn caller_is_user_index_canister() -> Result<(), String> {
if read_state(|state| state.is_caller_user_index_canister()) {
Ok(())
} else {
Err("Caller is not the user_index canister".to_owned())
}
}

pub fn caller_is_local_group_or_community_canister() -> Result<(), String> {
if read_state(|state| state.is_caller_local_group_canister() || state.is_caller_local_community_canister()) {
Ok(())
Expand Down
11 changes: 11 additions & 0 deletions backend/canisters/local_group_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use event_store_utils::EventDeduper;
use model::local_group_map::LocalGroupMap;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::HashSet;
use std::time::Duration;
use types::{
BuildVersion, CanisterId, CanisterWasm, ChunkedCanisterWasm, Cycles, Milliseconds, TimestampMillis, Timestamped, UserId,
Expand Down Expand Up @@ -51,6 +52,11 @@ impl RuntimeState {
self.data.group_index_canister_id == caller
}

pub fn is_caller_user_index_canister(&self) -> bool {
let caller = self.env.caller();
self.data.user_index_canister_id == caller
}

pub fn is_caller_local_group_canister(&self) -> bool {
let caller = self.env.caller();
self.data.local_groups.get(&caller.into()).is_some()
Expand Down Expand Up @@ -109,6 +115,7 @@ impl RuntimeState {
},
group_upgrades_failed: group_upgrades_metrics.failed,
community_upgrades_failed: community_upgrades_metrics.failed,
deleted_users: self.data.deleted_users.len() as u32,
}
}
}
Expand Down Expand Up @@ -143,6 +150,8 @@ struct Data {
pub ic_root_key: Vec<u8>,
pub event_store_client: EventStoreClient<CdkRuntime>,
pub event_deduper: EventDeduper,
#[serde(default)]
pub deleted_users: HashSet<UserId>,
pub rng_seed: [u8; 32],
}

Expand Down Expand Up @@ -196,6 +205,7 @@ impl Data {
.with_flush_delay(Duration::from_millis(MINUTE_IN_MS))
.build(),
event_deduper: EventDeduper::default(),
deleted_users: HashSet::new(),
}
}
}
Expand Down Expand Up @@ -228,6 +238,7 @@ pub struct Metrics {
pub canister_ids: CanisterIds,
pub group_upgrades_failed: Vec<FailedUpgradeCount>,
pub community_upgrades_failed: Vec<FailedUpgradeCount>,
pub deleted_users: u32,
}

#[derive(Serialize, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ mod upgrade_groups {
deposit_cycles_if_needed,
args: group_canister::post_upgrade::Args {
wasm_version: new_wasm_version,
deleted_users: state.data.deleted_users.clone(),
},
mode: CanisterInstallMode::Upgrade(None),
stop_start_canister: true,
Expand Down Expand Up @@ -201,6 +202,7 @@ mod upgrade_communities {
deposit_cycles_if_needed,
args: community_canister::post_upgrade::Args {
wasm_version: new_wasm_version,
deleted_users: state.data.deleted_users.clone(),
},
mode: CanisterInstallMode::Upgrade(None),
stop_start_canister: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::guards::caller_is_user_index_canister;
use crate::{mutate_state, RuntimeState};
use canister_api_macros::update;
use canister_tracing_macros::trace;
use local_group_index_canister::c2c_sync_deleted_users::{Response::*, *};

#[update(guard = "caller_is_user_index_canister", msgpack = true)]
#[trace]
fn c2c_sync_deleted_users(args: Args) -> Response {
mutate_state(|state| c2c_sync_deleted_users_impl(args, state))
}

fn c2c_sync_deleted_users_impl(args: Args, state: &mut RuntimeState) -> Response {
state.data.deleted_users.extend(args.user_ids);
Success
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod c2c_set_community_upgrade_concurrency;
pub mod c2c_set_group_upgrade_concurrency;
pub mod c2c_set_max_concurrent_community_upgrades;
pub mod c2c_set_max_concurrent_group_upgrades;
mod c2c_sync_deleted_users;
pub mod c2c_trigger_upgrade;
pub mod c2c_upgrade_community_canister_wasm;
pub mod c2c_upgrade_group_canister_wasm;
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Replace `chit_balances` with `users_chit` which includes streak ([#6238](https://github.com/open-chat-labs/open-chat/pull/6238))
- Remove references to deleted users ([#6241](https://github.com/open-chat-labs/open-chat/pull/6241))

## [[2.0.1291](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1291-user_index)] - 2024-08-14

Expand Down
2 changes: 2 additions & 0 deletions backend/canisters/user_index/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ identity_canister = { path = "../../identity/api" }
identity_canister_c2c_client = { path = "../../identity/c2c_client" }
itertools = { workspace = true }
jwt = { path = "../../../libraries/jwt" }
local_group_index_canister = { path = "../../local_group_index/api" }
local_group_index_canister_c2c_client = { path = "../../local_group_index/c2c_client" }
local_user_index_canister = { path = "../../local_user_index/api" }
local_user_index_canister_c2c_client = { path = "../../local_user_index/c2c_client" }
ledger_utils = { path = "../../../libraries/ledger_utils" }
Expand Down
30 changes: 29 additions & 1 deletion backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::Data;
use crate::{read_state, Data};
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use ic_cdk::post_upgrade;
use stable_memory::get_reader;
use std::time::Duration;
use tracing::info;
use types::CanisterId;
use user_index_canister::post_upgrade::Args;
use utils::cycles::init_cycles_dispenser_client;

Expand All @@ -24,4 +26,30 @@ fn post_upgrade(args: Args) {
init_state(env, data, args.wasm_version);

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

ic_cdk_timers::set_timer(Duration::ZERO, || ic_cdk::spawn(sync_deleted_users()));
}

async fn sync_deleted_users() {
let (deleted_users, test_mode) = read_state(|state| {
(
state.data.deleted_users.iter().map(|u| u.user_id).collect(),
state.data.test_mode,
)
});

let local_group_indexes = if test_mode {
vec![CanisterId::from_text("sbhuw-gyaaa-aaaaf-bfynq-cai").unwrap()]
} else {
vec![
CanisterId::from_text("suaf3-hqaaa-aaaaf-bfyoa-cai").unwrap(),
CanisterId::from_text("ainth-qaaaa-aaaar-aaaba-cai").unwrap(),
]
};

let args = local_group_index_canister::c2c_sync_deleted_users::Args { user_ids: deleted_users };

for canister_id in local_group_indexes {
let _ = local_group_index_canister_c2c_client::c2c_sync_deleted_users(canister_id, &args).await;
}
}
4 changes: 4 additions & 0 deletions backend/canisters/user_index/impl/src/model/user_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ impl From<UserMapTrimmed> for UserMap {
}
}

user_map
.suspended_or_unsuspended_users
.retain(|(_, u)| !user_map.deleted_users.contains_key(u));

user_map
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn handle_event(event: Event, state: &mut RuntimeState) {
}

fn process_new_user(
caller: Principal,
principal: Principal,
username: String,
user_id: UserId,
referred_by: Option<UserId>,
Expand All @@ -133,14 +133,14 @@ fn process_new_user(
state
.data
.users
.register(caller, user_id, username.clone(), now, referred_by, UserType::User, None);
.register(principal, user_id, username.clone(), now, referred_by, UserType::User, None);

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

state.push_event_to_all_local_user_indexes(
LocalUserIndexEvent::UserRegistered(UserRegistered {
user_id,
user_principal: caller,
user_principal: principal,
username: username.clone(),
user_type: UserType::User,
referred_by,
Expand Down Expand Up @@ -181,15 +181,15 @@ You can change your username at any time by clicking \"Profile settings\" from t
}

state.data.storage_index_user_sync_queue.push(UserConfig {
user_id: caller,
user_id: principal,
byte_limit: 100 * ONE_MB,
});
crate::jobs::sync_users_to_storage_index::try_run_now(state);

state
.data
.identity_canister_user_sync_queue
.push_back((caller, Some(user_id)));
.push_back((principal, Some(user_id)));
crate::jobs::sync_users_to_identity_canister::try_run_now(state);

if let Some(referrer) = referred_by {
Expand Down
Loading

0 comments on commit 2c7f9ae

Please sign in to comment.