1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Spawn a thread to send push notifications

This commit is contained in:
Pierre HUBERT 2021-04-16 08:35:14 +02:00
parent 5e3ffcbeda
commit 1567ae4241
2 changed files with 21 additions and 16 deletions

View File

@ -4,13 +4,13 @@
use std::collections::HashSet;
use crate::data::conversation::ConvID;
use crate::data::error::{ExecError, Res};
use crate::data::user::UserID;
use crate::data::conversation::ConvID;
pub type ConvMessageID = u64;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ConversationMessageFile {
pub path: String,
pub size: u64,
@ -19,19 +19,19 @@ pub struct ConversationMessageFile {
pub r#type: String,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct UserAddedAnotherUserToConversation {
pub user_who_added: UserID,
pub user_added: UserID,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct UserRemovedAnotherUserToConversation {
pub user_who_removed: UserID,
pub user_removed: UserID,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ConversationServerMessageType {
UserCreatedConversation(UserID),
UserAddedAnotherUserToConversation(UserAddedAnotherUserToConversation),
@ -98,7 +98,7 @@ impl ConversationServerMessageType {
}
/// Information about a single conversation message
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ConversationMessage {
pub id: ConvMessageID,
pub time_sent: u64,

View File

@ -169,21 +169,26 @@ pub fn cancel_conversation_notification(conv_id: ConvID, users: Option<Vec<UserI
/// Handle event. This method NEVER returns Err
pub fn handle_event(e: &Event) -> Res {
if let Err(e) = handle_event_internal(e) {
eprintln!("Failed to create push notifications associated with event! {}", e);
}
Ok(())
}
fn handle_event_internal(e: &Event) -> Res {
match e {
Event::NewConversationMessage(msg) => {
create_conversation_notification(msg)?;
let msg = (*msg).clone();
std::thread::spawn(move || {
if let Err(err) = create_conversation_notification(&msg) {
eprintln!("Failed to create push for conversation! {}", err);
}
});
}
Event::SeenLastConversationMessage(user_id, conv_id) => {
cancel_conversation_notification(*conv_id, Some(vec![user_id.as_owned()]))?;
let user_id = user_id.as_owned();
let conv_id = *conv_id;
std::thread::spawn(move || {
if let Err(e) = cancel_conversation_notification(conv_id, Some(vec![user_id])) {
eprintln!("Failed to cancel push conversation! {}", e);
}
});
}
_ => {}
}