- Removing Claim::new since its used once

+ New notify driver function which does the network magic we need
+ Adding event emitters for the following events
"delete-channel"
"create-channel"
"update-nick"
This commit is contained in:
shockrah 2021-04-02 13:55:21 -07:00
parent 17f281c91c
commit be54ce6a57

View File

@ -12,9 +12,9 @@
* is sufficiently large & securely transferred on both parties behalf
*/
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::{SystemTime, UNIX_EPOCH};
use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::{Error, Message};
use futures::{StreamExt, SinkExt};
use serde::Serialize;
use serde_json::json;
@ -32,7 +32,7 @@ lazy_static! {
}
macro_rules! event {
($type:literal, $payload:expr) => {
($type:expr, $payload:expr) => {
json!({"type": $type, $type: $payload})
}
}
@ -42,35 +42,68 @@ struct Claim {
nbf: i64
}
impl Claim {
pub fn new() -> Self {
let now = (SystemTime::now() + Duration::from_secs(0))
.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64;
let now = now / 1000;
Self {
nbf: now
}
}
fn url() -> String {
let claim = Claim {
nbf: SystemTime::now()
.duration_since(UNIX_EPOCH).unwrap()
.as_secs() as i64
};
let header = Header::new(Algorithm::HS512);
let jwt = jsonwebtoken::encode(&header, &claim, &WSS_KEY).unwrap();
format!("ws://localhost:5648/jwt/{}", jwt)
}
async fn notify<P>(event_name: &str, payload: P)
-> Result<(), Error> where
P: Serialize
{
// Flow: Connect -> Pick out stream -> Send Data over stream
// The stream/connection is destroyed by the end of this call
let (ws, _) = connect_async(url().as_str()).await?;
let (mut write, _) = ws.split();
let event = event!(event_name, &payload);
let msg = event.to_string();
write.send(Message::text(msg)).await?;
Ok(())
}
pub async fn new_message(message: db::Message) {
let claim = Claim::new();
let header = Header::new(Algorithm::HS512);
let jwt = jsonwebtoken::encode(&header, &claim, &WSS_KEY).unwrap();
match connect_async(format!("ws://localhost:5648/jwt/{}", jwt)).await {
Ok((ws, _read)) => {
let (mut write, _read) = ws.split();
let m = event!("message", &message);
let m = m.to_string();
if let Err(e) = write.send(Message::text(m)).await {
eprintln!("[RTC-COMM-ERR] {}", e)
}
},
Err(e) => {
eprintln!("unable to connect to rtc: {}", e);
}
};
if let Err(e) = notify("new-message", message).await {
eprintln!("[API-RTC] Unable to connect to RTC server: {}", e);
}
}
pub async fn delete_channel(id: u64) {
#[derive(Serialize)]
struct DeletedChannel {
id: u64
};
let channel = DeletedChannel { id };
if let Err(e) = notify("delete-channel", channel).await {
eprintln!("[API-RTC] Unable to connect to RTC server: {}", e);
}
}
pub async fn create_channel(channel: db::Channel) {
if let Err(e) = notify("create-channel", channel).await {
eprintln!("[API-RTC] Unable to connect to RTC server: {}", e);
}
}
pub async fn update_nickname<'s>(id: u64, name: &'s str) {
#[derive(Serialize)]
struct NewNick<'n> {
id: u64,
name: &'n str
};
let user = NewNick { id, name };
if let Err(e) = notify("update-nick", user).await {
eprintln!("[API-RTC] Unable to connect to RTC server: {}", e);
}
}