Skip to content

Commit fa22e86

Browse files
authored
fix(server): tighten asset visibility (#18699)
* tighten visibility * update sql * elevated access util function * fix potential sync issue * include in user stats * include hidden assets in size usage * filter visibility in search duplicates query * stack visibility
1 parent b5c3a67 commit fa22e86

21 files changed

+132
-107
lines changed

server/src/queries/activity.repository.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ where
7373
and "activity"."albumId" = $2
7474
and "activity"."isLiked" = $3
7575
and "assets"."deletedAt" is null
76+
and "assets"."visibility" != 'locked'

server/src/queries/album.repository.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ select
8080
where
8181
"albums_assets_assets"."albumsId" = "albums"."id"
8282
and "assets"."deletedAt" is null
83+
and "assets"."visibility" in ('archive', 'timeline')
8384
order by
8485
"assets"."fileCreatedAt" desc
8586
) as "asset"
@@ -178,7 +179,8 @@ from
178179
"assets"
179180
inner join "albums_assets_assets" as "album_assets" on "album_assets"."assetsId" = "assets"."id"
180181
where
181-
"album_assets"."albumsId" in ($1)
182+
"assets"."visibility" in ('archive', 'timeline')
183+
and "album_assets"."albumsId" in ($1)
182184
and "assets"."deletedAt" is null
183185
group by
184186
"album_assets"."albumsId"

server/src/queries/asset.job.repository.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ from
186186
inner join "smart_search" on "assets"."id" = "smart_search"."assetId"
187187
inner join "asset_job_status" as "job_status" on "job_status"."assetId" = "assets"."id"
188188
where
189-
"assets"."visibility" != $1
190-
and "assets"."deletedAt" is null
189+
"assets"."deletedAt" is null
190+
and "assets"."visibility" in ('archive', 'timeline')
191191
and "job_status"."duplicatesDetectedAt" is null
192192

193193
-- AssetJobRepository.streamForEncodeClip
@@ -349,7 +349,7 @@ from
349349
"assets" as "stacked"
350350
where
351351
"stacked"."deletedAt" is not null
352-
and "stacked"."visibility" != $1
352+
and "stacked"."visibility" = $1
353353
and "stacked"."stackId" = "asset_stack"."id"
354354
group by
355355
"asset_stack"."id"

server/src/queries/asset.repository.sql

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ select
130130
from
131131
"assets"
132132
left join "exif" on "assets"."id" = "exif"."assetId"
133-
left join "asset_stack" on "asset_stack"."id" = "assets"."stackId"
134133
where
135134
"assets"."id" = any ($1::uuid[])
136135

