From 522890fa00f24682c9586c2c3327a3253cb34a03 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 14 Apr 2021 22:46:31 -0700 Subject: [PATCH] + rtc::new_message now takes a Pool ref to make the second call for the user name ! Start taking public user params like this in the JWT to reduce on network hits It's cheap and reliable enough but the idea just now came to me so do that at some point --- json-api/src/messages.rs | 2 +- json-api/src/meta.rs | 11 +++++------ json-api/src/rtc.rs | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/json-api/src/messages.rs b/json-api/src/messages.rs index 20d9744..402413c 100644 --- a/json-api/src/messages.rs +++ b/json-api/src/messages.rs @@ -116,7 +116,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response, body: Body match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await { Ok(Row(msg)) => { use crate::rtc; - rtc::new_message(msg).await; + rtc::new_message(pool, msg).await; }, Ok(Empty) => { }, // idk anymore Ok(RestrictedInput(_msg)) => *response.status_mut() = StatusCode::BAD_REQUEST, diff --git a/json-api/src/meta.rs b/json-api/src/meta.rs index 5f3f873..b21bfeb 100644 --- a/json-api/src/meta.rs +++ b/json-api/src/meta.rs @@ -5,22 +5,21 @@ use hyper::{Response, Body}; use serde_json::to_string; use serde::Serialize; + #[derive( Serialize)] pub struct Config { pub name: String, pub description: String, - pub hostname: String, - pub port: u16, - pub protocol: String, + pub url: String, + pub wsurl: String, } pub fn get_config() -> Config { Config { name: var("SERVER_NAME").unwrap_or("No name".into()), description: var("SERVER_DESCRIPTION").unwrap_or("No description".into()), - hostname: var("SERVER_HOSTNAME").expect("Couldn't get url from environment"), - port: var("SERVER_PORT").expect("Couldn't get port from environment").parse::().unwrap(), - protocol: var("SERVER_PROTOCOL").expect("Couldn't get protocol from environment") + url: var("PUBLIC_URL").unwrap(), + wsurl: var("PUBLIC_WS_URL").unwrap() } } diff --git a/json-api/src/rtc.rs b/json-api/src/rtc.rs index 0970b43..f85c1e9 100644 --- a/json-api/src/rtc.rs +++ b/json-api/src/rtc.rs @@ -13,6 +13,7 @@ */ use std::time::{SystemTime, UNIX_EPOCH}; +use mysql_async::Pool; use tokio_tungstenite::connect_async; use tokio_tungstenite::tungstenite::{Error, Message}; use futures::{StreamExt, SinkExt}; @@ -65,7 +66,6 @@ P: Serialize { // Flow: Connect -> Pick out stream -> Send Data over stream // The stream/connection is destroyed by the end of this call - println!("NOTIFYING WSS"); let (ws, _) = connect_async(make_url()).await?; let (mut write, _) = ws.split(); let event = event!(event_name, &payload); @@ -75,7 +75,28 @@ P: Serialize } -pub async fn new_message(message: db::Message) { +pub async fn new_message(p: &Pool, message: db::Message) { + let uname = match db::Member::get(p, message.author_id).await { + Ok(response) => { + match response { + db::Response::Row(user) => user.name, + _ => String::new() + } + }, + Err(e) => { + eprintln!("[HTTP-RTC] Couldn't fetch username {}", e); + String::new() + } + }; + let message = db::UserMessage { + id: message.id, + time: message.time, + content: message.content, + content_type: message.content_type, + author_id: message.author_id, + channel_id: message.channel_id, + name: uname, + }; if let Err(e) = notify("new-message", message).await { eprintln!("[API-RTC] Unable to connect to RTC server: {}", e); }