work in progress

This commit is contained in:
Daan Meijer
2026-06-23 13:54:27 +02:00
parent d751cd4fdd
commit 1e33bfb50b
10 changed files with 70 additions and 25 deletions
+2 -2
View File
@@ -11,10 +11,10 @@ class DashboardController extends Controller
public function index(Request $request, ActivityService $activityService)
{
$user = $request->user();
$unreadDynamics = $activityService->getUnreadDynamicsGrouped($user);
$unreadEntities = $activityService->getUnreadEntitiesGrouped($user);
return Inertia::render('Dashboard', [
'unreadDynamics' => $unreadDynamics,
'unreadEntities' => $unreadEntities,
]);
}
}
+2 -4
View File
@@ -85,9 +85,7 @@ class LedgerController extends Controller
return Inertia::render('Ledgers/Show', [
'dynamic' => new DynamicResource($dynamic),
'ledger' => new LedgerResource($ledger),
'mutations' => MutationResource::collection($ledger->mutations),
'participants' => UserResource::collection($dynamic->participants),
'messages' => MessageResource::collection($dynamic->chat->messages()->with(['user', 'media'])->latest()->paginate(\App\Models\Message::PAGINATION_COUNT)),
'messages' => MessageResource::collection($dynamic->getOrCreateChat()->messages()->with(['user', 'media'])->latest()->paginate(\App\Models\Message::PAGINATION_COUNT)),
'can' => [
'update' => $request->user()->can('update', $ledger),
'close' => $request->user()->can('close', $ledger),
@@ -99,7 +97,7 @@ class LedgerController extends Controller
{
$this->authorize('view', $ledger);
return MessageResource::collection($dynamic->chat->messages()->with(['user', 'media'])->latest()->paginate(\App\Models\Message::PAGINATION_COUNT));
return MessageResource::collection($dynamic->getOrCreateChat()->messages()->with(['user', 'media'])->latest()->paginate(\App\Models\Message::PAGINATION_COUNT));
}
/**
@@ -50,7 +50,7 @@ class HandleInertiaRequests extends Middleware
$service = app(ActivityService::class);
return count($service->getUnreadDynamicsGrouped($request->user()));
return count($service->getUnreadEntitiesGrouped($request->user()));
},
];
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
class ChatResource extends BaseResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$data = parent::toArray($request);
if ($this->whenLoaded('messages')) {
$data['messages'] = MessageResource::collection($this->messages);
}
return $data;
}
}
+4 -1
View File
@@ -17,9 +17,12 @@ class DynamicResource extends BaseResource
if ($this->ledgers) {
$result['ledgers'] = LedgerResource::collection($this->ledgers);
}
if ($this->participants) {
if ($this->whenLoaded('participants')) {
$result['participants'] = ParticipantResource::collection($this->participants);
}
if ($this->whenLoaded('chat')) {
$result['chat'] = new ChatResource($this->chat);
}
return $result;
}
}
+5 -1
View File
@@ -13,6 +13,10 @@ class LedgerResource extends BaseResource
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
$data = parent::toArray($request);
$data['mutations'] = MutationResource::collection($this->whenLoaded('mutations'));
return $data;
}
}
+10
View File
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Str;
use App\Models\Chat;
class Dynamic extends Model
{
@@ -64,4 +65,13 @@ class Dynamic extends Model
public function getUrlAttribute(): string {
return route('dynamics.show', $this);
}
public function getOrCreateChat(): Chat
{
if ($this->chat) {
return $this->chat;
}
return $this->chat()->create([]);
}
}
+10 -4
View File
@@ -45,8 +45,9 @@ class ActivityService
return $cursor ? $cursor->read_at : Carbon::parse('1970-01-01');
}
public function createMessage($chat, $user, $content, $subject = null)
public function createMessage($dynamic, $user, $content, $subject = null)
{
$chat = $dynamic->getOrCreateChat();
$message = $chat->messages()->create([
'user_id' => $user ? $user->id : null,
'content' => $content,
@@ -92,6 +93,11 @@ class ActivityService
*/
public function getActivitiesForDynamic(Dynamic $dynamic): array
{
$chat = $dynamic->getOrCreateChat();
if (!$chat) {
return [];
}
$participants = $dynamic->participants()->withPivot('display_name')->get();
$participantsMap = $participants->reduce(function ($acc, $p) {
$acc[$p->id] = $p->pivot->display_name ?? $p->name;
@@ -99,7 +105,7 @@ class ActivityService
return $acc;
}, []);
$messages = Message::where('chat_id', $dynamic->chat->id)
$messages = Message::where('chat_id', $chat->id)
->with(['user', 'subject'])
->latest()
->get();
@@ -122,10 +128,10 @@ class ActivityService
/**
* Get unread activities grouped by active entities (Dynamics, Ledgers) for the given user.
*/
public function getUnreadDynamicsGrouped(User $user): array
public function getUnreadEntitiesGrouped(User $user): array
{
$groupedDynamics = [];
$participatingDynamics = $user->dynamics()->with('ledgers')->get();
$participatingDynamics = $user->dynamics()->with(['chat', 'ledgers'])->get();
foreach ($participatingDynamics as $dynamic) {
$readAt = $this->getCursorReadAt($user, $dynamic);