@@ -240,10 +239,7 @@ with
240239
"assets"
241240
where
242241
"assets"."deletedAt" is null
243-
and (
244-
"assets"."visibility" = $1
245-
or "assets"."visibility" = $2
246-
)
242+
and "assets"."visibility" in ('archive', 'timeline')
247243
)
248244
select
249245
"timeBucket",
@@ -300,21 +296,14 @@ with
300296
where
301297
"stacked"."stackId" = "assets"."stackId"
302298
and "stacked"."deletedAt" is null
303-
and "stacked"."visibility" != $1
299+
and "stacked"."visibility" = $1
304300
group by
305301
"stacked"."stackId"
306302
) as "stacked_assets" on true
307303
where
308304
"assets"."deletedAt" is null
309-
and (
310-
"assets"."visibility" = $2
311-
or "assets"."visibility" = $3
312-
)
313-
and date_trunc('MONTH', "localDateTime" at time zone 'UTC') at time zone 'UTC' = $4
314-
and (
315-
"assets"."visibility" = $5
316-
or "assets"."visibility" = $6
317-
)
305+
and "assets"."visibility" in ('archive', 'timeline')
306+
and date_trunc('MONTH', "localDateTime" at time zone 'UTC') at time zone 'UTC' = $2
318307
and not exists (
319308
select
320309
from
@@ -374,10 +363,10 @@ with
374363
"exif"."assetId" = "assets"."id"
375364
) as "asset" on true
376365
where
377-
"assets"."ownerId" = $1::uuid
366+
"assets"."visibility" in ('archive', 'timeline')
367+
and "assets"."ownerId" = $1::uuid
378368
and "assets"."duplicateId" is not null
379369
and "assets"."deletedAt" is null
380-
and "assets"."visibility" != $2
381370
and "assets"."stackId" is null
382371
group by
383372
"assets"."duplicateId"
@@ -388,12 +377,12 @@ with
388377
from
389378
"duplicates"
390379
where
391-
json_array_length("assets") = $3
380+
json_array_length("assets") = $2
392381
),
393382
"removed_unique" as (
394383
update "assets"
395384
set
396-
"duplicateId" = $4
385+
"duplicateId" = $3
397386
from
398387
"unique"
399388
where

server/src/queries/person.repository.sql

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,27 +182,42 @@ from
182182
"asset_faces"
183183
left join "assets" on "assets"."id" = "asset_faces"."assetId"
184184
and "asset_faces"."personId" = $1
185-
and "assets"."visibility" != $2
185+
and "assets"."visibility" = 'timeline'
186186
and "assets"."deletedAt" is null
187187
where
188188
"asset_faces"."deletedAt" is null
189189

190190
-- PersonRepository.getNumberOfPeople
191191
select
192-
count(distinct ("person"."id")) as "total",
193-
count(distinct ("person"."id")) filter (
194-
where
195-
"person"."isHidden" = $1
192+
coalesce(count(*), 0) as "total",
193+
coalesce(
194+
count(*) filter (
195+
where
196+
"isHidden" = $1
197+
),
198+
0
196199
) as "hidden"
197200
from
198201
"person"
199-
inner join "asset_faces" on "asset_faces"."personId" = "person"."id"
200-
inner join "assets" on "assets"."id" = "asset_faces"."assetId"
201-
and "assets"."deletedAt" is null
202-
and "assets"."visibility" != $2
203202
where
204-
"person"."ownerId" = $3
205-
and "asset_faces"."deletedAt" is null
203+
exists (
204+
select
205+
from
206+
"asset_faces"
207+
where
208+
"asset_faces"."personId" = "person"."id"
209+
and "asset_faces"."deletedAt" is null
210+
and exists (
211+
select
212+
from
213+
"assets"
214+
where
215+
"assets"."id" = "asset_faces"."assetId"
216+
and "assets"."visibility" = 'timeline'
217+
and "assets"."deletedAt" is null
218+
)
219+
)
220+
and "person"."ownerId" = $2
206221

207222
-- PersonRepository.refreshFaces
208223
with

server/src/queries/search.repository.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,23 @@ with
102102
"assets"
103103
inner join "smart_search" on "assets"."id" = "smart_search"."assetId"
104104
where
105-
"assets"."ownerId" = any ($2::uuid[])
105+
"assets"."visibility" in ('archive', 'timeline')
106+
and "assets"."ownerId" = any ($2::uuid[])
106107
and "assets"."deletedAt" is null
107-
and "assets"."visibility" != $3
108-
and "assets"."type" = $4
109-
and "assets"."id" != $5::uuid
108+
and "assets"."type" = $3
109+
and "assets"."id" != $4::uuid
110110
and "assets"."stackId" is null
111111
order by
112112
"distance"
113113
limit
114-
$6
114+
$5
115115
)
116116
select
117117
*
118118
from
119119
"cte"
120120
where
121-
"cte"."distance" <= $7
121+
"cte"."distance" <= $6
122122
commit
123123

124124
-- SearchRepository.searchFaces
@@ -241,7 +241,7 @@ from
241241
inner join "assets" on "assets"."id" = "exif"."assetId"
242242
where
243243
"ownerId" = any ($1::uuid[])
244-
and "visibility" != $2
244+
and "visibility" = $2
245245
and "deletedAt" is null
246246
and "state" is not null
247247

@@ -253,7 +253,7 @@ from
253253
inner join "assets" on "assets"."id" = "exif"."assetId"
254254
where
255255
"ownerId" = any ($1::uuid[])
256-
and "visibility" != $2
256+
and "visibility" = $2
257257
and "deletedAt" is null
258258
and "city" is not null
259259

@@ -265,7 +265,7 @@ from
265265
inner join "assets" on "assets"."id" = "exif"."assetId"
266266
where
267267
"ownerId" = any ($1::uuid[])
268-
and "visibility" != $2
268+
and "visibility" = $2
269269
and "deletedAt" is null
270270
and "make" is not null
271271

@@ -277,6 +277,6 @@ from
277277
inner join "assets" on "assets"."id" = "exif"."assetId"
278278
where
279279
"ownerId" = any ($1::uuid[])
280-
and "visibility" != $2
280+
and "visibility" = $2
281281
and "deletedAt" is null
282282
and "model" is not null

server/src/queries/stack.repository.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ select
5252
where
5353
"assets"."deletedAt" is null
5454
and "assets"."stackId" = "asset_stack"."id"
55+
and "assets"."visibility" in ('archive', 'timeline')
5556
) as agg
5657
) as "assets"
5758
from
@@ -135,6 +136,7 @@ select
135136
where
136137
"assets"."deletedAt" is null
137138
and "assets"."stackId" = "asset_stack"."id"
139+
and "assets"."visibility" in ('archive', 'timeline')
138140
) as agg
139141
) as "assets"
140142
from

