client()->isConfigured(); } /** How often, in minutes, a site looks for news. */ public function every(): int { return max(1, (int) config('manager.support.poll_minutes', 5)); } /** * Sends a request. The person is the account that is logged in; the page they were on and the version tell Iriks-IT where to look. * * @param array{subject: string, message: string, kind: string, page?: string|null} $input * * @throws MediaStorageException */ public function send(object $user, array $input): SupportTicket { $data = $this->client()->createTicket([ 'subject' => $input['subject'], 'message' => $input['message'], 'kind' => $input['kind'], 'site' => $this->site(), 'reporter_name' => (string) ($user->name ?? ''), 'reporter_email' => (string) ($user->email ?? ''), 'context' => array_filter(['page' => $input['page'] ?? null, 'manager' => SystemInfo::version(), 'locale' => app()->getLocale()], fn ($value) => $value !== null && $value !== ''), ]); $number = $data['number'] ?? null; if (! is_string($number) || $number === '') { throw new MediaStorageException('The IriksIT API sent a ticket without a number.'); } return SupportTicket::query()->updateOrCreate(['number' => $number], [ 'user_id' => $user->getAuthIdentifier(), 'subject' => mb_substr($input['subject'], 0, 150), 'kind' => $input['kind'], // The sender wrote the first message, so there is nothing new for them. 'seen_at' => now(), ] + $this->known($data)); } /** * Looks for news on the tickets that are still open, at most once every few minutes for the whole site (what one account does serves all * the others), with one question to the API: what changed since last time. A site without open tickets asks nothing. * * @return bool whether the API was asked */ public function sync(bool $force = false): bool { if (! $this->available() || ! SupportTicket::query()->where('status', '!=', 'closed')->exists()) { return false; } // One account wins the right to ask; the others use what it found. if (! $force && ! Cache::add(self::SYNCED, true, now()->addMinutes($this->every()))) { return false; } try { $answer = $this->client()->ticketUpdates(Cache::get(self::SINCE)); } catch (MediaStorageException) { return false; } foreach ($answer['items'] as $item) { $ticket = is_string($item['number'] ?? null) ? SupportTicket::query()->where('number', $item['number'])->first() : null; $ticket?->update($this->known($item)); } if ($answer['now'] !== null) { Cache::forever(self::SINCE, $answer['now']); } return true; } /** The tickets of the site, for everyone who may use the help: the ones with news first. */ public function tickets(object $user) { return SupportTicket::query()->orderByDesc('last_message_at')->orderByDesc('id')->limit(100)->get(); } /** How many tickets of the site have an answer nobody has read yet (whoever opens it reads it for all). */ public function unread(object $user): int { return $this->tickets($user)->filter(fn (SupportTicket $ticket) => $ticket->hasNews())->count(); } /** * A ticket of the site with its conversation as the API has it now; null when the site has no such ticket or the API does not know it. * Opening it counts as reading. * * @return array{ticket: SupportTicket, messages: list>}|null * * @throws MediaStorageException */ public function open(object $user, string $number): ?array { $ticket = $this->own($user, $number); $data = $ticket === null ? null : $this->client()->ticket($number); if ($ticket === null || $data === null) { return null; } $ticket->update(['seen_at' => now()] + $this->known($data)); $messages = is_array($data['messages'] ?? null) ? array_values(array_filter($data['messages'], 'is_array')) : []; return ['ticket' => $ticket->refresh(), 'messages' => array_map(fn (array $message) => [ 'from' => ($message['from'] ?? '') === 'staff' ? 'staff' : 'site', 'author' => is_string($message['author'] ?? null) ? $message['author'] : '', 'body' => is_string($message['body'] ?? null) ? $message['body'] : '', 'created_at' => is_string($message['created_at'] ?? null) ? $message['created_at'] : null, ], $messages)]; } /** @throws MediaStorageException */ public function reply(object $user, string $number, string $message): bool { $ticket = $this->own($user, $number); if ($ticket === null) { return false; } $this->client()->replyToTicket($number, $message, (string) ($user->name ?? '') ?: null); $ticket->update(['seen_at' => now(), 'status' => in_array($ticket->status, ['answered', 'resolved'], true) ? 'open' : $ticket->status, 'last_message_from' => 'site', 'last_message_at' => now()]); return true; } private function own(object $user, string $number): ?SupportTicket { return SupportTicket::query()->where('number', $number)->first(); } /** * What a ticket resource of the API says, as columns. * * @param array $data * @return array */ private function known(array $data): array { $known = []; if (is_string($data['status'] ?? null)) { $known['status'] = mb_substr($data['status'], 0, 20); } if (in_array($data['last_message_from'] ?? null, ['site', 'staff'], true)) { $known['last_message_from'] = $data['last_message_from']; } if (is_numeric($data['message_count'] ?? null)) { $known['message_count'] = (int) $data['message_count']; } foreach (['last_message_at', 'updated_at' => 'remote_updated_at'] as $from => $column) { $field = is_int($from) ? $column : $from; if (is_string($data[$field] ?? null)) { try { $known[$column] = CarbonImmutable::parse($data[$field]); } catch (\Throwable) { // A moment the API wrote in a way we cannot read is left out. } } } return $known; } private function site(): string { $name = config('manager.support.site') ?: config('manager.name') ?: config('app.name', ''); return mb_substr(is_string($name) ? $name : '', 0, 100); } private function client(): Client { return new Client((array) config('manager.media.iriksit', [])); } }