laravel-comments 0.2.0

Comments for Laravel Rust models: threaded replies, moderation, reactions, and events, without N+1 queries.
Documentation
//! The default answers to who may do what with a comment, for the application's policy.

use laravel::time::{Dates, ToSpan};

use crate::author::Commenter;
use crate::model::CommentModel;
use crate::ops;
use crate::status::Status;

/// Whether `user` wrote `comment`.
#[must_use]
pub fn is_author<C: CommentModel>(user: &C::Author, comment: &C) -> bool {
    comment.row().author_id == Some(user.commenter_id())
}

/// Moderators edit any comment; authors edit their own while it isn't removed, within
/// `COMMENTS_EDIT_WINDOW` minutes of posting.
#[must_use]
pub fn can_edit<C: CommentModel>(user: &C::Author, comment: &C) -> bool {
    let row = comment.row();
    if row.deleted_at.is_some() {
        return false;
    }
    if user.is_moderator() {
        return true;
    }
    if !is_author(user, comment) {
        return false;
    }
    let window = ops::config().edit_window;
    window <= 0
        || row
            .created_at
            .is_some_and(|posted| posted.plus(window.minutes()).is_future())
}

/// Moderators remove any comment; authors remove their own.
#[must_use]
pub fn can_remove<C: CommentModel>(user: &C::Author, comment: &C) -> bool {
    user.is_moderator() || is_author(user, comment)
}

/// Anyone may reply to an approved comment that isn't removed.
#[must_use]
pub fn can_reply<C: CommentModel>(_user: &C::Author, comment: &C) -> bool {
    let row = comment.row();
    row.status == Status::Approved && row.deleted_at.is_none()
}

/// Moderators moderate.
#[must_use]
pub fn can_moderate<U: Commenter>(user: &U) -> bool {
    user.is_moderator()
}

/// Declares a policy with the package's default abilities: `update`, `delete`, and `reply` on a
/// comment, and `moderate`. Check them as any policy's:
/// `user.authorize(CommentPolicy::update, &comment).await?`.
///
/// ```no_run
/// # use laravel::model::{model, morph};
/// # use laravel_comments::Commenter;
/// # #[model] pub struct User { pub id: i64 }
/// # impl Commenter for User { fn commenter_id(&self) -> i64 { self.id } }
/// # #[model] pub struct Post { pub id: i64 }
/// # #[morph] pub enum Commentable { Post(Post) }
/// # laravel_comments::comment_model! { pub struct Comment { commentable: Commentable, author: User } }
/// laravel_comments::comment_policy! {
///     pub struct CommentPolicy for Comment
/// }
/// ```
///
/// For other rules, write the policy yourself and call the functions of
/// [`abilities`](crate::abilities) where the defaults suit you.
#[macro_export]
macro_rules! comment_policy {
    ($(#[$meta:meta])* $vis:vis struct $name:ident for $comment:ident) => {
        $(#[$meta])*
        $vis struct $name;

        #[$crate::__private::laravel::auth::policy]
        impl $name {
            pub fn update(
                user: &<$comment as $crate::CommentModel>::Author,
                comment: &$comment,
            ) -> bool {
                $crate::abilities::can_edit(user, comment)
            }

            pub fn delete(
                user: &<$comment as $crate::CommentModel>::Author,
                comment: &$comment,
            ) -> bool {
                $crate::abilities::can_remove(user, comment)
            }

            pub fn reply(
                user: &<$comment as $crate::CommentModel>::Author,
                comment: &$comment,
            ) -> bool {
                $crate::abilities::can_reply(user, comment)
            }

            pub fn moderate(user: &<$comment as $crate::CommentModel>::Author) -> bool {
                $crate::abilities::can_moderate(user)
            }
        }
    };
}