Add SavedMessages channel type, add create DM.

This commit is contained in:
Paul Makles
2020-12-29 14:15:44 +00:00
parent eb382fa1ec
commit 84d09db9b3
7 changed files with 84 additions and 9 deletions

View File

@@ -26,13 +26,10 @@ pub async fn req(user: User) -> Result<JsonValue> {
.await
.map_err(|_| Error::DatabaseError { operation: "find", with: "channels" })?;
let mut channels: Vec<Channel> = vec![];
let mut channels = vec![];
while let Some(result) = cursor.next().await {
if let Ok(doc) = result {
channels.push(
from_bson(Bson::Document(doc))
.map_err(|_| Error::DatabaseError { operation: "from_bson", with: "channel" })?
);
channels.push(doc);
}
}

View File

@@ -2,10 +2,12 @@ use rocket::Route;
mod fetch_user;
mod fetch_dms;
mod open_dm;
pub fn routes() -> Vec<Route> {
routes! [
fetch_user::req,
fetch_dms::req
fetch_dms::req,
open_dm::req,
]
}

View File

@@ -0,0 +1,53 @@
use crate::database::entities::{Channel, User};
use crate::database::guards::reference::Ref;
use crate::util::result::{Error, Result};
use crate::database::get_collection;
use rocket_contrib::json::JsonValue;
use mongodb::bson::doc;
use ulid::Ulid;
#[get("/<target>/dm")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
let query = if user.id == target.id {
doc! {
"type": "SavedMessages",
"user": &user.id
}
} else {
doc! {
"type": "DirectMessage",
"recipients": {
"$all": [ &user.id, &target.id ]
}
}
};
let existing_channel = get_collection("channels")
.find_one(query, None)
.await
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "channel" })?;
if let Some(doc) = existing_channel {
Ok(json!(doc))
} else {
let id = Ulid::new().to_string();
let channel = if user.id == target.id {
Channel::SavedMessages {
id,
user: user.id
}
} else {
Channel::DirectMessage {
id,
active: false,
recipients: vec! [
user.id,
target.id
]
}
};
channel.save().await?;
Ok(json!(channel))
}
}