73 lines
2.6 KiB
PHP
73 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\DirectMessage;
|
|
use App\Models\DirectThread;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class ThreadController extends Controller
|
|
{
|
|
public function index(): JsonResponse
|
|
{
|
|
$threads = DirectThread::query()
|
|
->with(['messages' => fn ($query) => $query->orderByDesc('timestamp')->limit(1)])
|
|
->orderByDesc('last_activity_at')
|
|
->get()
|
|
->map(function (DirectThread $thread) {
|
|
$last = $thread->messages->first();
|
|
|
|
return [
|
|
'thread_id' => $thread->thread_id,
|
|
'thread_title' => $thread->thread_title,
|
|
'is_group' => $thread->is_group,
|
|
'participants' => $thread->participants,
|
|
'last_activity_at' => $thread->last_activity_at,
|
|
'muted' => $thread->muted,
|
|
'last_message' => $last ? [
|
|
'username' => $last->username,
|
|
'text' => $last->text,
|
|
'item_type' => $last->item_type,
|
|
'timestamp' => $last->timestamp,
|
|
] : null,
|
|
];
|
|
});
|
|
|
|
return response()->json($threads);
|
|
}
|
|
|
|
public function show(string $threadId): JsonResponse
|
|
{
|
|
$thread = DirectThread::findOrFail($threadId);
|
|
|
|
$messages = $thread->messages()
|
|
->orderBy('timestamp')
|
|
->get()
|
|
->map(fn (DirectMessage $message) => [
|
|
'id' => $message->id,
|
|
'user_id' => $message->user_id,
|
|
'username' => $message->username,
|
|
'full_name' => $message->full_name,
|
|
'item_type' => $message->item_type,
|
|
'text' => $message->text,
|
|
'timestamp' => $message->timestamp,
|
|
'shared_url' => $message->shared_url,
|
|
'shared_preview_image' => $message->shared_preview_image,
|
|
'shared_title' => $message->shared_title,
|
|
'shared_caption' => $message->shared_caption,
|
|
'reactions' => $message->reactions,
|
|
'replied_to_id' => $message->replied_to_id,
|
|
'replied_to_text' => $message->replied_to_text,
|
|
]);
|
|
|
|
return response()->json([
|
|
'thread_id' => $thread->thread_id,
|
|
'thread_title' => $thread->thread_title,
|
|
'is_group' => $thread->is_group,
|
|
'participants' => $thread->participants,
|
|
'muted' => $thread->muted,
|
|
'messages' => $messages,
|
|
]);
|
|
}
|
|
}
|