Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
use LinkRobins\Support\Console\CloseResolvedTicketsCommand;
use LinkRobins\Support\Console\HourlySchedule;
use LinkRobins\Support\Event\ReplyPosted;
use LinkRobins\Support\Event\TicketAssigned;
use LinkRobins\Support\Event\TicketChanged;
use LinkRobins\Support\Event\TicketDecided;
use LinkRobins\Support\Event\TicketStatusChanged;
use LinkRobins\Support\Api\Resource\SupportCategoryResource;
use LinkRobins\Support\Api\Resource\SupportReplyResource;
use LinkRobins\Support\Api\Resource\SupportTicketResource;
Expand All @@ -22,6 +25,10 @@
use LinkRobins\Support\Search\ReplySearcher;
use LinkRobins\Support\Search\TicketSearcher;
use LinkRobins\Support\Extend as SupportExtend;
use LinkRobins\Support\Listener\NotifyTicketTypeDecision;
use LinkRobins\Support\Listener\NotifyTicketTypeStatusChange;
use LinkRobins\Support\Listener\RecordAssignmentTimeline;
use LinkRobins\Support\Listener\RecordStatusChangeTimeline;
use LinkRobins\Support\SupportCategory;
use LinkRobins\Support\SupportEvent;
use LinkRobins\Support\SupportReply;
Expand Down Expand Up @@ -264,7 +271,11 @@
], true)) {
$event->value = $event->value === '' || $event->value === '0' ? '0' : '1';
}
}),
})
->listen(TicketStatusChanged::class, RecordStatusChangeTimeline::class)
->listen(TicketStatusChanged::class, NotifyTicketTypeStatusChange::class)
->listen(TicketAssigned::class, RecordAssignmentTimeline::class)
->listen(TicketDecided::class, NotifyTicketTypeDecision::class),

(new Extend\Settings())
->default('linkrobins-support.nav_in_sidebar', '1')
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Resource/SupportEventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* Read-only ticket history: status and assignment changes, shown inline in
* the ticket timeline. Rows are written by SupportEvent::recordChanges, never
* the ticket timeline. Rows are written by domain event listeners, never
* through the API.
*/
class SupportEventResource extends AbstractDatabaseResource
Expand Down
47 changes: 36 additions & 11 deletions src/Api/Resource/SupportTicketResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;
use LinkRobins\Support\Access\SupportAbilities;
use LinkRobins\Support\Event\TicketChanged;
use LinkRobins\Support\Job\NotifyAssigned;
use LinkRobins\Support\Job\NotifyStatusChanged;
use LinkRobins\Support\RateLimiter;
use LinkRobins\Support\SupportCategory;
use LinkRobins\Support\SupportServiceProvider;
use LinkRobins\Support\SupportTicket;
use LinkRobins\Support\UserState;
use Psr\Log\LoggerInterface;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Exception\BadRequestException;
use Tobyz\JsonApiServer\Exception\ForbiddenException;
Expand All @@ -39,6 +42,7 @@ public function __construct(
protected RateLimiter $rateLimiter,
protected TranslatorInterface $translator,
protected Dispatcher $bus,
protected LoggerInterface $log,
) {
}

Expand Down Expand Up @@ -171,7 +175,7 @@ public function fields(): array
}
$actor = $context->getActor();
if (SupportAbilities::isStaff($actor)) {
$ticket->status = $value;
$ticket->changeStatus($value, $actor);
return;
}
// Non-staff: the owner of a non-appeal ticket may make two
Expand All @@ -184,12 +188,12 @@ public function fields(): array
$this->ownerMayReopen($ticket, $actor)
&& $value === SupportTicket::STATUS_OPEN
) {
$ticket->status = $value;
$ticket->changeStatus($value, $actor);
} elseif (
$this->ownerMayConfirmSolved($ticket, $actor)
&& $value === SupportTicket::STATUS_CLOSED
) {
$ticket->status = $value;
$ticket->changeStatus($value, $actor);
}
}),

Expand Down Expand Up @@ -221,7 +225,7 @@ public function fields(): array
if (! $isStaff) {
return;
}
$ticket->decision = $value;
$ticket->changeDecision($value, $actor);
}),

