File size: 7,470 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
import { isJidGroup } from '@adiwajshing/baileys';
import {
  Body,
  Controller,
  Delete,
  Get,
  NotFoundException,
  Param,
  Post,
  Put,
  Query,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ChatIdApiParam } from '@waha/nestjs/params/ChatIdApiParam';
import { MessageIdApiParam } from '@waha/nestjs/params/MessageIdApiParam';
import {
  SessionApiParam,
  WorkingSessionParam,
} from '@waha/nestjs/params/SessionApiParam';

import { SessionManager } from '../core/abc/manager.abc';
import { WhatsappSession } from '../core/abc/session.abc';
import {
  ChatPictureQuery,
  ChatPictureResponse,
  ChatsPaginationParams,
  ChatSummary,
  GetChatMessageQuery,
  GetChatMessagesFilter,
  GetChatMessagesQuery,
  OverviewBodyRequest,
  OverviewFilter,
  OverviewPaginationParams,
  PinMessageRequest,
  ReadChatMessagesQuery,
  ReadChatMessagesResponse,
  transformAck,
} from '../structures/chats.dto';
import { EditMessageRequest } from '../structures/chatting.dto';

@ApiSecurity('api_key')
@Controller('api/:session/chats')
@ApiTags('💬 Chats')
@UsePipes(new ValidationPipe({ transform: true }))
class ChatsController {
  constructor(private manager: SessionManager) {}

  @Get('')
  @SessionApiParam
  @ApiOperation({ summary: 'Get chats' })
  getChats(
    @WorkingSessionParam session: WhatsappSession,
    @Query() pagination: ChatsPaginationParams,
  ) {
    return session.getChats(pagination);
  }

  @Get('overview')
  @SessionApiParam
  @ApiOperation({
    summary:
      'Get chats overview. Includes all necessary things to build UI "your chats overview" page - chat id, name, picture, last message. Sorting by last message timestamp',
  })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  getChatsOverview(
    @WorkingSessionParam session: WhatsappSession,
    @Query() pagination: OverviewPaginationParams,
    @Query() filter: OverviewFilter,
  ): Promise<ChatSummary[]> {
    return session.getChatsOverview(pagination, filter);
  }

  @Post('overview')
  @SessionApiParam
  @ApiOperation({
    summary:
      'Get chats overview. Use POST if you have too many "ids" params - GET can limit it',
  })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  postChatsOverview(
    @WorkingSessionParam session: WhatsappSession,
    @Body() body: OverviewBodyRequest,
  ): Promise<ChatSummary[]> {
    return session.getChatsOverview(body.pagination, body.filter);
  }

  @Delete(':chatId')
  @SessionApiParam
  @ApiOperation({ summary: 'Deletes the chat' })
  @ChatIdApiParam
  deleteChat(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
  ) {
    return session.deleteChat(chatId);
  }

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

  @Get(':chatId/messages')
  @SessionApiParam
  @ApiOperation({ summary: 'Gets messages in the chat' })
  @ChatIdApiParam
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  getChatMessages(
    @Query() query: GetChatMessagesQuery,
    @Query() filter: GetChatMessagesFilter,
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
  ) {
    filter = transformAck(filter);
    return session.getChatMessages(chatId, query, filter);
  }

  @Post(':chatId/messages/read')
  @SessionApiParam
  @ApiOperation({ summary: 'Read unread messages in the chat' })
  @ChatIdApiParam
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  readChatMessages(
    @Query() query: ReadChatMessagesQuery,
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
  ): Promise<ReadChatMessagesResponse> {
    return session.readChatMessages(chatId, query);
  }

  @Get(':chatId/messages/:messageId')
  @SessionApiParam
  @ApiOperation({ summary: 'Gets message by id' })
  @ChatIdApiParam
  async getChatMessage(
    @Query() query: GetChatMessageQuery,
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
    @Param('messageId') messageId: string,
  ) {
    const message = await session.getChatMessage(chatId, messageId, query);
    if (!message) {
      throw new NotFoundException('Message not found');
    }
    return message;
  }

  @Post(':chatId/messages/:messageId/pin')
  @SessionApiParam
  @ApiOperation({ summary: 'Pins a message in the chat' })
  @ChatIdApiParam
  async pinMessage(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
    @Param('messageId') messageId: string,
    @Body() body: PinMessageRequest,
  ) {
    const result = await session.pinMessage(chatId, messageId, body.duration);
    return { success: result };
  }

  @Post(':chatId/messages/:messageId/unpin')
  @SessionApiParam
  @ApiOperation({ summary: 'Unpins a message in the chat' })
  @ChatIdApiParam
  async unpinMessage(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
    @Param('messageId') messageId: string,
  ) {
    const result = await session.unpinMessage(chatId, messageId);
    return { success: result };
  }

  @Delete(':chatId/messages')
  @SessionApiParam
  @ApiOperation({ summary: 'Clears all messages from the chat' })
  @ChatIdApiParam
  clearMessages(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
  ) {
    return session.clearMessages(chatId);
  }

  @Delete(':chatId/messages/:messageId')
  @SessionApiParam
  @ChatIdApiParam
  @MessageIdApiParam
  @ApiOperation({ summary: 'Deletes a message from the chat' })
  deleteMessage(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
    @Param('messageId') messageId: string,
  ) {
    return session.deleteMessage(chatId, messageId);
  }

  @Put(':chatId/messages/:messageId')
  @SessionApiParam
  @ChatIdApiParam
  @MessageIdApiParam
  @ApiOperation({ summary: 'Edits a message in the chat' })
  editMessage(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
    @Param('messageId') messageId: string,
    @Body() body: EditMessageRequest,
  ) {
    return session.editMessage(chatId, messageId, body);
  }

  @Post(':chatId/archive')
  @SessionApiParam
  @ChatIdApiParam
  @ApiOperation({ summary: 'Archive the chat' })
  archiveChat(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
  ) {
    return session.chatsArchiveChat(chatId);
  }

  @Post(':chatId/unarchive')
  @SessionApiParam
  @ChatIdApiParam
  @ApiOperation({ summary: 'Unarchive the chat' })
  unarchiveChat(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
  ) {
    return session.chatsUnarchiveChat(chatId);
  }

  @Post(':chatId/unread')
  @SessionApiParam
  @ChatIdApiParam
  @ApiOperation({ summary: 'Unread the chat' })
  unreadChat(
    @WorkingSessionParam session: WhatsappSession,
    @Param('chatId') chatId: string,
  ) {
    return session.chatsUnreadChat(chatId);
  }
}

export { ChatsController };