import { Body, Controller, Get, Post, Query } from '@nestjs/common'; import type { PublicUser } from '../domain/models'; import { CurrentUser } from '../auth/auth.decorators'; import { SyncService } from '../application/sync.service'; import { PushUpdatesDto, UpdatesQueryDto } from './dto/sync.dto'; const ownerOf = (u: PublicUser | undefined): string | null => u?.id ?? null; // Local-first sync peer: clients pull update blobs they have not seen (by // per-doc cursor) and push the ones they created. Payloads are opaque Loro // updates; the server only stores and orders them. @Controller('api/sync') export class SyncController { constructor(private readonly service: SyncService) {} @Get('manifest') async manifest(@CurrentUser() user?: PublicUser) { return { docs: await this.service.manifest(ownerOf(user)) }; } @Get('updates') updates(@Query() query: UpdatesQueryDto, @CurrentUser() user?: PublicUser) { return this.service.updatesSince(ownerOf(user), query.doc_id!, query.after_seq ?? 0); } @Post('updates') push(@Body() dto: PushUpdatesDto, @CurrentUser() user?: PublicUser) { return this.service.push(ownerOf(user), dto.doc_id!, dto.updates ?? [], dto.device_id ?? null); } }