refactor: handle group stuff in a separate function

This commit is contained in:
Adhiraj Singh
2022-09-27 10:51:38 +05:30
parent 58b5df4d4b
commit da359aaee2

View File

@@ -189,20 +189,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
} }
const processNotification = async(node: BinaryNode) => { const handleGroupNotification = (
const result: Partial<proto.IWebMessageInfo> = { } participant: string,
const [child] = getAllBinaryNodeChildren(node) child: BinaryNode,
const nodeType = node.attrs.type msg: Partial<proto.IWebMessageInfo>
) => {
switch (nodeType) {
case 'w:gp2':
switch (child?.tag) { switch (child?.tag) {
case 'create': case 'create':
const metadata = extractGroupMetadata(child) const metadata = extractGroupMetadata(child)
result.messageStubType = WAMessageStubType.GROUP_CREATE msg.messageStubType = WAMessageStubType.GROUP_CREATE
result.messageStubParameters = [metadata.subject] msg.messageStubParameters = [metadata.subject]
result.key = { participant: metadata.owner } msg.key = { participant: metadata.owner }
ev.emit('chats.upsert', [{ ev.emit('chats.upsert', [{
id: metadata.id, id: metadata.id,
@@ -213,7 +211,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
break break
case 'ephemeral': case 'ephemeral':
case 'not_ephemeral': case 'not_ephemeral':
result.message = { msg.message = {
protocolMessage: { protocolMessage: {
type: proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING, type: proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
ephemeralExpiration: +(child.attrs.expiration || 0) ephemeralExpiration: +(child.attrs.expiration || 0)
@@ -226,38 +224,47 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
case 'add': case 'add':
case 'leave': case 'leave':
const stubType = `GROUP_PARTICIPANT_${child.tag!.toUpperCase()}` const stubType = `GROUP_PARTICIPANT_${child.tag!.toUpperCase()}`
result.messageStubType = WAMessageStubType[stubType] msg.messageStubType = WAMessageStubType[stubType]
const participants = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid) const participants = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid)
if( if(
participants.length === 1 && participants.length === 1 &&
// if recv. "remove" message and sender removed themselves // if recv. "remove" message and sender removed themselves
// mark as left // mark as left
areJidsSameUser(participants[0], node.attrs.participant) && areJidsSameUser(participants[0], participant) &&
child.tag === 'remove' child.tag === 'remove'
) { ) {
result.messageStubType = WAMessageStubType.GROUP_PARTICIPANT_LEAVE msg.messageStubType = WAMessageStubType.GROUP_PARTICIPANT_LEAVE
} }
result.messageStubParameters = participants msg.messageStubParameters = participants
break break
case 'subject': case 'subject':
result.messageStubType = WAMessageStubType.GROUP_CHANGE_SUBJECT msg.messageStubType = WAMessageStubType.GROUP_CHANGE_SUBJECT
result.messageStubParameters = [ child.attrs.subject ] msg.messageStubParameters = [ child.attrs.subject ]
break break
case 'announcement': case 'announcement':
case 'not_announcement': case 'not_announcement':
result.messageStubType = WAMessageStubType.GROUP_CHANGE_ANNOUNCE msg.messageStubType = WAMessageStubType.GROUP_CHANGE_ANNOUNCE
result.messageStubParameters = [ (child.tag === 'announcement') ? 'on' : 'off' ] msg.messageStubParameters = [ (child.tag === 'announcement') ? 'on' : 'off' ]
break break
case 'locked': case 'locked':
case 'unlocked': case 'unlocked':
result.messageStubType = WAMessageStubType.GROUP_CHANGE_RESTRICT msg.messageStubType = WAMessageStubType.GROUP_CHANGE_RESTRICT
result.messageStubParameters = [ (child.tag === 'locked') ? 'on' : 'off' ] msg.messageStubParameters = [ (child.tag === 'locked') ? 'on' : 'off' ]
break break
} }
}
const processNotification = async(node: BinaryNode) => {
const result: Partial<proto.IWebMessageInfo> = { }
const [child] = getAllBinaryNodeChildren(node)
const nodeType = node.attrs.type
switch (nodeType) {
case 'w:gp2':
handleGroupNotification(node.attrs.participant, child, result)
break break
case 'mediaretry': case 'mediaretry':
const event = decodeMediaRetryNode(node) const event = decodeMediaRetryNode(node)