fix-condition

This commit is contained in:
Untone 2023-10-16 17:03:57 +03:00
parent d475688197
commit 2b8d4469ab
2 changed files with 13 additions and 13 deletions

View File

@ -108,30 +108,30 @@ pub async fn is_fitting(
kind: String, kind: String,
payload: HashMap<String, String>, payload: HashMap<String, String>,
) -> Result<bool, &'static str> { ) -> Result<bool, &'static str> {
match kind.as_str() { match &kind[0..9] {
"new_follower" => { "new_follo" => {
// payload is AuthorFollower // payload is Author, kind is new_follower:<author_id>
Ok(payload.get("author").unwrap().to_string() == listener_id.to_string()) let author_id = kind.split(":").last().unwrap();
Ok(author_id.to_string() == listener_id.to_string())
} }
"new_reaction" => { "new_react" => {
// payload is Reaction // payload is Reaction, kind is new_reaction<reaction_kind>
let shout_id = payload.get("shout").unwrap(); let shout_id = payload.get("shout").unwrap();
let recipients = get_shout_followers(shout_id).await.unwrap(); let recipients = get_shout_followers(shout_id).await.unwrap();
Ok(recipients.contains(&listener_id)) Ok(recipients.contains(&listener_id))
} }
"new_shout" => { "new_shout" => {
// payload is Shout // payload is Shout, kind is "new_shout"
// TODO: check all community subscribers if no then // TODO: check all community subscribers if no then
// check all topics subscribers if no then // check all topics subscribers if no then
// check all authors subscribers // check all authors subscribers
Ok(true) Ok(true)
} }
"new_message" => { "new_messa" => {
// payload is Chat // payload is Message, kind is "new_message"
let members_str = payload.get("members").unwrap();
let members = serde_json::from_str::<Vec<String>>(members_str).unwrap(); Ok(true)
Ok(members.contains(&listener_id.to_string()))
} }
_ => { _ => {
eprintln!("unknown payload kind"); eprintln!("unknown payload kind");

View File

@ -79,7 +79,7 @@ async fn connect_handler(
while let Some(msg) = pubsub.on_message().next().await { while let Some(msg) = pubsub.on_message().next().await {
let message_str: String = msg.get_payload().unwrap(); let message_str: String = msg.get_payload().unwrap();
let message_data: RedisMessageData = serde_json::from_str(&message_str).unwrap(); let message_data: RedisMessageData = serde_json::from_str(&message_str).unwrap();
if data::is_fitting(listener_id, message_data.kind.to_string(), message_data.payload).await.is_ok() { if msg.get_channel_name().starts_with("chat:") || data::is_fitting(listener_id, message_data.kind.to_string(), message_data.payload).await.is_ok() {
let send_result = tx.send(message_str); let send_result = tx.send(message_str);
if send_result.is_err() { if send_result.is_err() {
let _ = con.srem::<&str, &i32, usize>("authors-online", &listener_id).await.map_err(|e| { let _ = con.srem::<&str, &i32, usize>("authors-online", &listener_id).await.map_err(|e| {