country = request()->route('country', config('app.domain_country')); $this->name = auth()->user()->name ?? ''; $this->loadAttendees(); } public function with(): array { return [ 'event' => $this->event->load('meetup'), ]; } private function getUserIdentifier(): string { return auth()->check() ? 'id_'.auth()->id() : 'anon_'.session()->getId(); } private function loadAttendees(): void { $identifier = $this->getUserIdentifier(); $attendees = collect($this->event->attendees ?? []); $mightAttendees = collect($this->event->might_attendees ?? []); // Check if user is in attendees $attendeeEntry = $attendees->first(fn($v) => str($v)->startsWith($identifier)); if ($attendeeEntry) { $this->name = str($attendeeEntry)->after('|')->toString(); $this->willShowUp = true; } // Check if user is in might_attendees $mightAttendeeEntry = $mightAttendees->first(fn($v) => str($v)->startsWith($identifier)); if ($mightAttendeeEntry) { $this->name = str($mightAttendeeEntry)->after('|')->toString(); $this->perhapsShowUp = true; } $this->attendees = $this->mapAttendees($attendees); $this->mightAttendees = $this->mapAttendees($mightAttendees); } private function mapAttendees($collection): array { return $collection->map(function ($value) { $isAnon = str($value)->contains('anon_'); $id = $isAnon ? -1 : str($value)->before('|')->after('id_')->toInteger(); return [ 'id' => $id, 'user' => $id > 0 ? User::query() ->select(['id', 'name', 'profile_photo_path']) ->find($id) ?->append('profile_photo_url') ->toArray() : null, 'name' => str($value)->after('|')->toString(), ]; })->toArray(); } public function attend(): void { $this->validate(); $this->removeFromLists(); $attendees = collect($this->event->attendees ?? []); $entry = $this->getUserIdentifier().'|'.$this->name; if (!$attendees->contains($entry)) { $attendees->push($entry); $this->event->update(['attendees' => $attendees->toArray()]); } $this->loadAttendees(); } public function mightAttend(): void { $this->validate(); $this->removeFromLists(); $mightAttendees = collect($this->event->might_attendees ?? []); $entry = $this->getUserIdentifier().'|'.$this->name; if (!$mightAttendees->contains($entry)) { $mightAttendees->push($entry); $this->event->update(['might_attendees' => $mightAttendees->toArray()]); } $this->loadAttendees(); } public function cannotCome(): void { $this->removeFromLists(); $this->loadAttendees(); } private function removeFromLists(): void { $identifier = $this->getUserIdentifier(); $attendees = collect($this->event->attendees ?? []) ->reject(fn($v) => str($v)->startsWith($identifier)); $mightAttendees = collect($this->event->might_attendees ?? []) ->reject(fn($v) => str($v)->startsWith($identifier)); $this->event->update([ 'attendees' => $attendees->toArray(), 'might_attendees' => $mightAttendees->toArray(), ]); $this->willShowUp = false; $this->perhapsShowUp = false; } }; ?>