fix: use correct status code for throwing media error

This commit is contained in:
Adhiraj Singh
2022-07-04 11:24:34 +05:30
parent 200b75d53e
commit de95694266
2 changed files with 17 additions and 12 deletions

View File

@@ -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
}

View File

@@ -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)
}
}
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