From 9dca968b30d5fdce942ef434f8b61adc037f7966 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Fri, 12 Jul 2024 23:14:12 +0300
Subject: [PATCH] feat: Message and ChatRequest events
Signed-off-by: Awiteb
---
.../oxidetalis/src/websocket/events/server.rs | 21 +++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/crates/oxidetalis/src/websocket/events/server.rs b/crates/oxidetalis/src/websocket/events/server.rs
index 9bd990a..84e7e15 100644
--- a/crates/oxidetalis/src/websocket/events/server.rs
+++ b/crates/oxidetalis/src/websocket/events/server.rs
@@ -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 {
/// 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 {
})
}
+ /// Create message event
+ pub fn message(msg: impl Into) -> 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 {
ServerEvent:: {