use laravel::broadcast::{Broadcast, Channel};
use laravel::event::Event;
use crate::Status;
use crate::config::BroadcastChannel;
use crate::ops;
pub trait CommentEvent {
fn commentable_type(&self) -> &str;
fn commentable_id(&self) -> i64;
fn channel(&self) -> String {
format!("comments.{}.{}", self.commentable_type(), self.commentable_id())
}
}
fn channels(event: &impl CommentEvent, shown: bool) -> laravel::Result<Vec<Channel>> {
if !shown {
return Ok(Vec::new());
}
Ok(match ops::config().broadcast {
BroadcastChannel::Off => Vec::new(),
BroadcastChannel::Public => vec![Channel::public(event.channel())?],
BroadcastChannel::Private => vec![Channel::private(event.channel())?],
})
}
macro_rules! comment_event {
($name:ident, |$event:ident| $shown:expr) => {
impl CommentEvent for $name {
fn commentable_type(&self) -> &str {
&self.commentable_type
}
fn commentable_id(&self) -> i64 {
self.commentable_id
}
}
impl Broadcast for $name {
fn broadcast_on(&self) -> laravel::Result<Vec<Channel>> {
let $event = self;
let shown = $shown;
let _ = $event;
channels(self, shown)
}
}
};
}
#[derive(Debug, Clone, Event)]
#[event(broadcast)]
#[non_exhaustive]
pub struct CommentPosted {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
pub parent_id: Option<i64>,
pub author_id: Option<i64>,
pub status: Status,
}
comment_event!(CommentPosted, |event| event.status == Status::Approved);
#[derive(Debug, Clone, Event)]
#[event(broadcast)]
#[non_exhaustive]
pub struct CommentApproved {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
}
comment_event!(CommentApproved, |event| true);
#[derive(Debug, Clone, Event)]
#[event(broadcast)]
#[non_exhaustive]
pub struct CommentModerated {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
pub from: Status,
pub to: Status,
}
comment_event!(CommentModerated, |event| event.from == Status::Approved
|| event.to == Status::Approved);
#[derive(Debug, Clone, Event)]
#[event(broadcast)]
#[non_exhaustive]
pub struct CommentEdited {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
pub status: Status,
}
comment_event!(CommentEdited, |event| event.status == Status::Approved);
#[derive(Debug, Clone, Event)]
#[event(broadcast)]
#[non_exhaustive]
pub struct CommentDeleted {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
pub placeholder: bool,
pub status: Status,
}
comment_event!(CommentDeleted, |event| event.status == Status::Approved);
#[derive(Debug, Clone, Event)]
#[event(broadcast)]
#[non_exhaustive]
pub struct ReactionAdded {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
pub user_id: i64,
pub reaction: String,
}
comment_event!(ReactionAdded, |event| true);
#[derive(Debug, Clone, Event)]
#[event(broadcast)]
#[non_exhaustive]
pub struct ReactionRemoved {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
pub user_id: i64,
pub reaction: String,
}
comment_event!(ReactionRemoved, |event| true);
#[derive(Debug, Clone, Event)]
#[non_exhaustive]
pub struct UserMentioned {
pub comment_id: i64,
pub commentable_type: String,
pub commentable_id: i64,
pub user_id: i64,
pub author_id: Option<i64>,
}