use std::future::Future;
use laravel::db::{Builder, Condition};
use laravel::model::{Column, Model, Morph, MorphType};
use laravel::time::Timestamp;
use laravel_nestedset::NestedSet;
use serde::Serialize;
use crate::author::{Author, Commenter};
use crate::revisions::CommentRevision;
use crate::status::{Status, Visibility};
use crate::thread::Threads;
use crate::{ops, reactions};
pub trait CommentModel: NestedSet<Key = i64> + Serialize {
type Commentable: Morph<Key = i64>;
type Author: Model<Key = i64> + Commenter;
const COLUMNS: CommentColumns<Self>;
fn row(&self) -> CommentRow<'_, Self>;
fn new_query() -> Builder<Self>;
fn with_author(query: Builder<Self>) -> Builder<Self>;
fn post(
on: impl Into<Self::Commentable> + Send,
author: Author<'_, Self::Author>,
body: &str,
) -> impl Future<Output = laravel::Result<Self>> + Send {
Box::pin(ops::post::<Self>(on.into(), author, body))
}
fn reply(
&mut self,
author: Author<'_, Self::Author>,
body: &str,
) -> impl Future<Output = laravel::Result<Self>> + Send {
Box::pin(ops::reply(self, author, body))
}
fn edit(&mut self, body: &str) -> impl Future<Output = laravel::Result<()>> + Send {
Box::pin(ops::edit(self, body))
}
fn approve(&mut self) -> impl Future<Output = laravel::Result<()>> + Send {
Box::pin(ops::moderate(self, Status::Approved))
}
fn reject(&mut self) -> impl Future<Output = laravel::Result<()>> + Send {
Box::pin(ops::moderate(self, Status::Rejected))
}
fn mark_spam(&mut self) -> impl Future<Output = laravel::Result<()>> + Send {
Box::pin(ops::moderate(self, Status::Spam))
}
fn set_status(&mut self, status: Status) -> impl Future<Output = laravel::Result<()>> + Send {
Box::pin(ops::moderate(self, status))
}
fn remove(self) -> impl Future<Output = laravel::Result<bool>> + Send {
Box::pin(ops::remove(self))
}
fn react(&self, user: &Self::Author, reaction: &str) -> impl Future<Output = laravel::Result<bool>> + Send {
Box::pin(reactions::react(self, user.commenter_id(), reaction))
}
fn unreact(&self, user: &Self::Author, reaction: &str) -> impl Future<Output = laravel::Result<bool>> + Send {
Box::pin(reactions::unreact(self, user.commenter_id(), reaction))
}
fn toggle_reaction(
&self,
user: &Self::Author,
reaction: &str,
) -> impl Future<Output = laravel::Result<bool>> + Send {
Box::pin(reactions::toggle(self, user.commenter_id(), reaction))
}
fn revisions(&self) -> Builder<CommentRevision> {
CommentRevision::query()
.filter(CommentRevision::comment_id.eq(self.row().id))
.order_by(CommentRevision::id.desc())
}
#[cfg(feature = "markdown")]
fn body_html(&self) -> String {
crate::markdown::to_html(self.row().body)
}
fn threads(on: impl Into<Self::Commentable>) -> Threads<Self> {
Threads::on(on.into())
}
fn thread_tree(&self) -> Threads<Self> {
Threads::thread(self.row().thread.to_owned())
}
fn on(on: impl Into<Self::Commentable>) -> Builder<Self> {
let on = on.into();
Self::new_query()
.filter(Self::COLUMNS.commentable_type.eq(on.morph()) & Self::COLUMNS.commentable_id.eq(*on.key()))
}
fn pending() -> Builder<Self> {
Self::new_query()
.filter(Self::where_status(Status::Pending))
.order_by(Self::META.key.asc())
}
#[must_use]
fn where_status(status: Status) -> Condition {
Self::COLUMNS.status.eq(status)
}
#[must_use]
fn where_visible() -> Condition {
Self::COLUMNS.status.eq(Status::Approved) & Self::COLUMNS.deleted_at.is_null()
}
fn visibility(&self) -> Visibility {
let row = self.row();
if row.deleted_at.is_some() {
Visibility::Deleted
} else if row.status == Status::Approved {
Visibility::Visible
} else {
Visibility::Hidden
}
}
}
pub struct CommentColumns<C: CommentModel> {
pub commentable_type: Column<C, MorphType<C::Commentable>>,
pub commentable_id: Column<C, i64>,
pub thread: Column<C, String>,
pub author_id: Column<C, Option<i64>>,
pub guest_name: Column<C, Option<String>>,
pub guest_email: Column<C, Option<String>>,
pub body: Column<C, String>,
pub status: Column<C, Status>,
pub moderated_at: Column<C, Option<Timestamp>>,
pub edited_at: Column<C, Option<Timestamp>>,
pub deleted_at: Column<C, Option<Timestamp>>,
pub replies_count: Column<C, i64>,
pub reactions_count: Column<C, i64>,
pub created_at: Column<C, Option<Timestamp>>,
}
pub struct CommentRow<'a, C: CommentModel> {
pub id: i64,
pub commentable_type: MorphType<C::Commentable>,
pub commentable_id: i64,
pub thread: &'a str,
pub parent_id: Option<i64>,
pub depth: i64,
pub author_id: Option<i64>,
pub guest_name: Option<&'a str>,
pub body: &'a str,
pub status: Status,
pub edited_at: Option<Timestamp>,
pub deleted_at: Option<Timestamp>,
pub replies_count: i64,
pub reactions_count: i64,
pub created_at: Option<Timestamp>,
}
#[macro_export]
macro_rules! comment_model {
(
$(#[$meta:meta])*
$vis:vis struct $name:ident {
commentable: $($commentable:ident)::+,
author: $($author:ident)::+ $(,)?
}
) => {
$(#[$meta])*
#[$crate::__private::laravel::model::model(table = "comments")]
$vis struct $name {
pub id: i64,
pub commentable_type: $crate::__private::laravel::model::MorphType<$($commentable)::+>,
pub commentable_id: i64,
pub thread: String,
#[default]
#[serde(skip)]
pub lft: i64,
#[default]
#[serde(skip)]
pub rgt: i64,
#[default]
pub depth: i64,
pub parent_id: Option<i64>,
pub author_id: Option<i64>,
pub guest_name: Option<String>,
#[serde(skip)]
pub guest_email: Option<String>,
pub body: String,
pub status: $crate::Status,
pub moderated_at: Option<$crate::__private::laravel::time::Timestamp>,
pub edited_at: Option<$crate::__private::laravel::time::Timestamp>,
pub deleted_at: Option<$crate::__private::laravel::time::Timestamp>,
#[default]
pub replies_count: i64,
#[default]
pub reactions_count: i64,
pub created_at: Option<$crate::__private::laravel::time::Timestamp>,
pub updated_at: Option<$crate::__private::laravel::time::Timestamp>,
#[morph_to]
pub commentable: $crate::__private::laravel::model::MorphTo<$($commentable)::+>,
#[belongs_to]
pub author: $crate::__private::laravel::model::BelongsTo<Option<$($author)::+>>,
}
impl $crate::__private::laravel_nestedset::NestedSet for $name {
fn lft_column() -> $crate::__private::laravel::model::Column<Self, i64> {
$name::lft
}
fn rgt_column() -> $crate::__private::laravel::model::Column<Self, i64> {
$name::rgt
}
fn parent_column() -> $crate::__private::laravel::model::Column<Self, Option<i64>> {
$name::parent_id
}
fn depth_column() -> Option<$crate::__private::laravel::model::Column<Self, i64>> {
Some($name::depth)
}
fn scope(&self) -> $crate::__private::laravel_nestedset::Scope<Self> {
$crate::__private::laravel_nestedset::Scope::new().by($name::thread, &self.thread)
}
}
impl $crate::CommentModel for $name {
type Commentable = $($commentable)::+;
type Author = $($author)::+;
const COLUMNS: $crate::CommentColumns<Self> = $crate::CommentColumns {
commentable_type: $name::commentable_type,
commentable_id: $name::commentable_id,
thread: $name::thread,
author_id: $name::author_id,
guest_name: $name::guest_name,
guest_email: $name::guest_email,
body: $name::body,
status: $name::status,
moderated_at: $name::moderated_at,
edited_at: $name::edited_at,
deleted_at: $name::deleted_at,
replies_count: $name::replies_count,
reactions_count: $name::reactions_count,
created_at: $name::created_at,
};
fn new_query() -> $crate::__private::laravel::db::Builder<Self> {
$name::query()
}
fn with_author(
query: $crate::__private::laravel::db::Builder<Self>,
) -> $crate::__private::laravel::db::Builder<Self> {
query.with($name::author)
}
fn row(&self) -> $crate::CommentRow<'_, Self> {
$crate::CommentRow {
id: self.id,
commentable_type: self.commentable_type,
commentable_id: self.commentable_id,
thread: &self.thread,
parent_id: self.parent_id,
depth: self.depth,
author_id: self.author_id,
guest_name: self.guest_name.as_deref(),
body: &self.body,
status: self.status,
edited_at: self.edited_at,
deleted_at: self.deleted_at,
replies_count: self.replies_count,
reactions_count: self.reactions_count,
created_at: self.created_at,
}
}
}
};
}