project: Move to ESM Modules
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { URL } from 'url'
|
||||
import { SocketConfig } from '../../Types'
|
||||
import type { SocketConfig } from '../../Types'
|
||||
|
||||
export abstract class AbstractSocketClient extends EventEmitter {
|
||||
abstract get isOpen(): boolean
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GetCatalogOptions, ProductCreate, ProductUpdate, SocketConfig } from '../Types'
|
||||
import type { GetCatalogOptions, ProductCreate, ProductUpdate, SocketConfig } from '../Types'
|
||||
import {
|
||||
parseCatalogNode,
|
||||
parseCollectionsNode,
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
toProductNode,
|
||||
uploadingNecessaryImagesOfProduct
|
||||
} from '../Utils/business'
|
||||
import { BinaryNode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
|
||||
import { type BinaryNode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
|
||||
import { getBinaryNodeChild } from '../WABinary/generic-utils'
|
||||
import { makeMessagesRecvSocket } from './messages-recv'
|
||||
|
||||
|
||||
+24
-20
@@ -2,8 +2,7 @@ import NodeCache from '@cacheable/node-cache'
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { proto } from '../../WAProto'
|
||||
import { DEFAULT_CACHE_TTLS, PROCESSABLE_HISTORY_TYPES } from '../Defaults'
|
||||
import {
|
||||
ALL_WA_PATCH_NAMES,
|
||||
import type {
|
||||
BotListInfo,
|
||||
ChatModification,
|
||||
ChatMutation,
|
||||
@@ -25,10 +24,11 @@ import {
|
||||
WAPrivacyValue,
|
||||
WAReadReceiptsValue
|
||||
} from '../Types'
|
||||
import { LabelActionBody } from '../Types/Label'
|
||||
import { ALL_WA_PATCH_NAMES } from '../Types'
|
||||
import type { LabelActionBody } from '../Types/Label'
|
||||
import {
|
||||
chatModificationToAppPatch,
|
||||
ChatMutationMap,
|
||||
type ChatMutationMap,
|
||||
decodePatches,
|
||||
decodeSyncdSnapshot,
|
||||
encodeSyncdPatch,
|
||||
@@ -41,7 +41,7 @@ import {
|
||||
import { makeMutex } from '../Utils/make-mutex'
|
||||
import processMessage from '../Utils/process-message'
|
||||
import {
|
||||
BinaryNode,
|
||||
type BinaryNode,
|
||||
getBinaryNodeChild,
|
||||
getBinaryNodeChildren,
|
||||
jidDecode,
|
||||
@@ -205,8 +205,8 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
if (section.attrs.type === 'all') {
|
||||
for (const bot of getBinaryNodeChildren(section, 'bot')) {
|
||||
botList.push({
|
||||
jid: bot.attrs.jid,
|
||||
personaId: bot.attrs['persona_id']
|
||||
jid: bot.attrs.jid!,
|
||||
personaId: bot.attrs['persona_id']!
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -219,7 +219,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
const usyncQuery = new USyncQuery().withContactProtocol().withLIDProtocol()
|
||||
|
||||
for (const jid of jids) {
|
||||
const phone = `+${jid.replace('+', '').split('@')[0].split(':')[0]}`
|
||||
const phone = `+${jid.replace('+', '').split('@')[0]?.split(':')[0]}`
|
||||
usyncQuery.withUser(new USyncUser().withPhone(phone))
|
||||
}
|
||||
|
||||
@@ -271,16 +271,18 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
|
||||
if (jidNormalizedUser(jid) !== jidNormalizedUser(authState.creds.me!.id)) {
|
||||
targetJid = jidNormalizedUser(jid) // in case it is someone other than us
|
||||
} else {
|
||||
targetJid = undefined
|
||||
}
|
||||
|
||||
const { img } = await generateProfilePicture(content, dimensions)
|
||||
await query({
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
target: targetJid,
|
||||
to: S_WHATSAPP_NET,
|
||||
type: 'set',
|
||||
xmlns: 'w:profile:picture'
|
||||
xmlns: 'w:profile:picture',
|
||||
...(targetJid ? { target: targetJid } : {})
|
||||
},
|
||||
content: [
|
||||
{
|
||||
@@ -303,15 +305,17 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
|
||||
if (jidNormalizedUser(jid) !== jidNormalizedUser(authState.creds.me!.id)) {
|
||||
targetJid = jidNormalizedUser(jid) // in case it is someone other than us
|
||||
} else {
|
||||
targetJid = undefined
|
||||
}
|
||||
|
||||
await query({
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
target: targetJid,
|
||||
to: S_WHATSAPP_NET,
|
||||
type: 'set',
|
||||
xmlns: 'w:profile:picture'
|
||||
xmlns: 'w:profile:picture',
|
||||
...(targetJid ? { target: targetJid } : {})
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -477,7 +481,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
const states = {} as { [T in WAPatchName]: LTHashState }
|
||||
const nodes: BinaryNode[] = []
|
||||
|
||||
for (const name of collectionsToHandle) {
|
||||
for (const name of collectionsToHandle as Set<WAPatchName>) {
|
||||
const result = await authState.keys.get('app-state-sync-version', [name])
|
||||
let state = result[name]
|
||||
|
||||
@@ -569,7 +573,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
// collection is done with sync
|
||||
collectionsToHandle.delete(name)
|
||||
}
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
// if retry attempts overshoot
|
||||
// or key not found
|
||||
const isIrrecoverableError =
|
||||
@@ -595,7 +599,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
|
||||
const { onMutation } = newAppStateChunkHandler(isInitialSync)
|
||||
for (const key in globalMutationMap) {
|
||||
onMutation(globalMutationMap[key])
|
||||
onMutation(globalMutationMap[key]!)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -689,7 +693,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
const jid = attrs.from
|
||||
const participant = attrs.participant || attrs.from
|
||||
|
||||
if (shouldIgnoreJid(jid) && jid !== '@s.whatsapp.net') {
|
||||
if (shouldIgnoreJid(jid!) && jid !== '@s.whatsapp.net') {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -700,12 +704,12 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
}
|
||||
} else if (Array.isArray(content)) {
|
||||
const [firstChild] = content
|
||||
let type = firstChild.tag as WAPresence
|
||||
let type = firstChild!.tag as WAPresence
|
||||
if (type === 'paused') {
|
||||
type = 'available'
|
||||
}
|
||||
|
||||
if (firstChild.attrs?.media === 'audio') {
|
||||
if (firstChild!.attrs?.media === 'audio') {
|
||||
type = 'recording'
|
||||
}
|
||||
|
||||
@@ -715,7 +719,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
if (presence) {
|
||||
ev.emit('presence.update', { id: jid, presences: { [participant]: presence } })
|
||||
ev.emit('presence.update', { id: jid!, presences: { [participant!]: presence } })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -790,7 +794,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
logger
|
||||
)
|
||||
for (const key in mutationMap) {
|
||||
onMutation(mutationMap[key])
|
||||
onMutation(mutationMap[key]!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-16
@@ -1,15 +1,9 @@
|
||||
import { proto } from '../../WAProto'
|
||||
import {
|
||||
GroupMetadata,
|
||||
GroupParticipant,
|
||||
ParticipantAction,
|
||||
SocketConfig,
|
||||
WAMessageKey,
|
||||
WAMessageStubType
|
||||
} from '../Types'
|
||||
import type { GroupMetadata, GroupParticipant, ParticipantAction, SocketConfig, WAMessageKey } from '../Types'
|
||||
import { WAMessageStubType } from '../Types'
|
||||
import { generateMessageIDV2, unixTimestampSeconds } from '../Utils'
|
||||
import {
|
||||
BinaryNode,
|
||||
type BinaryNode,
|
||||
getBinaryNodeChild,
|
||||
getBinaryNodeChildren,
|
||||
getBinaryNodeChildString,
|
||||
@@ -317,22 +311,22 @@ export const extractGroupMetadata = (result: BinaryNode) => {
|
||||
desc = getBinaryNodeChildString(descChild, 'body')
|
||||
descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined
|
||||
descOwnerJid = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined
|
||||
descTime = +descChild.attrs.t
|
||||
descTime = +descChild.attrs.t!
|
||||
descId = descChild.attrs.id
|
||||
}
|
||||
|
||||
const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us')
|
||||
const groupId = group.attrs.id!.includes('@') ? group.attrs.id : jidEncode(group.attrs.id!, 'g.us')
|
||||
const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration
|
||||
const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add'
|
||||
const metadata: GroupMetadata = {
|
||||
id: groupId,
|
||||
id: groupId!,
|
||||
addressingMode: group.attrs.addressing_mode as 'pn' | 'lid',
|
||||
subject: group.attrs.subject,
|
||||
subject: group.attrs.subject!,
|
||||
subjectOwner: group.attrs.s_o,
|
||||
subjectOwnerJid: group.attrs.s_o_pn,
|
||||
subjectTime: +group.attrs.s_t,
|
||||
subjectTime: +group.attrs.s_t!,
|
||||
size: getBinaryNodeChildren(group, 'participant').length,
|
||||
creation: +group.attrs.creation,
|
||||
creation: +group.attrs.creation!,
|
||||
owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
|
||||
ownerJid: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
|
||||
owner_country_code: group.attrs.creator_country_code,
|
||||
@@ -350,7 +344,7 @@ export const extractGroupMetadata = (result: BinaryNode) => {
|
||||
memberAddMode,
|
||||
participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
|
||||
return {
|
||||
id: attrs.jid,
|
||||
id: attrs.jid!,
|
||||
jid: isJidUser(attrs.jid) ? attrs.jid : jidNormalizedUser(attrs.phone_number),
|
||||
lid: isLidUser(attrs.jid) ? attrs.jid : attrs.lid,
|
||||
admin: (attrs.type || null) as GroupParticipant['admin']
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
import { DEFAULT_CONNECTION_CONFIG } from '../Defaults'
|
||||
import { UserFacingSocketConfig } from '../Types'
|
||||
import type { UserFacingSocketConfig } from '../Types'
|
||||
import { makeBusinessSocket } from './business'
|
||||
|
||||
// export the last socket layer
|
||||
|
||||
+55
-48
@@ -1,20 +1,19 @@
|
||||
import NodeCache from '@cacheable/node-cache'
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { randomBytes } from 'crypto'
|
||||
import Long = require('long')
|
||||
import Long from 'long'
|
||||
import { proto } from '../../WAProto'
|
||||
import { DEFAULT_CACHE_TTLS, KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
|
||||
import {
|
||||
import type {
|
||||
MessageReceiptType,
|
||||
MessageRelayOptions,
|
||||
MessageUserReceipt,
|
||||
SocketConfig,
|
||||
WACallEvent,
|
||||
WAMessageKey,
|
||||
WAMessageStatus,
|
||||
WAMessageStubType,
|
||||
WAPatchName
|
||||
} from '../Types'
|
||||
import { WAMessageStatus, WAMessageStubType } from '../Types'
|
||||
import {
|
||||
aesDecryptCTR,
|
||||
aesEncryptGCM,
|
||||
@@ -42,7 +41,7 @@ import {
|
||||
import { makeMutex } from '../Utils/make-mutex'
|
||||
import {
|
||||
areJidsSameUser,
|
||||
BinaryNode,
|
||||
type BinaryNode,
|
||||
getAllBinaryNodeChildren,
|
||||
getBinaryNodeChild,
|
||||
getBinaryNodeChildBuffer,
|
||||
@@ -108,8 +107,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const stanza: BinaryNode = {
|
||||
tag: 'ack',
|
||||
attrs: {
|
||||
id: attrs.id,
|
||||
to: attrs.from,
|
||||
id: attrs.id!,
|
||||
to: attrs.from!,
|
||||
class: tag
|
||||
}
|
||||
}
|
||||
@@ -194,15 +193,15 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
attrs: {
|
||||
id: msgId,
|
||||
type: 'retry',
|
||||
to: node.attrs.from
|
||||
to: node.attrs.from!
|
||||
},
|
||||
content: [
|
||||
{
|
||||
tag: 'retry',
|
||||
attrs: {
|
||||
count: retryCount.toString(),
|
||||
id: node.attrs.id,
|
||||
t: node.attrs.t,
|
||||
id: node.attrs.id!,
|
||||
t: node.attrs.t!,
|
||||
v: '1'
|
||||
}
|
||||
},
|
||||
@@ -226,7 +225,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const { update, preKeys } = await getNextPreKeys(authState, 1)
|
||||
|
||||
const [keyId] = Object.keys(preKeys)
|
||||
const key = preKeys[+keyId]
|
||||
const key = preKeys[+keyId!]
|
||||
|
||||
const content = receipt.content! as BinaryNode[]
|
||||
content.push({
|
||||
@@ -235,7 +234,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
content: [
|
||||
{ tag: 'type', attrs: {}, content: Buffer.from(KEY_BUNDLE_TYPE) },
|
||||
{ tag: 'identity', attrs: {}, content: identityKey.public },
|
||||
xmppPreKey(key, +keyId),
|
||||
xmppPreKey(key!, +keyId!),
|
||||
xmppSignedPreKey(signedPreKey),
|
||||
{ tag: 'device-identity', attrs: {}, content: deviceIdentity }
|
||||
]
|
||||
@@ -254,7 +253,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const from = node.attrs.from
|
||||
if (from === S_WHATSAPP_NET) {
|
||||
const countChild = getBinaryNodeChild(node, 'count')
|
||||
const count = +countChild!.attrs.value
|
||||
const count = +countChild!.attrs.value!
|
||||
const shouldUploadMorePreKeys = count < MIN_PREKEY_COUNT
|
||||
|
||||
logger.debug({ count, shouldUploadMorePreKeys }, 'recv pre-key count')
|
||||
@@ -307,7 +306,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
break
|
||||
case 'modify':
|
||||
const oldNumber = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid)
|
||||
const oldNumber = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid!)
|
||||
msg.messageStubParameters = oldNumber || []
|
||||
msg.messageStubType = WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER
|
||||
break
|
||||
@@ -317,9 +316,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
case 'add':
|
||||
case 'leave':
|
||||
const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`
|
||||
msg.messageStubType = WAMessageStubType[stubType]
|
||||
msg.messageStubType = WAMessageStubType[stubType as keyof typeof WAMessageStubType]
|
||||
|
||||
const participants = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid)
|
||||
const participants = getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid!)
|
||||
if (
|
||||
participants.length === 1 &&
|
||||
// if recv. "remove" message and sender removed themselves
|
||||
@@ -334,7 +333,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
break
|
||||
case 'subject':
|
||||
msg.messageStubType = WAMessageStubType.GROUP_CHANGE_SUBJECT
|
||||
msg.messageStubParameters = [child.attrs.subject]
|
||||
msg.messageStubParameters = [child.attrs.subject!]
|
||||
break
|
||||
case 'description':
|
||||
const description = getBinaryNodeChild(child, 'body')?.content?.toString()
|
||||
@@ -353,7 +352,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
break
|
||||
case 'invite':
|
||||
msg.messageStubType = WAMessageStubType.GROUP_CHANGE_INVITE_LINK
|
||||
msg.messageStubParameters = [child.attrs.code]
|
||||
msg.messageStubParameters = [child.attrs.code!]
|
||||
break
|
||||
case 'member_add_mode':
|
||||
const addMode = child.content
|
||||
@@ -367,13 +366,13 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const approvalMode = getBinaryNodeChild(child, 'group_join')
|
||||
if (approvalMode) {
|
||||
msg.messageStubType = WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE
|
||||
msg.messageStubParameters = [approvalMode.attrs.state]
|
||||
msg.messageStubParameters = [approvalMode.attrs.state!]
|
||||
}
|
||||
|
||||
break
|
||||
case 'created_membership_requests':
|
||||
msg.messageStubType = WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD
|
||||
msg.messageStubParameters = [participantJid, 'created', child.attrs.request_method]
|
||||
msg.messageStubParameters = [participantJid, 'created', child.attrs.request_method!]
|
||||
break
|
||||
case 'revoked_membership_requests':
|
||||
const isDenied = areJidsSameUser(participantJid, participant)
|
||||
@@ -412,7 +411,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
await handleMexNewsletterNotification(node)
|
||||
break
|
||||
case 'w:gp2':
|
||||
handleGroupNotification(node.attrs.participant, child, result)
|
||||
handleGroupNotification(node.attrs.participant!, child!, result)
|
||||
break
|
||||
case 'mediaretry':
|
||||
const event = decodeMediaRetryNode(node)
|
||||
@@ -423,7 +422,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
break
|
||||
case 'devices':
|
||||
const devices = getBinaryNodeChildren(child, 'device')
|
||||
if (areJidsSameUser(child.attrs.jid, authState.creds.me!.id)) {
|
||||
if (areJidsSameUser(child!.attrs.jid, authState.creds.me!.id)) {
|
||||
const deviceJids = devices.map(d => d.attrs.jid)
|
||||
logger.info({ deviceJids }, 'got my own devices')
|
||||
}
|
||||
@@ -453,7 +452,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
result.messageStubType = WAMessageStubType.GROUP_CHANGE_ICON
|
||||
|
||||
if (setPicture) {
|
||||
result.messageStubParameters = [setPicture.attrs.id]
|
||||
result.messageStubParameters = [setPicture.attrs.id!]
|
||||
}
|
||||
|
||||
result.participant = node?.attrs.author
|
||||
@@ -465,9 +464,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
break
|
||||
case 'account_sync':
|
||||
if (child.tag === 'disappearing_mode') {
|
||||
const newDuration = +child.attrs.duration
|
||||
const timestamp = +child.attrs.t
|
||||
if (child!.tag === 'disappearing_mode') {
|
||||
const newDuration = +child!.attrs.duration!
|
||||
const timestamp = +child!.attrs.t!
|
||||
|
||||
logger.info({ newDuration }, 'updated account disappearing mode')
|
||||
|
||||
@@ -480,11 +479,11 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
}
|
||||
})
|
||||
} else if (child.tag === 'blocklist') {
|
||||
} else if (child!.tag === 'blocklist') {
|
||||
const blocklists = getBinaryNodeChildren(child, 'item')
|
||||
|
||||
for (const { attrs } of blocklists) {
|
||||
const blocklist = [attrs.jid]
|
||||
const blocklist = [attrs.jid!]
|
||||
const type = attrs.action === 'block' ? 'add' : 'remove'
|
||||
ev.emit('blocklist.update', { blocklist, type })
|
||||
}
|
||||
@@ -614,7 +613,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
for (const [i, msg] of msgs.entries()) {
|
||||
if (msg) {
|
||||
updateSendMessageAgainCount(ids[i], participant)
|
||||
updateSendMessageAgainCount(ids[i]!, participant)
|
||||
const msgRelayOpts: MessageRelayOptions = { messageId: ids[i] }
|
||||
|
||||
if (sendToAll) {
|
||||
@@ -622,7 +621,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
} else {
|
||||
msgRelayOpts.participant = {
|
||||
jid: participant,
|
||||
count: +retryNode.attrs.count
|
||||
count: +retryNode.attrs.count!
|
||||
}
|
||||
}
|
||||
|
||||
@@ -635,7 +634,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
const handleReceipt = async (node: BinaryNode) => {
|
||||
const { attrs, content } = node
|
||||
const isLid = attrs.from.includes('lid')
|
||||
const isLid = attrs.from!.includes('lid')
|
||||
const isNodeFromMe = areJidsSameUser(
|
||||
attrs.participant || attrs.from,
|
||||
isLid ? authState.creds.me?.lid : authState.creds.me?.id
|
||||
@@ -650,16 +649,16 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
participant: attrs.participant
|
||||
}
|
||||
|
||||
if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
|
||||
if (shouldIgnoreJid(remoteJid!) && remoteJid !== '@s.whatsapp.net') {
|
||||
logger.debug({ remoteJid }, 'ignoring receipt from jid')
|
||||
await sendMessageAck(node)
|
||||
return
|
||||
}
|
||||
|
||||
const ids = [attrs.id]
|
||||
const ids = [attrs.id!]
|
||||
if (Array.isArray(content)) {
|
||||
const items = getBinaryNodeChildren(content[0], 'item')
|
||||
ids.push(...items.map(i => i.attrs.id))
|
||||
ids.push(...items.map(i => i.attrs.id!))
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -672,7 +671,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
// or another device of ours has read some messages
|
||||
(status >= proto.WebMessageInfo.Status.SERVER_ACK || !isNodeFromMe)
|
||||
) {
|
||||
if (isJidGroup(remoteJid) || isJidStatusBroadcast(remoteJid)) {
|
||||
if (isJidGroup(remoteJid) || isJidStatusBroadcast(remoteJid!)) {
|
||||
if (attrs.participant) {
|
||||
const updateKey: keyof MessageUserReceipt =
|
||||
status === proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp'
|
||||
@@ -682,7 +681,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
key: { ...key, id },
|
||||
receipt: {
|
||||
userJid: jidNormalizedUser(attrs.participant),
|
||||
[updateKey]: +attrs.t
|
||||
[updateKey]: +attrs.t!
|
||||
}
|
||||
}))
|
||||
)
|
||||
@@ -702,12 +701,12 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
// correctly set who is asking for the retry
|
||||
key.participant = key.participant || attrs.from
|
||||
const retryNode = getBinaryNodeChild(node, 'retry')
|
||||
if (willSendMessageAgain(ids[0], key.participant)) {
|
||||
if (willSendMessageAgain(ids[0]!, key.participant!)) {
|
||||
if (key.fromMe) {
|
||||
try {
|
||||
logger.debug({ attrs, key }, 'recv retry request')
|
||||
await sendMessagesAgain(key, ids, retryNode!)
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
logger.error({ key, ids, trace: error.stack }, 'error in sending message again')
|
||||
}
|
||||
} else {
|
||||
@@ -726,7 +725,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
const handleNotification = async (node: BinaryNode) => {
|
||||
const remoteJid = node.attrs.from
|
||||
if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
|
||||
if (shouldIgnoreJid(remoteJid!) && remoteJid !== '@s.whatsapp.net') {
|
||||
logger.debug({ remoteJid, id: node.attrs.id }, 'ignored notification')
|
||||
await sendMessageAck(node)
|
||||
return
|
||||
@@ -746,7 +745,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
...(msg.key || {})
|
||||
}
|
||||
msg.participant ??= node.attrs.participant
|
||||
msg.messageTimestamp = +node.attrs.t
|
||||
msg.messageTimestamp = +node.attrs.t!
|
||||
|
||||
const fullMsg = proto.WebMessageInfo.fromObject(msg)
|
||||
await upsertMessage(fullMsg, 'append')
|
||||
@@ -759,7 +758,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
const handleMessage = async (node: BinaryNode) => {
|
||||
if (shouldIgnoreJid(node.attrs.from) && node.attrs.from !== '@s.whatsapp.net') {
|
||||
if (shouldIgnoreJid(node.attrs.from!) && node.attrs.from !== '@s.whatsapp.net') {
|
||||
logger.debug({ key: node.attrs.key }, 'ignored message')
|
||||
await sendMessageAck(node)
|
||||
return
|
||||
@@ -786,8 +785,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
logger.debug('received unavailable message, acked and requested resend from phone')
|
||||
} else {
|
||||
if (placeholderResendCache.get(node.attrs.id)) {
|
||||
placeholderResendCache.del(node.attrs.id)
|
||||
if (placeholderResendCache.get(node.attrs.id!)) {
|
||||
placeholderResendCache.del(node.attrs.id!)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -806,7 +805,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
msg.message?.protocolMessage?.type === proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER &&
|
||||
node.attrs.sender_pn
|
||||
) {
|
||||
ev.emit('chats.phoneNumberShare', { lid: node.attrs.from, jid: node.attrs.sender_pn })
|
||||
ev.emit('chats.phoneNumberShare', { lid: node.attrs.from!, jid: node.attrs.sender_pn })
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -938,14 +937,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const handleCall = async (node: BinaryNode) => {
|
||||
const { attrs } = node
|
||||
const [infoChild] = getAllBinaryNodeChildren(node)
|
||||
const callId = infoChild.attrs['call-id']
|
||||
const from = infoChild.attrs.from || infoChild.attrs['call-creator']
|
||||
if (!infoChild) {
|
||||
throw new Boom('Missing call info in call node')
|
||||
}
|
||||
|
||||
const callId = infoChild.attrs['call-id']!
|
||||
const from = infoChild.attrs.from! || infoChild.attrs['call-creator']!
|
||||
const status = getCallStatusFromNode(infoChild)
|
||||
const call: WACallEvent = {
|
||||
chatId: attrs.from,
|
||||
chatId: attrs.from!,
|
||||
from,
|
||||
id: callId,
|
||||
date: new Date(+attrs.t * 1000),
|
||||
date: new Date(+attrs.t! * 1000),
|
||||
offline: !!attrs.offline,
|
||||
status
|
||||
}
|
||||
@@ -1263,6 +1266,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
})
|
||||
|
||||
ev.on('call', ([call]) => {
|
||||
if (!call) {
|
||||
return
|
||||
}
|
||||
|
||||
// missed call + group call notification message generation
|
||||
if (call.status === 'timeout' || (call.status === 'offer' && call.isGroup)) {
|
||||
const msg: proto.IWebMessageInfo = {
|
||||
|
||||
+17
-13
@@ -2,7 +2,7 @@ import NodeCache from '@cacheable/node-cache'
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { proto } from '../../WAProto'
|
||||
import { DEFAULT_CACHE_TTLS, WA_DEFAULT_EPHEMERAL } from '../Defaults'
|
||||
import {
|
||||
import type {
|
||||
AnyMessageContent,
|
||||
MediaConnInfo,
|
||||
MessageReceiptType,
|
||||
@@ -33,8 +33,8 @@ import {
|
||||
import { getUrlInfo } from '../Utils/link-preview'
|
||||
import {
|
||||
areJidsSameUser,
|
||||
BinaryNode,
|
||||
BinaryNodeAttributes,
|
||||
type BinaryNode,
|
||||
type BinaryNodeAttributes,
|
||||
getBinaryNodeChild,
|
||||
getBinaryNodeChildren,
|
||||
isJidGroup,
|
||||
@@ -42,7 +42,7 @@ import {
|
||||
jidDecode,
|
||||
jidEncode,
|
||||
jidNormalizedUser,
|
||||
JidWithDevice,
|
||||
type JidWithDevice,
|
||||
S_WHATSAPP_NET
|
||||
} from '../WABinary'
|
||||
import { USyncQuery, USyncUser } from '../WAUSync'
|
||||
@@ -93,14 +93,14 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
},
|
||||
content: [{ tag: 'media_conn', attrs: {} }]
|
||||
})
|
||||
const mediaConnNode = getBinaryNodeChild(result, 'media_conn')
|
||||
const mediaConnNode = getBinaryNodeChild(result, 'media_conn')!
|
||||
const node: MediaConnInfo = {
|
||||
hosts: getBinaryNodeChildren(mediaConnNode, 'host').map(({ attrs }) => ({
|
||||
hostname: attrs.hostname,
|
||||
maxContentLengthBytes: +attrs.maxContentLengthBytes
|
||||
hostname: attrs.hostname!,
|
||||
maxContentLengthBytes: +attrs.maxContentLengthBytes!
|
||||
})),
|
||||
auth: mediaConnNode!.attrs.auth,
|
||||
ttl: +mediaConnNode!.attrs.ttl,
|
||||
auth: mediaConnNode.attrs.auth!,
|
||||
ttl: +mediaConnNode.attrs.ttl!,
|
||||
fetchDate: new Date()
|
||||
}
|
||||
logger.debug('fetched media conn')
|
||||
@@ -121,10 +121,14 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
messageIds: string[],
|
||||
type: MessageReceiptType
|
||||
) => {
|
||||
if (!messageIds || messageIds.length === 0) {
|
||||
throw new Boom('missing ids in receipt')
|
||||
}
|
||||
|
||||
const node: BinaryNode = {
|
||||
tag: 'receipt',
|
||||
attrs: {
|
||||
id: messageIds[0]
|
||||
id: messageIds[0]!
|
||||
}
|
||||
}
|
||||
const isReadReceipt = type === 'read' || type === 'read-self'
|
||||
@@ -226,7 +230,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
|
||||
for (const item of extracted) {
|
||||
deviceMap[item.user] = deviceMap[item.user] || []
|
||||
deviceMap[item.user].push(item)
|
||||
deviceMap[item.user]?.push(item)
|
||||
|
||||
deviceResults.push(item)
|
||||
}
|
||||
@@ -394,7 +398,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
messageContextInfo: message.messageContextInfo
|
||||
}
|
||||
|
||||
const extraAttrs = {}
|
||||
const extraAttrs: BinaryNodeAttributes = {}
|
||||
|
||||
if (participant) {
|
||||
// when the retry request is not for a group
|
||||
@@ -762,7 +766,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
content.url = getUrlFromDirectPath(content.directPath!)
|
||||
|
||||
logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful')
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
error = err
|
||||
}
|
||||
}
|
||||
|
||||
+10
-9
@@ -10,7 +10,8 @@ import {
|
||||
MIN_PREKEY_COUNT,
|
||||
NOISE_WA_HEADER
|
||||
} from '../Defaults'
|
||||
import { DisconnectReason, SocketConfig } from '../Types'
|
||||
import type { SocketConfig } from '../Types'
|
||||
import { DisconnectReason } from '../Types'
|
||||
import {
|
||||
addTransactionCapability,
|
||||
aesEncryptCTR,
|
||||
@@ -32,7 +33,7 @@ import {
|
||||
} from '../Utils'
|
||||
import {
|
||||
assertNodeErrorFree,
|
||||
BinaryNode,
|
||||
type BinaryNode,
|
||||
binaryNodeToString,
|
||||
encodeBinaryNode,
|
||||
getBinaryNodeChild,
|
||||
@@ -178,8 +179,8 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
* @param timeoutMs timeout after which the promise will reject
|
||||
*/
|
||||
const waitForMessage = async <T>(msgId: string, timeoutMs = defaultQueryTimeoutMs) => {
|
||||
let onRecv: (json) => void
|
||||
let onErr: (err) => void
|
||||
let onRecv: (json: any) => void
|
||||
let onErr: (err: Boom | Error) => void
|
||||
try {
|
||||
const result = await promiseTimeout<T>(timeoutMs, (resolve, reject) => {
|
||||
onRecv = resolve
|
||||
@@ -268,8 +269,8 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
},
|
||||
content: [{ tag: 'count', attrs: {} }]
|
||||
})
|
||||
const countChild = getBinaryNodeChild(result, 'count')
|
||||
return +countChild!.attrs.value
|
||||
const countChild = getBinaryNodeChild(result, 'count')!
|
||||
return +countChild.attrs.value!
|
||||
}
|
||||
|
||||
/** generates and uploads a set of pre-keys to the server */
|
||||
@@ -553,7 +554,7 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
ws.on('open', async () => {
|
||||
try {
|
||||
await validateConnection()
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
logger.error({ err }, 'error in validating connection')
|
||||
end(err)
|
||||
}
|
||||
@@ -571,7 +572,7 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
attrs: {
|
||||
to: S_WHATSAPP_NET,
|
||||
type: 'result',
|
||||
id: stanza.attrs.id
|
||||
id: stanza.attrs.id!
|
||||
}
|
||||
}
|
||||
await sendNode(iq)
|
||||
@@ -621,7 +622,7 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
ev.emit('connection.update', { isNewLogin: true, qr: undefined })
|
||||
|
||||
await sendNode(reply)
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
logger.info({ trace: error.stack }, 'error in pairing')
|
||||
end(error)
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { SocketConfig } from '../Types'
|
||||
import { BinaryNode, S_WHATSAPP_NET } from '../WABinary'
|
||||
import type { SocketConfig } from '../Types'
|
||||
import { type BinaryNode, S_WHATSAPP_NET } from '../WABinary'
|
||||
import { USyncQuery } from '../WAUSync'
|
||||
import { makeSocket } from './socket'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user