mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Servers: Create and delete invites. Create and fetch servers.
This commit is contained in:
@@ -186,6 +186,27 @@ impl Channel {
|
||||
with: "channel",
|
||||
})?;
|
||||
|
||||
// Remove from server object.
|
||||
if let Channel::TextChannel { server, .. } = &self {
|
||||
get_collection("servers")
|
||||
.update_one(
|
||||
doc !{
|
||||
"_id": server
|
||||
},
|
||||
doc! {
|
||||
"$pull": {
|
||||
"channels": id
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "servers",
|
||||
})?;
|
||||
}
|
||||
|
||||
ClientboundNotification::ChannelDelete { id: id.to_string() }.publish(id.to_string());
|
||||
|
||||
if let Channel::Group { icon, .. } = self {
|
||||
|
||||
@@ -13,6 +13,7 @@ pub enum Invite {
|
||||
Server {
|
||||
#[serde(rename = "_id")]
|
||||
code: String,
|
||||
server: String,
|
||||
creator: String,
|
||||
channel: String,
|
||||
},
|
||||
@@ -35,6 +36,13 @@ impl Invite {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn creator(&self) -> &String {
|
||||
match &self {
|
||||
Invite::Server { creator, .. } => creator,
|
||||
Invite::Group { creator, .. } => creator,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(code: &str) -> Result<Invite> {
|
||||
let doc = get_collection("invites")
|
||||
.find_one(doc! { "_id": code }, None)
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use crate::database::*;
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use mongodb::bson::Document;
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::bson::from_document;
|
||||
use futures::StreamExt;
|
||||
use mongodb::bson::to_document;
|
||||
use mongodb::options::FindOptions;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -26,7 +28,8 @@ pub struct Member {
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Ban {
|
||||
pub id: String,
|
||||
#[serde(rename = "_id")]
|
||||
pub id: MemberCompositeKey,
|
||||
pub reason: String,
|
||||
}
|
||||
|
||||
@@ -84,7 +87,123 @@ impl Server {
|
||||
}
|
||||
|
||||
pub async fn delete(&self) -> Result<()> {
|
||||
unimplemented!()
|
||||
let messages = get_collection("messages");
|
||||
|
||||
// Check if there are any attachments we need to delete.
|
||||
let message_ids = messages
|
||||
.find(
|
||||
doc! {
|
||||
"server": &self.id,
|
||||
"attachment": {
|
||||
"$exists": 1
|
||||
}
|
||||
},
|
||||
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "fetch_many",
|
||||
with: "messages",
|
||||
})?
|
||||
.filter_map(async move |s| s.ok())
|
||||
.collect::<Vec<Document>>()
|
||||
.await
|
||||
.into_iter()
|
||||
.filter_map(|x| x.get_str("_id").ok().map(|x| x.to_string()))
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
// If we found any, mark them as deleted.
|
||||
if message_ids.len() > 0 {
|
||||
get_collection("attachments")
|
||||
.update_many(
|
||||
doc! {
|
||||
"message_id": {
|
||||
"$in": message_ids
|
||||
}
|
||||
},
|
||||
doc! {
|
||||
"$set": {
|
||||
"deleted": true
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_many",
|
||||
with: "attachments",
|
||||
})?;
|
||||
}
|
||||
|
||||
// And then delete said messages.
|
||||
messages
|
||||
.delete_many(
|
||||
doc! {
|
||||
"server": &self.id
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "delete_many",
|
||||
with: "messages",
|
||||
})?;
|
||||
|
||||
// Delete all channels, members, bans and invites.
|
||||
for with in [ "channels", "invites" ] {
|
||||
get_collection(with)
|
||||
.delete_many(
|
||||
doc! {
|
||||
"server": &self.id
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "delete_many",
|
||||
with,
|
||||
})?;
|
||||
}
|
||||
|
||||
for with in [ "server_members", "server_bans" ] {
|
||||
get_collection(with)
|
||||
.delete_many(
|
||||
doc! {
|
||||
"_id.server": &self.id
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "delete_many",
|
||||
with,
|
||||
})?;
|
||||
}
|
||||
|
||||
get_collection("servers")
|
||||
.delete_one(
|
||||
doc! {
|
||||
"_id": &self.id
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "delete_one",
|
||||
with: "server",
|
||||
})?;
|
||||
|
||||
ClientboundNotification::ServerDelete { id: self.id.clone() }.publish(self.id.clone());
|
||||
|
||||
if let Some(attachment) = &self.icon {
|
||||
attachment.delete().await?;
|
||||
}
|
||||
|
||||
if let Some(attachment) = &self.banner {
|
||||
attachment.delete().await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn join_member(&self, id: &str) -> Result<()> {
|
||||
|
||||
@@ -38,7 +38,7 @@ impl Ref {
|
||||
operation: "find_one",
|
||||
with: &collection,
|
||||
})?
|
||||
.ok_or_else(|| Error::UnknownUser)?;
|
||||
.ok_or_else(|| Error::NotFound)?;
|
||||
|
||||
Ok(from_document::<T>(doc).map_err(|_| Error::DatabaseError {
|
||||
operation: "from_document",
|
||||
|
||||
Reference in New Issue
Block a user