mirror of
https://github.com/immich-app/immich
synced 2026-08-15 13:03:57 +00:00
feat: random memories sort order (#20025)
This commit is contained in:
parent
6c6b00067b
commit
d92df63f84
11 changed files with 218 additions and 12 deletions
|
|
@ -4,7 +4,7 @@ import { IsInt, IsObject, IsPositive, ValidateNested } from 'class-validator';
|
|||
import { Memory } from 'src/database';
|
||||
import { AssetResponseDto, mapAsset } from 'src/dtos/asset-response.dto';
|
||||
import { AuthDto } from 'src/dtos/auth.dto';
|
||||
import { MemoryType } from 'src/enum';
|
||||
import { AssetOrderWithRandom, MemoryType } from 'src/enum';
|
||||
import { ValidateBoolean, ValidateDate, ValidateEnum, ValidateUUID } from 'src/validation';
|
||||
|
||||
class MemoryBaseDto {
|
||||
|
|
@ -27,6 +27,15 @@ export class MemorySearchDto {
|
|||
|
||||
@ValidateBoolean({ optional: true })
|
||||
isSaved?: boolean;
|
||||
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
@Type(() => Number)
|
||||
@ApiProperty({ type: 'integer', description: 'Number of memories to return' })
|
||||
size?: number;
|
||||
|
||||
@ValidateEnum({ enum: AssetOrderWithRandom, name: 'MemorySearchOrder', optional: true })
|
||||
order?: AssetOrderWithRandom;
|
||||
}
|
||||
|
||||
class OnThisDayDto {
|
||||
|
|
|
|||
|
|
@ -71,6 +71,14 @@ export enum MemoryType {
|
|||
OnThisDay = 'on_this_day',
|
||||
}
|
||||
|
||||
export enum AssetOrderWithRandom {
|
||||
// Include existing values
|
||||
Asc = AssetOrder.Asc,
|
||||
Desc = AssetOrder.Desc,
|
||||
/** Randomly Ordered */
|
||||
Random = 'random',
|
||||
}
|
||||
|
||||
export enum Permission {
|
||||
All = 'all',
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { Insertable, Kysely, sql, Updateable } from 'kysely';
|
||||
import { Insertable, Kysely, OrderByDirection, sql, Updateable } from 'kysely';
|
||||
import { jsonArrayFrom } from 'kysely/helpers/postgres';
|
||||
import { DateTime } from 'luxon';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { Chunked, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
|
||||
import { MemorySearchDto } from 'src/dtos/memory.dto';
|
||||
import { AssetVisibility } from 'src/enum';
|
||||
import { AssetOrderWithRandom, AssetVisibility } from 'src/enum';
|
||||
import { DB } from 'src/schema';
|
||||
import { MemoryTable } from 'src/schema/tables/memory.table';
|
||||
import { IBulkAsset } from 'src/types';
|
||||
|
|
@ -72,7 +72,12 @@ export class MemoryRepository implements IBulkAsset {
|
|||
).as('assets'),
|
||||
)
|
||||
.selectAll('memory')
|
||||
.orderBy('memoryAt', 'desc')
|
||||
.$call((qb) =>
|
||||
dto.order === AssetOrderWithRandom.Random
|
||||
? qb.orderBy(sql`RANDOM()`)
|
||||
: qb.orderBy('memoryAt', (dto.order?.toLowerCase() || 'desc') as OrderByDirection),
|
||||
)
|
||||
.$if(dto.size !== undefined, (qb) => qb.limit(dto.size!))
|
||||
.execute();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue