+ 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
This commit is contained in:
shockrah 2021-04-14 22:46:31 -07:00
parent 894a5bae34
commit 522890fa00
3 changed files with 29 additions and 9 deletions

View File

@ -116,7 +116,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await { match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await {
Ok(Row(msg)) => { Ok(Row(msg)) => {
use crate::rtc; use crate::rtc;
rtc::new_message(msg).await; rtc::new_message(pool, msg).await;
}, },
Ok(Empty) => { }, // idk anymore Ok(Empty) => { }, // idk anymore
Ok(RestrictedInput(_msg)) => *response.status_mut() = StatusCode::BAD_REQUEST, Ok(RestrictedInput(_msg)) => *response.status_mut() = StatusCode::BAD_REQUEST,

View File

@ -5,22 +5,21 @@ use hyper::{Response, Body};
use serde_json::to_string; use serde_json::to_string;
use serde::Serialize; use serde::Serialize;
#[derive( Serialize)] #[derive( Serialize)]
pub struct Config { pub struct Config {
pub name: String, pub name: String,
pub description: String, pub description: String,
pub hostname: String, pub url: String,
pub port: u16, pub wsurl: String,
pub protocol: String,
} }
pub fn get_config() -> Config { pub fn get_config() -> Config {
Config { Config {
name: var("SERVER_NAME").unwrap_or("No name".into()), name: var("SERVER_NAME").unwrap_or("No name".into()),
description: var("SERVER_DESCRIPTION").unwrap_or("No description".into()), description: var("SERVER_DESCRIPTION").unwrap_or("No description".into()),
hostname: var("SERVER_HOSTNAME").expect("Couldn't get url from environment"), url: var("PUBLIC_URL").unwrap(),
port: var("SERVER_PORT").expect("Couldn't get port from environment").parse::<u16>().unwrap(), wsurl: var("PUBLIC_WS_URL").unwrap()
protocol: var("SERVER_PROTOCOL").expect("Couldn't get protocol from environment")
} }
} }

View File

@ -13,6 +13,7 @@
*/ */
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use mysql_async::Pool;
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::{Error, Message}; use tokio_tungstenite::tungstenite::{Error, Message};
use futures::{StreamExt, SinkExt}; use futures::{StreamExt, SinkExt};
@ -65,7 +66,6 @@ P: Serialize
{ {
// Flow: Connect -> Pick out stream -> Send Data over stream // Flow: Connect -> Pick out stream -> Send Data over stream
// The stream/connection is destroyed by the end of this call // The stream/connection is destroyed by the end of this call
println!("NOTIFYING WSS");
let (ws, _) = connect_async(make_url()).await?; let (ws, _) = connect_async(make_url()).await?;
let (mut write, _) = ws.split(); let (mut write, _) = ws.split();
let event = event!(event_name, &payload); 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 { if let Err(e) = notify("new-message", message).await {
eprintln!("[API-RTC] Unable to connect to RTC server: {}", e); eprintln!("[API-RTC] Unable to connect to RTC server: {}", e);
} }