Add serialisation, ready payload and GET guilds.

This commit is contained in:
Paul Makles
2020-08-12 15:40:56 +02:00
parent 74b4238f04
commit 7e7eb34f65
7 changed files with 314 additions and 125 deletions

View File

@@ -4,6 +4,7 @@ use lru::LruCache;
use mongodb::bson::{doc, from_bson, Bson};
use rocket::http::RawStr;
use rocket::request::FromParam;
use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
@@ -40,6 +41,40 @@ pub struct Channel {
pub description: Option<String>,
}
impl Channel {
pub fn serialise(self) -> JsonValue {
match self.channel_type {
0 => json!({
"id": self.id,
"type": self.channel_type,
"last_message": self.last_message,
"recipients": self.recipients,
}),
1 => {
json!({
"id": self.id,
"type": self.channel_type,
"last_message": self.last_message,
"recipients": self.recipients,
"name": self.name,
"owner": self.owner,
"description": self.description,
})
}
2 => {
json!({
"id": self.id,
"type": self.channel_type,
"guild": self.guild,
"name": self.name,
"description": self.description,
})
}
_ => unreachable!(),
}
}
}
lazy_static! {
static ref CACHE: Arc<Mutex<LruCache<String, Channel>>> =
Arc::new(Mutex::new(LruCache::new(4_000_000)));
@@ -83,13 +118,13 @@ pub fn fetch_channels(ids: &Vec<String>) -> Result<Vec<Channel>, String> {
{
if let Ok(mut cache) = CACHE.lock() {
for gid in ids {
let existing = cache.get(gid);
for id in ids {
let existing = cache.get(id);
if let Some(channel) = existing {
channels.push((*channel).clone());
} else {
missing.push(gid);
missing.push(id);
}
}
} else {