| 123456789101112131415161718192021222324 |
- <?php
- namespace App\Services;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use InvalidArgumentException;
- /** Written inside the same transaction as the change; consumed by the member WebSocket worker. */
- final class SiteRealtimeService
- {
- public static function changed(string $module, int $id, string $action, array $boundaries = []): void
- {
- if (!in_array($module, ['message', 'announcement', 'maintenance'], true) || $id < 1) throw new InvalidArgumentException('Invalid site event');
- $now = Carbon::now('Asia/Shanghai')->format('Y-m-d H:i:s');
- DB::table('site_realtime_events')->where('module', $module)->where('resource_id', $id)
- ->whereNull('dispatched_at')->where('available_at', '>', $now)->delete();
- $rows = [['module' => $module, 'resource_id' => $id, 'action' => $action, 'available_at' => $now, 'created_at' => $now]];
- foreach ($boundaries as $edge => $at) {
- if ($at !== null && $at > $now) $rows[] = ['module' => $module, 'resource_id' => $id, 'action' => $edge, 'available_at' => $at, 'created_at' => $now];
- }
- DB::table('site_realtime_events')->insert($rows);
- }
- }
|