From 67b9b8ceee75e006e330fcead301f698ab9c3595 Mon Sep 17 00:00:00 2001 From: Paul <paulmakles@gmail.com> Date: Tue, 25 May 2021 13:37:12 +0100 Subject: [PATCH] Users: Serve early adopter badge from server. --- set_version.sh | 2 +- src/database/entities/user.rs | 31 ++++++++++++++++++++++------- src/database/permissions/channel.rs | 6 +++--- src/util/variables.rs | 2 ++ src/version.rs | 2 +- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/set_version.sh b/set_version.sh index 7e7ea8e..44ddcfd 100755 --- a/set_version.sh +++ b/set_version.sh @@ -1,3 +1,3 @@ #!/bin/bash -export version=0.4.1-alpha.12 +export version=0.4.1-alpha.13 echo "pub const VERSION: &str = \"${version}\";" > src/version.rs diff --git a/src/database/entities/user.rs b/src/database/entities/user.rs index 48befa4..6648ad1 100644 --- a/src/database/entities/user.rs +++ b/src/database/entities/user.rs @@ -6,11 +6,15 @@ use mongodb::{ }; use serde::{Deserialize, Serialize}; use validator::Validate; +use num_enum::TryFromPrimitive; +use ulid::Ulid; +use std::ops; use crate::database::permissions::user::UserPermissions; use crate::database::*; use crate::notifications::websocket::is_online; use crate::util::result::{Error, Result}; +use crate::util::variables::EARLY_ADOPTER_BADGE; #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub enum RelationshipStatus { @@ -30,13 +34,6 @@ pub struct Relationship { pub status: RelationshipStatus, } -/* -pub enum Badge { - Developer = 1, - Translator = 2, -} -*/ - #[derive(Serialize, Deserialize, Debug)] pub enum Presence { Online, @@ -62,6 +59,17 @@ pub struct UserProfile { pub background: Option<File>, } +#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)] +#[repr(i32)] +pub enum Badges { + Developer = 1, + Translator = 2, + Supporter = 4, + EarlyAdopter = 256 +} + +impl_op_ex_commutative!(+ |a: &i32, b: &Badges| -> i32 { *a | *b as i32 }); + // When changing this struct, update notifications/payload.rs#80 #[derive(Serialize, Deserialize, Debug)] pub struct User { @@ -110,6 +118,15 @@ impl User { /// Mutate the user object to appear as seen by user. pub fn with(mut self, permissions: UserPermissions<[u32; 1]>) -> User { + let mut badges = self.badges.unwrap_or_else(|| 0); + if let Ok(id) = Ulid::from_string(&self.id) { + if id.datetime().timestamp_millis() < *EARLY_ADOPTER_BADGE { + badges = badges + Badges::EarlyAdopter; + } + } + + self.badges = Some(badges); + if permissions.get_view_profile() { self.online = Some(is_online(&self.id)); } else { diff --git a/src/database/permissions/channel.rs b/src/database/permissions/channel.rs index d58886b..2009bb4 100644 --- a/src/database/permissions/channel.rs +++ b/src/database/permissions/channel.rs @@ -16,6 +16,9 @@ pub enum ChannelPermission { VoiceCall = 16, } +impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u32 { *a as u32 | *b as u32 }); +impl_op_ex_commutative!(+ |a: &u32, b: &ChannelPermission| -> u32 { *a | *b as u32 }); + bitfield! { pub struct ChannelPermissions(MSB0 [u32]); u32; @@ -26,9 +29,6 @@ bitfield! { pub get_voice_call, _: 27; } -impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u32 { *a as u32 | *b as u32 }); -impl_op_ex_commutative!(+ |a: &u32, b: &ChannelPermission| -> u32 { *a | *b as u32 }); - impl<'a> PermissionCalculator<'a> { pub async fn calculate_channel(self) -> Result<u32> { let channel = if let Some(channel) = self.channel { diff --git a/src/util/variables.rs b/src/util/variables.rs index 788bb94..9d4a136 100644 --- a/src/util/variables.rs +++ b/src/util/variables.rs @@ -64,6 +64,8 @@ lazy_static! { // Application Logic Settings pub static ref MAX_GROUP_SIZE: usize = env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap(); + pub static ref EARLY_ADOPTER_BADGE: i64 = + env::var("REVOLT_EARLY_ADOPTER_BADGE").unwrap_or_else(|_| "0".to_string()).parse().unwrap(); } pub fn preflight_checks() { diff --git a/src/version.rs b/src/version.rs index be9c274..3ea1721 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1 +1 @@ -pub const VERSION: &str = "0.4.1-alpha.12"; +pub const VERSION: &str = "0.4.1-alpha.13"; -- GitLab