diff --git a/json-api/src/main.rs b/json-api/src/main.rs index a13636d..0f23570 100644 --- a/json-api/src/main.rs +++ b/json-api/src/main.rs @@ -37,6 +37,7 @@ mod members; mod perms; mod messages; mod admin; +mod rtc; mod http; mod testing; diff --git a/json-api/src/messages.rs b/json-api/src/messages.rs index fac6f3b..c9d49e9 100644 --- a/json-api/src/messages.rs +++ b/json-api/src/messages.rs @@ -108,7 +108,12 @@ pub async fn send_message(pool: &Pool, response: &mut Response, body: Body else { // ctype safe unwrap match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await { - Ok(Empty) => {/* TODO: put something here to notify the rtc server if its there*/}, + #[cfg(feature = "rtc")] + Ok(Row(msg)) => { + use crate::rtc; + rtc::new_message(msg).await; + }, + Ok(Empty) => { }, // idk anymore Ok(RestrictedInput(_msg)) => *response.status_mut() = StatusCode::BAD_REQUEST, Ok(Other(msg)) => { eprintln!("{}", msg); diff --git a/json-api/src/rtc.rs b/json-api/src/rtc.rs new file mode 100644 index 0000000..9c49727 --- /dev/null +++ b/json-api/src/rtc.rs @@ -0,0 +1,41 @@ +/* + * This whole module contains functions that are optionally built for those + * (((weirdos))) that don't want rtc capabilities. + * + * General configuration things + * RTC server should run on the same system + * + * Features TODO: make this fully module so that rtc & api can run on seperate + * servers if need be. This can be configured with .env most likely and the rtc + * private api can probably be a really simple auth'd REST API, + * prolly don't even need much in the way of authentication as long as the API key + * is sufficiently large & securely transferred on both parties behalf + */ + + +#[cfg(feature = "rtc")] +pub async fn new_message(message: db::Message) { + use hyper::{Body, Request}; + use hyper::client::Client; + use serde_json::json; + + let payload = json!({ + "event": "new-message", + "message": message + }); + println!("Sending event payload => {}", payload); + let request = Request::builder() + .method("POST") + .uri("http://localhost:6750/") + .body(Body::from(payload.to_string())) + .expect("[ERROR] Couldn't make event request"); + + println!("[INFO] Notifying RTC -> new-message"); + + let client = Client::new(); + // capturing both ok/err for loggin purposes + match client.request(request).await { + Ok(response) => println!("[RTC-NOTIFY] All good id-{}", message.id), + Err(e) => eprintln!("{}", e) + } +}