From da359aaee2f884631e0781e6eaa5eeffb642fe46 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 27 Sep 2022 10:51:38 +0530 Subject: [PATCH] refactor: handle group stuff in a separate function --- src/Socket/messages-recv.ts | 131 +++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 62 deletions(-) diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 207869e..9d5515e 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -189,6 +189,74 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { } } + const handleGroupNotification = ( + participant: string, + child: BinaryNode, + msg: Partial + ) => { + switch (child?.tag) { + case 'create': + const metadata = extractGroupMetadata(child) + + msg.messageStubType = WAMessageStubType.GROUP_CREATE + msg.messageStubParameters = [metadata.subject] + msg.key = { participant: metadata.owner } + + ev.emit('chats.upsert', [{ + id: metadata.id, + name: metadata.subject, + conversationTimestamp: metadata.creation, + }]) + ev.emit('groups.upsert', [metadata]) + break + case 'ephemeral': + case 'not_ephemeral': + msg.message = { + protocolMessage: { + type: proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING, + ephemeralExpiration: +(child.attrs.expiration || 0) + } + } + break + case 'promote': + case 'demote': + case 'remove': + case 'add': + case 'leave': + const stubType = `GROUP_PARTICIPANT_${child.tag!.toUpperCase()}` + msg.messageStubType = WAMessageStubType[stubType] + + const participants = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid) + if( + participants.length === 1 && + // if recv. "remove" message and sender removed themselves + // mark as left + areJidsSameUser(participants[0], participant) && + child.tag === 'remove' + ) { + msg.messageStubType = WAMessageStubType.GROUP_PARTICIPANT_LEAVE + } + + msg.messageStubParameters = participants + break + case 'subject': + msg.messageStubType = WAMessageStubType.GROUP_CHANGE_SUBJECT + msg.messageStubParameters = [ child.attrs.subject ] + break + case 'announcement': + case 'not_announcement': + msg.messageStubType = WAMessageStubType.GROUP_CHANGE_ANNOUNCE + msg.messageStubParameters = [ (child.tag === 'announcement') ? 'on' : 'off' ] + break + case 'locked': + case 'unlocked': + msg.messageStubType = WAMessageStubType.GROUP_CHANGE_RESTRICT + msg.messageStubParameters = [ (child.tag === 'locked') ? 'on' : 'off' ] + break + + } + } + const processNotification = async(node: BinaryNode) => { const result: Partial = { } const [child] = getAllBinaryNodeChildren(node) @@ -196,68 +264,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { switch (nodeType) { case 'w:gp2': - switch (child?.tag) { - case 'create': - const metadata = extractGroupMetadata(child) - - result.messageStubType = WAMessageStubType.GROUP_CREATE - result.messageStubParameters = [metadata.subject] - result.key = { participant: metadata.owner } - - ev.emit('chats.upsert', [{ - id: metadata.id, - name: metadata.subject, - conversationTimestamp: metadata.creation, - }]) - ev.emit('groups.upsert', [metadata]) - break - case 'ephemeral': - case 'not_ephemeral': - result.message = { - protocolMessage: { - type: proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING, - ephemeralExpiration: +(child.attrs.expiration || 0) - } - } - break - case 'promote': - case 'demote': - case 'remove': - case 'add': - case 'leave': - const stubType = `GROUP_PARTICIPANT_${child.tag!.toUpperCase()}` - result.messageStubType = WAMessageStubType[stubType] - - const participants = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid) - if( - participants.length === 1 && - // if recv. "remove" message and sender removed themselves - // mark as left - areJidsSameUser(participants[0], node.attrs.participant) && - child.tag === 'remove' - ) { - result.messageStubType = WAMessageStubType.GROUP_PARTICIPANT_LEAVE - } - - result.messageStubParameters = participants - break - case 'subject': - result.messageStubType = WAMessageStubType.GROUP_CHANGE_SUBJECT - result.messageStubParameters = [ child.attrs.subject ] - break - case 'announcement': - case 'not_announcement': - result.messageStubType = WAMessageStubType.GROUP_CHANGE_ANNOUNCE - result.messageStubParameters = [ (child.tag === 'announcement') ? 'on' : 'off' ] - break - case 'locked': - case 'unlocked': - result.messageStubType = WAMessageStubType.GROUP_CHANGE_RESTRICT - result.messageStubParameters = [ (child.tag === 'locked') ? 'on' : 'off' ] - break - - } - + handleGroupNotification(node.attrs.participant, child, result) break case 'mediaretry': const event = decodeMediaRetryNode(node)