server/src/queries/user.repository.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ order by
290290
select
291291
"users"."id" as "userId",
292292
"users"."name" as "userName",
293-
"users"."quotaSizeInBytes" as "quotaSizeInBytes",
293+
"users"."quotaSizeInBytes",
294294
count(*) filter (
295295
where
296296
(
@@ -335,9 +335,8 @@ select
335335
from
336336
"users"
337337
left join "assets" on "assets"."ownerId" = "users"."id"
338+
and "assets"."deletedAt" is null
338339
left join "exif" on "exif"."assetId" = "assets"."id"
339-
where
340-
"assets"."deletedAt" is null
341340
group by
342341
"users"."id"
343342
order by

server/src/repositories/activity.repository.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { InjectKysely } from 'nestjs-kysely';
55
import { columns } from 'src/database';
66
import { Activity, DB } from 'src/db';
77
import { DummyValue, GenerateSql } from 'src/decorators';
8+
import { AssetVisibility } from 'src/enum';
89
import { asUuid } from 'src/utils/database';
910

1011
export interface ActivitySearch {
@@ -76,6 +77,7 @@ export class ActivityRepository {
7677
.where('activity.albumId', '=', albumId)
7778
.where('activity.isLiked', '=', false)
7879
.where('assets.deletedAt', 'is', null)
80+
.where('assets.visibility', '!=', sql.lit(AssetVisibility.LOCKED))
7981
.executeTakeFirstOrThrow();
8082

8183
return count;

server/src/repositories/album.repository.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { columns, Exif } from 'src/database';
66
import { Albums, DB } from 'src/db';
77
import { Chunked, ChunkedArray, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
88
import { AlbumUserCreateDto } from 'src/dtos/album.dto';
9+
import { withDefaultVisibility } from 'src/utils/database';
910

1011
export interface AlbumAssetCount {
1112
albumId: string;
@@ -58,6 +59,7 @@ const withAssets = (eb: ExpressionBuilder<DB, 'albums'>) => {
5859
.innerJoin('albums_assets_assets', 'albums_assets_assets.assetsId', 'assets.id')
5960
.whereRef('albums_assets_assets.albumsId', '=', 'albums.id')
6061
.where('assets.deletedAt', 'is', null)
62+
.$call(withDefaultVisibility)
6163
.orderBy('assets.fileCreatedAt', 'desc')
6264
.as('asset'),
6365
)
@@ -121,6 +123,7 @@ export class AlbumRepository {
121123
return (
122124
this.db
123125
.selectFrom('assets')
126+
.$call(withDefaultVisibility)
124127
.innerJoin('albums_assets_assets as album_assets', 'album_assets.assetsId', 'assets.id')
125128
.select('album_assets.albumsId as albumId')
126129
.select((eb) => eb.fn.min(sql<Date>`("assets"."localDateTime" AT TIME ZONE 'UTC'::text)::date`).as('startDate'))

0 commit comments

Comments
 (0)