Files
immich/server/src/controllers/sync.controller.ts

108 lines
3.9 KiB
TypeScript
Raw Normal View History

import { Body, Controller, Delete, Get, Header, HttpCode, HttpStatus, Post, Res } from '@nestjs/common';
2025-11-13 08:18:43 -05:00
import { ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
2025-11-13 08:18:43 -05:00
import { Endpoint, HistoryBuilder } from 'src/decorators';
import { AssetResponseDto } from 'src/dtos/asset-response.dto';
import { AuthDto } from 'src/dtos/auth.dto';
import {
AssetDeltaSyncDto,
AssetDeltaSyncResponseDto,
AssetFullSyncDto,
SyncAckDeleteDto,
SyncAckDto,
SyncAckSetDto,
SyncStreamDto,
} from 'src/dtos/sync.dto';
2025-11-11 17:01:14 -05:00
import { ApiTag, Permission } from 'src/enum';
import { Auth, Authenticated } from 'src/middleware/auth.guard';
import { GlobalExceptionFilter } from 'src/middleware/global-exception.filter';
import { SyncService } from 'src/services/sync.service';
2025-11-11 17:01:14 -05:00
@ApiTags(ApiTag.Sync)
@Controller('sync')
export class SyncController {
constructor(
private service: SyncService,
private errorService: GlobalExceptionFilter,
) {}
@Post('full-sync')
@Authenticated()
@HttpCode(HttpStatus.OK)
2025-11-13 08:18:43 -05:00
@Endpoint({
2025-11-11 17:01:14 -05:00
summary: 'Get full sync for user',
description: 'Retrieve all assets for a full synchronization for the authenticated user.',
2025-11-13 08:18:43 -05:00
history: new HistoryBuilder().added('v1').deprecated('v2'),
2025-11-11 17:01:14 -05:00
})
getFullSyncForUser(@Auth() auth: AuthDto, @Body() dto: AssetFullSyncDto): Promise<AssetResponseDto[]> {
return this.service.getFullSync(auth, dto);
}
@Post('delta-sync')
@Authenticated()
@HttpCode(HttpStatus.OK)
2025-11-13 08:18:43 -05:00
@Endpoint({
2025-11-11 17:01:14 -05:00
summary: 'Get delta sync for user',
description: 'Retrieve changed assets since the last sync for the authenticated user.',
2025-11-13 08:18:43 -05:00
history: new HistoryBuilder().added('v1').deprecated('v2'),
2025-11-11 17:01:14 -05:00
})
getDeltaSync(@Auth() auth: AuthDto, @Body() dto: AssetDeltaSyncDto): Promise<AssetDeltaSyncResponseDto> {
return this.service.getDeltaSync(auth, dto);
}
@Post('stream')
@Authenticated({ permission: Permission.SyncStream })
@Header('Content-Type', 'application/jsonlines+json')
@HttpCode(HttpStatus.OK)
2025-11-13 08:18:43 -05:00
@Endpoint({
2025-11-11 17:01:14 -05:00
summary: 'Stream sync changes',
description:
'Retrieve a JSON lines streamed response of changes for synchronization. This endpoint is used by the mobile app to efficiently stay up to date with changes.',
2025-11-13 08:18:43 -05:00
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
2025-11-11 17:01:14 -05:00
})
async getSyncStream(@Auth() auth: AuthDto, @Res() res: Response, @Body() dto: SyncStreamDto) {
try {
await this.service.stream(auth, res, dto);
} catch (error: Error | any) {
res.setHeader('Content-Type', 'application/json');
this.errorService.handleError(res, error);
}
}
@Get('ack')
@Authenticated({ permission: Permission.SyncCheckpointRead })
2025-11-13 08:18:43 -05:00
@Endpoint({
2025-11-11 17:01:14 -05:00
summary: 'Retrieve acknowledgements',
description: 'Retrieve the synchronization acknowledgments for the current session.',
2025-11-13 08:18:43 -05:00
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
2025-11-11 17:01:14 -05:00
})
getSyncAck(@Auth() auth: AuthDto): Promise<SyncAckDto[]> {
return this.service.getAcks(auth);
}
@Post('ack')
@Authenticated({ permission: Permission.SyncCheckpointUpdate })
@HttpCode(HttpStatus.NO_CONTENT)
2025-11-13 08:18:43 -05:00
@Endpoint({
2025-11-11 17:01:14 -05:00
summary: 'Acknowledge changes',
description:
'Send a list of synchronization acknowledgements to confirm that the latest changes have been received.',
2025-11-13 08:18:43 -05:00
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
2025-11-11 17:01:14 -05:00
})
sendSyncAck(@Auth() auth: AuthDto, @Body() dto: SyncAckSetDto) {
return this.service.setAcks(auth, dto);
}
@Delete('ack')
@Authenticated({ permission: Permission.SyncCheckpointDelete })
@HttpCode(HttpStatus.NO_CONTENT)
2025-11-13 08:18:43 -05:00
@Endpoint({
2025-11-11 17:01:14 -05:00
summary: 'Delete acknowledgements',
description: 'Delete specific synchronization acknowledgments.',
2025-11-13 08:18:43 -05:00
history: new HistoryBuilder().added('v1').beta('v1').stable('v2'),
2025-11-11 17:01:14 -05:00
})
deleteSyncAck(@Auth() auth: AuthDto, @Body() dto: SyncAckDeleteDto): Promise<void> {
return this.service.deleteAcks(auth, dto);
}
}