mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor: handle group stuff in a separate function
This commit is contained in:
@@ -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 processNotification = async(node: BinaryNode) => {
|
||||||
const result: Partial<proto.IWebMessageInfo> = { }
|
const result: Partial<proto.IWebMessageInfo> = { }
|
||||||
const [child] = getAllBinaryNodeChildren(node)
|
const [child] = getAllBinaryNodeChildren(node)
|
||||||
@@ -196,68 +264,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
switch (nodeType) {
|
switch (nodeType) {
|
||||||
case 'w:gp2':
|
case 'w:gp2':
|
||||||
switch (child?.tag) {
|
handleGroupNotification(node.attrs.participant, child, result)
|
||||||
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
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
break
|
break
|
||||||
case 'mediaretry':
|
case 'mediaretry':
|
||||||
const event = decodeMediaRetryNode(node)
|
const event = decodeMediaRetryNode(node)
|
||||||
|
|||||||
Reference in New Issue
Block a user