Refractor to use UserRef.

This commit is contained in:
Paul Makles
2020-04-09 09:49:10 +01:00
parent b21a6f2231
commit 7af9a77df4
10 changed files with 496 additions and 138 deletions

View File

@@ -1,6 +1,8 @@
use bson::{bson, doc, from_bson};
use bson::{bson, doc, from_bson, Document};
use mongodb::options::FindOneOptions;
use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{Deserialize, Serialize};
use crate::database;
@@ -24,6 +26,56 @@ impl<'r> FromParam<'r> for Channel {
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChannelRef {
#[serde(rename = "_id")]
pub id: String,
#[serde(rename = "type")]
pub channel_type: u8,
// information required for permission calculations
pub recipients: Option<Vec<String>>,
pub guild: Option<String>,
}
impl ChannelRef {
pub fn fetch_data(&self, projection: Document) -> Option<Document> {
database::get_collection("channels")
.find_one(
doc! { "_id": &self.id },
FindOneOptions::builder().projection(projection).build(),
)
.expect("Failed to fetch channel from database.")
}
}
impl<'r> FromParam<'r> for ChannelRef {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
let id = param.to_string();
let result = database::get_collection("channels")
.find_one(
doc! { "_id": id },
FindOneOptions::builder()
.projection(doc! {
"_id": 1,
"type": 1,
"recipients": 1,
"guild": 1,
})
.build(),
)
.unwrap();
if let Some(channel) = result {
Ok(from_bson(bson::Bson::Document(channel)).expect("Failed to deserialize channel."))
} else {
Err(param)
}
}
}
impl<'r> FromParam<'r> for Message {
type Error = &'r RawStr;