New pubsub backend.

This commit is contained in:
Paul Makles
2020-09-03 16:26:32 +01:00
parent c95dc19f67
commit c3362a6e4f
19 changed files with 581 additions and 56 deletions

View File

@@ -107,6 +107,7 @@ pub fn fetch_guild(id: &str) -> Result<Option<Guild>, String> {
let col = get_collection("guilds");
if let Ok(result) = col.find_one(doc! { "_id": id }, None) {
if let Some(doc) = result {
dbg!(doc.to_string());
if let Ok(guild) = from_bson(Bson::Document(doc)) as Result<Guild, _> {
let mut cache = CACHE.lock().unwrap();
cache.put(id.to_string(), guild.clone());
@@ -152,6 +153,7 @@ pub fn fetch_guilds(ids: &Vec<String>) -> Result<Vec<Guild>, String> {
for item in result {
let mut cache = CACHE.lock().unwrap();
if let Ok(doc) = item {
dbg!(doc.to_string());
if let Ok(guild) = from_bson(Bson::Document(doc)) as Result<Guild, _> {
cache.put(guild.id.clone(), guild.clone());
guilds.push(guild);

View File

@@ -3,6 +3,7 @@ use crate::database::channel::Channel;
use crate::notifications;
use crate::notifications::events::message::Create;
use crate::notifications::events::Notification;
use crate::pubsub::hive;
use crate::routes::channel::ChannelType;
use mongodb::bson::from_bson;
@@ -11,6 +12,8 @@ use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{Deserialize, Serialize};
use log::warn;
#[derive(Serialize, Deserialize, Debug)]
pub struct PreviousEntry {
pub content: String,
@@ -51,6 +54,23 @@ impl Message {
&target,
);
if hive::publish(
&target.id,
crate::pubsub::events::Notification::message_create(
crate::pubsub::events::message::Create {
id: self.id.clone(),
nonce: self.nonce.clone(),
channel: self.channel.clone(),
author: self.author.clone(),
content: self.content.clone(),
},
),
)
.is_err()
{
warn!("Saved message but couldn't send notification.");
}
let short_content: String = self.content.chars().take(24).collect();
// !! this stuff can be async