Add ability to create DMs

This commit is contained in:
Paul Makles
2020-01-26 15:06:31 +00:00
parent 20c82ddef3
commit 0e5211e705
3 changed files with 84 additions and 4 deletions

5
src/routes/channel.rs Normal file
View File

@@ -0,0 +1,5 @@
pub enum ChannelType {
DM = 0,
GROUP_DM = 1,
GUILD_CHANNEL = 2,
}

View File

@@ -1,7 +1,8 @@
use rocket::Rocket;
mod account;
mod user;
pub mod account;
pub mod user;
pub mod channel;
pub fn mount(rocket: Rocket) -> Rocket {
rocket

View File

@@ -1,10 +1,12 @@
use crate::auth::User;
use crate::database;
use crate::routes::channel;
use rocket_contrib::json::{ Json, JsonValue };
use serde::{ Serialize, Deserialize };
use mongodb::options::FindOptions;
use bson::{ bson, doc };
use ulid::Ulid;
/// retrieve your user information
#[get("/@me")]
@@ -60,13 +62,85 @@ pub fn lookup(_user: User, query: Json<Query>) -> JsonValue {
/// retrieve all of your DMs
#[get("/@me/dms")]
pub fn dms(user: User) -> JsonValue {
json!([])
let col = database::get_collection("channels");
let results = col.find(
doc! {
"$or": [
{
"type": channel::ChannelType::DM as i32
},
{
"type": channel::ChannelType::GROUP_DM as i32
}
],
"recipients": user.0.to_string()
},
None
).expect("Failed channel lookup");
let mut channels = Vec::new();
for res in results {
let doc = res.expect("Failed to unwrap document");
let mut recipients = Vec::new();
for user in doc.get_array("recipients").expect("DB[recipients]") {
recipients.push(
user.as_str()
.expect("Should be a string.")
);
}
let active =
match doc.get_bool("active") {
Ok(x) => x,
Err(_) => true
};
channels.push(
json!({
"id": doc.get_str("_id").expect("DB[id]"),
"type": doc.get_i32("type").expect("DB[type]"),
"recipients": recipients,
"active": active
})
);
}
json!(channels)
}
/// open a DM with a user
#[get("/<target>/dm")]
pub fn dm(user: User, target: User) -> JsonValue {
json!([])
let col = database::get_collection("channels");
match col.find_one(
doc! { "type": channel::ChannelType::DM as i32, "recipients": [ user.0.to_string(), target.0.to_string() ] },
None
).expect("Failed channel lookup") {
Some(channel) =>
json!({
"id": channel.get_str("_id").expect("DB[id]")
}),
None => {
let id = Ulid::new();
col.insert_one(
doc! {
"_id": id.to_string(),
"type": channel::ChannelType::DM as i32,
"recipients": [ user.0.to_string(), target.0.to_string() ],
"active": false
},
None
).expect("Failed insert query.");
json!({
"id": id.to_string()
})
}
}
}
enum Relationship {