feat: add media retry for MD

This commit is contained in:
Adhiraj Singh
2022-06-01 13:04:45 +05:30
parent 4c51800b09
commit 06ce5f9be0
4 changed files with 152 additions and 8 deletions

View File

@@ -2,7 +2,7 @@
import { proto } from '../../WAProto'
import { KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
import { BaileysEventMap, MessageReceiptType, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageStubType } from '../Types'
import { debouncedTimeout, decodeMessageStanza, delay, encodeBigEndian, generateSignalPubKey, getCallStatusFromNode, getNextPreKeys, getStatusFromReceiptType, normalizeMessageContent, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils'
import { debouncedTimeout, decodeMediaRetryNode, decodeMessageStanza, delay, encodeBigEndian, generateSignalPubKey, getCallStatusFromNode, getNextPreKeys, getStatusFromReceiptType, normalizeMessageContent, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils'
import { makeKeyedMutex, makeMutex } from '../Utils/make-mutex'
import processMessage, { cleanMessage } from '../Utils/process-message'
import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
@@ -171,12 +171,13 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const normalizedContent = !!msg.message ? normalizeMessageContent(msg.message) : undefined
const isAnyHistoryMsg = !!normalizedContent?.protocolMessage?.historySyncNotification
if(isAnyHistoryMsg) {
const jid = jidEncode(jidDecode(msg.key.remoteJid!).user, 'c.us')
await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync')
// we only want to sync app state once we've all the history
// restart the app state sync timeout
logger.debug('restarting app sync timeout')
appStateSyncTimeout.start()
const jid = jidEncode(jidDecode(msg.key.remoteJid!).user, 'c.us')
await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync')
}
return newEvents
@@ -208,8 +209,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const processNotification = async(node: BinaryNode): Promise<Partial<proto.IWebMessageInfo>> => {
const result: Partial<proto.IWebMessageInfo> = { }
const [child] = getAllBinaryNodeChildren(node)
const nodeType = node.attrs.type
if(node.attrs.type === 'w:gp2') {
if(nodeType === 'w:gp2') {
switch (child?.tag) {
case 'create':
const metadata = extractGroupMetadata(child)
@@ -271,6 +273,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
break
}
} else if(nodeType === 'mediaretry') {
const event = decodeMediaRetryNode(node)
ev.emit('messages.media-update', [event])
} else {
switch (child.tag) {
case 'devices':

View File

@@ -3,7 +3,7 @@ import NodeCache from 'node-cache'
import { proto } from '../../WAProto'
import { WA_DEFAULT_EPHEMERAL } from '../Defaults'
import { AnyMessageContent, MediaConnInfo, MessageReceiptType, MessageRelayOptions, MiscMessageGenerationOptions, SocketConfig, WAMessageKey } from '../Types'
import { aggregateMessageKeysNotFromMe, encodeWAMessage, encryptSenderKeyMsgSignalProto, encryptSignalProto, extractDeviceJids, generateMessageID, generateWAMessage, getWAUploadToServer, jidToSignalProtocolAddress, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils'
import { aggregateMessageKeysNotFromMe, assertMediaContent, bindWaitForEvent, decryptMediaRetryData, encodeWAMessage, encryptMediaRetryRequest, encryptSenderKeyMsgSignalProto, encryptSignalProto, extractDeviceJids, generateMessageID, generateWAMessage, getUrlFromDirectPath, getWAUploadToServer, jidToSignalProtocolAddress, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils'
import { getUrlInfo } from '../Utils/link-preview'
import { BinaryNode, BinaryNodeAttributes, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidEncode, jidNormalizedUser, JidWithDevice, reduceBinaryNodeToDictionary, S_WHATSAPP_NET } from '../WABinary'
import { makeGroupsSocket } from './groups'
@@ -454,6 +454,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
const waUploadToServer = getWAUploadToServer(config, refreshMediaConn)
const waitForMsgMediaUpdate = bindWaitForEvent(ev, 'messages.media-update')
return {
...sock,
assertSessions,
@@ -464,6 +466,43 @@ export const makeMessagesSocket = (config: SocketConfig) => {
refreshMediaConn,
waUploadToServer,
fetchPrivacySettings,
updateMediaMessage: async(message: proto.IWebMessageInfo) => {
const content = assertMediaContent(message.message)
const mediaKey = content.mediaKey!
const meId = authState.creds.me!.id
const node = encryptMediaRetryRequest(message.key, mediaKey, meId)
let error: Error | undefined = undefined
await Promise.all(
[
sendNode(node),
waitForMsgMediaUpdate(update => {
const result = update.find(c => c.key.id === message.key.id)
if(result) {
if(result.error) {
error = result.error
} else {
try {
const media = decryptMediaRetryData(result.media!, mediaKey, result.key.id)
content.directPath = media.directPath
content.url = getUrlFromDirectPath(content.directPath!)
} catch(err) {
error = err
}
}
return true
}
})
]
)
if(error) {
throw error
}
return message
},
sendMessage: async(
jid: string,
content: AnyMessageContent,