refactor(quark): port member_edit, member_experimental_query, member_fetch_all, member_remove, permissions_set_default, permissions_set

#283
This commit is contained in:
Paul Makles
2024-04-07 21:35:45 +01:00
parent 03340a2b5a
commit 9de963fb56
14 changed files with 308 additions and 216 deletions

View File

@@ -5,6 +5,8 @@ use crate::{events::client::EventV1, Database, File, RatelimitEvent};
use once_cell::sync::Lazy;
use rand::seq::SliceRandom;
use revolt_config::config;
use revolt_models::v0;
use revolt_presence::filter_online;
use revolt_result::{create_error, Error, ErrorType, Result};
use ulid::Ulid;
@@ -264,7 +266,27 @@ impl User {
Ok(username)
}
// Find a free discriminator for a given username
/// Helper function to fetch many users as a mutually connected user
/// (while optimising the online ID query)
pub async fn fetch_many_ids_as_mutuals(
db: &Database,
perspective: &User,
ids: &[String],
) -> Result<Vec<v0::User>> {
let online_ids = filter_online(ids).await;
Ok(db
.fetch_users(ids)
.await?
.into_iter()
.map(|user| {
let is_online = online_ids.contains(&user.id);
user.into_known(perspective, is_online)
})
.collect())
}
/// Find a free discriminator for a given username
pub async fn find_discriminator(
db: &Database,
username: &str,

View File

@@ -500,6 +500,17 @@ impl From<crate::FieldsMember> for FieldsMember {
}
}
impl From<FieldsMember> for crate::FieldsMember {
fn from(value: FieldsMember) -> crate::FieldsMember {
match value {
FieldsMember::Avatar => crate::FieldsMember::Avatar,
FieldsMember::Nickname => crate::FieldsMember::Nickname,
FieldsMember::Roles => crate::FieldsMember::Roles,
FieldsMember::Timeout => crate::FieldsMember::Timeout,
}
}
}
impl From<crate::RemovalIntention> for RemovalIntention {
fn from(value: crate::RemovalIntention) -> Self {
match value {
@@ -702,7 +713,8 @@ impl crate::User {
}
}
pub async fn into_known<'a, P>(self, perspective: P) -> User
/// Convert user object into user model assuming mutual connection
pub fn into_known<'a, P>(self, perspective: P, is_online: bool) -> User
where
P: Into<Option<&'a crate::User>>,
{
@@ -725,10 +737,7 @@ impl crate::User {
})
.unwrap_or_default();
// TODO: refactor all of this to be less code? (as in with the method above)
// TODO: also not sure if this is the best solution, but we don't want to
// TODO: fetch mutual information again here
let can_see_profile = relationship == RelationshipStatus::Friend;
let can_see_profile = relationship != RelationshipStatus::BlockedOther;
(relationship, can_see_profile)
}
} else {
@@ -768,7 +777,7 @@ impl crate::User {
privileged: self.privileged,
bot: self.bot.map(|bot| bot.into()),
relationship,
online: can_see_profile && revolt_presence::is_online(&self.id).await,
online: can_see_profile && is_online,
id: self.id,
}
}

View File

@@ -1,9 +1,17 @@
use super::File;
use std::collections::HashMap;
use super::{File, Role, User};
use iso8601_timestamp::Timestamp;
use once_cell::sync::Lazy;
use regex::Regex;
#[cfg(feature = "validator")]
use validator::Validate;
#[cfg(feature = "rocket")]
use rocket::FromForm;
/// Regex for valid role colours
///
/// Allows the use of named colours, rgb(a), variables and all gradients.
@@ -77,4 +85,46 @@ auto_derived!(
Kick,
Ban,
}
/// Member response
#[serde(untagged)]
pub enum MemberResponse {
Member(Member),
MemberWithRoles {
member: Member,
roles: HashMap<String, Role>,
},
}
/// Options for fetching all members
#[cfg_attr(feature = "rocket", derive(FromForm))]
pub struct OptionsFetchAllMembers {
/// Whether to exclude offline users
pub exclude_offline: Option<bool>,
}
/// Response with all members
pub struct AllMemberResponse {
/// List of members
pub members: Vec<Member>,
/// List of users
pub users: Vec<User>,
}
/// New member information
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct DataMemberEdit {
/// Member nickname
#[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
pub nickname: Option<String>,
/// Attachment Id to set for avatar
pub avatar: Option<String>,
/// Array of role ids
pub roles: Option<Vec<String>>,
/// Timestamp this member is timed out until
pub timeout: Option<Timestamp>,
/// Fields to remove from channel object
#[cfg_attr(feature = "validator", validate(length(min = 1)))]
pub remove: Option<Vec<FieldsMember>>,
}
);

View File

@@ -1,6 +1,6 @@
use super::{Channel, File};
use revolt_permissions::OverrideField;
use revolt_permissions::{Override, OverrideField};
use std::collections::HashMap;
#[cfg(feature = "validator")]
@@ -160,6 +160,12 @@ auto_derived!(
pub nsfw: Option<bool>,
}
/// New role permissions
pub struct DataSetServerRolePermission {
/// Allow / deny values for the role in this server.
pub permissions: Override,
}
/// Information returned when creating server
pub struct CreateServerLegacyResponse {
/// Server object