Schema\DateTime::make('createdAt')
Expand Down Expand Up @@ -375,7 +379,7 @@ public function fields(): array
return;
}
if ($value === null) {
$ticket->assigned_staff_id = null;
$ticket->assignTo(null, $actor);
return;
}
$targetId = null;
Expand All @@ -396,7 +400,7 @@ public function fields(): array
if (! $target || ! SupportAbilities::isStaff($target)) {
throw new BadRequestException($this->translator->trans('linkrobins-support.api.assign_staff_only'));
}
$ticket->assigned_staff_id = $targetId;
$ticket->assignTo($target, $actor);
}),

Schema\Relationship\ToOne::make('category')
Expand Down Expand Up @@ -659,11 +663,6 @@ public function updating(object $model, Context $context): ?object
}
}

// Credit status and assignment changes in the ticket timeline to the
// person making this request (see SupportEvent::recordChanges).
$model->eventActor = $actor->isGuest() ? null : $actor;
$model->eventActorId = $actor->isGuest() ? null : (int) $actor->id;

$this->before[(int) $model->id] = [
'status' => $model->getOriginal('status'),
'assignee' => $model->getOriginal('assigned_staff_id') === null
Expand Down Expand Up @@ -720,6 +719,32 @@ public function saved(object $model, Context $context): ?object
return $model;
}

public function updated(object $model, Context $context): ?object
{
/** @var SupportTicket $model */
if ($model->wasChanged()) {
$actor = $context->getActor();
$model->raise(new TicketChanged($model, $actor->isGuest() ? null : $actor));
}

return $model;
}

/**
* Dispatch raised domain events after commit with per-listener isolation so a
* failing listener never causes the API request to return 500 after saving.
*/
public function dispatchEventsFor(mixed $entity, ?User $actor = null): void
{
if (! method_exists($entity, 'releaseEvents')) {
return;
}

foreach ($entity->releaseEvents() as $event) {
SupportServiceProvider::dispatchAfterCommit($this->events, $event, $this->log);
}
}

/**
* Authorize permanent deletion of a ticket.
*
Expand Down
17 changes: 11 additions & 6 deletions src/Console/CloseResolvedTicketsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use Carbon\Carbon;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use LinkRobins\Support\Event\TicketChanged;
use LinkRobins\Support\SupportServiceProvider;
use LinkRobins\Support\SupportTicket;
use Psr\Log\LoggerInterface;

/**
* Close tickets that have sat in Resolved with no activity for the
Expand Down Expand Up @@ -41,7 +45,7 @@ public static function days(mixed $value): int
return max(0, (int) $value);
}

public function handle(SettingsRepositoryInterface $settings): int
public function handle(SettingsRepositoryInterface $settings, Dispatcher $events, LoggerInterface $log): int
{
$days = self::days($settings->get(self::SETTING));

Expand All @@ -59,18 +63,19 @@ public function handle(SettingsRepositoryInterface $settings): int
->whereNotNull('status_changed_at')
->where('status_changed_at', '<=', $cutoff)
->where(fn ($q) => $q->whereNull('last_reply_at')->orWhere('last_reply_at', '<=', $cutoff))
->chunkById(100, function ($tickets) use (&$closed) {
->chunkById(100, function ($tickets) use (&$closed, $events, $log) {
foreach ($tickets as $ticket) {
/** @var SupportTicket $ticket */
// Re-check on the row itself: a reply may have reopened
// it since the chunk was read.
if ($ticket->fresh()?->status !== SupportTicket::STATUS_RESOLVED) {
continue;
}
$ticket->status = SupportTicket::STATUS_CLOSED;
$ticket->eventActorId = null;
$ticket->eventAutomatic = true;
$ticket->save();
$ticket->changeStatus(SupportTicket::STATUS_CLOSED, actor: null, isAutomatic: true)->save();
foreach ($ticket->releaseEvents() as $event) {
SupportServiceProvider::dispatchAfterCommit($events, $event, $log);
}
SupportServiceProvider::dispatchAfterCommit($events, new TicketChanged($ticket, null), $log);
$closed++;
}
});
Expand Down
7 changes: 7 additions & 0 deletions src/Event/TicketAssigned.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
*/
class TicketAssigned
{
public readonly ?int $assigneeId;
public readonly ?int $oldAssigneeId;

public function __construct(
public readonly SupportTicket $ticket,
public readonly ?User $actor,
public readonly ?User $assignee,
public readonly ?User $oldAssignee = null,
?int $assigneeId = null,
?int $oldAssigneeId = null,
) {
$this->assigneeId = $assigneeId ?? ($assignee ? (int) $assignee->id : null);
$this->oldAssigneeId = $oldAssigneeId ?? ($oldAssignee ? (int) $oldAssignee->id : null);
}
}
1 change: 1 addition & 0 deletions src/Event/TicketStatusChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
public readonly ?User $actor,
public readonly ?string $oldStatus,
public readonly string $newStatus,
public readonly bool $isAutomatic = false,
) {
}
}
33 changes: 33 additions & 0 deletions src/Listener/NotifyTicketTypeDecision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace LinkRobins\Support\Listener;

