import { Controller, Delete, Get, Param, Put, Req } from '@nestjs/common'; import { WATCH_TARGET_TYPES, type WatchListView, type WatchStateView, type WatchTargetType, } from '@dorfteich/shared'; import { NotFoundException } from '@nestjs/common'; import { AuthedRequest } from '../auth/auth.guard'; import { AuthenticatedOnly } from '../permissions/permission.decorators'; import { WatchesService } from './watches.service'; function asTargetType(value: string): WatchTargetType { if (!(WATCH_TARGET_TYPES as readonly string[]).includes(value)) throw new NotFoundException(); return value as WatchTargetType; } /** Watch/unwatch pages and ponds + the account's watch list (issue #93). */ @Controller() export class WatchesController { constructor(private readonly watches: WatchesService) {} @Get('users/me/watches') @AuthenticatedOnly() async list(@Req() request: AuthedRequest): Promise { return this.watches.listOwn(request.user!); } @Get('watches/:targetType/:id') @AuthenticatedOnly() async state( @Param('targetType') targetType: string, @Param('id') id: string, @Req() request: AuthedRequest, ): Promise { return this.watches.state(request.user!, asTargetType(targetType), id); } @Put('watches/:targetType/:id') @AuthenticatedOnly() async watch( @Param('targetType') targetType: string, @Param('id') id: string, @Req() request: AuthedRequest, ): Promise { return this.watches.watch(request.user!, asTargetType(targetType), id); } @Delete('watches/:targetType/:id') @AuthenticatedOnly() async unwatch( @Param('targetType') targetType: string, @Param('id') id: string, @Req() request: AuthedRequest, ): Promise { return this.watches.unwatch(request.user!, asTargetType(targetType), id); } }