Skip to content

Commit

Permalink
Merge branch 'feat/nullable-dates' of https://github.com/immich-app/i…
Browse files Browse the repository at this point in the history
…mmich into feat/inline-offline-check
  • Loading branch information
etnoy committed Feb 5, 2025
2 parents df56b41 + 921ef80 commit 7f73f2e
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 74 deletions.
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
blank_issues_enabled: false
blank_pull_request_template_enabled: false
22 changes: 0 additions & 22 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md

This file was deleted.

36 changes: 36 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Description

<!--- Describe your changes in detail -->
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->

Fixes # (issue)

## How Has This Been Tested?

<!-- Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration -->

- [ ] Test A
- [ ] Test B

<details><summary><h2>Screenshots (if appropriate)</h2></summary>

<!-- Images go below this line. -->

</details>

<!-- API endpoint changes (if relevant)
## API Changes
The `/api/something` endpoint is now `/api/something-else`
-->

## Checklist:

- [ ] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation if applicable
- [ ] I have no unrelated changes in the PR.
- [ ] I have confirmed that any new dependencies are strictly necessary.
- [ ] I have written tests for new code (if applicable)
- [ ] I have followed naming conventions/patterns in the surrounding code
- [ ] All code in `src/services` uses repositories implementations for database calls, filesystem operations, etc.
- [ ] All code in `src/repositories/` is pretty basic/simple and does not have any immich specific logic (that belongs in `src/services`)
2 changes: 1 addition & 1 deletion docs/docs/install/unraid.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ alt="Select Plugins > Compose.Manager > Add New Stack > Label it Immich"
</ul>
</details>

