quoter/src/main.rs

132 lines
3.9 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 10:16:57 +00:00
use std::error::Error;
2023-10-02 11:48:27 +00:00
use futures::{StreamExt, FutureExt};
2023-10-02 09:22:04 +00:00
use tokio::sync::broadcast::{self, Receiver};
2023-09-27 23:08:48 +00:00
2023-10-02 10:16:57 +00:00
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum PayloadKind {
NewMessage,
NewFollower,
NewShout,
NewApproval,
NewComment,
NewRate
}
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-10-02 10:16:57 +00:00
author_id: Option<i32>,
topic_id: Option<i32>,
reaction_id: Option<i32>,
community_id: Option<i32>,
kind: PayloadKind,
body: String,
2023-09-27 23:08:48 +00:00
}
2023-10-02 10:16:57 +00:00
async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
2023-09-28 10:12:07 +00:00
let api_base = env::var("API_BASE")?;
2023-10-02 10:16:57 +00:00
let gql = match api_base.contains("v2") {
true => r#"mutation { getSession { user { id } } }"#, // v2
_ => r#"query { sessiom { user { id } } }"# // authorizer
2023-10-02 09:22:04 +00:00
};
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-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 {
2023-10-02 10:16:57 +00:00
let author_id = match get_auth_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
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 11:48:27 +00:00
let server_event = rx.get_ref().into_stream().map(|result| {
2023-10-02 09:22:04 +00:00
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)))
}
2023-10-02 11:48:27 +00:00
Err(_) => Err(actix_web::Error::from("An error occurred")),
2023-10-02 09:22:04 +00:00
}
});
let _: () = con
.srem("authors-online", &author_id)
.await
.unwrap();
HttpResponse::Ok()
2023-10-02 11:24:45 +00:00
.append_header(("content-type", "text/event-stream"))
2023-10-02 09:22:04 +00:00
.streaming(server_event)
2023-09-27 23:08:48 +00:00
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
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();
pubsub.subscribe("new_follower").await.unwrap();
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
2023-10-02 11:48:27 +00:00
while let Some(msg) = pubsub.on_message().await {
2023-09-28 10:12:07 +00:00
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-10-02 11:48:27 +00:00
.app_data(web::Data::new(rx.clone()))
.app_data(web::Data::new(client.clone()))
2023-10-02 09:22:04 +00:00
.route("/aware/{token}", web::get().to(sse_handler))
2023-09-27 23:08:48 +00:00
})
.bind("127.0.0.1:8080")?
.run()
.await
}