Skip to content

Commit 877f658

Browse files
committed
squash
1 parent 50e467b commit 877f658

34 files changed

Lines changed: 277 additions & 92 deletions

app/Actions/Metrics/GetMetrics.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@ public function get(): Collection
2121
/** @var User $user */
2222
$user = Auth::user() ?? throw new UnauthorizedException();
2323

24+
// If this query becomes too slow, then we probably will need to refactor it for something that does not use
25+
// relationship magic but instead extracts the values directly from the database.
2426
return LiveMetrics::query()->with(['photo', 'photo.size_variants', 'album', 'album_impl', 'album.thumb'])
2527
->join('photos', 'photos.id', '=', 'live_metrics.photo_id', 'left')
2628
->join('base_albums', 'base_albums.id', '=', 'live_metrics.album_id', 'left')
2729
->join('albums', 'albums.id', '=', 'live_metrics.album_id', 'left')
2830

31+
// Unnecessary but safer as that avoids some issues in the front-end if the migration failed for some reasons...
32+
->whereNotNull('live_metrics.album_id')
33+
2934
// Owner check (if not admin)
3035
->when(!$user->may_administrate, fn ($q_owner) => $q_owner
3136
->where(fn ($q) => $q
32-
->where('base_albums.owner_id', $user->id)
33-
->orWhere('photos.owner_id', $user->id))
37+
->where('base_albums.owner_id', '=', $user->id)
38+
->orWhere('photos.owner_id', '=', $user->id))
3439
)
3540

3641
// Do not fetch the visit for photos (too noisy)
3742
->where(fn ($q) => $q->where('live_metrics.action', '!=', 'visit')
38-
->orWhere(fn ($q1) => $q1->where('live_metrics.action', 'visit')
39-
->whereNotNull('live_metrics.album_id')))
43+
->orWhere(fn ($q1) => $q1->whereNull('live_metrics.photo_id')))
4044

4145
// Do not fetch the tag albums too.
4246
// Maybe refactor if we decide to unify tag albums and normal albums...
@@ -45,8 +49,6 @@ public function get(): Collection
4549

4650
->select([
4751
'live_metrics.*',
48-
// // // 'photos.title as photo_title',
49-
// // // 'base_albums.title as album_title',
5052
])
5153
->orderBy('live_metrics.created_at', 'desc')
5254
->get();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
namespace App\Contracts\Http\Requests;
10+
11+
interface HasFromId
12+
{
13+
/**
14+
* @return string|null
15+
*/
16+
public function from_id(): ?string;
17+
}

app/Contracts/Http/Requests/RequestAttribute.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class RequestAttribute
2222

2323
public const PARENT_ID_ATTRIBUTE = 'parent_id';
2424

25+
public const FROM_ID_ATTRIBUTE = 'from_id';
2526
public const ALBUM_ID_ATTRIBUTE = 'album_id';
2627
public const ALBUM_IDS_ATTRIBUTE = 'album_ids';
2728
public const ALBUM_DECORATION_ATTRIBUTE = 'album_decoration';

