feat: new parameters to fetchAllGroups

This commit is contained in:
João Victor Souza 2024-07-10 15:25:19 -03:00
parent 549ecd8801
commit ba5539b4e2
7 changed files with 61 additions and 26 deletions

View File

@ -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<T> = {
request: Request;
@ -186,25 +187,25 @@ export abstract class RouterBroker {
return await execute(instance, ref);
}
public async getParticipantsValidate<T>(args: DataValidate<T>) {
public async fetchAllGroupsValidate<T>(args: DataValidate<T>) {
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);
}
}

View File

@ -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) {

View File

@ -24,8 +24,10 @@ export class GroupJid {
groupJid: string;
}
export class GetParticipant {
export class FetchAllGroupsDto {
getParticipants: string;
getProfilePicture?: string;
delay?: string;
}
export class GroupInvite {

View File

@ -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<GetParticipant>({
const response = await this.fetchAllGroupsValidate<FetchAllGroupsDto>({
request: req,
schema: getParticipantsSchema,
ClassRef: GetParticipant,
schema: fetchAllGroupsSchema,
ClassRef: FetchAllGroupsDto,
execute: (instance, data) => groupController.fetchAllGroups(instance, data),
});

View File

@ -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;
}

View File

@ -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:

View File

@ -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',