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
- API_BASE
- IM_BASE
- REDIS_URL

View File

@ -5,22 +5,39 @@ use serde::{Serialize, Deserialize};
use serde_json::Value;
use std::collections::HashMap;
use std::env;
use std::error::Error;
use futures::FutureExt;
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)]
struct Payload {
chat_id: Option<String>,
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 автора из токена
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 gql = match api_base.contains("auth") {
true => r#"query { sessiom { user { id } } }"#,
_ => r#"mutation { getSession { user { id } } }"#
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
@ -42,7 +59,7 @@ async fn sse_handler(
rx: web::Data<Receiver<String>>,
redis: web::Data<Client>,
) -> impl Responder {
let author_id = match get_author_id(&token).await {
let author_id = match get_auth_id(&token).await {
Ok(id) => id,
Err(e) => {
eprintln!("Не удалось проверить токен: {}", e);