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,6 +189,74 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
}
const handleGroupNotification = (
participant: string,
child: BinaryNode,
msg: Partial<proto.IWebMessageInfo>
) => {
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<proto.IWebMessageInfo> = { }
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)