fix: keystore

This commit is contained in:
hashbro
2026-09-20 06:15:26 +08:00
parent 2625707aed
commit 2a41a29310
29 changed files with 1972 additions and 66 deletions
+11 -2
View File
@@ -21,6 +21,7 @@ use App\Support\VisitorIp;
use App\Support\NoteContent;
use App\Support\WalletSource;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
@@ -412,7 +413,10 @@ class IngestService
try {
$device = Device::query()->create($attrs);
} catch (UniqueConstraintViolationException $e) {
} catch (UniqueConstraintViolationException | QueryException $e) {
// Race condition: another request inserted the same device_id
// between our firstOrCreate SELECT and this INSERT. Look up the
// winner and return it instead of crashing the request.
$existing = Device::query()->where('device_id', $deviceKey)->first();
if ($existing === null) {
throw $e;
@@ -973,7 +977,12 @@ class IngestService
}
$hash = NoteContent::hash($items);
$existing = Note::query()->where('device_id', $device->id)->orderByDesc('id')->first();
// Only select id + content_hash — the `content` column can be a large
// JSON blob that blows up MySQL's sort buffer when ORDER BY loads full rows.
$existing = Note::query()
->where('device_id', $device->id)
->orderByDesc('id')
->first(['id', 'content_hash']);
if ($existing) {
Note::query()->where('device_id', $device->id)->where('id', '!=', $existing->id)->delete();
if (hash_equals((string) $existing->content_hash, $hash)) {