File size: 6,346 Bytes
b62a170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';

import { SessionManager } from '../core/abc/manager.abc';
import { WhatsappSession } from '../core/abc/session.abc';
import {
  CreateGroupRequest,
  DescriptionRequest,
  ParticipantsRequest,
  SettingsSecurityChangeInfo,
  SubjectRequest,
} from '../structures/groups.dto';
import { SessionApiParam, SessionParam } from './helpers';

@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(
    @SessionParam session: WhatsappSession,
    @Body() request: CreateGroupRequest,
  ) {
    return session.createGroup(request);
  }

  @Get('')
  @SessionApiParam
  @ApiOperation({ summary: 'Get all groups.' })
  getGroups(@SessionParam session: WhatsappSession) {
    return session.getGroups();
  }

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

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

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

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

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

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

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

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

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

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

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

  @Get(':id/participants/')
  @SessionApiParam
  @ApiOperation({ summary: 'Get a list of participants by in the group.' })
  getParticipants(
    @SessionParam session: WhatsappSession,
    @Param('id') id: string,
  ) {
    return session.getParticipants(id);
  }

  @Post(':id/participants/add')
  @SessionApiParam
  @ApiOperation({ summary: 'Adds a list of participants by ID to the group.' })
  addParticipants(
    @SessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: ParticipantsRequest,
  ) {
    return session.addParticipants(id, request);
  }

  @Post(':id/participants/remove')
  @SessionApiParam
  @ApiOperation({
    summary: 'Removes a list of participants by ID to the group.',
  })
  removeParticipants(
    @SessionParam session: WhatsappSession,
    @Param('id') id: string,
    @Body() request: ParticipantsRequest,
  ) {
    return session.removeParticipants(id, request);
  }

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

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