2023-10-02 15:36:02 +00:00
|
|
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder, web::Bytes};
|
2023-10-02 09:22:04 +00:00
|
|
|
use redis::{Client, AsyncCommands};
|
2023-09-27 23:08:48 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
2023-10-02 12:37:56 +00:00
|
|
|
use futures::StreamExt;
|
2023-10-03 10:43:21 +00:00
|
|
|
use tokio::sync::broadcast;
|
2023-10-02 20:35:49 +00:00
|
|
|
|
2023-10-03 10:43:21 +00:00
|
|
|
mod data;
|
2023-10-02 13:47:22 +00:00
|
|
|
|
2023-09-28 10:12:07 +00:00
|
|
|
async fn sse_handler(
|
|
|
|
token: web::Path<String>,
|
2023-10-02 09:22:04 +00:00
|
|
|
redis: web::Data<Client>,
|
2023-09-28 10:12:07 +00:00
|
|
|
) -> impl Responder {
|
2023-10-03 10:43:21 +00:00
|
|
|
|
2023-10-03 08:30:59 +00:00
|
|
|
let author_id = match data::get_auth_id(&token).await {
|
2023-09-27 23:08:48 +00:00
|
|
|
Ok(id) => id,
|
|
|
|
Err(e) => {
|
2023-10-02 13:55:31 +00:00
|
|
|
eprintln!("TOKEN check failed: {}", e);
|
2023-10-02 09:22:04 +00:00
|
|
|
return HttpResponse::Unauthorized().finish();
|
2023-09-27 23:08:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-02 18:55:10 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-02 20:28:36 +00:00
|
|
|
let _ = match con.sadd::<&str, &i32, usize>("authors-online", &author_id).await {
|
2023-10-02 18:55:10 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Failed to add author to online list: {}", e);
|
|
|
|
return HttpResponse::InternalServerError().finish();
|
|
|
|
}
|
|
|
|
};
|
2023-10-02 09:22:04 +00:00
|
|
|
|
2023-10-02 20:12:33 +00:00
|
|
|
let chats: Vec<String> = match con.smembers::<String, Vec<String>>(format!("chats_by_author/{}", author_id)).await {
|
2023-10-02 13:47:22 +00:00
|
|
|
Ok(chats) => {
|
|
|
|
if chats.is_empty() {
|
2023-10-03 08:30:59 +00:00
|
|
|
match data::create_first_chat(author_id, &mut con).await {
|
2023-10-02 18:55:10 +00:00
|
|
|
Ok(chat) => chat,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Failed to create first chat: {}", e);
|
|
|
|
return HttpResponse::InternalServerError().finish();
|
|
|
|
}
|
|
|
|
}
|
2023-10-02 13:47:22 +00:00
|
|
|
} else {
|
|
|
|
chats
|
|
|
|
}
|
|
|
|
},
|
2023-10-02 18:55:10 +00:00
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Failed to get chats by author: {}", e);
|
2023-10-03 08:30:59 +00:00
|
|
|
match data::create_first_chat(author_id, &mut con).await {
|
2023-10-02 20:22:05 +00:00
|
|
|
Ok(chat) => chat,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Failed to create first chat: {}", e);
|
|
|
|
return HttpResponse::InternalServerError().finish();
|
2023-10-02 18:55:10 +00:00
|
|
|
}
|
2023-10-02 20:22:05 +00:00
|
|
|
}
|
2023-10-02 18:55:10 +00:00
|
|
|
}
|
2023-10-02 13:47:22 +00:00
|
|
|
};
|
2023-09-28 10:12:07 +00:00
|
|
|
|
2023-10-03 10:43:21 +00:00
|
|
|
let (tx, mut rx) = broadcast::channel(100);
|
|
|
|
let _handle = tokio::spawn(async move {
|
|
|
|
let conn = redis.get_async_connection().await.expect("Failed to get async connection");
|
|
|
|
let mut pubsub = conn.into_pubsub();
|
2023-09-28 10:12:07 +00:00
|
|
|
|
2023-10-03 10:43:21 +00:00
|
|
|
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");
|
|
|
|
for chat_id in &chats {
|
|
|
|
let channel_name = format!("chat:{}", chat_id);
|
|
|
|
pubsub
|
|
|
|
.subscribe(channel_name.clone())
|
|
|
|
.await
|
|
|
|
.expect(&format!("Failed to subscribe to {}", channel_name));
|
2023-10-03 05:02:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 10:43:21 +00:00
|
|
|
while let Some(msg) = pubsub.on_message().next().await {
|
|
|
|
let payload: HashMap<String, String> = msg.get_payload().expect("Failed to get payload");
|
|
|
|
tx.clone().send(serde_json::to_string(&payload).expect("Failed to serialize payload")).expect("Failed to send payload");
|
2023-10-03 04:52:48 +00:00
|
|
|
}
|
2023-10-03 10:43:21 +00:00
|
|
|
});
|
|
|
|
|
2023-10-03 04:52:48 +00:00
|
|
|
|
2023-10-03 10:43:21 +00:00
|
|
|
let server_event = match rx.recv().await {
|
2023-10-02 18:55:10 +00:00
|
|
|
Ok(event) => event,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Failed to receive server event: {}", e);
|
|
|
|
return HttpResponse::InternalServerError().finish();
|
|
|
|
}
|
|
|
|
};
|
2023-10-02 15:27:55 +00:00
|
|
|
let server_event_stream = futures::stream::once(async move { Ok::<_, actix_web::Error>(Bytes::from(server_event)) });
|
|
|
|
|
2023-10-02 09:22:04 +00:00
|
|
|
HttpResponse::Ok()
|
2023-10-02 11:24:45 +00:00
|
|
|
.append_header(("content-type", "text/event-stream"))
|
2023-10-02 15:27:55 +00:00
|
|
|
.streaming(server_event_stream)
|
2023-09-27 23:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2023-10-02 18:55:10 +00:00
|
|
|
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");
|
|
|
|
|
2023-09-27 23:08:48 +00:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2023-10-02 11:48:27 +00:00
|
|
|
.app_data(web::Data::new(client.clone()))
|
2023-10-02 18:55:10 +00:00
|
|
|
.route("/presence/{token}", web::get().to(sse_handler))
|
2023-09-27 23:08:48 +00:00
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|