Skip to main content

laravel_comments/
moderation.rs

1//! The rule that gives a new comment its status.
2
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7use crate::Status;
8
9/// A comment about to be posted, as a moderation rule sees it.
10#[derive(Debug, Clone)]
11pub struct NewComment {
12    /// The morph name of the model it belongs to, such as `post`.
13    pub commentable_type: &'static str,
14    /// The key of the model it belongs to.
15    pub commentable_id: i64,
16    /// The parent's key, for replies.
17    pub parent_id: Option<i64>,
18    /// The author's key, for comments by users.
19    pub author_id: Option<i64>,
20    /// The guest's email address, for comments by guests.
21    pub guest_email: Option<String>,
22    /// The text.
23    pub body: String,
24}
25
26type RuleFuture = Pin<Box<dyn Future<Output = laravel::Result<Status>> + Send>>;
27
28/// The application's moderation rule, registered with
29/// [`CommentsServiceProvider::moderate_with`](crate::CommentsServiceProvider::moderate_with).
30#[derive(Clone)]
31pub(crate) struct ModerationRule(pub(crate) Option<Arc<dyn Fn(NewComment) -> RuleFuture + Send + Sync>>);
32
33impl ModerationRule {
34    pub(crate) fn new<F, Fut>(rule: F) -> Self
35    where
36        F: Fn(NewComment) -> Fut + Send + Sync + 'static,
37        Fut: Future<Output = laravel::Result<Status>> + Send + 'static,
38    {
39        Self(Some(Arc::new(move |comment| Box::pin(rule(comment)))))
40    }
41}