Nachrichten-Plugin 1.0.0: Direktnachrichten, Gruppen, Anhänge, GIFs, Emojis
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
2844452002
55 changed files with 5448 additions and 0 deletions
112
src/Presenter.php
Normal file
112
src/Presenter.php
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Chat;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use Inhaus\Plugins\Chat\Models\Attachment;
|
||||
use Inhaus\Plugins\Chat\Models\Conversation;
|
||||
use Inhaus\Plugins\Chat\Models\Message;
|
||||
|
||||
class Presenter
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function person(?User $user): ?array
|
||||
{
|
||||
return $user ? ['id' => $user->id, 'name' => $user->name, 'first_name' => $user->first_name, 'initials' => $user->initials, 'avatar_url' => $user->avatarUrl(96), 'job_title' => $user->job_title] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Conversation> $conversations mit members und lastMessage.user
|
||||
* @param array<int, int> $unread
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public static function conversations(Collection $conversations, User $me, array $unread): array
|
||||
{
|
||||
$last = Message::query()->with('user')->whereIn('id', $conversations->pluck('last_message_id')->filter())->get()->keyBy('id');
|
||||
|
||||
return $conversations->map(function (Conversation $c) use ($me, $unread, $last) {
|
||||
$others = $c->members->reject(fn (User $u) => $u->is($me))->values();
|
||||
$mine = $c->members->firstWhere('id', $me->id);
|
||||
$message = $last[$c->last_message_id] ?? null;
|
||||
|
||||
return [
|
||||
'id' => $c->id,
|
||||
'type' => $c->type,
|
||||
'name' => $c->isGroup() ? $c->name : ($others->first()?->name ?? 'Unbekannt'),
|
||||
'person' => $c->isGroup() ? null : self::person($others->first()),
|
||||
'members' => $c->members->map(fn (User $u) => [...self::person($u), 'admin' => $u->pivot->role === 'admin'])->values()->all(),
|
||||
'is_admin' => $mine?->pivot->role === 'admin',
|
||||
'muted' => (bool) $mine?->pivot->muted,
|
||||
'unread' => $unread[$c->id] ?? 0,
|
||||
'last_read_id' => $mine?->pivot->last_read_message_id,
|
||||
// Bis hierhin haben alle anderen gelesen („gelesen“ unter eigenen Nachrichten)
|
||||
'others_read_id' => $others->isEmpty() ? null : $others->min(fn (User $u) => (int) $u->pivot->last_read_message_id),
|
||||
'last_message_at' => $c->last_message_at?->toIso8601String(),
|
||||
'preview' => $message ? self::preview($message, $me, $c->isGroup()) : null,
|
||||
];
|
||||
})->values()->all();
|
||||
}
|
||||
|
||||
private static function preview(Message $message, User $me, bool $group): string
|
||||
{
|
||||
$text = match (true) {
|
||||
$message->deleted_at !== null => 'Nachricht gelöscht',
|
||||
$message->kind === 'system' => (string) $message->body,
|
||||
$message->kind === 'gif' => 'GIF',
|
||||
filled($message->body) => Str::limit(preg_replace('/\s+/u', ' ', $message->body), 80),
|
||||
default => '📎 Datei',
|
||||
};
|
||||
|
||||
if ($message->kind === 'system') {
|
||||
return $text;
|
||||
}
|
||||
|
||||
$who = $message->user_id === $me->id ? 'Sie' : ($group ? ($message->user?->first_name ?: $message->user?->name) : null);
|
||||
|
||||
return $who ? "{$who}: {$text}" : $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Message> $messages mit user, attachments, reactions, replyTo.user
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public static function messages(Collection $messages, User $me): array
|
||||
{
|
||||
return $messages->map(fn (Message $m) => [
|
||||
'id' => $m->id,
|
||||
'conversation_id' => $m->conversation_id,
|
||||
'kind' => $m->kind,
|
||||
'user' => self::person($m->user),
|
||||
'mine' => $m->user_id === $me->id,
|
||||
'body' => $m->deleted_at ? null : $m->body,
|
||||
'gif_url' => $m->deleted_at ? null : $m->gif_url,
|
||||
'deleted' => $m->deleted_at !== null,
|
||||
'edited' => $m->edited_at !== null,
|
||||
'created_at' => $m->created_at?->toIso8601String(),
|
||||
'reply_to' => $m->replyTo ? [
|
||||
'id' => $m->replyTo->id,
|
||||
'name' => $m->replyTo->user?->name,
|
||||
'text' => $m->replyTo->deleted_at ? 'Nachricht gelöscht' : Str::limit((string) ($m->replyTo->body ?: ($m->replyTo->gif_url ? 'GIF' : 'Datei')), 100),
|
||||
] : null,
|
||||
'attachments' => $m->attachments->map(fn (Attachment $a) => [
|
||||
'id' => $a->id,
|
||||
'name' => $a->name,
|
||||
'mime' => $a->mime,
|
||||
'size' => $a->size,
|
||||
'image' => $a->isImage(),
|
||||
'width' => $a->width,
|
||||
'height' => $a->height,
|
||||
'url' => route('chat.attachment', $a),
|
||||
])->all(),
|
||||
'reactions' => $m->reactions->groupBy('emoji')->map(fn ($group, $emoji) => [
|
||||
'emoji' => $emoji,
|
||||
'count' => $group->count(),
|
||||
'mine' => $group->contains('user_id', $me->id),
|
||||
])->values()->all(),
|
||||
])->values()->all();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue