diff --git a/src/api/labels.controller.ts b/src/api/labels.controller.ts index fc4abb06..27407f99 100644 --- a/src/api/labels.controller.ts +++ b/src/api/labels.controller.ts @@ -1,8 +1,8 @@ -import { Controller, Get } from '@nestjs/common'; +import { Body, Controller, Get, Param, Put } from '@nestjs/common'; import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; import { SessionApiParam, SessionParam } from '@waha/api/helpers'; import { WhatsappSession } from '@waha/core/abc/session.abc'; -import { Label } from '@waha/structures/labels.dto'; +import { Label, LabelID, SetLabelsRequest } from '@waha/structures/labels.dto'; import { SessionManager } from '../core/abc/manager.abc'; @@ -18,4 +18,35 @@ export class LabelsController { getAll(@SessionParam session: WhatsappSession): Promise { return session.getLabels(); } + + @Get('/chats/:chatId') + @SessionApiParam + @ApiOperation({ summary: 'Get labels for the chat' }) + getChatLabels( + @SessionParam session: WhatsappSession, + @Param('chatId') chatId: string, + ): Promise { + return session.getChatLabels(chatId); + } + + @Put('/chats/:chatId') + @SessionApiParam + @ApiOperation({ summary: 'Save labels for the chat' }) + putChatLabels( + @SessionParam session: WhatsappSession, + @Param('chatId') chatId: string, + @Body() request: SetLabelsRequest, + ) { + return session.putLabelsToChat(chatId, request.labels); + } + + @Get('/:labelId/chats') + @SessionApiParam + @ApiOperation({ summary: 'Get chats by label' }) + getChatsByLabel( + @SessionParam session: WhatsappSession, + @Param('labelId') labelId: string, + ) { + return session.getChatsByLabelId(labelId); + } } diff --git a/src/core/abc/session.abc.ts b/src/core/abc/session.abc.ts index cfc7c0fc..a40e0aa5 100644 --- a/src/core/abc/session.abc.ts +++ b/src/core/abc/session.abc.ts @@ -4,7 +4,7 @@ import { ListChannelsQuery, } from '@waha/structures/channels.dto'; import { GetChatsQuery } from '@waha/structures/chats.dto'; -import { Label } from '@waha/structures/labels.dto'; +import { Label, LabelID } from '@waha/structures/labels.dto'; import { LoggerBuilder } from '@waha/utils/logging'; import { EventEmitter } from 'events'; import * as fs from 'fs'; @@ -341,6 +341,18 @@ export abstract class WhatsappSession { throw new NotImplementedByEngineError(); } + public getChatsByLabelId(labelId: string) { + throw new NotImplementedByEngineError(); + } + + public getChatLabels(chatId: string): Promise { + throw new NotImplementedByEngineError(); + } + + public putLabelsToChat(chatId: string, labels: LabelID[]) { + throw new NotImplementedByEngineError(); + } + /** * Contacts methods */ diff --git a/src/core/engines/webjs/session.webjs.core.ts b/src/core/engines/webjs/session.webjs.core.ts index 30bafad4..aa85b673 100644 --- a/src/core/engines/webjs/session.webjs.core.ts +++ b/src/core/engines/webjs/session.webjs.core.ts @@ -51,7 +51,7 @@ import { ParticipantsRequest, SettingsSecurityChangeInfo, } from '@waha/structures/groups.dto'; -import { Label } from '@waha/structures/labels.dto'; +import { Label, LabelID } from '@waha/structures/labels.dto'; import { WAMessage, WAMessageReaction } from '@waha/structures/responses.dto'; import { MeInfo } from '@waha/structures/sessions.dto'; import { WAMessageRevokedBody } from '@waha/structures/webhooks.dto'; @@ -492,6 +492,22 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { return labels.map(this.toLabel); } + public getChatsByLabelId(labelId: string) { + return this.whatsapp.getChatsByLabelId(labelId); + } + + public async getChatLabels(chatId: string): Promise { + const id = this.ensureSuffix(chatId); + const labels = await this.whatsapp.getChatLabels(id); + return labels.map(this.toLabel); + } + + public async putLabelsToChat(chatId: string, labels: LabelID[]) { + const labelIds = labels.map((label) => label.id); + const chatIds = [this.ensureSuffix(chatId)]; + await this.whatsapp.addOrRemoveLabels(labelIds, chatIds); + } + protected toLabel(label: WEBJSLabel): Label { return { id: label.id, diff --git a/src/structures/labels.dto.ts b/src/structures/labels.dto.ts index 2e935ead..1a7c6b0c 100644 --- a/src/structures/labels.dto.ts +++ b/src/structures/labels.dto.ts @@ -19,3 +19,15 @@ export class Label { }) color: number; } + +export class LabelID { + @ApiProperty({ + example: '1', + description: 'Label ID', + }) + id: string; +} + +export class SetLabelsRequest { + labels: LabelID[]; +}