use laravel::time::{Dates, ToSpan};
use crate::author::Commenter;
use crate::model::CommentModel;
use crate::ops;
use crate::status::Status;
#[must_use]
pub fn is_author<C: CommentModel>(user: &C::Author, comment: &C) -> bool {
comment.row().author_id == Some(user.commenter_id())
}
#[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())
}
#[must_use]
pub fn can_remove<C: CommentModel>(user: &C::Author, comment: &C) -> bool {
user.is_moderator() || is_author(user, comment)
}
#[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()
}
#[must_use]
pub fn can_moderate<U: Commenter>(user: &U) -> bool {
user.is_moderator()
}
#[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)
}
}
};
}