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