5. Click "**Save Changes**", you will be promoted to edit stack UI labels, just leave this blank and click "**Ok**"
5. Click "**Save Changes**", you will be prompted to edit stack UI labels, just leave this blank and click "**Ok**"
6. Select the cog ⚙️ next to Immich, click "**Edit Stack**", then click "**Env File**"
7. Paste the entire contents of the [Immich example.env](https://github.com/immich-app/immich/releases/latest/download/example.env) file into the Unraid editor, then **before saving** edit the following:

Expand Down
12 changes: 1 addition & 11 deletions server/src/dtos/asset-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,12 @@ const hexOrBufferToBase64 = (encoded: string | Buffer) => {
export function mapAsset(entity: AssetEntity, options: AssetMapOptions = {}): AssetResponseDto {
const { stripMetadata = false, withStack = false } = options;

if (entity.localDateTime === null) {
throw new Error(`Asset ${entity.id} has no localDateTime`);
}
if (entity.fileCreatedAt === null) {
throw new Error(`Asset ${entity.id} has no fileCreatedAt`);
}
if (entity.fileModifiedAt === null) {
throw new Error(`Asset ${entity.id} has no fileModifiedAt`);
}

if (stripMetadata) {
const sanitizedAssetResponse: SanitizedAssetResponseDto = {
id: entity.id,
type: entity.type,
originalMimeType: mimeTypes.lookup(entity.originalFileName),
thumbhash: entity.thumbhash?.toString('base64') ?? null,
thumbhash: entity.thumbhash ? hexOrBufferToBase64(entity.thumbhash) : null,
localDateTime: entity.localDateTime,
duration: entity.duration ?? '0:00:00.00000',
livePhotoVideoId: entity.livePhotoVideoId,
Expand Down
12 changes: 9 additions & 3 deletions server/src/entities/asset.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ export class AssetEntity {

@Index('idx_asset_file_created_at')
@Column({ type: 'timestamptz', nullable: true, default: null })
fileCreatedAt!: Date | null;
fileCreatedAt!: Date;

@Column({ type: 'timestamptz', nullable: true, default: null })
localDateTime!: Date | null;
localDateTime!: Date;

@Column({ type: 'timestamptz', nullable: true, default: null })
fileModifiedAt!: Date | null;
fileModifiedAt!: Date;

@Column({ type: 'boolean', default: false })
isFavorite!: boolean;
Expand Down Expand Up @@ -180,6 +180,12 @@ export class AssetEntity {
duplicateId!: string | null;
}

export type AssetEntityPlaceholder = AssetEntity & {
fileCreatedAt: Date | null;
fileModifiedAt: Date | null;
localDateTime: Date | null;
};

export function withExif<O>(qb: SelectQueryBuilder<DB, 'assets', O>) {
return qb.leftJoin('exif', 'assets.id', 'exif.assetId').select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo'));
}
Expand Down
3 changes: 1 addition & 2 deletions server/src/queries/asset.repository.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ with
and "assets"."deletedAt" is null
and "assets"."fileCreatedAt" is not null
and "assets"."fileModifiedAt" is not null
and "assets"."localDateTime" is not null
order by
(assets."localDateTime" at time zone 'UTC')::date desc
limit
Expand Down Expand Up @@ -461,8 +462,6 @@ from
where
"assets"."ownerId" = any ($1::uuid[])
and "isVisible" = $2
and "assets"."fileCreatedAt" is not null
and "assets"."fileModifiedAt" is not null
and "updatedAt" > $3
limit
$4
22 changes: 22 additions & 0 deletions server/src/queries/map.repository.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- NOTE: This file is auto generated by ./sql-generator

-- MapRepository.getMapMarkers
select
"id",
"exif"."latitude" as "lat",
"exif"."longitude" as "lon",
"exif"."city",
"exif"."state",
"exif"."country"
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
and "exif"."latitude" is not null
and "exif"."longitude" is not null
left join "albums_assets_assets" on "assets"."id" = "albums_assets_assets"."assetsId"
where
"isVisible" = $1
and "deletedAt" is null
and "ownerId" in ($2)
order by
"fileCreatedAt" desc
17 changes: 12 additions & 5 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Injectable } from '@nestjs/common';
import { Insertable, Kysely, UpdateResult, Updateable, sql } from 'kysely';
import { Insertable, Kysely, NotNull, UpdateResult, Updateable, sql } from 'kysely';
import { isEmpty, isUndefined, omitBy } from 'lodash';
import { InjectKysely } from 'nestjs-kysely';
import { ASSET_FILE_CONFLICT_KEYS, EXIF_CONFLICT_KEYS, JOB_STATUS_CONFLICT_KEYS } from 'src/constants';
import { AssetFiles, AssetJobStatus, Assets, DB, Exif } from 'src/db';
import { Chunked, ChunkedArray, DummyValue, GenerateSql } from 'src/decorators';
import {
AssetEntity,
AssetEntityPlaceholder,
hasPeople,
searchAssetBuilder,
truncatedDate,
Expand Down Expand Up @@ -80,8 +81,12 @@ export class AssetRepository implements IAssetRepository {
.execute();
}

create(asset: Insertable<Assets>): Promise<AssetEntity> {
return this.db.insertInto('assets').values(asset).returningAll().executeTakeFirst() as any as Promise<AssetEntity>;
create(asset: Insertable<Assets>): Promise<AssetEntityPlaceholder> {
return this.db
.insertInto('assets')
.values(asset)
.returningAll()
.executeTakeFirst() as any as Promise<AssetEntityPlaceholder>;
}

createAll(assets: Insertable<Assets>[]): Promise<AssetEntity[]> {
Expand Down Expand Up @@ -128,13 +133,17 @@ export class AssetRepository implements IAssetRepository {
.where('assets.deletedAt', 'is', null)
.where('assets.fileCreatedAt', 'is not', null)
.where('assets.fileModifiedAt', 'is not', null)
.where('assets.localDateTime', 'is not', null)
.orderBy(sql`(assets."localDateTime" at time zone 'UTC')::date`, 'desc')
.limit(20)
.as('a'),
(join) => join.onTrue(),
)
.innerJoin('exif', 'a.id', 'exif.assetId')
.selectAll('a')
.$narrowType<{ fileCreatedAt: NotNull }>()
.$narrowType<{ fileModifiedAt: NotNull }>()
.$narrowType<{ localDateTime: NotNull }>()
.select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo')),
)
.selectFrom('res')
Expand Down Expand Up @@ -857,8 +866,6 @@ export class AssetRepository implements IAssetRepository {
.select((eb) => eb.fn.toJson(eb.table('stacked_assets')).as('stack'))
.where('assets.ownerId', '=', anyUuid(options.userIds))
.where('isVisible', '=', true)
.where('assets.fileCreatedAt', 'is not', null)
.where('assets.fileModifiedAt', 'is not', null)
.where('updatedAt', '>', options.updatedAfter)
.limit(options.limit)
.execute() as any as Promise<AssetEntity[]>;
Expand Down
5 changes: 4 additions & 1 deletion server/src/repositories/config.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ const getEnv = (): EnvData => {
}

const driverOptions = {
...parsedOptions,
onnotice: (notice: Notice) => {
if (notice['severity'] !== 'NOTICE') {
console.warn('Postgres notice:', notice);
Expand All @@ -247,7 +248,9 @@ const getEnv = (): EnvData => {
serialize: (value: number) => value.toString(),
},
},
...parsedOptions,
connection: {
TimeZone: 'UTC',
},
};

return {
Expand Down
43 changes: 17 additions & 26 deletions server/src/repositories/map.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { readFile } from 'node:fs/promises';
import readLine from 'node:readline';
import { citiesFile } from 'src/constants';
import { DB, GeodataPlaces, NaturalearthCountries } from 'src/db';
import { AssetEntity, withExif } from 'src/entities/asset.entity';
import { DummyValue, GenerateSql } from 'src/decorators';
import { NaturalEarthCountriesTempEntity } from 'src/entities/natural-earth-countries.entity';
import { LogLevel, SystemMetadataKey } from 'src/enum';
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
Expand Down Expand Up @@ -76,50 +76,41 @@ export class MapRepository {
this.logger.log('Geodata import completed');
}

async getMapMarkers(
ownerIds: string[],
albumIds: string[],
options: MapMarkerSearchOptions = {},
): Promise<MapMarker[]> {
@GenerateSql({ params: [[DummyValue.UUID], []] })
getMapMarkers(ownerIds: string[], albumIds: string[], options: MapMarkerSearchOptions = {}) {
const { isArchived, isFavorite, fileCreatedAfter, fileCreatedBefore } = options;

const assets = (await this.db
return this.db
.selectFrom('assets')
.$call(withExif)
.select('id')
.innerJoin('exif', (builder) =>
builder
.onRef('assets.id', '=', 'exif.assetId')
.on('exif.latitude', 'is not', null)
.on('exif.longitude', 'is not', null),
)
.select(['id', 'exif.latitude as lat', 'exif.longitude as lon', 'exif.city', 'exif.state', 'exif.country'])
.leftJoin('albums_assets_assets', (join) => join.onRef('assets.id', '=', 'albums_assets_assets.assetsId'))
.where('isVisible', '=', true)
.$if(isArchived !== undefined, (q) => q.where('isArchived', '=', isArchived!))
.$if(isFavorite !== undefined, (q) => q.where('isFavorite', '=', isFavorite!))
.$if(fileCreatedAfter !== undefined, (q) => q.where('fileCreatedAt', '>=', fileCreatedAfter!))
.$if(fileCreatedBefore !== undefined, (q) => q.where('fileCreatedAt', '<=', fileCreatedBefore!))
.where('deletedAt', 'is', null)
.where('exif.latitude', 'is not', null)
.where('exif.longitude', 'is not', null)
.where((eb) => {
const ors: Expression<SqlBool>[] = [];
.where((builder) => {
const expression: Expression<SqlBool>[] = [];

if (ownerIds.length > 0) {
ors.push(eb('ownerId', 'in', ownerIds));
expression.push(builder('ownerId', 'in', ownerIds));
}

if (albumIds.length > 0) {
ors.push(eb('albums_assets_assets.albumsId', 'in', albumIds));
expression.push(builder('albums_assets_assets.albumsId', 'in', albumIds));
}

return eb.or(ors);
return builder.or(expression);
})
.orderBy('fileCreatedAt', 'desc')
.execute()) as any as AssetEntity[];

return assets.map((asset) => ({
id: asset.id,
lat: asset.exifInfo!.latitude!,
lon: asset.exifInfo!.longitude!,
city: asset.exifInfo!.city,
state: asset.exifInfo!.state,
country: asset.exifInfo!.country,
}));
.execute() as Promise<MapMarker[]>;
}

async reverseGeocode(point: GeoPoint): Promise<ReverseGeocodeResult> {
Expand Down
3 changes: 3 additions & 0 deletions server/src/repositories/view-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export class ViewRepository {
.where('deletedAt', 'is', null)
.where('originalPath', 'like', `%${normalizedPath}/%`)
.where('originalPath', 'not like', `%${normalizedPath}/%/%`)
.$narrowType<{ fileCreatedAt: Date }>()
.$narrowType<{ fileModifiedAt: Date }>()
.$narrowType<{ localDateTime: Date }>()
.orderBy(
(eb) => eb.fn('regexp_replace', ['assets.originalPath', eb.val('.*/(.+)'), eb.val(String.raw`\1`)]),
'asc',
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/actions/click-outside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export function clickOutside(node: HTMLElement, options: Options = {}): ActionRe
}
};

document.addEventListener('click', handleClick, true);
document.addEventListener('mousedown', handleClick, true);
node.addEventListener('keydown', handleKey, false);

return {
destroy() {
document.removeEventListener('click', handleClick, true);
document.removeEventListener('mousedown', handleClick, true);
node.removeEventListener('keydown', handleKey, false);
},
};
Expand Down

0 comments on commit 7f73f2e

Please sign in to comment.