diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index ad08fdd..83be76b 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -4,7 +4,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, assertMediaContent, bindWaitForEvent, decryptMediaRetryData, encodeWAMessage, encryptMediaRetryRequest, encryptSenderKeyMsgSignalProto, encryptSignalProto, extractDeviceJids, generateMessageID, generateWAMessage, getUrlFromDirectPath, getWAUploadToServer, jidToSignalProtocolAddress, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils' +import { aggregateMessageKeysNotFromMe, assertMediaContent, bindWaitForEvent, decryptMediaRetryData, encodeWAMessage, encryptMediaRetryRequest, encryptSenderKeyMsgSignalProto, encryptSignalProto, extractDeviceJids, generateMessageID, generateWAMessage, getStatusCodeForMediaRetry, getUrlFromDirectPath, getWAUploadToServer, jidToSignalProtocolAddress, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils' import { getUrlInfo } from '../Utils/link-preview' import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidEncode, jidNormalizedUser, JidWithDevice, S_WHATSAPP_NET } from '../WABinary' import { makeGroupsSocket } from './groups' @@ -543,7 +543,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { const resultStr = proto.MediaRetryNotification.MediaRetryNotificationResultType[media.result] throw new Boom( `Media re-upload failed by device (${resultStr})`, - { data: media, statusCode: MEDIA_RETRY_STATUS_MAP[media.result] || 404 } + { data: media, statusCode: getStatusCodeForMediaRetry(media.result) || 404 } ) } @@ -623,11 +623,4 @@ export const makeMessagesSocket = (config: SocketConfig) => { } } } -} - -const MEDIA_RETRY_STATUS_MAP = { - [proto.MediaRetryNotification.MediaRetryNotificationResultType.SUCCESS]: 200, - [proto.MediaRetryNotification.MediaRetryNotificationResultType.DECRYPTION_ERROR]: 412, - [proto.MediaRetryNotification.MediaRetryNotificationResultType.NOT_FOUND]: 404, - [proto.MediaRetryNotification.MediaRetryNotificationResultType.GENERAL_ERROR]: 418, -} as const +} \ No newline at end of file diff --git a/src/Utils/messages-media.ts b/src/Utils/messages-media.ts index 0529842..2fb9394 100644 --- a/src/Utils/messages-media.ts +++ b/src/Utils/messages-media.ts @@ -633,7 +633,10 @@ export const decodeMediaRetryNode = (node: BinaryNode) => { const errorNode = getBinaryNodeChild(node, 'error') if(errorNode) { const errorCode = +errorNode.attrs.code - event.error = new Boom(`Failed to re-upload media (${errorCode})`, { data: errorNode.attrs }) + event.error = new Boom( + `Failed to re-upload media (${errorCode})`, + { data: errorNode.attrs, statusCode: getStatusCodeForMediaRetry(errorCode) } + ) } else { const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt') const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p') @@ -656,4 +659,13 @@ export const decryptMediaRetryData = ( const retryKey = getMediaRetryKey(mediaKey) const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId)) return proto.MediaRetryNotification.decode(plaintext) -} \ No newline at end of file +} + +export const getStatusCodeForMediaRetry = (code: number) => MEDIA_RETRY_STATUS_MAP[code] + +const MEDIA_RETRY_STATUS_MAP = { + [proto.MediaRetryNotification.MediaRetryNotificationResultType.SUCCESS]: 200, + [proto.MediaRetryNotification.MediaRetryNotificationResultType.DECRYPTION_ERROR]: 412, + [proto.MediaRetryNotification.MediaRetryNotificationResultType.NOT_FOUND]: 404, + [proto.MediaRetryNotification.MediaRetryNotificationResultType.GENERAL_ERROR]: 418, +} as const