Skip to content

Commit

Permalink
Require Diamond membership to set a display name (#4910)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 4, 2023
1 parent 81d08c6 commit a954b41
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 85 deletions.
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 @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Changed

- Remove `display_name` from `register_user` args ([#4910](https://github.com/open-chat-labs/open-chat/pull/4910))

## [[2.0.948](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.948-local_user_index)] - 2023-11-28

### Added
Expand Down
4 changes: 0 additions & 4 deletions backend/canisters/local_user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ type InviteUsersToGroupResponse = variant {

type RegisterUserArgs = record {
username : text;
display_name : opt text;
referral_code : opt text;
public_key : blob;
};
Expand All @@ -138,9 +137,6 @@ type RegisterUserResponse = variant {
UsernameInvalid;
UsernameTooShort : nat16;
UsernameTooLong : nat16;
DisplayNameInvalid;
DisplayNameTooShort : nat16;
DisplayNameTooLong : nat16;
CyclesBalanceTooLow;
InternalError : text;
PublicKeyInvalid : text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use types::UserId;
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub username: String,
pub display_name: Option<String>,
pub referral_code: Option<String>,
pub public_key: Vec<u8>,
}
Expand All @@ -19,9 +18,6 @@ pub enum Response {
UsernameInvalid,
UsernameTooShort(u16),
UsernameTooLong(u16),
DisplayNameInvalid,
DisplayNameTooShort(u16),
DisplayNameTooLong(u16),
CyclesBalanceTooLow,
InternalError(String),
PublicKeyInvalid(String),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use user_index_canister::{Event as UserIndexEvent, JoinUserToGroup, UserRegister
use utils::canister;
use utils::canister::CreateAndInstallError;
use utils::consts::{min_cycles_balance, CREATE_CANISTER_CYCLES_FEE};
use utils::text_validation::{validate_display_name, validate_username, UsernameValidationError};
use utils::text_validation::{validate_username, UsernameValidationError};
use x509_parser::prelude::FromDer;
use x509_parser::x509::SubjectPublicKeyInfo;

Expand Down Expand Up @@ -49,17 +49,7 @@ async fn register_user(args: Args) -> Response {
{
Ok(canister_id) => {
let user_id = canister_id.into();
mutate_state(|state| {
commit(
caller,
user_id,
args.username,
args.display_name,
wasm_version,
referral_code,
state,
)
});
mutate_state(|state| commit(caller, user_id, args.username, wasm_version, referral_code, state));
Success(SuccessResult {
user_id,
icp_account: default_ledger_account(user_id.into()),
Expand Down Expand Up @@ -121,15 +111,6 @@ fn prepare(args: &Args, state: &mut RuntimeState) -> Result<PrepareOk, Response>
Err(UsernameValidationError::Invalid) => return Err(UsernameInvalid),
};

if let Some(display_name) = args.display_name.as_ref() {
match validate_display_name(display_name) {
Ok(_) => {}
Err(UsernameValidationError::TooShort(s)) => return Err(DisplayNameTooShort(s.min_length as u16)),
Err(UsernameValidationError::TooLong(l)) => return Err(DisplayNameTooLong(l.max_length as u16)),
Err(UsernameValidationError::Invalid) => return Err(DisplayNameInvalid),
};
}

let openchat_bot_messages = if referral_code
.as_ref()
.filter(|c| matches!(c, ReferralCode::BtcMiami(_)))
Expand Down Expand Up @@ -171,7 +152,6 @@ fn prepare(args: &Args, state: &mut RuntimeState) -> Result<PrepareOk, Response>
proposals_bot_canister_id: state.data.proposals_bot_canister_id,
wasm_version: canister_wasm.version,
username: args.username.clone(),
display_name: args.display_name.clone(),
openchat_bot_messages,
test_mode: state.data.test_mode,
};
Expand All @@ -192,7 +172,6 @@ fn commit(
principal: Principal,
user_id: UserId,
username: String,
display_name: Option<String>,
wasm_version: BuildVersion,
referral_code: Option<ReferralCode>,
state: &mut RuntimeState,
Expand All @@ -205,7 +184,6 @@ fn commit(
principal,
user_id,
username: username.clone(),
display_name: display_name.clone(),
referred_by: referral_code.as_ref().and_then(|r| r.user()),
})));

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/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/).
- In modclub reports only show public message links ([#4847](https://github.com/open-chat-labs/open-chat/pull/4847))
- Add `local_user_index_canister_id` to group/community summaries ([#4857](https://github.com/open-chat-labs/open-chat/pull/4857))
- Switch to `c2c_send_message` when sending messages c2c to groups or channels ([#4895](https://github.com/open-chat-labs/open-chat/pull/4895))
- Remove `display_name` from `init` args ([#4910](https://github.com/open-chat-labs/open-chat/pull/4910))

## [[2.0.947](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.947-user)] - 2023-11-24

Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub struct Args {
pub proposals_bot_canister_id: CanisterId,
pub wasm_version: BuildVersion,
pub username: String,
pub display_name: Option<String>,
pub openchat_bot_messages: Vec<MessageContent>,
pub test_mode: bool,
}
3 changes: 1 addition & 2 deletions backend/canisters/user/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ impl Data {
notifications_canister_id: CanisterId,
proposals_bot_canister_id: CanisterId,
username: String,
display_name: Option<String>,
test_mode: bool,
now: TimestampMillis,
) -> Data {
Expand All @@ -213,7 +212,7 @@ impl Data {
is_platform_moderator: false,
hot_group_exclusions: HotGroupExclusions::default(),
username: Timestamped::new(username, now),
display_name: Timestamped::new(display_name, now),
display_name: Timestamped::default(),
bio: Timestamped::new("".to_string(), now),
cached_group_summaries: None,
storage_limit: 0,
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user/impl/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ fn init(args: Args) {
args.notifications_canister_id,
args.proposals_bot_canister_id,
args.username,
args.display_name,
args.test_mode,
env.now(),
);
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 @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Top up NNS neuron when users pay ICP for lifetime Diamond membership ([#4880](https://github.com/open-chat-labs/open-chat/pull/4880))
- Add `diamond_membership_status` to user summaries ([#4887](https://github.com/open-chat-labs/open-chat/pull/4887))
- Allow extending Diamond membership even if > 3 month remaining ([#4909](https://github.com/open-chat-labs/open-chat/pull/4909))
- Require Diamond membership to set a display name ([#4910](https://github.com/open-chat-labs/open-chat/pull/4910))

## [[2.0.952](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.952-user_index)] - 2023-11-28

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 @@ -24,6 +24,7 @@ type SetDisplayNameArgs = record {

type SetDisplayNameResponse = variant {
Success;
Unauthorized;
UserNotFound;
DisplayNameInvalid;
DisplayNameTooShort : nat16;
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub struct UserRegistered {
pub principal: Principal,
pub user_id: UserId,
pub username: String,
pub display_name: Option<String>,
pub referred_by: Option<UserId>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Args {
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
Unauthorized,
UserNotFound,
DisplayNameInvalid,
DisplayNameTooShort(u16),
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ impl Data {
proposals_bot_canister_id,
proposals_bot_canister_id.into(),
"ProposalsBot".to_string(),
None,
0,
None,
true,
Expand Down
6 changes: 2 additions & 4 deletions backend/canisters/user_index/impl/src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,16 @@ impl User {
principal: Principal,
user_id: UserId,
username: String,
display_name: Option<String>,
now: TimestampMillis,
referred_by: Option<UserId>,
is_bot: bool,
) -> User {
let display_name_upper = display_name.as_ref().map(|s| s.to_uppercase());
User {
principal,
user_id,
username,
display_name,
display_name_upper,
display_name: None,
display_name_upper: None,
date_created: now,
date_updated: now,
upgrade_in_progress: false,
Expand Down
13 changes: 5 additions & 8 deletions backend/canisters/user_index/impl/src/model/user_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ impl UserMap {
}
}

#[allow(clippy::too_many_arguments)]
pub fn register(
&mut self,
principal: Principal,
user_id: UserId,
username: String,
display_name: Option<String>,
now: TimestampMillis,
referred_by: Option<UserId>,
is_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, display_name, now, referred_by, is_bot);
let user = User::new(principal, user_id, username, now, referred_by, is_bot);
self.users.insert(user_id, user);

if let Some(ref_by) = referred_by {
Expand Down Expand Up @@ -282,7 +280,6 @@ impl UserMap {
user.principal,
user.user_id,
user.username.clone(),
user.display_name.clone(),
user.date_created,
None,
false,
Expand Down Expand Up @@ -346,9 +343,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(), None, 1, None, false);
user_map.register(principal2, user_id2, username2.clone(), None, 2, None, false);
user_map.register(principal3, user_id3, username3.clone(), None, 3, None, false);
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);

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

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

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

if let Some(original) = user_map.get_by_principal(&principal) {
let mut updated = original.clone();
Expand Down
24 changes: 5 additions & 19 deletions backend/canisters/user_index/impl/src/updates/c2c_notify_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,7 @@ fn handle_event(event: Event, state: &mut RuntimeState) {
let caller: CanisterId = state.env.caller();

match event {
Event::UserRegistered(ev) => process_new_user(
ev.principal,
ev.username,
ev.display_name,
ev.user_id,
ev.referred_by,
caller,
state,
),
Event::UserRegistered(ev) => process_new_user(ev.principal, ev.username, ev.user_id, ev.referred_by, caller, state),
Event::UserJoinedGroup(ev) => {
state.push_event_to_local_user_index(
ev.user_id,
Expand Down Expand Up @@ -89,7 +81,6 @@ fn handle_event(event: Event, state: &mut RuntimeState) {
fn process_new_user(
caller: Principal,
username: String,
display_name: Option<String>,
user_id: UserId,
referred_by: Option<UserId>,
local_user_index_canister_id: CanisterId,
Expand All @@ -106,15 +97,10 @@ fn process_new_user(
}
};

state.data.users.register(
caller,
user_id,
username.clone(),
display_name.clone(),
now,
referred_by,
false,
);
state
.data
.users
.register(caller, user_id, username.clone(), now, referred_by, false);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn c2c_register_bot_impl(args: Args, state: &mut RuntimeState) -> Response {
state
.data
.users
.register(caller, user_id, args.username.clone(), None, now, None, true);
.register(caller, user_id, args.username.clone(), now, None, true);

state.push_event_to_all_local_user_indexes(
Event::UserRegistered(UserRegistered {
Expand Down
23 changes: 13 additions & 10 deletions backend/canisters/user_index/impl/src/updates/set_display_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ fn set_display_name(args: Args) -> Response {

fn set_display_name_impl(args: Args, state: &mut RuntimeState) -> Response {
let caller = state.env.caller();
if let Some(user) = state.data.users.get_by_principal(&caller) {
if let Some(display_name) = args.display_name.as_ref() {
match validate_display_name(display_name) {
Ok(_) => {}
Err(UsernameValidationError::TooShort(s)) => return DisplayNameTooShort(s.min_length as u16),
Err(UsernameValidationError::TooLong(l)) => return DisplayNameTooLong(l.max_length as u16),
Err(UsernameValidationError::Invalid) => return DisplayNameInvalid,
};
}

if let Some(display_name) = args.display_name.as_ref() {
match validate_display_name(display_name) {
Ok(_) => {}
Err(UsernameValidationError::TooShort(s)) => return DisplayNameTooShort(s.min_length as u16),
Err(UsernameValidationError::TooLong(l)) => return DisplayNameTooLong(l.max_length as u16),
Err(UsernameValidationError::Invalid) => return DisplayNameInvalid,
};
}
let now = state.env.now();
if !user.diamond_membership_details.is_active(now) && user.display_name.is_none() {
return Unauthorized;
}

if let Some(user) = state.data.users.get_by_principal(&caller) {
let mut user_to_update = user.clone();
user_to_update.display_name = args.display_name.clone();
let user_id = user.user_id;
let now = state.env.now();
match state.data.users.update(user_to_update, now) {
UpdateUserResult::Success => {
state.push_event_to_local_user_index(
Expand Down
1 change: 0 additions & 1 deletion backend/integration_tests/src/client/local_user_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub mod happy_path {
canister_id,
&local_user_index_canister::register_user::Args {
username: principal_to_username(principal),
display_name: None,
referral_code,
public_key,
},
Expand Down
1 change: 0 additions & 1 deletion backend/integration_tests/src/register_user_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ fn register_user_with_duplicate_username_appends_suffix() {
canister_ids.local_user_index,
&local_user_index_canister::register_user::Args {
username: username.clone(),
display_name: None,
referral_code: None,
public_key,
},
Expand Down
Loading

0 comments on commit a954b41

Please sign in to comment.