82 lines
1.9 KiB
Rust
82 lines
1.9 KiB
Rust
use std::sync::Arc;
|
|
|
|
use serde::Serialize;
|
|
use uuid::Uuid;
|
|
|
|
use crate::queue::NatsQueue;
|
|
|
|
#[derive(Clone)]
|
|
pub struct ImNats {
|
|
inner: Arc<NatsQueue>,
|
|
}
|
|
|
|
impl ImNats {
|
|
pub fn new(nats: Arc<NatsQueue>) -> Self {
|
|
Self { inner: nats }
|
|
}
|
|
|
|
pub async fn emit<T: Serialize>(&self, subject: &str, request_id: Uuid, event: &T) {
|
|
if let Err(e) = self
|
|
.inner
|
|
.publish_with_headers(
|
|
subject,
|
|
&serde_json::to_vec(event).unwrap_or_default(),
|
|
vec![("X-Request-Id".into(), request_id.to_string())],
|
|
)
|
|
.await
|
|
{
|
|
tracing::warn!(subject, error = %e, "nats emit failed");
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn channel_subject(channel_id: Uuid) -> String {
|
|
format!("im.channel.{channel_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn message_subject(channel_id: Uuid) -> String {
|
|
format!("im.message.{channel_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn thread_subject(channel_id: Uuid, thread_id: Uuid) -> String {
|
|
format!("im.thread.{channel_id}.{thread_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn member_subject(channel_id: Uuid) -> String {
|
|
format!("im.member.{channel_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn reaction_subject(channel_id: Uuid) -> String {
|
|
format!("im.reaction.{channel_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn typing_subject(channel_id: Uuid) -> String {
|
|
format!("im.typing.{channel_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn presence_subject(user_id: Uuid) -> String {
|
|
format!("im.presence.{user_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn poll_subject(channel_id: Uuid) -> String {
|
|
format!("im.poll.{channel_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn article_subject(channel_id: Uuid) -> String {
|
|
format!("im.article.{channel_id}")
|
|
}
|
|
|
|
#[inline]
|
|
pub fn workspace_channels_subject(workspace_name: &str) -> String {
|
|
format!("im.ws_channels.{workspace_name}")
|
|
}
|
|
}
|