1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Console\Commands;
7
8use App\Models\Chat\Channel;
9use Illuminate\Console\Command;
10
11class ChatChannelSetLastMessageId extends Command
12{
13 protected $signature = 'chat:channel-set-last-message-id {--chunk-size=1000} {--delay=1}';
14
15 protected $description = 'Updates channels where last_message_id is not set.';
16
17 public function handle()
18 {
19 $delay = get_int($this->option('delay')) ?? 1;
20 $chunkSize = get_int($this->option('chunk-size')) ?? 1000;
21 if ($chunkSize < 1) {
22 return;
23 }
24
25 $this->line("Updating chat channels without last_message_id with {$delay}s delay between chunks of {$chunkSize}...");
26
27 $progress = $this->output->createProgressBar();
28 $max = Channel::max('channel_id');
29 Channel::where('last_message_id', null)->where('channel_id', '<=', $max)->chunkById($chunkSize, function ($chunk) use ($delay, $progress) {
30 foreach ($chunk as $channel) {
31 $lastMessageId = $channel->messages()->max('message_id');
32 if ($lastMessageId !== null) {
33 $channel->update(['last_message_id' => $lastMessageId]);
34 }
35
36 $progress->advance();
37 }
38
39 sleep($delay);
40 });
41
42 $progress->finish();
43 $this->line('');
44 }
45}