Skip to content

Commit 6ede76f

Browse files
Merge branch 'develop' into develop
2 parents 2fee505 + 604c9f9 commit 6ede76f

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

src/api/controllers/chat.controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,8 @@ export class ChatController {
121121
remoteJid: data.remoteJid,
122122
};
123123
return await this.waMonitor.waInstances[instanceName].baileysDecryptPollVote(pollCreationMessageKey);
124+
125+
public async fetchChannels({ instanceName }: InstanceDto, query: Query<Contact>) {
126+
return await this.waMonitor.waInstances[instanceName].fetchChannels(query);
124127
}
125128
}

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,9 +3511,24 @@ export class BaileysStartupService extends ChannelStartupService {
35113511
users: { number: string; jid: string; name?: string }[];
35123512
} = { groups: [], broadcast: [], users: [] };
35133513

3514+
const onWhatsapp: OnWhatsAppDto[] = [];
3515+
35143516
data.numbers.forEach((number) => {
35153517
const jid = createJid(number);
35163518

3519+
if (isJidNewsletter(jid)) {
3520+
onWhatsapp.push(
3521+
new OnWhatsAppDto(
3522+
jid,
3523+
true, // Newsletters are always valid
3524+
number,
3525+
undefined, // Can be fetched later if needed
3526+
'newsletter', // Indicate it's a newsletter type
3527+
),
3528+
);
3529+
return;
3530+
}
3531+
35173532
if (isJidGroup(jid)) {
35183533
jids.groups.push({ number, jid });
35193534
} else if (jid === 'status@broadcast') {
@@ -3523,8 +3538,6 @@ export class BaileysStartupService extends ChannelStartupService {
35233538
}
35243539
});
35253540

3526-
const onWhatsapp: OnWhatsAppDto[] = [];
3527-
35283541
// BROADCAST
35293542
onWhatsapp.push(...jids.broadcast.map(({ jid, number }) => new OnWhatsAppDto(jid, false, number)));
35303543

@@ -4700,6 +4713,10 @@ export class BaileysStartupService extends ChannelStartupService {
47004713
}
47014714
}
47024715

4716+
if (isJidNewsletter(message.key.remoteJid) && message.key.fromMe) {
4717+
messageRaw.status = status[3]; // DELIVERED MESSAGE TO NEWSLETTER CHANNEL
4718+
}
4719+
47034720
return messageRaw;
47044721
}
47054722

@@ -5365,5 +5382,52 @@ export class BaileysStartupService extends ChannelStartupService {
53655382
this.logger.error(`Error decrypting poll votes: ${error}`);
53665383
throw new InternalServerErrorException('Error decrypting poll votes', error.toString());
53675384
}
5385+
5386+
public async fetchChannels(query: Query<Contact>) {
5387+
const page = Number((query as any)?.page ?? 1);
5388+
const limit = Number((query as any)?.limit ?? (query as any)?.rows ?? 50);
5389+
const skip = (page - 1) * limit;
5390+
5391+
const messages = await this.prismaRepository.message.findMany({
5392+
where: {
5393+
instanceId: this.instanceId,
5394+
AND: [{ key: { path: ['remoteJid'], not: null } }],
5395+
},
5396+
orderBy: { messageTimestamp: 'desc' },
5397+
select: {
5398+
key: true,
5399+
messageTimestamp: true,
5400+
},
5401+
});
5402+
5403+
const channelMap = new Map<string, { remoteJid: string; pushName: undefined; lastMessageTimestamp: number }>();
5404+
5405+
for (const msg of messages) {
5406+
const key = msg.key as any;
5407+
const remoteJid = key?.remoteJid as string | undefined;
5408+
if (!remoteJid || !isJidNewsletter(remoteJid)) continue;
5409+
5410+
if (!channelMap.has(remoteJid)) {
5411+
channelMap.set(remoteJid, {
5412+
remoteJid,
5413+
pushName: undefined, // Push name is never stored for channels, so we set it as undefined
5414+
lastMessageTimestamp: msg.messageTimestamp,
5415+
});
5416+
}
5417+
}
5418+
5419+
const allChannels = Array.from(channelMap.values());
5420+
5421+
const total = allChannels.length;
5422+
const pages = Math.ceil(total / limit);
5423+
const records = allChannels.slice(skip, skip + limit);
5424+
5425+
return {
5426+
total,
5427+
pages,
5428+
currentPage: page,
5429+
limit,
5430+
records,
5431+
};
53685432
}
53695433
}

src/api/routes/chat.router.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ export class ChatRouter extends RouterBroker {
290290
schema: decryptPollVoteSchema,
291291
ClassRef: DecryptPollVoteDto,
292292
execute: (instance, data) => chatController.decryptPollVote(instance, data),
293+
.post(this.routerPath('findChannels'), ...guards, async (req, res) => {
294+
const response = await this.dataValidate({
295+
request: req,
296+
schema: contactValidateSchema,
297+
ClassRef: Query<Contact>,
298+
execute: (instance, query) => chatController.fetchChannels(instance, query as any),
293299
});
294300

295301
return res.status(HttpStatus.OK).json(response);

src/utils/createJid.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ function formatBRNumber(jid: string) {
3535
export function createJid(number: string): string {
3636
number = number.replace(/:\d+/, '');
3737

38-
if (number.includes('@g.us') || number.includes('@s.whatsapp.net') || number.includes('@lid')) {
38+
if (
39+
number.includes('@g.us') ||
40+
number.includes('@s.whatsapp.net') ||
41+
number.includes('@lid') ||
42+
number.includes('@newsletter')
43+
) {
3944
return number;
4045
}
4146

0 commit comments

Comments
 (0)