feat: Message and ChatRequest events

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-12 23:14:12 +03:00
parent ff37501b74
commit 9dca968b30
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -19,7 +19,10 @@
use std::marker::PhantomData;
use chrono::Utc;
use oxidetalis_core::{cipher::K256Secret, types::Signature};
use oxidetalis_core::{
cipher::K256Secret,
types::{PublicKey, Signature},
};
use salvo::websocket::Message;
use serde::Serialize;
@ -42,12 +45,16 @@ pub struct ServerEvent<T> {
/// server websocket event type
#[derive(Serialize, Clone, Eq, PartialEq, Debug)]
#[serde(rename_all = "PascalCase")]
#[serde(rename_all = "PascalCase", tag = "event", content = "data")]
pub enum ServerEventType {
/// Ping event
Ping { timestamp: u64 },
/// Pong event
Pong { timestamp: u64 },
/// Message event, contain a message to the client
Message { msg: String },
/// New chat request from someone
ChatRequest { from: PublicKey },
/// Error event
Error {
name: &'static str,
@ -88,6 +95,16 @@ impl ServerEvent<Unsigned> {
})
}
/// Create message event
pub fn message(msg: impl Into<String>) -> Self {
Self::new(ServerEventType::Message { msg: msg.into() })
}
/// Create chat request event
pub fn chat_request(from: &PublicKey) -> Self {
Self::new(ServerEventType::ChatRequest { from: *from })
}
/// Sign the event
pub fn sign(self, shared_secret: &[u8; 32]) -> ServerEvent<Signed> {
ServerEvent::<Signed> {