quoter/src/main.rs

123 lines
4.1 KiB
Rust
Raw Normal View History

2023-10-02 09:22:04 +00:00
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use redis::{Client, AsyncCommands};
use reqwest::Client as HTTPClient;
use serde::{Serialize, Deserialize};
2023-09-28 10:12:07 +00:00
use serde_json::Value;
2023-09-27 23:08:48 +00:00
use std::collections::HashMap;
use std::env;
2023-10-02 09:22:04 +00:00
use futures::FutureExt;
use tokio::sync::broadcast::{self, Receiver};
2023-09-27 23:08:48 +00:00
2023-10-02 09:22:04 +00:00
#[derive(Debug, Serialize, Deserialize)]
2023-09-28 10:12:07 +00:00
struct Payload {
2023-10-02 09:22:04 +00:00
chat_id: Option<String>,
shout_id: Option<i32>,
2023-09-27 23:08:48 +00:00
}
2023-10-02 09:22:04 +00:00
// Получаем id автора из токена
async fn get_auth_id(token: &str) -> Result<i32, Box<dyn std::error::Error>> {
2023-09-28 10:12:07 +00:00
let api_base = env::var("API_BASE")?;
2023-10-02 09:22:04 +00:00
let gql = match api_base.contains("auth") {
true => r#"query { sessiom { user { id } } }"#,
_ => r#"mutation { getSession { user { id } } }"#
};
let client = HTTPClient::new();
2023-09-28 10:12:07 +00:00
let response = client
.post(api_base)
2023-10-02 09:22:04 +00:00
.bearer_auth(token) // NOTE: auth token is here
2023-09-28 10:12:07 +00:00
.body(gql)
2023-09-27 23:08:48 +00:00
.send()
.await?;
2023-10-02 09:22:04 +00:00
let response_body: Value = response.json().await?;
2023-09-28 10:12:07 +00:00
let id = response_body["data"]["getSession"]["user"]["id"]
.as_i64()
.ok_or("Failed to get user id by token")? as i32;
Ok(id)
2023-09-27 23:08:48 +00:00
}
2023-10-02 09:22:04 +00:00
// Обработчик SSE
2023-09-28 10:12:07 +00:00
async fn sse_handler(
token: web::Path<String>,
rx: web::Data<Receiver<String>>,
2023-10-02 09:22:04 +00:00
redis: web::Data<Client>,
2023-09-28 10:12:07 +00:00
) -> impl Responder {
let author_id = match get_author_id(&token).await {
2023-09-27 23:08:48 +00:00
Ok(id) => id,
Err(e) => {
2023-10-02 09:22:04 +00:00
eprintln!("Не удалось проверить токен: {}", e);
return HttpResponse::Unauthorized().finish();
2023-09-27 23:08:48 +00:00
}
};
2023-10-02 09:22:04 +00:00
let mut con = redis.get_async_connection().await.unwrap();
let _: () = con
.sadd("authors-online", &author_id)
.await
.unwrap();
// Получаем все чаты авторизованного пользователя
let chats: Vec<String> = con
.smembers(format!("chats_by_author/{}", author_id))
.await
.unwrap();
2023-09-28 10:12:07 +00:00
2023-10-02 09:22:04 +00:00
// Подписываемся на каждый чат в Redis Pub/Sub
let mut pubsub = con.into_pubsub();
for chat_id in chats {
pubsub.subscribe(format!("message:{}", chat_id)).await.unwrap();
2023-09-28 10:12:07 +00:00
}
2023-10-02 09:22:04 +00:00
let server_event = rx.get_ref().clone().into_stream().map(|result| {
match result {
Ok(payload) => {
let payload: Payload = serde_json::from_str(&payload).unwrap();
Ok::<_, actix_web::Error>(web::Bytes::from(format!("data: {:?}\n\n", payload.chat_id)))
}
Err(_) => Err(actix_web::Error::from(())),
}
});
let _: () = con
.srem("authors-online", &author_id)
.await
.unwrap();
HttpResponse::Ok()
.header("content-type", "text/event-stream")
.streaming(server_event)
2023-09-27 23:08:48 +00:00
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2023-10-02 09:22:04 +00:00
// Создаем одно соединение с Redis Pub/Sub и канал вещания
2023-09-28 10:12:07 +00:00
let (tx, rx) = broadcast::channel(100);
let redis_url = env::var("REDIS_URL").unwrap();
2023-10-02 09:22:04 +00:00
let client = redis::Client::open(redis_url).unwrap();
2023-09-28 10:12:07 +00:00
let _handle = tokio::spawn(async move {
let mut conn = client.get_async_connection().await.unwrap();
let mut pubsub = conn.into_pubsub();
2023-10-02 09:22:04 +00:00
// новый подписчик автора
2023-09-28 10:12:07 +00:00
pubsub.subscribe("new_follower").await.unwrap();
2023-10-02 09:22:04 +00:00
// в теме, автор, сообщество
2023-09-28 10:12:07 +00:00
pubsub.subscribe("new_shout").await.unwrap();
2023-10-02 09:22:04 +00:00
// оценка, комментарий, одобрение в криках автора и реагировавших криках
pubsub.subscribe("new_reaction").await.unwrap();
2023-09-28 10:12:07 +00:00
while let Some(msg) = pubsub.on_message().next().await {
let payload: HashMap<String, String> = msg.get_payload().unwrap();
tx.send(serde_json::to_string(&payload).unwrap()).unwrap();
}
});
2023-09-27 23:08:48 +00:00
HttpServer::new(move || {
App::new()
2023-09-28 10:12:07 +00:00
.data(rx.clone())
2023-10-02 09:22:04 +00:00
.data(client.clone())
.route("/aware/{token}", web::get().to(sse_handler))
2023-09-27 23:08:48 +00:00
})
.bind("127.0.0.1:8080")?
.run()
.await
}