Servers: Add invites. Create, view and join.

This commit is contained in:
Paul
2021-06-03 20:00:08 +01:00
parent 812fa2a98f
commit 681b2b8ab6
21 changed files with 335 additions and 81 deletions

View File

@@ -3,7 +3,7 @@ use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use futures::StreamExt;
use mongodb::{
bson::{doc, from_document, to_document, Document},
bson::{doc, to_document, Document},
options::FindOptions,
};
use rocket_contrib::json::JsonValue;
@@ -76,22 +76,6 @@ impl Channel {
}
}
pub async fn get(id: &str) -> Result<Channel> {
let doc = get_collection("channels")
.find_one(doc! { "_id": id }, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "channel",
})?
.ok_or_else(|| Error::UnknownChannel)?;
from_document::<Channel>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "channel",
})
}
pub async fn publish(self) -> Result<()> {
get_collection("channels")
.insert_one(

View File

@@ -0,0 +1,88 @@
use mongodb::bson::doc;
use mongodb::bson::from_document;
use mongodb::bson::to_document;
use serde::{Deserialize, Serialize};
use crate::database::get_collection;
use crate::util::result::Error;
use crate::util::result::Result;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum Invite {
Server {
#[serde(rename = "_id")]
code: String,
creator: String,
channel: String,
},
Group {
#[serde(rename = "_id")]
code: String,
creator: String,
channel: String,
}, /* User {
code: String,
user: String
} */
}
impl Invite {
pub fn code(&self) -> &String {
match &self {
Invite::Server { code, .. } => code,
Invite::Group { code, .. } => code,
}
}
pub async fn get(code: &str) -> Result<Invite> {
let doc = get_collection("invites")
.find_one(doc! { "_id": code }, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "invite",
})?
.ok_or_else(|| Error::UnknownServer)?;
from_document::<Invite>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "invite",
})
}
pub async fn save(self) -> Result<()> {
get_collection("invites")
.insert_one(
to_document(&self).map_err(|_| Error::DatabaseError {
operation: "to_bson",
with: "invite",
})?,
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "invite",
})?;
Ok(())
}
pub async fn delete(&self) -> Result<()> {
get_collection("invites")
.delete_one(
doc! {
"_id": self.code()
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "delete_one",
with: "invite",
})?;
Ok(())
}
}

View File

@@ -1,4 +1,5 @@
mod channel;
mod invites;
mod message;
mod microservice;
mod server;
@@ -9,6 +10,7 @@ use microservice::*;
pub use autumn::*;
pub use channel::*;
pub use invites::*;
pub use january::*;
pub use message::*;
pub use server::*;

View File

@@ -17,14 +17,11 @@ pub struct MemberCompositeKey {
pub struct Member {
#[serde(rename = "_id")]
pub id: MemberCompositeKey,
pub nickname: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Invite {
pub code: String,
pub creator: String,
pub channel: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar: Option<File>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -54,22 +51,6 @@ pub struct Server {
}
impl Server {
pub async fn get(id: &str) -> Result<Server> {
let doc = get_collection("servers")
.find_one(doc! { "_id": id }, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "server",
})?
.ok_or_else(|| Error::UnknownServer)?;
from_document::<Server>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "server",
})
}
pub async fn publish(self) -> Result<()> {
get_collection("servers")
.insert_one(
@@ -105,4 +86,30 @@ impl Server {
pub async fn delete(&self) -> Result<()> {
unimplemented!()
}
pub async fn join_member(&self, id: &str) -> Result<()> {
get_collection("server_members")
.insert_one(
doc! {
"_id": {
"server": &self.id,
"user": &id
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "server_members",
})?;
ClientboundNotification::ServerMemberJoin {
id: self.id.clone(),
user: id.to_string()
}
.publish(self.id.clone());
Ok(())
}
}

View File

@@ -9,13 +9,20 @@ use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
pub struct Ref {
#[validate(length(min = 26, max = 26))]
#[validate(length(min = 1, max = 26))]
pub id: String,
}
impl Ref {
pub fn from_unchecked(id: String) -> Ref {
Ref { id }
}
pub fn from(id: String) -> Result<Ref> {
Ok(Ref { id })
let r = Ref { id };
r.validate()
.map_err(|error| Error::FailedValidation { error })?;
Ok(r)
}
pub async fn fetch<T: DeserializeOwned>(&self, collection: &'static str) -> Result<T> {
@@ -51,6 +58,10 @@ impl Ref {
self.fetch("servers").await
}
pub async fn fetch_invite(&self) -> Result<Invite> {
self.fetch("invites").await
}
pub async fn fetch_message(&self, channel: &Channel) -> Result<Message> {
let message: Message = self.fetch("messages").await?;
if &message.channel != channel.id() {

View File

@@ -14,6 +14,7 @@ pub enum ChannelPermission {
ManageMessages = 4,
ManageChannel = 8,
VoiceCall = 16,
InviteOthers = 32,
}
impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u32 { *a as u32 | *b as u32 });
@@ -27,6 +28,7 @@ bitfield! {
pub get_manage_messages, _: 29;
pub get_manage_channel, _: 28;
pub get_voice_call, _: 27;
pub get_invite_others, _: 26;
}
impl<'a> PermissionCalculator<'a> {
@@ -76,18 +78,21 @@ impl<'a> PermissionCalculator<'a> {
Ok(ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::ManageChannel
+ ChannelPermission::VoiceCall)
+ ChannelPermission::VoiceCall
+ ChannelPermission::InviteOthers)
} else {
Ok(0)
}
}
Channel::TextChannel { server, .. } => {
let server = Server::get(server).await?;
let server = Ref::from_unchecked(server.clone()).fetch_server().await?;
if self.perspective.id == server.owner {
Ok(u32::MAX)
} else {
Ok(ChannelPermission::View + ChannelPermission::SendMessage)
Ok(ChannelPermission::View
+ ChannelPermission::SendMessage
+ ChannelPermission::InviteOthers)
}
}
}