This commit is contained in:
Tony Rewin 2023-10-02 13:16:57 +03:00
parent adc65909f1
commit c1ff00e354
2 changed files with 23 additions and 7 deletions

View File

@ -1,5 +1,4 @@
### ENV ### ENV
- API_BASE - API_BASE
- IM_BASE
- REDIS_URL - REDIS_URL

View File

@ -5,22 +5,39 @@ use serde::{Serialize, Deserialize};
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::error::Error;
use futures::FutureExt; use futures::FutureExt;
use tokio::sync::broadcast::{self, Receiver}; use tokio::sync::broadcast::{self, Receiver};
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum PayloadKind {
NewMessage,
NewFollower,
NewShout,
NewApproval,
NewComment,
NewRate
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Payload { struct Payload {
chat_id: Option<String>, chat_id: Option<String>,
shout_id: Option<i32>, shout_id: Option<i32>,
author_id: Option<i32>,
topic_id: Option<i32>,
reaction_id: Option<i32>,
community_id: Option<i32>,
kind: PayloadKind,
body: String,
} }
// Получаем id автора из токена // Получаем id автора из токена
async fn get_auth_id(token: &str) -> Result<i32, Box<dyn std::error::Error>> { async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
let api_base = env::var("API_BASE")?; let api_base = env::var("API_BASE")?;
let gql = match api_base.contains("auth") { let gql = match api_base.contains("v2") {
true => r#"query { sessiom { user { id } } }"#, true => r#"mutation { getSession { user { id } } }"#, // v2
_ => r#"mutation { getSession { user { id } } }"# _ => r#"query { sessiom { user { id } } }"# // authorizer
}; };
let client = HTTPClient::new(); let client = HTTPClient::new();
let response = client let response = client
@ -42,7 +59,7 @@ async fn sse_handler(
rx: web::Data<Receiver<String>>, rx: web::Data<Receiver<String>>,
redis: web::Data<Client>, redis: web::Data<Client>,
) -> impl Responder { ) -> impl Responder {
let author_id = match get_author_id(&token).await { let author_id = match get_auth_id(&token).await {
Ok(id) => id, Ok(id) => id,
Err(e) => { Err(e) => {
eprintln!("Не удалось проверить токен: {}", e); eprintln!("Не удалось проверить токен: {}", e);