v0.9.13
This commit is contained in:
commit
4769402b1b
298 files changed
+20934
No files matched your search
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Support;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use IriksIT\Manager\Exceptions\MediaStorageException;
|
||||
use IriksIT\Manager\Models\SupportTicket;
|
||||
use IriksIT\Manager\Support\Helpdesk;
|
||||
|
||||
class SupportController
|
||||
{
|
||||
public function __construct(private readonly Helpdesk $helpdesk) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
if ($this->helpdesk->available()) {
|
||||
$this->helpdesk->sync();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'available' => $this->helpdesk->available(),
|
||||
'every' => $this->helpdesk->every(),
|
||||
'kinds' => Helpdesk::KINDS,
|
||||
'tickets' => $this->rows($request),
|
||||
]);
|
||||
}
|
||||
|
||||
/** What the menu asks every few minutes: look for news (the site asks the API at most that often) and say how many answers wait. */
|
||||
public function poll(Request $request): JsonResponse
|
||||
{
|
||||
$this->helpdesk->sync();
|
||||
|
||||
return response()->json(['unread' => $this->helpdesk->unread($request->user()), 'every' => $this->helpdesk->every()])->header('Cache-Control', 'no-store');
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'subject' => ['required', 'string', 'min:3', 'max:150'],
|
||||
'message' => ['required', 'string', 'min:3', 'max:5000'],
|
||||
'kind' => ['nullable', 'in:'.implode(',', Helpdesk::KINDS)],
|
||||
'page' => ['nullable', 'string', 'max:300'],
|
||||
]);
|
||||
|
||||
if (! $this->helpdesk->available()) {
|
||||
return response()->json(['message' => __('Help is not set up on this site.')], 503);
|
||||
}
|
||||
|
||||
try {
|
||||
$ticket = $this->helpdesk->send($request->user(), ['subject' => trim($data['subject']), 'message' => trim($data['message']), 'kind' => $data['kind'] ?? 'question', 'page' => $data['page'] ?? null]);
|
||||
} catch (MediaStorageException $e) {
|
||||
return $this->failed($e);
|
||||
}
|
||||
|
||||
return response()->json(['ticket' => $this->row($ticket)], 201);
|
||||
}
|
||||
|
||||
public function show(Request $request, string $number): JsonResponse
|
||||
{
|
||||
try {
|
||||
$opened = $this->helpdesk->available() ? $this->helpdesk->open($request->user(), $number) : null;
|
||||
} catch (MediaStorageException $e) {
|
||||
return $this->failed($e);
|
||||
}
|
||||
|
||||
return $opened === null
|
||||
? response()->json(['message' => __('This request was not found.')], 404)
|
||||
: response()->json(['ticket' => $this->row($opened['ticket']), 'messages' => $opened['messages']])->header('Cache-Control', 'no-store');
|
||||
}
|
||||
|
||||
public function reply(Request $request, string $number): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['message' => ['required', 'string', 'min:1', 'max:5000']]);
|
||||
|
||||
try {
|
||||
$sent = $this->helpdesk->available() && $this->helpdesk->reply($request->user(), $number, trim($data['message']));
|
||||
} catch (MediaStorageException $e) {
|
||||
return $this->failed($e);
|
||||
}
|
||||
|
||||
return $sent ? response()->json(['ok' => true], 201) : response()->json(['message' => __('This request was not found.')], 404);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
/** Every ticket of the site, with the name of the one who sent it. */
|
||||
private function rows(Request $request): array
|
||||
{
|
||||
$tickets = $this->helpdesk->tickets($request->user());
|
||||
$names = $request->user()::query()->whereIn($request->user()->getKeyName(), $tickets->pluck('user_id')->filter()->unique()->all())->pluck('name', $request->user()->getKeyName());
|
||||
|
||||
return $tickets->map(fn (SupportTicket $ticket) => $this->row($ticket) + ['by' => (string) ($names[$ticket->user_id] ?? '')])->values()->all();
|
||||
}
|
||||
|
||||
private function row(SupportTicket $ticket): array
|
||||
{
|
||||
return [
|
||||
'number' => $ticket->number,
|
||||
'subject' => $ticket->subject,
|
||||
'kind' => $ticket->kind,
|
||||
'status' => $ticket->status,
|
||||
'news' => $ticket->hasNews(),
|
||||
'messages' => $ticket->message_count,
|
||||
'last_message_at' => $ticket->last_message_at?->toIso8601String(),
|
||||
'created_at' => $ticket->created_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
|
||||
/** The API said no or could not be reached: a closed ticket is a plain "no", the rest "try again later". Never the API's own words. */
|
||||
private function failed(MediaStorageException $e): JsonResponse
|
||||
{
|
||||
return response()->json(['message' => $e->status === 422 ? __('The request was not accepted. Check the text and try again.') : __('Iriks-IT could not be reached right now. Try again in a few minutes.')], $e->status === 422 ? 422 : 502);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Support;
|
||||
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
use IriksIT\Manager\Support\Helpdesk;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
use IriksIT\Manager\Support\Permission;
|
||||
|
||||
/**
|
||||
* Help from Iriks-IT: a form to ask a question or report a problem, which becomes a ticket at the IriksIT API with a number to follow it by,
|
||||
* and a list of the tickets of the person with what Iriks-IT answered. Only there when the site has an API account (`manager.media.iriksit`).
|
||||
*/
|
||||
class SupportModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'support';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Help';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function tables(): array
|
||||
{
|
||||
return ['support_tickets'];
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [new Permission('support.use', 'Ask Iriks-IT for help and follow the requests', 'Site')];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
if (! app(Helpdesk::class)->available()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [['label' => 'Help', 'section' => 'Iriks-IT', 'route' => Names::route('support.index'), 'permission' => 'support.use', 'simple' => true, 'icon' => 'help', 'hint' => 'Ask a question or report a problem', 'order' => 90]];
|
||||
}
|
||||
|
||||
public function actions(): array
|
||||
{
|
||||
return ['support.store' => ['Sent a request for help'], 'support.reply' => ['Answered a request for help']];
|
||||
}
|
||||
|
||||
public function views(): array
|
||||
{
|
||||
return ['support.index' => 'Opened the help page', 'support.show' => 'Opened a request for help'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
use IriksIT\Manager\Support\Tables;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Tables::create(Names::table('support_tickets'), function (Blueprint $table) {
|
||||
$table->id();
|
||||
// The number the API gave, like IT-2026-00042.
|
||||
$table->string('number', 40)->unique();
|
||||
// The account that sent it: it sees its own tickets.
|
||||
$table->unsignedBigInteger('user_id')->nullable()->index();
|
||||
$table->string('subject', 150);
|
||||
$table->string('kind', 20)->default('question');
|
||||
$table->string('status', 20)->default('open');
|
||||
$table->string('last_message_from', 10)->nullable();
|
||||
$table->unsignedInteger('message_count')->default(1);
|
||||
$table->dateTime('last_message_at')->nullable();
|
||||
// What the API said the last time (its own clock), and when the sender last opened the ticket.
|
||||
$table->dateTime('remote_updated_at')->nullable();
|
||||
$table->dateTime('seen_at')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists(Names::table('support_tickets'));
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use IriksIT\Manager\Modules\Support\SupportController;
|
||||
|
||||
Route::prefix('/support')->where(['number' => '[A-Za-z0-9-]{1,40}'])->middleware('can:support.use')->group(function () {
|
||||
Route::get('/', [SupportController::class, 'index'])->name('support.index');
|
||||
Route::get('/poll', [SupportController::class, 'poll'])->name('support.poll');
|
||||
Route::post('/', [SupportController::class, 'store'])->name('support.store')->middleware('throttle:10,1');
|
||||
Route::get('/{number}', [SupportController::class, 'show'])->name('support.show');
|
||||
Route::post('/{number}/messages', [SupportController::class, 'reply'])->name('support.reply')->middleware('throttle:20,1');
|
||||
});
|
||||
Reference in new issue
Block a user