Spaces:
Runtime error
Runtime error
| 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'; | |
| ('api_key') | |
| ('api/:session/chats') | |
| ('💬 Chats') | |
| (new ValidationPipe({ transform: true })) | |
| class ChatsController { | |
| constructor(private manager: SessionManager) {} | |
| ('') | |
| ({ summary: 'Get chats' }) | |
| getChats( | |
| session: WhatsappSession, | |
| () pagination: ChatsPaginationParams, | |
| ) { | |
| return session.getChats(pagination); | |
| } | |
| ('overview') | |
| ({ | |
| 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', | |
| }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| getChatsOverview( | |
| session: WhatsappSession, | |
| () pagination: OverviewPaginationParams, | |
| () filter: OverviewFilter, | |
| ): Promise<ChatSummary[]> { | |
| return session.getChatsOverview(pagination, filter); | |
| } | |
| ('overview') | |
| ({ | |
| summary: | |
| 'Get chats overview. Use POST if you have too many "ids" params - GET can limit it', | |
| }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| postChatsOverview( | |
| session: WhatsappSession, | |
| () body: OverviewBodyRequest, | |
| ): Promise<ChatSummary[]> { | |
| return session.getChatsOverview(body.pagination, body.filter); | |
| } | |
| (':chatId') | |
| ({ summary: 'Deletes the chat' }) | |
| deleteChat( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ) { | |
| return session.deleteChat(chatId); | |
| } | |
| (':chatId/picture') | |
| ({ summary: 'Gets chat picture' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async getChatPicture( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| () query: ChatPictureQuery, | |
| ): Promise<ChatPictureResponse> { | |
| const url = await session.getContactProfilePicture(chatId, query.refresh); | |
| return { url: url }; | |
| } | |
| (':chatId/messages') | |
| ({ summary: 'Gets messages in the chat' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| getChatMessages( | |
| () query: GetChatMessagesQuery, | |
| () filter: GetChatMessagesFilter, | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ) { | |
| filter = transformAck(filter); | |
| return session.getChatMessages(chatId, query, filter); | |
| } | |
| (':chatId/messages/read') | |
| ({ summary: 'Read unread messages in the chat' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| readChatMessages( | |
| () query: ReadChatMessagesQuery, | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ): Promise<ReadChatMessagesResponse> { | |
| return session.readChatMessages(chatId, query); | |
| } | |
| (':chatId/messages/:messageId') | |
| ({ summary: 'Gets message by id' }) | |
| async getChatMessage( | |
| () query: GetChatMessageQuery, | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ('messageId') messageId: string, | |
| ) { | |
| const message = await session.getChatMessage(chatId, messageId, query); | |
| if (!message) { | |
| throw new NotFoundException('Message not found'); | |
| } | |
| return message; | |
| } | |
| (':chatId/messages/:messageId/pin') | |
| ({ summary: 'Pins a message in the chat' }) | |
| async pinMessage( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ('messageId') messageId: string, | |
| () body: PinMessageRequest, | |
| ) { | |
| const result = await session.pinMessage(chatId, messageId, body.duration); | |
| return { success: result }; | |
| } | |
| (':chatId/messages/:messageId/unpin') | |
| ({ summary: 'Unpins a message in the chat' }) | |
| async unpinMessage( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ('messageId') messageId: string, | |
| ) { | |
| const result = await session.unpinMessage(chatId, messageId); | |
| return { success: result }; | |
| } | |
| (':chatId/messages') | |
| ({ summary: 'Clears all messages from the chat' }) | |
| clearMessages( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ) { | |
| return session.clearMessages(chatId); | |
| } | |
| (':chatId/messages/:messageId') | |
| ({ summary: 'Deletes a message from the chat' }) | |
| deleteMessage( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ('messageId') messageId: string, | |
| ) { | |
| return session.deleteMessage(chatId, messageId); | |
| } | |
| (':chatId/messages/:messageId') | |
| ({ summary: 'Edits a message in the chat' }) | |
| editMessage( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ('messageId') messageId: string, | |
| () body: EditMessageRequest, | |
| ) { | |
| return session.editMessage(chatId, messageId, body); | |
| } | |
| (':chatId/archive') | |
| ({ summary: 'Archive the chat' }) | |
| archiveChat( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ) { | |
| return session.chatsArchiveChat(chatId); | |
| } | |
| (':chatId/unarchive') | |
| ({ summary: 'Unarchive the chat' }) | |
| unarchiveChat( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ) { | |
| return session.chatsUnarchiveChat(chatId); | |
| } | |
| (':chatId/unread') | |
| ({ summary: 'Unread the chat' }) | |
| unreadChat( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ) { | |
| return session.chatsUnreadChat(chatId); | |
| } | |
| } | |
| export { ChatsController }; | |