From ba5539b4e2d0eea5210e9253e7102409d19a7548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Souza?= Date: Wed, 10 Jul 2024 15:25:19 -0300 Subject: [PATCH] feat: new parameters to fetchAllGroups --- src/api/abstract/abstract.router.ts | 29 ++++++++++--------- src/api/controllers/group.controller.ts | 6 ++-- src/api/dto/group.dto.ts | 4 ++- src/api/routes/group.router.ts | 9 +++--- .../channels/whatsapp.baileys.service.ts | 14 ++++++--- src/docs/swagger.yaml | 13 +++++++++ src/validate/validate.schema.ts | 12 ++++++++ 7 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/api/abstract/abstract.router.ts b/src/api/abstract/abstract.router.ts index 18770ffa..bc993ec6 100644 --- a/src/api/abstract/abstract.router.ts +++ b/src/api/abstract/abstract.router.ts @@ -6,8 +6,9 @@ import { validate } from 'jsonschema'; import { Logger } from '../../config/logger.config'; import { BadRequestException } from '../../exceptions'; -import { GetParticipant, GroupInvite } from '../dto/group.dto'; +import { FetchAllGroupsDto, GroupInvite } from '../dto/group.dto'; import { InstanceDto } from '../dto/instance.dto'; +import { fetchAllGroupsSchema } from '../../validate/validate.schema'; type DataValidate = { request: Request; @@ -186,25 +187,25 @@ export abstract class RouterBroker { return await execute(instance, ref); } - public async getParticipantsValidate(args: DataValidate) { + public async fetchAllGroupsValidate(args: DataValidate) { const { request, ClassRef, schema, execute } = args; - - const getParticipants = request.query as unknown as GetParticipant; - - if (!getParticipants?.getParticipants) { + + const fetchAllGroups = request.query as unknown as FetchAllGroupsDto; + + if (!fetchAllGroups?.getParticipants) { throw new BadRequestException('The getParticipants needs to be informed in the query'); } - + const instance = request.params as unknown as InstanceDto; const body = request.body; - + const ref = new ClassRef(); - - Object.assign(body, getParticipants); + + Object.assign(body, fetchAllGroups); Object.assign(ref, body); - - const v = validate(ref, schema); - + + const v = validate(ref, schema || fetchAllGroupsSchema); + if (!v.valid) { const message: any[] = v.errors.map(({ property, stack, schema }) => { let message: string; @@ -221,7 +222,7 @@ export abstract class RouterBroker { logger.error([...message]); throw new BadRequestException(...message); } - + return await execute(instance, ref); } } diff --git a/src/api/controllers/group.controller.ts b/src/api/controllers/group.controller.ts index e452bc0c..5bb2544a 100644 --- a/src/api/controllers/group.controller.ts +++ b/src/api/controllers/group.controller.ts @@ -2,7 +2,7 @@ import { Logger } from '../../config/logger.config'; import { AcceptGroupInvite, CreateGroupDto, - GetParticipant, + FetchAllGroupsDto, GroupDescriptionDto, GroupInvite, GroupJid, @@ -46,9 +46,9 @@ export class GroupController { return await this.waMonitor.waInstances[instance.instanceName].findGroup(groupJid); } - public async fetchAllGroups(instance: InstanceDto, getPaticipants: GetParticipant) { + public async fetchAllGroups(instance: InstanceDto, fetchAllGroupsData: FetchAllGroupsDto) { logger.verbose('requested fetchAllGroups from ' + instance.instanceName + ' instance'); - return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups(getPaticipants); + return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups(fetchAllGroupsData); } public async inviteCode(instance: InstanceDto, groupJid: GroupJid) { diff --git a/src/api/dto/group.dto.ts b/src/api/dto/group.dto.ts index 293329d2..c3730fd0 100644 --- a/src/api/dto/group.dto.ts +++ b/src/api/dto/group.dto.ts @@ -24,8 +24,10 @@ export class GroupJid { groupJid: string; } -export class GetParticipant { +export class FetchAllGroupsDto { getParticipants: string; + getProfilePicture?: string; + delay?: string; } export class GroupInvite { diff --git a/src/api/routes/group.router.ts b/src/api/routes/group.router.ts index 244dfee2..9c19762b 100644 --- a/src/api/routes/group.router.ts +++ b/src/api/routes/group.router.ts @@ -4,6 +4,7 @@ import { Logger } from '../../config/logger.config'; import { AcceptGroupInviteSchema, createGroupSchema, + fetchAllGroupsSchema, getParticipantsSchema, groupInviteSchema, groupJidSchema, @@ -19,7 +20,7 @@ import { RouterBroker } from '../abstract/abstract.router'; import { AcceptGroupInvite, CreateGroupDto, - GetParticipant, + FetchAllGroupsDto, GroupDescriptionDto, GroupInvite, GroupJid, @@ -127,10 +128,10 @@ export class GroupRouter extends RouterBroker { logger.verbose('request query: '); logger.verbose(req.query); - const response = await this.getParticipantsValidate({ + const response = await this.fetchAllGroupsValidate({ request: req, - schema: getParticipantsSchema, - ClassRef: GetParticipant, + schema: fetchAllGroupsSchema, + ClassRef: FetchAllGroupsDto, execute: (instance, data) => groupController.fetchAllGroups(instance, data), }); diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index d31e0238..f5423aa5 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -92,7 +92,7 @@ import { import { AcceptGroupInvite, CreateGroupDto, - GetParticipant, + FetchAllGroupsDto, GroupDescriptionDto, GroupInvite, GroupJid, @@ -3293,7 +3293,7 @@ export class BaileysStartupService extends ChannelStartupService { } } - public async fetchAllGroups(getParticipants: GetParticipant) { + public async fetchAllGroups(fetchAllGroupsData: FetchAllGroupsDto) { if (this.localSettings.groups_ignore === true) { return; } @@ -3303,7 +3303,13 @@ export class BaileysStartupService extends ChannelStartupService { const fetch = Object.values(await this.client.groupFetchAllParticipating()); let groups = []; for (const group of fetch) { - const picture = await this.profilePicture(group.id); + let picture; + if (!fetchAllGroupsData.getProfilePicture || fetchAllGroupsData.getProfilePicture != 'false') { + picture = await this.profilePicture(group.id); + } + if (fetchAllGroupsData.delay) { + await delay(+fetchAllGroupsData.delay); + } const result = { id: group.id, @@ -3320,7 +3326,7 @@ export class BaileysStartupService extends ChannelStartupService { announce: group.announce, }; - if (getParticipants.getParticipants == 'true') { + if (fetchAllGroupsData.getParticipants == 'true') { result['participants'] = group.participants; } diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index 8b73c5d9..e172b420 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -1646,6 +1646,19 @@ paths: schema: type: boolean description: "- required - Indicates whether to retrieve the participants of the group." + - name: getProfilePicture + in: query + schema: + type: boolean + required: false + description: "Indicates whether to retrieve the profile pictures of the participants." + - name: delay + in: query + schema: + type: string + pattern: '^\\d+$' + required: false + description: "Optional delay in milliseconds before the response is sent." - name: instanceName in: path schema: diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 8f7cb1a0..8db69269 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -818,6 +818,18 @@ export const getParticipantsSchema: JSONSchema7 = { ...isNotEmpty('getParticipants'), }; +export const fetchAllGroupsSchema: JSONSchema7 = { + $id: v4(), + type: 'object', + properties: { + getParticipants: { type: 'string', enum: ['true', 'false'] }, + getProfilePicture: { type: 'string', enum: ['true', 'false'] }, + delay: { type: 'string', pattern: '^\\d+$' }, + }, + required: ['getParticipants'], + ...isNotEmpty('getParticipants'), +}; + export const groupSendInviteSchema: JSONSchema7 = { $id: v4(), type: 'object',