forked from jmug/stoatchat
Add ability to create DMs
This commit is contained in:
5
src/routes/channel.rs
Normal file
5
src/routes/channel.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pub enum ChannelType {
|
||||||
|
DM = 0,
|
||||||
|
GROUP_DM = 1,
|
||||||
|
GUILD_CHANNEL = 2,
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
use rocket::Rocket;
|
use rocket::Rocket;
|
||||||
|
|
||||||
mod account;
|
pub mod account;
|
||||||
mod user;
|
pub mod user;
|
||||||
|
pub mod channel;
|
||||||
|
|
||||||
pub fn mount(rocket: Rocket) -> Rocket {
|
pub fn mount(rocket: Rocket) -> Rocket {
|
||||||
rocket
|
rocket
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
use crate::auth::User;
|
use crate::auth::User;
|
||||||
use crate::database;
|
use crate::database;
|
||||||
|
use crate::routes::channel;
|
||||||
|
|
||||||
use rocket_contrib::json::{ Json, JsonValue };
|
use rocket_contrib::json::{ Json, JsonValue };
|
||||||
use serde::{ Serialize, Deserialize };
|
use serde::{ Serialize, Deserialize };
|
||||||
use mongodb::options::FindOptions;
|
use mongodb::options::FindOptions;
|
||||||
use bson::{ bson, doc };
|
use bson::{ bson, doc };
|
||||||
|
use ulid::Ulid;
|
||||||
|
|
||||||
/// retrieve your user information
|
/// retrieve your user information
|
||||||
#[get("/@me")]
|
#[get("/@me")]
|
||||||
@@ -60,13 +62,85 @@ pub fn lookup(_user: User, query: Json<Query>) -> JsonValue {
|
|||||||
/// retrieve all of your DMs
|
/// retrieve all of your DMs
|
||||||
#[get("/@me/dms")]
|
#[get("/@me/dms")]
|
||||||
pub fn dms(user: User) -> JsonValue {
|
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
|
/// open a DM with a user
|
||||||
#[get("/<target>/dm")]
|
#[get("/<target>/dm")]
|
||||||
pub fn dm(user: User, target: User) -> JsonValue {
|
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 {
|
enum Relationship {
|
||||||
|
|||||||
Reference in New Issue
Block a user