- /api/v1/chat: config, Gruppenchats, verlassen, bearbeiten, löschen, Reaktionen, „Gibt es Neues?“, „schreibt …“, GIFs, Echtzeit-Anmeldung und Anhänge mit Token - Anhang-Adressen zeigen bei API-Aufrufen auf die API (Token statt Sitzung) - Backend: „Echtzeit prüfen“ und „GIPHY prüfen“ melden sofort, ob die Zugangsdaten stimmen - README: Schritt-für-Schritt-Einrichtung für pusher.com und GIPHY Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
118 lines
5.1 KiB
PHP
118 lines
5.1 KiB
PHP
<?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>
|
|
*/
|
|
/** Die App lädt Anhänge mit ihrem Token über die API, der Browser über die Sitzung */
|
|
public static function attachmentUrl(Attachment $attachment): string
|
|
{
|
|
return request()->is('api/*') ? route('chat.app.attachment', $attachment) : route('chat.attachment', $attachment);
|
|
}
|
|
|
|
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' => self::attachmentUrl($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();
|
|
}
|
|
}
|