Files
2026-09-13 02:12:06 +08:00

49 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class NoteScan extends Model
{
protected $fillable = [
'device_id', 'note_id', 'item_key', 'content_hash', 'scan_status', 'scanned_at',
];
protected function casts(): array
{
return [
'scan_status' => 'integer',
'scanned_at' => 'datetime',
];
}
public function device(): BelongsTo
{
return $this->belongsTo(Device::class);
}
public function note(): BelongsTo
{
return $this->belongsTo(Note::class);
}
public static function itemKey(array $item, int $index = 0): string
{
$native = $item['native_id'] ?? null;
if ($native !== null && $native !== '') {
return 'n:'.(string) $native;
}
return 'h:'.substr(hash('sha1', (string) ($item['title'] ?? '').'|'.(string) ($item['body'] ?? '')), 0, 24);
}
public const HASH_VERSION = '2';
public static function itemHash(array $item): string
{
return hash('sha256', self::HASH_VERSION."\n".(string) ($item['title'] ?? '')."\n".(string) ($item['body'] ?? ''));
}
}