2024-04-23 17:23:13 +02:00
|
|
|
pub mod antenna;
|
2024-04-23 21:13:29 +02:00
|
|
|
pub mod chat;
|
2024-04-25 22:44:37 +02:00
|
|
|
pub mod moderation;
|
2024-04-23 17:23:13 +02:00
|
|
|
|
2024-04-21 17:36:06 +02:00
|
|
|
use crate::config::CONFIG;
|
2024-04-19 23:54:06 +02:00
|
|
|
use crate::database::redis_conn;
|
|
|
|
use redis::{Commands, RedisError};
|
|
|
|
|
2024-04-21 17:17:33 +02:00
|
|
|
#[derive(strum::Display)]
|
2024-04-19 23:54:06 +02:00
|
|
|
pub enum Stream {
|
|
|
|
#[strum(serialize = "internal")]
|
|
|
|
Internal,
|
|
|
|
#[strum(serialize = "broadcast")]
|
|
|
|
Broadcast,
|
2024-04-25 22:44:37 +02:00
|
|
|
#[strum(to_string = "adminStream:{moderator_id}")]
|
|
|
|
Moderation { moderator_id: String },
|
2024-04-21 17:28:05 +02:00
|
|
|
#[strum(to_string = "user:{user_id}")]
|
|
|
|
User { user_id: String },
|
|
|
|
#[strum(to_string = "channelStream:{channel_id}")]
|
|
|
|
Channel { channel_id: String },
|
|
|
|
#[strum(to_string = "noteStream:{note_id}")]
|
|
|
|
Note { note_id: String },
|
2024-04-19 23:54:06 +02:00
|
|
|
#[strum(serialize = "notesStream")]
|
|
|
|
Notes,
|
2024-04-21 17:28:05 +02:00
|
|
|
#[strum(to_string = "userListStream:{list_id}")]
|
|
|
|
UserList { list_id: String },
|
|
|
|
#[strum(to_string = "mainStream:{user_id}")]
|
|
|
|
Main { user_id: String },
|
|
|
|
#[strum(to_string = "driveStream:{user_id}")]
|
|
|
|
Drive { user_id: String },
|
|
|
|
#[strum(to_string = "antennaStream:{antenna_id}")]
|
|
|
|
Antenna { antenna_id: String },
|
|
|
|
#[strum(to_string = "messagingStream:{sender_user_id}-{receiver_user_id}")]
|
|
|
|
Chat {
|
|
|
|
sender_user_id: String,
|
|
|
|
receiver_user_id: String,
|
|
|
|
},
|
|
|
|
#[strum(to_string = "messagingStream:{group_id}")]
|
|
|
|
GroupChat { group_id: String },
|
|
|
|
#[strum(to_string = "messagingIndexStream:{user_id}")]
|
|
|
|
MessagingIndex { user_id: String },
|
2024-04-19 23:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("Redis error: {0}")]
|
|
|
|
RedisError(#[from] RedisError),
|
2024-04-21 18:47:56 +02:00
|
|
|
#[error("Json (de)serialization error: {0}")]
|
|
|
|
JsonError(#[from] serde_json::Error),
|
2024-04-19 23:54:06 +02:00
|
|
|
#[error("Value error: {0}")]
|
|
|
|
ValueError(String),
|
|
|
|
}
|
|
|
|
|
2024-04-21 15:43:09 +02:00
|
|
|
pub fn publish_to_stream(
|
|
|
|
stream: &Stream,
|
2024-04-23 21:13:29 +02:00
|
|
|
kind: Option<String>,
|
2024-04-21 15:43:09 +02:00
|
|
|
value: Option<String>,
|
|
|
|
) -> Result<(), Error> {
|
2024-04-19 23:54:06 +02:00
|
|
|
let message = if let Some(kind) = kind {
|
2024-04-20 02:54:02 +02:00
|
|
|
format!(
|
|
|
|
"{{ \"type\": \"{}\", \"body\": {} }}",
|
|
|
|
kind,
|
2024-04-21 17:21:10 +02:00
|
|
|
value.unwrap_or("null".to_string()),
|
2024-04-20 02:54:02 +02:00
|
|
|
)
|
2024-04-19 23:54:06 +02:00
|
|
|
} else {
|
|
|
|
value.ok_or(Error::ValueError("Invalid streaming message".to_string()))?
|
|
|
|
};
|
|
|
|
|
2024-04-20 02:54:02 +02:00
|
|
|
redis_conn()?.publish(
|
|
|
|
&CONFIG.host,
|
|
|
|
format!(
|
|
|
|
"{{ \"channel\": \"{}\", \"message\": {} }}",
|
2024-04-21 15:43:09 +02:00
|
|
|
stream, message,
|
2024-04-20 02:54:02 +02:00
|
|
|
),
|
|
|
|
)?;
|
2024-04-19 23:54:06 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod unit_test {
|
|
|
|
use super::Stream;
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn channel_to_string() {
|
|
|
|
assert_eq!(Stream::Internal.to_string(), "internal");
|
|
|
|
assert_eq!(Stream::Broadcast.to_string(), "broadcast");
|
|
|
|
assert_eq!(
|
2024-04-25 22:44:37 +02:00
|
|
|
Stream::Moderation {
|
|
|
|
moderator_id: "9tb42br63g5apjcq".to_string()
|
2024-04-19 23:54:06 +02:00
|
|
|
}
|
|
|
|
.to_string(),
|
|
|
|
"adminStream:9tb42br63g5apjcq"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|