Skip to content

Commit

Permalink
uploaded changes
Browse files Browse the repository at this point in the history
  • Loading branch information
toastxc committed Dec 27, 2023
1 parent 25016ef commit 89213f6
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 17 deletions.
24 changes: 16 additions & 8 deletions crates/delta/src/routes/servers/member_fetch.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use revolt_quark::{
models::{Member, User},
perms, Db, Ref, Result,
};
use revolt_quark::models::server_member::MemberResponse;
use revolt_quark::{models::User, perms, Db, Ref, Result};
use rocket::serde::json::Json;

/// # Fetch Member
///
/// Retrieve a member.
#[openapi(tag = "Server Members")]
#[get("/<target>/members/<member>")]
pub async fn req(db: &Db, user: User, target: Ref, member: Ref) -> Result<Json<Member>> {
#[get("/<target>/members/<member>?<roles>")]
pub async fn req(
db: &Db,
user: User,
target: Ref,
member: Ref,
roles: Option<bool>,
) -> Result<Json<MemberResponse>> {
let server = target.as_server(db).await?;
perms(&user).server(&server).calc(db).await?;

member.as_member(db, &server.id).await.map(Json)
let member_response: MemberResponse = match roles {
Some(true) => member.as_member_with_roles(db, &server.id).await?.into(),
_ => member.as_member(db, &server.id).await?.into(),
};

Ok(Json(member_response))
}
2 changes: 2 additions & 0 deletions crates/delta/src/routes/servers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod permissions_set_default;
mod roles_create;
mod roles_delete;
mod roles_edit;
mod roles_fetch;
mod server_ack;
mod server_create;
mod server_delete;
Expand All @@ -42,6 +43,7 @@ pub fn routes() -> (Vec<Route>, OpenApi) {
invites_fetch::req,
roles_create::req,
roles_edit::req,
roles_fetch::req,
roles_delete::req,
permissions_set::req,
permissions_set_default::req,
Expand Down
19 changes: 19 additions & 0 deletions crates/delta/src/routes/servers/roles_fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use revolt_quark::{models::server::Role, Db, Error, Ref, Result};
use rocket::serde::json::Json;

/// # Fetch Role
///
/// Fetch a role by its id.
#[openapi(tag = "Server Permissions")]
#[get("/<target>/roles/<role_id>")]
pub async fn req(db: &Db, target: Ref, role_id: String) -> Result<Json<Role>> {
let server = target.as_server(db).await?;

let role = server.roles.get(&role_id);

if let Some(role) = role {
Ok(Json(role.clone()))
} else {
Err(Error::NotFound)
}
}
7 changes: 6 additions & 1 deletion crates/quark/src/impl/dummy/servers/server_member.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::models::server_member::{FieldsMember, Member, MemberCompositeKey, PartialMember};
use crate::models::server_member::{
FieldsMember, Member, MemberCompositeKey, MemberWithRoles, PartialMember,
};
use crate::{AbstractServerMember, Result};

use super::super::DummyDb;
Expand All @@ -8,6 +10,9 @@ impl AbstractServerMember for DummyDb {
async fn fetch_member(&self, server: &str, user: &str) -> Result<Member> {
Ok(Member::new(server.into(), user.into()))
}
async fn fetch_member_with_roles(&self, server: &str, user: &str) -> Result<MemberWithRoles> {
Ok(MemberWithRoles::new(server.into(), user.into()))
}

async fn insert_member(&self, member: &Member) -> Result<()> {
info!("Create {member:?}");
Expand Down
31 changes: 30 additions & 1 deletion crates/quark/src/impl/generic/servers/server_member.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::collections::HashMap;

use iso8601_timestamp::Timestamp;

use crate::{
events::client::EventV1,
models::{
server_member::{FieldsMember, MemberCompositeKey, PartialMember},
server::Role,
server_member::{FieldsMember, MemberCompositeKey, MemberWithRoles, PartialMember},
Member, Server,
},
Database, Result,
Expand Down Expand Up @@ -81,4 +84,30 @@ impl Member {
FieldsMember::Timeout => self.timeout = None,
}
}

pub fn with_roles(&self, roles: HashMap<String, Role>) -> MemberWithRoles {
MemberWithRoles {
id: self.id.clone(),
joined_at: self.joined_at,
nickname: self.nickname.clone(),
avatar: self.avatar.clone(),
roles,
timeout: self.timeout,
}
}
}
impl MemberWithRoles {
pub fn new(server_id: String, user_id: String) -> Self {
Self {
id: MemberCompositeKey {
server: server_id,
user: user_id,
},
joined_at: Timestamp::now_utc(),
nickname: None,
avatar: None,
roles: HashMap::new(),
timeout: None,
}
}
}
13 changes: 10 additions & 3 deletions crates/quark/src/impl/mongo/servers/server_member.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bson::Document;

use super::super::MongoDb;
use crate::models::server_member::MemberWithRoles;
use crate::models::server_member::{FieldsMember, Member, MemberCompositeKey, PartialMember};
use crate::r#impl::mongo::IntoDocumentPath;
use crate::{AbstractServerMember, Error, Result};

use super::super::MongoDb;
use crate::{AbstractServer, AbstractServerMember, Error, Result};

static COL: &str = "server_members";

Expand All @@ -21,6 +21,13 @@ impl AbstractServerMember for MongoDb {
.await
}

async fn fetch_member_with_roles(&self, server: &str, user: &str) -> Result<MemberWithRoles> {
Ok(self
.fetch_member(server, user)
.await?
.with_roles(self.fetch_server(server).await?.roles))
}

async fn insert_member(&self, member: &Member) -> Result<()> {
self.insert_one(COL, member).await.map(|_| ())
}
Expand Down
49 changes: 49 additions & 0 deletions crates/quark/src/models/servers/server_member.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::collections::HashMap;

use iso8601_timestamp::Timestamp;

use serde::{Deserialize, Serialize};

use crate::models::attachment::File;

use super::server::Role;

/// Composite primary key consisting of server and user id
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Default)]
pub struct MemberCompositeKey {
Expand Down Expand Up @@ -41,6 +46,50 @@ pub struct Member {
pub timeout: Option<Timestamp>,
}

/// Representation of a member of a server on Revolt With Role Data
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct)]
#[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)]
#[opt_skip_serializing_none]
#[opt_some_priority]
pub struct MemberWithRoles {
/// Unique member id
#[serde(rename = "_id")]
pub id: MemberCompositeKey,

/// Time at which this user joined the server
pub joined_at: Timestamp,

/// Member's nickname
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
/// Avatar attachment
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar: Option<File>,

/// Member's roles
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
pub roles: HashMap<String, Role>,
/// Timestamp this member is timed out until
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<Timestamp>,
}

