File size: 10,614 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  Post,
  Put,
  Query,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { GroupIdApiParam } from '@waha/nestjs/params/ChatIdApiParam';
import {
  SessionApiParam,
  WorkingSessionParam,
} from '@waha/nestjs/params/SessionApiParam';
import { CountResponse, Result } from '@waha/structures/base.dto';
import {
  ChatPictureQuery,
  ChatPictureResponse,
} from '@waha/structures/chats.dto';
import { ProfilePictureRequest } from '@waha/structures/profile.dto';

import { SessionManager } from '../core/abc/manager.abc';
import { parseGroupInviteLink, WhatsappSession } from '../core/abc/session.abc';
import {
  CreateGroupRequest,
  DescriptionRequest,
  GroupField,
  GroupsListFields,
  GroupsPaginationParams,
  JoinGroupRequest,
  JoinGroupResponse,
  ParticipantsRequest,
  SettingsSecurityChangeInfo,
  SubjectRequest,
} from '../structures/groups.dto';

@ApiSecurity('api_key')
@Controller('api/:session/groups')
@ApiTags('👥 Groups')
export class GroupsController {
  constructor(private manager: SessionManager) {}

  @Post('')
  @SessionApiParam
  @ApiOperation({ summary: 'Create a new group.' })
  createGroup(
    @WorkingSessionParam session: WhatsappSession,
    @Body() request: CreateGroupRequest,
  ) {
    return session.createGroup(request);
  }

  @Get('join-info')
  @SessionApiParam
  @ApiOperation({ summary: 'Get info about the group before joining.' })
  async joinInfoGroup(
    @WorkingSessionParam session: WhatsappSession,
    @Query() query: JoinGroupRequest,
  ): Promise<any> {
    const code = parseGroupInviteLink(query.code);
    return session.joinInfoGroup(code);
  }

  @Post('join')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @ApiOperation({ summary: 'Join group via code' })
  async joinGroup(
    @WorkingSessionParam session: WhatsappSession,
    @Body() request: JoinGroupRequest,
  ): Promise<JoinGroupResponse> {
    const code = parseGroupInviteLink(request.code);
    const id = await session.joinGroup(code);
    return { id: id };
  }