app/Events/Metrics/AlbumDownload.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@
1313
/**
1414
* This event is fired when an album is downloaded.
1515
*/
16-
final class AlbumDownload extends BaseMetricsEvent
16+
final class AlbumDownload extends BaseAlbumMetricsEvent
1717
{
18-
public function key(): string
19-
{
20-
return 'album_id';
21-
}
22-
2318
public function metricAction(): MetricsAction
2419
{
2520
return MetricsAction::DOWNLOAD;

app/Events/Metrics/AlbumShared.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@
1313
/**
1414
* This event is fired when a direct link to an album is used.
1515
*/
16-
final class AlbumShared extends BaseMetricsEvent
16+
final class AlbumShared extends BaseAlbumMetricsEvent
1717
{
18-
public function key(): string
19-
{
20-
return 'album_id';
21-
}
22-
2318
public function metricAction(): MetricsAction
2419
{
2520
return MetricsAction::SHARED;

app/Events/Metrics/AlbumVisit.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@
1313
/**
1414
* This event is fired when an album is open.
1515
*/
16-
final class AlbumVisit extends BaseMetricsEvent
16+
final class AlbumVisit extends BaseAlbumMetricsEvent
1717
{
18-
public function key(): string
19-
{
20-
return 'album_id';
21-
}
22-
2318
public function metricAction(): MetricsAction
2419
{
2520
return MetricsAction::VISIT;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
namespace App\Events\Metrics;
10+
11+
use App\Enum\MetricsAction;
12+
use Illuminate\Support\Carbon;
13+
14+
abstract class BaseAlbumMetricsEvent extends BaseMetricsEvent
15+
{
16+
/**
17+
* Return the type of key : photo_id or album_id.
18+
*
19+
* @return string
20+
*
21+
* @codeCoverageIgnore, abstract method can't be covered
22+
*/
23+
final public function key(): string
24+
{
25+
return 'album_id';
26+
}
27+
28+
/**
29+
* Convert the event to an array for insertion into the database.
30+
*
31+
* @return array{visitor_id:string,action:MetricsAction,album_id:string,created_at:Carbon}
32+
*/
33+
final public function toArray(): array
34+
{
35+
return [
36+
'visitor_id' => $this->visitor_id,
37+
'action' => $this->metricAction(),
38+
'album_id' => $this->id,
39+
'created_at' => now(),
40+
];
41+
}
42+
}

app/Events/Metrics/BaseMetricsEvent.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Enum\MetricsAction;
1212
use Illuminate\Broadcasting\InteractsWithSockets;
1313
use Illuminate\Foundation\Events\Dispatchable;
14+
use Illuminate\Support\Carbon;
1415

1516
abstract class BaseMetricsEvent
1617
{
@@ -40,4 +41,11 @@ abstract public function key(): string;
4041
* @codeCoverageIgnore, abstract method can't be covered
4142
*/
4243
abstract public function metricAction(): MetricsAction;
44+
45+
/**
46+
* Convert the event to an array for insertion into the database.
47+
*
48+
* @return array<string,string|MetricsAction|Carbon>
49+
*/
50+
abstract public function toArray(): array;
4351
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
namespace App\Events\Metrics;
10+
11+
use App\Enum\MetricsAction;
12+
use Illuminate\Support\Carbon;
13+
14+
abstract class BasePhotoMetricsEvent extends BaseMetricsEvent
15+
{
16+
public readonly string $album_id;
17+
18+
public function __construct(
19+
string $visitor_id,
20+
string $id,
21+
string $album_id,
22+
) {
23+
parent::__construct($visitor_id, $id);
24+
$this->album_id = $album_id;
25+
}
26+
27+
/**
28+
* Return the type of key : photo_id or album_id.
29+
*
30+
* @return string
31+
*
32+
* @codeCoverageIgnore, abstract method can't be covered
33+
*/
34+
final public function key(): string
35+
{
36+
return 'photo_id';
37+
}
38+
39+
/**
40+
* Convert the event to an array for insertion into the database.
41+
*
42+
* @return array{visitor_id:string,action:MetricsAction,album_id:string,photo_id:string,created_at:Carbon}
43+
*/
44+
final public function toArray(): array
45+
{
46+
return [
47+
'visitor_id' => $this->visitor_id,
48+
'action' => $this->metricAction(),
49+
'album_id' => $this->album_id,
50+
'photo_id' => $this->id,
51+
'created_at' => now(),
52+
];
53+
}
54+
}

app/Events/Metrics/PhotoDownload.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@
1313
/**
1414
* This event is fired when one or multiple photos are downloaded.
1515
*/
16-
final class PhotoDownload extends BaseMetricsEvent
16+
final class PhotoDownload extends BasePhotoMetricsEvent
1717
{
18-
public function key(): string
19-
{
20-
return 'photo_id';
21-
}
22-
2318
public function metricAction(): MetricsAction
2419
{
2520
return MetricsAction::DOWNLOAD;

0 commit comments

Comments
 (0)