feat: Chat request implementation #14

Manually merged
awiteb merged 55 commits from awiteb/chat-request-and-response into master 2024-07-18 14:21:39 +02:00 AGit
Showing only changes of commit 9dca968b30 - Show all commits

View file

@ -19,7 +19,10 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use chrono::Utc; use chrono::Utc;
use oxidetalis_core::{cipher::K256Secret, types::Signature}; use oxidetalis_core::{
cipher::K256Secret,
types::{PublicKey, Signature},
};
use salvo::websocket::Message; use salvo::websocket::Message;
use serde::Serialize; use serde::Serialize;
@ -42,12 +45,16 @@ pub struct ServerEvent<T> {
/// server websocket event type /// server websocket event type
#[derive(Serialize, Clone, Eq, PartialEq, Debug)] #[derive(Serialize, Clone, Eq, PartialEq, Debug)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase", tag = "event", content = "data")]
pub enum ServerEventType { pub enum ServerEventType {
/// Ping event /// Ping event
Ping { timestamp: u64 }, Ping { timestamp: u64 },
/// Pong event /// Pong event
Pong { timestamp: u64 }, 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 event
Error { Error {
awiteb marked this conversation as resolved
Review

in the client, you mentioned to make it alphabetical, should it be the same here?

in the client, you mentioned to make it alphabetical, should it be the same here?
Review

Yes, I forgot it

Yes, I forgot it
name: &'static str, 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 /// Sign the event
pub fn sign(self, shared_secret: &[u8; 32]) -> ServerEvent<Signed> { pub fn sign(self, shared_secret: &[u8; 32]) -> ServerEvent<Signed> {
ServerEvent::<Signed> { ServerEvent::<Signed> {