#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub enum MemberResponse {
Members(Member),
MembersWithRoles(MemberWithRoles),
}

impl From<Member> for MemberResponse {
fn from(value: Member) -> Self {
Self::Members(value)
}
}
impl From<MemberWithRoles> for MemberResponse {
fn from(value: MemberWithRoles) -> Self {
Self::MembersWithRoles(value)
}
}
/// Optional fields on server member object
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Clone)]
pub enum FieldsMember {
Expand Down
6 changes: 5 additions & 1 deletion crates/quark/src/traits/servers/server_member.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use crate::models::server_member::{FieldsMember, Member, MemberCompositeKey, PartialMember};
use crate::models::server_member::{
FieldsMember, Member, MemberCompositeKey, MemberWithRoles, PartialMember,
};
use crate::Result;

#[async_trait]
pub trait AbstractServerMember: Sync + Send {
/// Fetch a server member by their id
async fn fetch_member(&self, server: &str, user: &str) -> Result<Member>;

async fn fetch_member_with_roles(&self, server: &str, user: &str) -> Result<MemberWithRoles>;

/// Insert a new server member into the database
async fn insert_member(&self, member: &Member) -> Result<()>;

Expand Down
13 changes: 10 additions & 3 deletions crates/quark/src/util/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use schemars::schema::{InstanceType, Schema, SchemaObject, SingleOrVec};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::models::{
Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User,
};
use crate::models::server_member::MemberWithRoles;
use crate::models::{Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User};
use crate::{Database, Error, Result};

/// Reference to some object in the database
Expand Down Expand Up @@ -66,6 +65,14 @@ impl Ref {
db.fetch_member(server, &self.id).await
}

pub async fn as_member_with_roles(
&self,
db: &Database,
server: &str,
) -> Result<MemberWithRoles> {
db.fetch_member_with_roles(server, &self.id).await
}

/// Fetch ban from Ref
pub async fn as_ban(&self, db: &Database, server: &str) -> Result<ServerBan> {
db.fetch_ban(server, &self.id).await
Expand Down

0 comments on commit 89213f6

Please sign in to comment.