@@ -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 ();
0 commit comments