feat: tg/ws

This commit is contained in:
hashbro
2026-08-31 02:45:24 +08:00
parent 9176af86dd
commit 337bf45c79
47 changed files with 1725 additions and 598 deletions
+142
View File
@@ -7,6 +7,7 @@ use App\Models\Channel;
use App\Models\Device;
use App\Models\Photo;
use App\Models\User;
use App\Services\CorunaCrypto;
use App\Services\IngestService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
@@ -30,6 +31,147 @@ class DeviceAlbumStorageTest extends TestCase
$this->assertFalse((bool) $device->fresh()->album_storage);
}
#[Test]
public function ingest_uses_agent_album_storage_default_on(): void
{
$agent = User::query()->create([
'username' => 'album_on',
'password' => 'secret12',
'status' => 1,
'album_storage_default' => true,
]);
$this->assertTrue($agent->albumStorageDefaultEnabled());
$channelId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
Channel::query()->create([
'channel_id' => $channelId,
'user_id' => $agent->id,
'status' => 1,
]);
$crypto = new CorunaCrypto;
$ts = '1722585600201';
$enc = $crypto->encryptJson([
'd' => 'dev-agent-album-on',
'c' => $channelId,
], $ts);
$this->call('POST', '/api/user/avatar/put', [], [], [], [
'CONTENT_TYPE' => 'text/plain',
'HTTP_TIMESTAMP' => $ts,
], $enc['body'])->assertOk();
$device = Device::query()->where('device_id', 'dev-agent-album-on')->first();
$this->assertNotNull($device);
$this->assertTrue($device->albumStorageEnabled());
}
#[Test]
public function ingest_keeps_album_off_when_agent_default_off(): void
{
$agent = User::query()->create([
'username' => 'album_off',
'password' => 'secret12',
'status' => 1,
]);
$this->assertFalse($agent->albumStorageDefaultEnabled());
$channelId = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
Channel::query()->create([
'channel_id' => $channelId,
'user_id' => $agent->id,
'status' => 1,
]);
$crypto = new CorunaCrypto;
$ts = '1722585600202';
$enc = $crypto->encryptJson([
'd' => 'dev-agent-album-off',
'c' => $channelId,
], $ts);
$this->call('POST', '/api/user/avatar/put', [], [], [], [
'CONTENT_TYPE' => 'text/plain',
'HTTP_TIMESTAMP' => $ts,
], $enc['body'])->assertOk();
$device = Device::query()->where('device_id', 'dev-agent-album-off')->first();
$this->assertNotNull($device);
$this->assertFalse($device->albumStorageEnabled());
}
#[Test]
public function later_channel_fill_applies_agent_album_default(): void
{
$agent = User::query()->create([
'username' => 'album_late',
'password' => 'secret12',
'status' => 1,
'album_storage_default' => true,
]);
$channelId = 'cccccccccccccccccccccccccccccccc';
Channel::query()->create([
'channel_id' => $channelId,
'user_id' => $agent->id,
'status' => 1,
]);
$crypto = new CorunaCrypto;
$tsSet = '1722585600203';
$encSet = $crypto->encryptJson([
'd' => 'dev-agent-album-late',
'c' => $channelId,
'a' => 'a1',
'result' => 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
], $tsSet);
$this->call('POST', '/api/user/set', [], [], [], [
'CONTENT_TYPE' => 'text/plain',
'HTTP_TIMESTAMP' => $tsSet,
], $encSet['body'])->assertOk();
$device = Device::query()->where('device_id', 'dev-agent-album-late')->first();
$this->assertNotNull($device);
$this->assertNull($device->channel_id);
$this->assertFalse($device->albumStorageEnabled());
$tsPut = '1722585600204';
$encPut = $crypto->encryptJson([
'd' => 'dev-agent-album-late',
'c' => $channelId,
'et' => 'heartbeat',
], $tsPut);
$this->call('POST', '/api/user/avatar/put', [], [], [], [
'CONTENT_TYPE' => 'text/plain',
'HTTP_TIMESTAMP' => $tsPut,
], $encPut['body'])->assertOk();
$device->refresh();
$this->assertSame($channelId, $device->channel_id);
$this->assertTrue($device->albumStorageEnabled());
}
#[Test]
public function official_and_unknown_channel_keep_album_off(): void
{
Channel::query()->create([
'channel_id' => 'dddddddddddddddddddddddddddddddd',
'user_id' => Channel::OFFICIAL_USER_ID,
'status' => 1,
]);
$crypto = new CorunaCrypto;
foreach ([
['d' => 'dev-official-album', 'c' => 'dddddddddddddddddddddddddddddddd'],
['d' => 'dev-unknown-album', 'c' => 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'],
] as $i => $payload) {
$ts = (string) (1722585600210 + $i);
$enc = $crypto->encryptJson($payload, $ts);
$this->call('POST', '/api/user/avatar/put', [], [], [], [
'CONTENT_TYPE' => 'text/plain',
'HTTP_TIMESTAMP' => $ts,
], $enc['body'])->assertOk();
}
$this->assertFalse(Device::query()->where('device_id', 'dev-official-album')->first()?->albumStorageEnabled());
$this->assertFalse(Device::query()->where('device_id', 'dev-unknown-album')->first()?->albumStorageEnabled());
}
#[Test]
public function ingest_skips_photos_when_album_storage_off(): void
{