Skip to content

Commit

Permalink
Add diamond_membership_status to user summaries (#4887)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 29, 2023
1 parent 12cd575 commit a031cb1
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 57 deletions.
1 change: 1 addition & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- 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))

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

Expand Down
15 changes: 0 additions & 15 deletions backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,6 @@ type UserResponse = variant {
UserNotFound;
};

type UsersArgs = record {
user_groups : vec record {
users : vec UserId;
updated_since : TimestampMillis;
};
};

type UsersResponse = variant {
Success : record {
users : vec PartialUserSummary;
timestamp : TimestampMillis;
};
};

type UsersV2Args = record {
user_groups : vec record {
users : vec UserId;
Expand Down Expand Up @@ -359,7 +345,6 @@ service : {
user : (UserArgs) -> (UserResponse) query;

// Gets some users by id
users : (UsersArgs) -> (UsersResponse) query;
users_v2 : (UsersV2Args) -> (UsersV2Response) query;

// Search for users matching some query
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ fn main() {
generate_candid_method!(user_index, suspected_bots, query);
generate_candid_method!(user_index, user, query);
generate_candid_method!(user_index, user_registration_canister, query);
generate_candid_method!(user_index, users, query);
generate_candid_method!(user_index, users_v2, query);

generate_candid_method!(user_index, add_platform_moderator, update);
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ pub mod search;
pub mod suspected_bots;
pub mod user;
pub mod user_registration_canister;
pub mod users;
pub mod users_v2;
16 changes: 0 additions & 16 deletions backend/canisters/user_index/api/src/queries/users.rs

This file was deleted.

2 changes: 1 addition & 1 deletion backend/canisters/user_index/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ generate_query_call!(search);
generate_query_call!(platform_moderators);
generate_query_call!(platform_operators);
generate_query_call!(user);
generate_query_call!(users);
generate_query_call!(users_v2);

// Updates
generate_update_call!(add_local_user_index_canister);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use serde::{Deserialize, Serialize};
use std::cmp::max;
use types::{
Cryptocurrency, DiamondMembershipDetails, DiamondMembershipPlanDuration, DiamondMembershipSubscription, Milliseconds,
TimestampMillis,
Cryptocurrency, DiamondMembershipDetails, DiamondMembershipPlanDuration, DiamondMembershipStatus,
DiamondMembershipSubscription, Milliseconds, TimestampMillis,
};
use user_index_canister::pay_for_diamond_membership::CannotExtendResult;
use utils::time::DAY_IN_MS;

const LIFETIME_TIMESTAMP: TimestampMillis = 30000000000000; // This timestamp is in the year 2920

#[derive(Serialize, Deserialize, Clone, Default)]
#[serde(from = "DiamondMembershipDetailsInternalCombined")]
pub struct DiamondMembershipDetailsInternal {
Expand Down Expand Up @@ -71,6 +73,14 @@ impl DiamondMembershipDetailsInternal {
self.expires_at.map_or(false, |ts| now < ts)
}

pub fn status(&self, now: TimestampMillis) -> DiamondMembershipStatus {
match self.expires_at {
Some(ts) if ts > LIFETIME_TIMESTAMP => DiamondMembershipStatus::Lifetime,
Some(ts) if ts > now => DiamondMembershipStatus::Active,
_ => DiamondMembershipStatus::Inactive,
}
}

pub fn is_recurring(&self) -> bool {
self.subscription.is_active()
}
Expand Down Expand Up @@ -112,7 +122,7 @@ impl DiamondMembershipDetailsInternal {
}

pub fn is_lifetime_diamond_member(&self) -> bool {
self.expires_at > Some(30000000000000) // This timestamp is in the year 2920
self.expires_at > Some(LIFETIME_TIMESTAMP)
}

#[allow(clippy::too_many_arguments)]
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/impl/src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl User {
is_bot: self.is_bot,
suspended: self.suspension_details.is_some(),
diamond_member: self.diamond_membership_details.is_active(now),
diamond_membership_status: self.diamond_membership_details.status(now),
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions backend/libraries/types/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -1060,15 +1060,7 @@ type UserSummary = record {
is_bot : bool;
suspended : bool;
diamond_member : bool;
};

type PartialUserSummary = record {
user_id : UserId;
username : opt text;
avatar_id : opt nat;
is_bot : bool;
suspended : bool;
diamond_member : bool;
diamond_membership_status : DiamondMembershipStatus;
};

type BuildVersion = record {
Expand Down Expand Up @@ -1449,6 +1441,12 @@ type DiamondMembershipSubscription = variant {
OneYear;
};

type DiamondMembershipStatus = variant {
Inactive;
Active;
Lifetime;
};

type AccessGate = variant {
DiamondMember;
VerifiedCredential : VerifiedCredentialGate;
Expand Down
8 changes: 8 additions & 0 deletions backend/libraries/types/src/diamond_membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ pub struct DiamondMembershipDetails {
pub subscription: DiamondMembershipSubscription,
}

#[derive(CandidType, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(u8)]
pub enum DiamondMembershipStatus {
Inactive = 0,
Active = 1,
Lifetime = 2,
}

#[derive(CandidType, Serialize, Deserialize, Clone, Copy, Debug)]
#[repr(u8)]
pub enum DiamondMembershipPlanDuration {
Expand Down
13 changes: 2 additions & 11 deletions backend/libraries/types/src/user_summary.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::UserId;
use crate::{DiamondMembershipStatus, UserId};
use candid::CandidType;
use serde::{Deserialize, Serialize};

Expand All @@ -11,14 +11,5 @@ pub struct UserSummary {
pub is_bot: bool,
pub suspended: bool,
pub diamond_member: bool,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct PartialUserSummary {
pub user_id: UserId,
pub username: Option<String>,
pub avatar_id: Option<u128>,
pub is_bot: bool,
pub suspended: bool,
pub diamond_member: bool,
pub diamond_membership_status: DiamondMembershipStatus,
}

0 comments on commit a031cb1

Please sign in to comment.