refactor(quark): port edit_user, fetch_dms, fetch_profile, fetch_self, fetch_user_flags, fetch_user, find_mutual, open_dm

#283
This commit is contained in:
Paul Makles
2024-04-07 19:38:08 +01:00
parent 49bb235938
commit b2d3344ddd
16 changed files with 286 additions and 208 deletions

View File

@@ -5,6 +5,7 @@ use revolt_models::v0::{self, MessageAuthor};
use revolt_permissions::OverrideField;
use revolt_result::Result;
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use crate::{
events::client::EventV1, tasks::ack::AckEvent, Database, File, IntoDocumentPath, PartialServer,
@@ -295,6 +296,43 @@ impl Channel {
Ok(channel)
}
/// Create a DM (or return the existing one / saved messages)
pub async fn create_dm(db: &Database, user_a: &User, user_b: &User) -> Result<Channel> {
// Try to find existing channel
if let Ok(channel) = db.find_direct_message_channel(&user_a.id, &user_b.id).await {
Ok(channel)
} else {
let channel = if user_a.id == user_b.id {
// Create a new saved messages channel
Channel::SavedMessages {
id: Ulid::new().to_string(),
user: user_a.id.to_string(),
}
} else {
// Create a new DM channel
Channel::DirectMessage {
id: Ulid::new().to_string(),
active: true, // show by default
recipients: vec![user_a.id.clone(), user_b.id.clone()],
last_message_id: None,
}
};
db.insert_channel(&channel).await?;
match &channel {
Channel::DirectMessage { .. } => {
let event = EventV1::ChannelCreate(channel.clone().into());
event.clone().private(user_a.id.clone()).await;
event.private(user_b.id.clone()).await;
}
_ => {}
};
Ok(channel)
}
}
/// Add user to a group
pub async fn add_user_to_group(
&mut self,

View File

@@ -94,20 +94,22 @@ auto_derived!(
}
/// User's active status
#[derive(Default)]
pub struct UserStatus {
/// Custom status text
#[serde(skip_serializing_if = "String::is_empty", default)]
pub text: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
/// Current presence option
#[serde(skip_serializing_if = "Option::is_none")]
pub presence: Option<Presence>,
}
/// User's profile
#[derive(Default)]
pub struct UserProfile {
/// Text content on user's profile
#[serde(skip_serializing_if = "String::is_empty", default)]
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// Background visible on user's profile
#[serde(skip_serializing_if = "Option::is_none")]
pub background: Option<File>,
@@ -541,7 +543,7 @@ impl User {
FieldsUser::Avatar => self.avatar = None,
FieldsUser::StatusText => {
if let Some(x) = self.status.as_mut() {
x.text = String::new();
x.text = None;
}
}
FieldsUser::StatusPresence => {
@@ -551,7 +553,7 @@ impl User {
}
FieldsUser::ProfileContent => {
if let Some(x) = self.profile.as_mut() {
x.content = String::new();
x.content = None;
}
}
FieldsUser::ProfileBackground => {

View File

@@ -15,6 +15,7 @@ impl crate::Bot {
description: user
.profile
.map(|profile| profile.content)
.flatten()
.unwrap_or_default(),
}
}
@@ -892,6 +893,18 @@ impl From<crate::Presence> for Presence {
}
}
impl From<Presence> for crate::Presence {
fn from(value: Presence) -> crate::Presence {
match value {
Presence::Online => crate::Presence::Online,
Presence::Idle => crate::Presence::Idle,
Presence::Focus => crate::Presence::Focus,
Presence::Busy => crate::Presence::Busy,
Presence::Invisible => crate::Presence::Invisible,
}
}
}
impl From<crate::UserStatus> for UserStatus {
fn from(value: crate::UserStatus) -> Self {
UserStatus {

View File

@@ -3,12 +3,21 @@ use regex::Regex;
use super::File;
#[cfg(feature = "validator")]
use validator::Validate;
/// Regex for valid usernames
///
/// Block zero width space
/// Block lookalike characters
pub static RE_USERNAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\p{L}|[\d_.-])+$").unwrap());
/// Regex for valid display names
///
/// Block zero width space
/// Block newline and carriage return
pub static RE_DISPLAY_NAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[^\u200B\n\r]+$").unwrap());
auto_derived_partial!(
/// User
pub struct User {
@@ -123,20 +132,26 @@ auto_derived!(
}
/// User's active status
#[derive(Default)]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct UserStatus {
/// Custom status text
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "String::is_empty"))]
pub text: String,
#[validate(length(min = 0, max = 128))]
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub text: Option<String>,
/// Current presence option
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub presence: Option<Presence>,
}
/// User's profile
#[derive(Default)]
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct UserProfile {
/// Text content on user's profile
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "String::is_empty"))]
pub content: String,
#[validate(length(min = 0, max = 2000))]
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub content: Option<String>,
/// Background visible on user's profile
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub background: Option<File>,
@@ -182,6 +197,67 @@ auto_derived!(
Spam = 8,
}
/// New user profile data
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct DataUserProfile {
/// Text to set as user profile description
#[cfg_attr(feature = "validator", validate(length(min = 0, max = 2000)))]
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub content: Option<String>,
/// Attachment Id for background
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
pub background: Option<String>,
}
/// New user information
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct DataEditUser {
/// New display name
#[cfg_attr(
feature = "validator",
validate(length(min = 2, max = 32), regex = "RE_DISPLAY_NAME")
)]
pub display_name: Option<String>,
/// Attachment Id for avatar
#[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
pub avatar: Option<String>,
/// New user status
#[cfg_attr(feature = "validator", validate)]
pub status: Option<UserStatus>,
/// New user profile data
///
/// This is applied as a partial.
#[cfg_attr(feature = "validator", validate)]
pub profile: Option<DataUserProfile>,
/// Bitfield of user badges
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub badges: Option<i32>,
/// Enum of user flags
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub flags: Option<i32>,
/// Fields to remove from user object
#[cfg_attr(feature = "validator", validate(length(min = 1)))]
pub remove: Option<Vec<FieldsUser>>,
}
/// User flag reponse
pub struct FlagResponse {
/// Flags
pub flags: i32,
}
/// Mutual friends and servers response
pub struct MutualResponse {
/// Array of mutual user IDs that both users are friends with
pub users: Vec<String>,
/// Array of mutual server IDs that both users are in
pub servers: Vec<String>,
}
/// Bot information for if the user is a bot
pub struct BotInformation {
/// Id of the owner of this bot

View File

@@ -53,6 +53,17 @@ impl PermissionValue {
self.has(permission as u64)
}
/// Throw if missing user permission
pub fn throw_if_lacking_user_permission(&self, permission: UserPermission) -> Result<()> {
if self.has_user_permission(permission) {
Ok(())
} else {
Err(create_error!(MissingPermission {
permission: permission.to_string()
}))
}
}
/// Throw if missing channel permission
pub fn throw_if_lacking_channel_permission(&self, permission: ChannelPermission) -> Result<()> {
if self.has_channel_permission(permission) {