Files
relaticle-comments/src/Mentions/MentionParser.php
manukminasyan 29fcbd8aec feat: initial release of relaticle/comments
Filament comments package with:
- Polymorphic commenting on any Eloquent model
- Threaded replies with configurable depth
- @mentions with autocomplete and user search
- Emoji reactions with toggle and who-reacted tooltips
- File attachments via Livewire uploads
- Reply and mention notifications via Filament notification system
- Thread subscriptions for notification control
- Real-time broadcasting (opt-in Echo) with polling fallback
- Dark mode support
- CommentsAction, CommentsTableAction, CommentsEntry for Filament integration
- 204 tests, 421 assertions
2026-03-26 23:02:56 +04:00

53 lines
1.4 KiB
PHP

<?php
namespace Relaticle\Comments\Mentions;
use Illuminate\Support\Collection;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Config;
use Relaticle\Comments\Contracts\MentionResolver;
use Relaticle\Comments\Events\UserMentioned;
class MentionParser
{
public function __construct(
protected MentionResolver $resolver,
) {}
/** @return Collection<int, int> */
public function parse(string $body): Collection
{
$text = html_entity_decode(strip_tags($body), ENT_QUOTES, 'UTF-8');
preg_match_all('/(?<=@)[\w]+/', $text, $matches);
$names = array_unique($matches[0] ?? []);
if (empty($names)) {
return collect();
}
return $this->resolver->resolveByNames($names)->pluck('id');
}
public function syncMentions(Comment $comment): void
{
$newMentionIds = $this->parse($comment->body);
$existingMentionIds = $comment->mentions()->pluck('comment_mentions.user_id');
$addedIds = $newMentionIds->diff($existingMentionIds);
$comment->mentions()->sync($newMentionIds->all());
$commenterModel = Config::getCommenterModel();
$addedIds->each(function ($userId) use ($comment, $commenterModel) {
$mentionedUser = $commenterModel::find($userId);
if ($mentionedUser) {
UserMentioned::dispatch($comment, $mentionedUser);
}
});
}
}