sse-format

This commit is contained in:
2023-10-19 17:43:00 +03:00
parent 8473694757
commit 5cf79bf531
5 changed files with 106 additions and 66 deletions

View File

@@ -1,10 +1,12 @@
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
use reqwest::Client as HTTPClient;
use serde_json::{json, Value};
use serde_json::json;
use std::collections::HashMap;
use std::env;
use std::error::Error;
use crate::SSEMessageData;
pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
let auth_api_base = env::var("AUTH_URL")?;
let (query_name, query_type) = match auth_api_base.contains("auth.discours.io") {
@@ -105,32 +107,29 @@ async fn get_shout_followers(shout_id: &str) -> Result<Vec<i32>, Box<dyn Error>>
pub async fn is_fitting(
listener_id: i32,
kind: String,
payload: HashMap<String, Value>,
message_data: SSEMessageData,
) -> Result<bool, &'static str> {
match &kind[0..9] {
"new_react" => {
// payload is Reaction, kind is new_reaction<reaction_kind>
let shout_id = payload.get("shout").unwrap().as_str().unwrap();
let recipients = get_shout_followers(shout_id).await.unwrap();
if message_data.entity == "reaction" {
// payload is Reaction
let shout_id = message_data.payload.get("shout").unwrap().as_str().unwrap();
let recipients = get_shout_followers(shout_id).await.unwrap();
Ok(recipients.contains(&listener_id))
}
"new_shout" => {
// payload is Shout, kind is "new_shout"
// TODO: check all community subscribers if no then
// check all topics subscribers if no then
// check all authors subscribers
Ok(true)
}
"new_messa" => {
println!("own message passed");
Ok(false)
},
_ => {
eprintln!("unknown payload kind");
eprintln!("{:?}", payload);
Ok(false)
}
Ok(recipients.contains(&listener_id))
} else if message_data.entity == "shout" {
// payload is Shout
// TODO: check all community subscribers if no then
// TODO: check all topics subscribers if no then
// TODO: check all authors subscribers
Ok(true)
} else if message_data.entity == "chat" {
// payload is Message or Chat
Ok(true)
} else if message_data.entity == "follower" {
// payload is Author
Ok(true)
}else {
eprintln!("[data] unknown entity");
eprintln!("{:?}", message_data);
Ok(false)
}
}