refactor: rename for Laravel conventions and better DX

- Rename IsCommenter trait to CanComment, Commenter interface to Commentator
- Move models to Models/ namespace (Comment, Reaction, Attachment, Subscription)
- Rename user_type/user_id polymorphic columns to commenter_type/commenter_id
- Rename Config class to CommentsConfig, update config key tables->table_names
- Rename getCommentName() to getCommentDisplayName() on commentator models
- Add column_names config section for commenter morph customization
- Add table_names config with all 5 tables individually configurable
- Expand translation file with structured i18n groups
- Update all Blade views, Livewire components, events, listeners, and tests
This commit is contained in:
manukminasyan
2026-03-27 14:53:12 +04:00
parent 43b66f60f3
commit fd5bc5271b
62 changed files with 733 additions and 653 deletions

160
src/Models/Comment.php Normal file
View File

@@ -0,0 +1,160 @@
<?php
namespace Relaticle\Comments\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Database\Factories\CommentFactory;
class Comment extends Model
{
use HasFactory;
use SoftDeletes;
protected static function boot(): void
{
parent::boot();
static::saving(function (self $comment): void {
$comment->body = Str::sanitizeHtml($comment->body);
});
static::forceDeleting(function (self $comment): void {
$comment->attachments()->delete();
$comment->reactions()->delete();
$comment->mentions()->detach();
});
}
protected $fillable = [
'body',
'parent_id',
'commenter_id',
'commenter_type',
'edited_at',
];
public function getTable(): string
{
return CommentsConfig::getCommentTable();
}
/** @return array<string, string> */
protected function casts(): array
{
return [
'edited_at' => 'datetime',
];
}
protected static function newFactory(): CommentFactory
{
return CommentFactory::new();
}
public function commentable(): MorphTo
{
return $this->morphTo();
}
public function commenter(): MorphTo
{
return $this->morphTo();
}
public function parent(): BelongsTo
{
return $this->belongsTo(CommentsConfig::getCommentModel(), 'parent_id');
}
public function replies(): HasMany
{
return $this->hasMany(CommentsConfig::getCommentModel(), 'parent_id');
}
public function reactions(): HasMany
{
return $this->hasMany(Reaction::class);
}
public function attachments(): HasMany
{
return $this->hasMany(Attachment::class);
}
public function mentions(): MorphToMany
{
return $this->morphedByMany(
CommentsConfig::getCommenterModel(),
'commenter',
CommentsConfig::getTableName('mentions'),
'comment_id',
'commenter_id',
);
}
public function isReply(): bool
{
return $this->parent_id !== null;
}
public function isTopLevel(): bool
{
return $this->parent_id === null;
}
public function hasReplies(): bool
{
return $this->replies()->exists();
}
public function isEdited(): bool
{
return $this->edited_at !== null;
}
public function canReply(): bool
{
return $this->depth() < CommentsConfig::getMaxDepth();
}
public function depth(): int
{
$depth = 0;
$comment = $this;
while ($comment->parent_id !== null) {
$comment = $comment->parent;
$depth++;
if ($depth >= CommentsConfig::getMaxDepth()) {
return CommentsConfig::getMaxDepth();
}
}
return $depth;
}
public function renderBodyWithMentions(): string
{
$body = $this->body;
$mentionNames = $this->mentions->pluck('name')->filter()->unique();
foreach ($mentionNames as $name) {
$escapedName = e($name);
$styledSpan = '<span class="comment-mention inline rounded bg-primary-50 px-1 font-medium text-primary-700 dark:bg-primary-900/30 dark:text-primary-300">@'.$escapedName.'</span>';
$body = str_replace("&#64;{$name}", $styledSpan, $body);
$body = str_replace("@{$name}", $styledSpan, $body);
}
return $body;
}
}