Feat: Push notification server (#387)
* feat: create base of push daemon Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * Add outbound senders * Make web_push send to rabbit instead (temp stuff) * feat: stability and friend requests * make vapid fr stuff not suck * swap naming of queue * move pushd into daemons folder * fix cargo file for move into daemons folder * feat: probably working fcm push notifs * comment out fcm webpush stuff since the config keys dont exist * fix fcm, name queues according to their prod status and configure routing keys * add pushd to docker * mix: Remove old code, add stuff to pushd * fix: lockfile * feat: update rocket to 5.0.1 * fix: fix queues and ack bugs * Move rabbit messsage processing into ack queue * chore: update readme * chore: optimizations for ack database hits * pushd flowchart * misc: update flowchart * exit dependancy hell * add rocket_impl flag to authifier * make the tests file of delta actually compile * fix: don't silence every push message * fix: don't silence all messages * add debug logging for sending data to rabbit from message events * validate mentions at a server membership level * put back that import that was actually important * minor fix to lockfile * update delta authifier * feat: proper permissions for push notifications * add unit test for mention sanitization * remove local file dependancy on authifier * update ports to proper defaults * fixTM the node bindings * Theoretically configure docker releases for pushd Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * declare exchange in pushd and delta * fix createbuckets script Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: reference db implementation Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: remove finally redundant code Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: changes Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: other changes Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> * fix: make channel name return result Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> --------- Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com>
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
// Queue Type: Debounced
|
||||
use crate::Database;
|
||||
use crate::{Database, Message, AMQP};
|
||||
|
||||
use deadqueue::limited::Queue;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
use revolt_models::v0::PushNotification;
|
||||
use rocket::form::validate::Contains;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
time::Duration,
|
||||
};
|
||||
use validator::HasLen;
|
||||
|
||||
use revolt_result::Result;
|
||||
|
||||
use super::{apple_notifications::{self, ApnJob}, DelayedTask};
|
||||
use super::DelayedTask;
|
||||
|
||||
/// Enumeration of possible events
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum AckEvent {
|
||||
/// Add mentions for a user in a channel
|
||||
AddMention {
|
||||
/// Message IDs
|
||||
ids: Vec<String>,
|
||||
/// Add mentions for a channel
|
||||
ProcessMessage {
|
||||
/// push notification, message, recipients, push silenced
|
||||
messages: Vec<(Option<PushNotification>, Message, Vec<String>, bool)>,
|
||||
},
|
||||
|
||||
/// Acknowledge message in a channel for a user
|
||||
@@ -30,7 +36,7 @@ struct Data {
|
||||
/// Channel to ack
|
||||
channel: String,
|
||||
/// User to ack for
|
||||
user: String,
|
||||
user: Option<String>,
|
||||
/// Event
|
||||
event: AckEvent,
|
||||
}
|
||||
@@ -43,21 +49,49 @@ struct Task {
|
||||
static Q: Lazy<Queue<Data>> = Lazy::new(|| Queue::new(10_000));
|
||||
|
||||
/// Queue a new task for a worker
|
||||
pub async fn queue(channel: String, user: String, event: AckEvent) {
|
||||
pub async fn queue_ack(channel: String, user: String, event: AckEvent) {
|
||||
Q.try_push(Data {
|
||||
channel,
|
||||
user,
|
||||
user: Some(user),
|
||||
event,
|
||||
})
|
||||
.ok();
|
||||
|
||||
info!("Queue is using {} slots from {}.", Q.len(), Q.capacity());
|
||||
info!(
|
||||
"Queue is using {} slots from {}. Queued type: ACK",
|
||||
Q.len(),
|
||||
Q.capacity()
|
||||
);
|
||||
}
|
||||
|
||||
pub async fn handle_ack_event(event: &AckEvent, db: &Database, authifier_db: &authifier::Database, user: &str, channel: &str) -> Result<()> {
|
||||
pub async fn queue_message(channel: String, event: AckEvent) {
|
||||
Q.try_push(Data {
|
||||
channel,
|
||||
user: None,
|
||||
event,
|
||||
})
|
||||
.ok();
|
||||
|
||||
info!(
|
||||
"Queue is using {} slots from {}. Queued type: MENTION",
|
||||
Q.len(),
|
||||
Q.capacity()
|
||||
);
|
||||
}
|
||||
|
||||
pub async fn handle_ack_event(
|
||||
event: &AckEvent,
|
||||
db: &Database,
|
||||
amqp: &AMQP,
|
||||
user: &Option<String>,
|
||||
channel: &str,
|
||||
) -> Result<()> {
|
||||
match &event {
|
||||
#[allow(clippy::disallowed_methods)] // event is sent by higher level function
|
||||
AckEvent::AckMessage { id } => {
|
||||
let user = user.as_ref().unwrap();
|
||||
let user: &str = user.as_str();
|
||||
|
||||
let unread = db.fetch_unread(user, channel).await?;
|
||||
let updated = db.acknowledge_message(channel, user, id).await?;
|
||||
|
||||
@@ -68,21 +102,67 @@ pub async fn handle_ack_event(event: &AckEvent, db: &Database, authifier_db: &au
|
||||
let mentions_acked = before_mentions - after_mentions;
|
||||
|
||||
if mentions_acked > 0 {
|
||||
if let Ok(sessions) = authifier_db.find_sessions(user).await {
|
||||
for session in sessions {
|
||||
if let Some(sub) = session.subscription {
|
||||
if sub.endpoint == "apn" {
|
||||
apple_notifications::queue(ApnJob::from_ack(session.id, user.to_string(), sub.auth)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Err(err) = amqp
|
||||
.ack_message(user.to_string(), channel.to_string(), id.to_owned())
|
||||
.await
|
||||
{
|
||||
revolt_config::capture_error(&err);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
},
|
||||
AckEvent::AddMention { ids } => {
|
||||
db.add_mention_to_unread(channel, user, ids).await?;
|
||||
}
|
||||
AckEvent::ProcessMessage { messages } => {
|
||||
let mut users: HashSet<&String> = HashSet::new();
|
||||
debug!(
|
||||
"Processing {} messages from channel {}",
|
||||
messages.len(),
|
||||
messages[0].1.channel
|
||||
);
|
||||
|
||||
// find all the users we'll be notifying
|
||||
messages
|
||||
.iter()
|
||||
.for_each(|(_, _, recipents, _)| users.extend(recipents.iter()));
|
||||
|
||||
debug!("Found {} users to notify.", users.len());
|
||||
|
||||
for user in users {
|
||||
let message_ids: Vec<String> = messages
|
||||
.iter()
|
||||
.filter(|(_, _, recipients, _)| recipients.contains(user))
|
||||
.map(|(_, message, _, _)| message.id.clone())
|
||||
.collect();
|
||||
|
||||
if !message_ids.is_empty() {
|
||||
db.add_mention_to_unread(channel, user, &message_ids)
|
||||
.await?;
|
||||
}
|
||||
debug!("Added {} mentions for user {}", message_ids.len(), &user);
|
||||
}
|
||||
|
||||
for (push, _, recipients, silenced) in messages {
|
||||
if *silenced || recipients.is_empty() || push.is_none() {
|
||||
debug!(
|
||||
"Rejecting push: silenced: {}, recipient count: {}, push exists: {:?}",
|
||||
*silenced,
|
||||
recipients.length(),
|
||||
push
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
debug!(
|
||||
"Sending push event to AMQP; message {} for {} users",
|
||||
push.as_ref().unwrap().message.id,
|
||||
recipients.len()
|
||||
);
|
||||
if let Err(err) = amqp
|
||||
.message_sent(recipients.clone(), push.clone().unwrap())
|
||||
.await
|
||||
{
|
||||
revolt_config::capture_error(&err);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -90,9 +170,9 @@ pub async fn handle_ack_event(event: &AckEvent, db: &Database, authifier_db: &au
|
||||
}
|
||||
|
||||
/// Start a new worker
|
||||
pub async fn worker(db: Database, authifier_db: authifier::Database) {
|
||||
let mut tasks = HashMap::<(String, String), DelayedTask<Task>>::new();
|
||||
let mut keys = vec![];
|
||||
pub async fn worker(db: Database, amqp: AMQP) {
|
||||
let mut tasks = HashMap::<(Option<String>, String, u8), DelayedTask<Task>>::new();
|
||||
let mut keys: Vec<(Option<String>, String, u8)> = vec![];
|
||||
|
||||
loop {
|
||||
// Find due tasks.
|
||||
@@ -106,12 +186,13 @@ pub async fn worker(db: Database, authifier_db: authifier::Database) {
|
||||
for key in &keys {
|
||||
if let Some(task) = tasks.remove(key) {
|
||||
let Task { event } = task.data;
|
||||
let (user, channel) = key;
|
||||
let (user, channel, _) = key;
|
||||
|
||||
if let Err(err) = handle_ack_event(&event, &db, &authifier_db, user, channel).await {
|
||||
error!("{err:?} for {event:?}. ({user}, {channel})");
|
||||
if let Err(err) = handle_ack_event(&event, &db, &amqp, user, channel).await {
|
||||
revolt_config::capture_error(&err);
|
||||
error!("{err:?} for {event:?}. ({user:?}, {channel})");
|
||||
} else {
|
||||
info!("User {user} ack in {channel} with {event:?}");
|
||||
info!("User {user:?} ack in {channel} with {event:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,20 +207,41 @@ pub async fn worker(db: Database, authifier_db: authifier::Database) {
|
||||
mut event,
|
||||
}) = Q.try_pop()
|
||||
{
|
||||
let key = (user, channel);
|
||||
let key: (Option<String>, String, u8) = (
|
||||
user,
|
||||
channel,
|
||||
match &event {
|
||||
AckEvent::AckMessage { .. } => 0,
|
||||
AckEvent::ProcessMessage { .. } => 1,
|
||||
},
|
||||
);
|
||||
if let Some(task) = tasks.get_mut(&key) {
|
||||
task.delay();
|
||||
|
||||
match &mut event {
|
||||
AckEvent::AddMention { ids } => {
|
||||
if let AckEvent::AddMention { ids: existing } = &mut task.data.event {
|
||||
existing.append(ids);
|
||||
AckEvent::ProcessMessage { messages: new_data } => {
|
||||
if let AckEvent::ProcessMessage { messages: existing } =
|
||||
&mut task.data.event
|
||||
{
|
||||
// add the new message to the list of messages to be processed.
|
||||
existing.append(new_data);
|
||||
|
||||
// put a cap on the amount of messages that can be queued, for particularly active channels
|
||||
if (existing.length() as u16)
|
||||
< revolt_config::config()
|
||||
.await
|
||||
.features
|
||||
.advanced
|
||||
.process_message_delay_limit
|
||||
{
|
||||
task.delay();
|
||||
}
|
||||
} else {
|
||||
task.data.event = event;
|
||||
panic!("Somehow got an ack message in the add mention arm");
|
||||
}
|
||||
}
|
||||
AckEvent::AckMessage { .. } => {
|
||||
// replace the last acked message with the new acked message
|
||||
task.data.event = event;
|
||||
task.delay();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user