File size: 7,125 Bytes
57a889c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { Body, Controller, Get, Headers, HttpCode, Param, Post, Put, Req, Res, UseGuards } from '@nestjs/common';
import type { Request, Response } from 'express';
import type { User } from '../../types';
import { MemoriesService } from './memories.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { getClientIp } from '../../services/auditLog';

/**
 * /api/integrations/memories/immich — Immich connection, browse/search, asset
 * proxy and album linking.
 *
 * Byte-identical to the legacy Express router (server/src/routes/memories/immich.ts):
 * `/status` and `/test` answer 200 even on connection failure (the service shapes
 * `{ connected: false, ... }`); `/settings` PUT validates with a 400; the asset
 * routes do the 400 invalid-id guard then the canAccessUserPhoto 403 ('Forbidden')
 * before streaming or returning info; the album sync answers 200 then broadcasts.
 * The legacy `canAccessTrip` import there is dead code — intentionally not ported.
 */
@Controller('api/integrations/memories/immich')
@UseGuards(JwtAuthGuard)
export class ImmichMemoriesController {
  constructor(private readonly memories: MemoriesService) {}

  @Get('settings')
  getSettings(@CurrentUser() user: User) {
    return this.memories.immichGetConnectionSettings(user.id);
  }

  @Put('settings')
  async putSettings(
    @CurrentUser() user: User,
    @Body() body: { immich_url?: string; immich_api_key?: string; auto_upload?: unknown },
    @Req() req: Request,
    @Res() res: Response,
  ): Promise<void> {
    const { immich_url, immich_api_key, auto_upload } = body;
    const result = await this.memories.immichSaveSettings(user.id, immich_url, immich_api_key, getClientIp(req));
    if (!result.success) {
      res.status(400).json({ error: result.error });
      return;
    }
    if (typeof auto_upload === 'boolean') {
      this.memories.immichSetAutoUpload(user.id, auto_upload);
    }
    if (result.warning) {
      res.json({ success: true, warning: result.warning });
      return;
    }
    res.json({ success: true });
  }

  @Get('status')
  async getStatus(@CurrentUser() user: User) {
    return this.memories.immichGetConnectionStatus(user.id);
  }

  @Post('test')
  @HttpCode(200)
  async test(@Body() body: { immich_url?: string; immich_api_key?: string }) {
    const { immich_url, immich_api_key } = body;
    if (!immich_url || !immich_api_key) {
      return { connected: false, error: 'URL and API key required' };
    }
    return this.memories.immichTestConnection(immich_url, immich_api_key);
  }

  @Get('browse')
  async browse(@CurrentUser() user: User, @Res() res: Response): Promise<void> {
    const result = await this.memories.immichBrowseTimeline(user.id);
    if (result.error) {
      res.status(result.status!).json({ error: result.error });
      return;
    }
    res.json({ buckets: result.buckets });
  }

  @Post('search')
  @HttpCode(200)
  async search(@CurrentUser() user: User, @Body() body: Record<string, unknown>, @Res() res: Response): Promise<void> {
    const { from, to, size, page } = body as { from?: string; to?: string; size?: unknown; page?: unknown };
    const pageNum = Math.max(1, Number(page) || 1);
    const pageSize = Math.min(Number(size) || 50, 200);
    const result = await this.memories.immichSearchPhotos(user.id, from, to, pageNum, pageSize);
    if (result.error) {
      res.status(result.status!).json({ error: result.error });
      return;
    }
    res.json({ assets: result.assets || [], hasMore: !!result.hasMore });
  }

  @Get('assets/:tripId/:assetId/:ownerId/info')
  async assetInfo(
    @CurrentUser() user: User,
    @Param('tripId') tripId: string,
    @Param('assetId') assetId: string,
    @Param('ownerId') ownerId: string,
    @Res() res: Response,
  ): Promise<void> {
    if (!this.memories.immichIsValidAssetId(assetId)) {
      res.status(400).json({ error: 'Invalid asset ID' });
      return;
    }
    if (!this.memories.canAccessUserPhoto(user.id, Number(ownerId), tripId, assetId, 'immich')) {
      res.status(403).json({ error: 'Forbidden' });
      return;
    }
    const result = await this.memories.immichGetAssetInfo(user.id, assetId, Number(ownerId));
    if (result.error) {
      res.status(result.status!).json({ error: result.error });
      return;
    }
    res.json(result.data);
  }

  @Get('assets/:tripId/:assetId/:ownerId/thumbnail')
  async assetThumbnail(
    @CurrentUser() user: User,
    @Param('tripId') tripId: string,
    @Param('assetId') assetId: string,
    @Param('ownerId') ownerId: string,
    @Res() res: Response,
  ): Promise<void> {
    if (!this.memories.immichIsValidAssetId(assetId)) {
      res.status(400).json({ error: 'Invalid asset ID' });
      return;
    }
    if (!this.memories.canAccessUserPhoto(user.id, Number(ownerId), tripId, assetId, 'immich')) {
      res.status(403).json({ error: 'Forbidden' });
      return;
    }
    await this.memories.immichStreamAsset(res, user.id, assetId, 'thumbnail', Number(ownerId));
  }

  @Get('assets/:tripId/:assetId/:ownerId/original')
  async assetOriginal(
    @CurrentUser() user: User,
    @Param('tripId') tripId: string,
    @Param('assetId') assetId: string,
    @Param('ownerId') ownerId: string,
    @Res() res: Response,
  ): Promise<void> {
    if (!this.memories.immichIsValidAssetId(assetId)) {
      res.status(400).json({ error: 'Invalid asset ID' });
      return;
    }
    if (!this.memories.canAccessUserPhoto(user.id, Number(ownerId), tripId, assetId, 'immich')) {
      res.status(403).json({ error: 'Forbidden' });
      return;
    }
    await this.memories.immichStreamAsset(res, user.id, assetId, 'original', Number(ownerId));
  }

  @Get('albums')
  async albums(@CurrentUser() user: User, @Res() res: Response): Promise<void> {
    const result = await this.memories.immichListAlbums(user.id);
    if (result.error) {
      res.status(result.status!).json({ error: result.error });
      return;
    }
    res.json({ albums: result.albums });
  }

  @Get('albums/:albumId/photos')
  async albumPhotos(@CurrentUser() user: User, @Param('albumId') albumId: string, @Res() res: Response): Promise<void> {
    const result = await this.memories.immichGetAlbumPhotos(user.id, albumId);
    if (result.error) {
      res.status(result.status!).json({ error: result.error });
      return;
    }
    res.json({ assets: result.assets });
  }

  @Post('trips/:tripId/album-links/:linkId/sync')
  @HttpCode(200)
  async sync(
    @CurrentUser() user: User,
    @Param('tripId') tripId: string,
    @Param('linkId') linkId: string,
    @Headers('x-socket-id') sid: string,
    @Res() res: Response,
  ): Promise<void> {
    const result = await this.memories.immichSyncAlbumAssets(tripId, linkId, user.id, sid);
    if (result.error) {
      res.status(result.status!).json({ error: result.error });
      return;
    }
    res.json({ success: true, added: result.added, total: result.total });
    if (result.added! > 0) {
      this.memories.broadcast(tripId, 'memories:updated', { userId: user.id }, sid);
    }
  }
}