  @Get('')
  @SessionApiParam
  @ApiOperation({ summary: 'Get all groups.' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async getGroups(
    @WorkingSessionParam session: WhatsappSession,
    @Query() pagination: GroupsPaginationParams,
    @Query() fields: GroupsListFields,
  ) {
    let groups: any = await session.getGroups(pagination);
    groups = session.filterGroupsFields(groups, fields);
    return groups;
  }

  @Get('/count')
  @SessionApiParam
  @ApiOperation({ summary: 'Get the number of groups.' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async getGroupsCount(
    @WorkingSessionParam session: WhatsappSession,
  ): Promise<CountResponse> {
    const data: any = await session.getGroups({});
    const groups: any[] = Array.isArray(data) ? data : Object.values(data);
    return {
      count: groups.length,
    };
  }

  @Post('refresh')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @ApiOperation({ summary: 'Refresh groups from the server.' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async refreshGroups(@WorkingSessionParam session: WhatsappSession) {
    return { success: await session.refreshGroups() };
  }

  @Get(':id')
  @GroupIdApiParam
  @SessionApiParam
  @ApiOperation({ summary: 'Get the group.' })
  getGroup(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ) {
    return session.getGroup(id);
  }

  @Delete(':id')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Delete the group.' })
  deleteGroup(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ) {
    return session.deleteGroup(id);
  }

  @Post(':id/leave')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Leave the group.' })
  leaveGroup(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ) {
    return session.leaveGroup(id);
  }

  @Get(':id/picture')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Get group picture' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async getChatPicture(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Query() query: ChatPictureQuery,
  ): Promise<ChatPictureResponse> {
    const url = await session.getContactProfilePicture(id, query.refresh);
    return { url: url };
  }

  @Put(':id/picture')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Set group picture' })
  async setPicture(
    @Param('id') id: string,
    @WorkingSessionParam session: WhatsappSession,
    @Body() request: ProfilePictureRequest,
  ): Promise<Result> {
    const success = await session.updateGroupPicture(id, request.file);
    return { success: success ?? true };
  }

  @Delete(':id/picture')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Delete group picture' })
  async deletePicture(
    @Param('id') id: string,
    @WorkingSessionParam session: WhatsappSession,
  ): Promise<Result> {
    const success = await session.updateGroupPicture(id, null);
    return { success: success || true };
  }

  @Put(':id/description')
  @ApiOperation({
    summary: 'Updates the group description.',
    description:
      'Returns "true" if the subject was properly updated. This can return "false" if the user does not have the necessary permissions.',
  })
  @SessionApiParam
  @GroupIdApiParam
  setDescription(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: DescriptionRequest,
  ) {
    return session.setDescription(id, request.description);
  }

  @Put(':id/subject')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({
    summary: 'Updates the group subject',
    description:
      'Returns "true" if the subject was properly updated. This can return "false" if the user does not have the necessary permissions.',
  })
  setSubject(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: SubjectRequest,
  ) {
    return session.setSubject(id, request.subject);
  }

  @Put(':id/settings/security/info-admin-only')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({
    summary: 'Updates the group "info admin only" settings.',
    description:
      'You can allow only admins to edit group info (title, description, photo).',
  })
  setInfoAdminOnly(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: SettingsSecurityChangeInfo,
  ) {
    return session.setInfoAdminsOnly(id, request.adminsOnly);
  }

  @Get(':id/settings/security/info-admin-only')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({
    summary: "Get the group's 'info admin only' settings.",
    description:
      'You can allow only admins to edit group info (title, description, photo).',
  })
  getInfoAdminOnly(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ): Promise<SettingsSecurityChangeInfo> {
    return session.getInfoAdminsOnly(id);
  }

  @Put(':id/settings/security/messages-admin-only')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({
    summary: 'Update settings - who can send messages',
    description:
      'Updates the group settings to only allow admins to send messages.',
  })
  setMessagesAdminOnly(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: SettingsSecurityChangeInfo,
  ) {
    return session.setMessagesAdminsOnly(id, request.adminsOnly);
  }

  @Get(':id/settings/security/messages-admin-only')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({
    summary: 'Get settings - who can send messages',
    description: 'The group settings to only allow admins to send messages.',
  })
  getMessagesAdminOnly(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ): Promise<SettingsSecurityChangeInfo> {
    return session.getMessagesAdminsOnly(id);
  }

  @Get(':id/invite-code')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Gets the invite code for the group.' })
  getInviteCode(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ): Promise<string> {
    return session.getInviteCode(id);
  }

  @Post(':id/invite-code/revoke')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({
    summary:
      'Invalidates the current group invite code and generates a new one.',
  })
  revokeInviteCode(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ): Promise<string> {
    return session.revokeInviteCode(id);
  }

  @Get(':id/participants/')
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Get participants' })
  getParticipants(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
  ) {
    return session.getParticipants(id);
  }

  @Post(':id/participants/add')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Add participants' })
  addParticipants(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: ParticipantsRequest,
  ) {
    return session.addParticipants(id, request);
  }

  @Post(':id/participants/remove')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({
    summary: 'Remove participants',
  })
  removeParticipants(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: ParticipantsRequest,
  ) {
    return session.removeParticipants(id, request);
  }

  @Post(':id/admin/promote')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Promote participants to admin users.' })
  promoteToAdmin(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: ParticipantsRequest,
  ) {
    return session.promoteParticipantsToAdmin(id, request);
  }

  @Post(':id/admin/demote')
  @HttpCode(HttpStatus.OK)
  @SessionApiParam
  @GroupIdApiParam
  @ApiOperation({ summary: 'Demotes participants to regular users.' })
  demoteToAdmin(
    @WorkingSessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: ParticipantsRequest,
  ) {
    return session.demoteParticipantsToUser(id, request);
  }
}