use LinkRobins\Support\Event\TicketDecided;
use Psr\Log\LoggerInterface;

class NotifyTicketTypeDecision
{
public function __construct(
protected LoggerInterface $log,
) {
}

public function __invoke(TicketDecided $event): void
{
$category = $event->ticket->currentCategory();
if (! $category || $event->newDecision === null) {
return;
}

try {
$category->ticketType()->onDecided(
$event->ticket,
$event->newDecision,
$event->oldDecision,
$event->actor,
);
} catch (\Throwable $e) {
$this->log->error('[linkrobins/support] ticket type onDecided failed', ['exception' => $e]);
}
}
}
33 changes: 33 additions & 0 deletions src/Listener/NotifyTicketTypeStatusChange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace LinkRobins\Support\Listener;

use LinkRobins\Support\Event\TicketStatusChanged;
use Psr\Log\LoggerInterface;

class NotifyTicketTypeStatusChange
{
public function __construct(
protected LoggerInterface $log,
) {
}

public function __invoke(TicketStatusChanged $event): void
{
$category = $event->ticket->currentCategory();
if (! $category) {
return;
}

try {
$category->ticketType()->onStatusChanged(
$event->ticket,
$event->newStatus,
$event->oldStatus,
$event->actor,
);
} catch (\Throwable $e) {
$this->log->error('[linkrobins/support] ticket type onStatusChanged failed', ['exception' => $e]);
}
}
}
34 changes: 34 additions & 0 deletions src/Listener/RecordAssignmentTimeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace LinkRobins\Support\Listener;

use LinkRobins\Support\Event\TicketAssigned;
use LinkRobins\Support\SupportEvent;
use Psr\Log\LoggerInterface;

class RecordAssignmentTimeline
{
public function __construct(
protected LoggerInterface $log,
) {
}

public function __invoke(TicketAssigned $event): void
{
try {
SupportEvent::recordAssignment(
ticket: $event->ticket,
actor: $event->actor,
fromUser: $event->oldAssignee,
toUser: $event->assignee,
fromUserId: $event->oldAssigneeId,
toUserId: $event->assigneeId,
);
} catch (\Throwable $e) {
$this->log->warning('[linkrobins/support] could not record assignment timeline event', [
'ticket_id' => $event->ticket->id,
'exception' => $e,
]);
}
}
}
33 changes: 33 additions & 0 deletions src/Listener/RecordStatusChangeTimeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace LinkRobins\Support\Listener;

use LinkRobins\Support\Event\TicketStatusChanged;
use LinkRobins\Support\SupportEvent;
use Psr\Log\LoggerInterface;

class RecordStatusChangeTimeline
{
public function __construct(
protected LoggerInterface $log,
) {
}

public function __invoke(TicketStatusChanged $event): void
{
try {
SupportEvent::recordStatusChange(
ticket: $event->ticket,
actor: $event->actor,
fromStatus: $event->oldStatus,
toStatus: $event->newStatus,
isAutomatic: $event->isAutomatic,
);
} catch (\Throwable $e) {
$this->log->warning('[linkrobins/support] could not record status timeline event', [
'ticket_id' => $event->ticket->id,
'exception' => $e,
]);
}
}
}
Loading
Loading