Skip to content

Commit 52fa931

Browse files
committed
feat(channel): add support for @newsletter in sendMessage and findChannels
1 parent cd800f2 commit 52fa931

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

src/api/controllers/chat.controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ export class ChatController {
113113
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
114114
return await this.waMonitor.waInstances[instanceName].blockUser(data);
115115
}
116+
117+
public async fetchChannels({ instanceName }: InstanceDto, query: Query<Contact>) {
118+
return await this.waMonitor.waInstances[instanceName].fetchChannels(query);
119+
}
116120
}

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

@@ -5119,4 +5136,51 @@ export class BaileysStartupService extends ChannelStartupService {
51195136
},
51205137
};
51215138
}
5139+
public async fetchChannels(query: Query<Contact>) {
5140+
const page = Number((query as any)?.page ?? 1);
5141+
const limit = Number((query as any)?.limit ?? (query as any)?.rows ?? 50);
5142+
const skip = (page - 1) * limit;
5143+
5144+
const messages = await this.prismaRepository.message.findMany({
5145+
where: {
5146+
instanceId: this.instanceId,
5147+
AND: [{ key: { path: ['remoteJid'], not: null } }],
5148+
},
5149+
orderBy: { messageTimestamp: 'desc' },
5150+
select: {
5151+
key: true,
5152+
messageTimestamp: true,
5153+
},
5154+
});
5155+
5156+
const channelMap = new Map<string, { remoteJid: string; pushName: undefined; lastMessageTimestamp: number }>();
5157+
5158+
for (const msg of messages) {
5159+
const key = msg.key as any;
5160+
const remoteJid = key?.remoteJid as string | undefined;
5161+
if (!remoteJid || !isJidNewsletter(remoteJid)) continue;
5162+
5163+
if (!channelMap.has(remoteJid)) {
5164+
channelMap.set(remoteJid, {
5165+
remoteJid,
5166+
pushName: undefined, // Push name is never stored for channels, so we set it as undefined
5167+
lastMessageTimestamp: msg.messageTimestamp,
5168+
});
5169+
}
5170+
}
5171+
5172+
const allChannels = Array.from(channelMap.values());
5173+
5174+
const total = allChannels.length;
5175+
const pages = Math.ceil(total / limit);
5176+
const records = allChannels.slice(skip, skip + limit);
5177+
5178+
return {
5179+
total,
5180+
pages,
5181+
currentPage: page,
5182+
limit,
5183+
records,
5184+
};
5185+
}
51225186
}

src/api/routes/chat.router.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ export class ChatRouter extends RouterBroker {
281281
});
282282

283283
return res.status(HttpStatus.CREATED).json(response);
284+
})
285+
.post(this.routerPath('findChannels'), ...guards, async (req, res) => {
286+
const response = await this.dataValidate({
287+
request: req,
288+
schema: contactValidateSchema,
289+
ClassRef: Query<Contact>,
290+
execute: (instance, query) => chatController.fetchChannels(instance, query as any),
291+
});
292+
293+
return res.status(HttpStatus.OK).json(response);
284294
});
285295
}
286296

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)