Skip to content
Snippets Groups Projects
Commit 84d09db9 authored by insert's avatar insert
Browse files

Add SavedMessages channel type, add create DM.

parent eb382fa1
Branches
Tags
No related merge requests found
use serde::{Deserialize, Serialize};
use crate::{database::get_collection, util::result::{Error, Result}};
use mongodb::bson::to_document;
/*#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
......@@ -33,6 +35,11 @@ pub struct Channel {
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Channel {
SavedMessages {
#[serde(rename = "_id")]
id: String,
user: String
},
DirectMessage {
#[serde(rename = "_id")]
id: String,
......@@ -48,3 +55,18 @@ pub enum Channel {
recipients: Vec<String>,
}
}
impl Channel {
pub async fn save(&self) -> Result<()> {
get_collection("channels")
.insert_one(
to_document(&self)
.map_err(|_| Error::DatabaseError { operation: "to_bson", with: "channel" })?,
None
)
.await
.map_err(|_| Error::DatabaseError { operation: "insert_one", with: "channel" })?;
Ok(())
}
}
......@@ -7,6 +7,7 @@ use rocket::request::{self, FromRequest, Outcome, Request};
#[derive(Serialize, Deserialize, Debug)]
pub struct Relationship {
#[serde(rename = "_id")]
pub id: String,
pub status: u8,
}
......
......@@ -10,7 +10,7 @@ use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
pub struct Ref {
#[validate(length(min = 26, max = 26))]
id: String,
pub id: String,
}
impl Ref {
......
......@@ -18,8 +18,8 @@ bitfield! {
pub get_invite, _: 29;
}
impl_op_ex!(+ |a: &UserPermission, b: &UserPermission| -> u32 { *a + *b });
impl_op_ex!(+ |a: &u32, b: &UserPermission| -> u32 { *a + *b });
impl_op_ex!(+ |a: &UserPermission, b: &UserPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex_commutative!(+ |a: &u32, b: &UserPermission| -> u32 { *a | *b as u32 });
pub async fn temp_calc_perm(_user: &User, _target: &User) -> UserPermissions<[u32; 1]> {
// if friends; Access + Message + Invite
......
......@@ -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);
}
}
......
......@@ -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,
]
}
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))
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment