use actix_web::{web, App, HttpResponse, HttpServer, Responder, web::Bytes}; use redis::{Client, AsyncCommands}; use reqwest::Client as HTTPClient; use serde::{Serialize, Deserialize}; use serde_json::Value; use std::collections::HashMap; use std::env; use std::error::Error; use futures::StreamExt; use tokio::sync::broadcast::{self, Receiver}; use uuid::Uuid; use chrono::Utc; #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] enum PayloadKind { NewMessage, NewFollower, NewShout, NewApproval, NewComment, NewRate } #[derive(Debug, Serialize, Deserialize)] struct Payload { chat_id: Option, shout_id: Option, author_id: Option, topic_id: Option, reaction_id: Option, community_id: Option, kind: PayloadKind, body: String, } async fn get_auth_id(token: &str) -> Result> { let api_base = env::var("API_BASE")?; let gql = match api_base.contains("v2") { true => r#"mutation { getSession { user { id } } }"#, // v2 _ => r#"query { sessiom { user { id } } }"# // authorizer }; let client = HTTPClient::new(); let response = client .post(api_base) .bearer_auth(token) // NOTE: auth token is here .body(gql) .send() .await?; let response_body: Value = response.json().await?; let id = response_body["data"]["getSession"]["user"]["id"] .as_i64() .ok_or("Failed to get user id by token")? as i32; Ok(id) } async fn create_first_chat(author_id: i32, con: &mut redis::aio::Connection) -> Result, Box> { let chat_id = Uuid::new_v4().to_string(); let members = vec![author_id.to_string(), "1".to_string()]; let timestamp = Utc::now().timestamp(); let chat = serde_json::json!({ "id": chat_id.clone(), "admins": members.clone(), "members": members.clone(), "title": "", "createdBy": author_id, "createdAt": timestamp, "updatedAt": timestamp, }); let _: () = redis::pipe() .atomic() .cmd("SADD") .arg(format!("chats_by_author/{}", author_id)) .arg(&chat_id) .ignore() .set(format!("chats/{}", chat_id), chat.to_string()) .ignore() .set(format!("chats/{}/next_message_id", chat_id), "0") .ignore() .query_async(con) .await?; Ok(vec![chat_id]) } async fn sse_handler( token: web::Path, rx: web::Data>, redis: web::Data, ) -> impl Responder { let author_id = match get_auth_id(&token).await { Ok(id) => id, Err(e) => { eprintln!("TOKEN check failed: {}", e); return HttpResponse::Unauthorized().finish(); } }; let mut con = match redis.get_async_connection().await { Ok(con) => con, Err(e) => { eprintln!("Failed to get async connection: {}", e); return HttpResponse::InternalServerError().finish(); } }; let _ = match con.sadd::<&str, &i32, usize>("authors-online", &author_id).await { Ok(_) => (), Err(e) => { eprintln!("Failed to add author to online list: {}", e); return HttpResponse::InternalServerError().finish(); } }; let chats: Vec = match con.smembers::>(format!("chats_by_author/{}", author_id)).await { Ok(chats) => { if chats.is_empty() { match create_first_chat(author_id, &mut con).await { Ok(chat) => chat, Err(e) => { eprintln!("Failed to create first chat: {}", e); return HttpResponse::InternalServerError().finish(); } } } else { chats } }, Err(e) => { eprintln!("Failed to get chats by author: {}", e); match create_first_chat(author_id, &mut con).await { Ok(chat) => chat, Err(e) => { eprintln!("Failed to create first chat: {}", e); return HttpResponse::InternalServerError().finish(); } } } }; let mut pubsub = con.into_pubsub(); for chat_id in &chats { if let Err(e) = pubsub.subscribe(format!("chat:{}", chat_id)).await { eprintln!("Failed to subscribe to chat: {}", e); return HttpResponse::InternalServerError().finish(); } } let _ = match con.srem::<&str, &i32, usize>("authors-online", &author_id).await { Ok(_) => (), Err(e) => { eprintln!("Failed to remove author from online list: {}", e); return HttpResponse::InternalServerError().finish(); } }; let server_event = match rx.recv().await { Ok(event) => event, Err(e) => { eprintln!("Failed to receive server event: {}", e); return HttpResponse::InternalServerError().finish(); } }; let server_event_stream = futures::stream::once(async move { Ok::<_, actix_web::Error>(Bytes::from(server_event)) }); HttpResponse::Ok() .append_header(("content-type", "text/event-stream")) .streaming(server_event_stream) } #[actix_web::main] async fn main() -> std::io::Result<()> { let (tx, _rx) = broadcast::channel(100); let redis_url = env::var("REDIS_URL").expect("REDIS_URL must be set"); let client = redis::Client::open(redis_url).expect("Failed to open Redis client"); let _handle = tokio::spawn(async move { let mut conn = client.get_async_connection().await.expect("Failed to get async connection"); let mut pubsub = conn.into_pubsub(); pubsub.subscribe("new_follower").await.expect("Failed to subscribe to new_follower"); pubsub.subscribe("new_shout").await.expect("Failed to subscribe to new_shout"); pubsub.subscribe("new_reaction").await.expect("Failed to subscribe to new_reaction"); while let Some(msg) = pubsub.on_message().next().await { let payload: HashMap = msg.get_payload().expect("Failed to get payload"); tx.send(serde_json::to_string(&payload).expect("Failed to serialize payload")).expect("Failed to send payload"); } }); HttpServer::new(move || { let rx = tx.subscribe(); App::new() .app_data(web::Data::new(rx)) .app_data(web::Data::new(client.clone())) .route("/presence/{token}", web::get().to(sse_handler)) }) .bind("127.0.0.1:8080")? .run() .await }