feat: add "strictNullChecks"

This commit is contained in:
Adhiraj Singh
2022-07-08 10:38:25 +05:30
parent 7426b7aa2f
commit 40a1e268aa
42 changed files with 350 additions and 339 deletions

View File

@@ -75,13 +75,17 @@ export const prepareWAMessageMedia = async(
) => {
const logger = options.logger
let mediaType: typeof MEDIA_KEYS[number]
let mediaType: typeof MEDIA_KEYS[number] | undefined
for(const key of MEDIA_KEYS) {
if(key in message) {
mediaType = key
}
}
if(!mediaType) {
throw new Boom('Invalid media type', { statusCode: 400 })
}
const uploadData: MediaUploadData = {
...message,
media: message[mediaType]
@@ -106,15 +110,14 @@ export const prepareWAMessageMedia = async(
// check for cache hit
if(cacheableKey) {
const mediaBuff: Buffer = options.mediaCache!.get(cacheableKey)
const mediaBuff = options.mediaCache!.get<Buffer>(cacheableKey)
if(mediaBuff) {
logger?.debug({ cacheableKey }, 'got media cache hit')
const obj = WAProto.Message.decode(mediaBuff)
const key = `${mediaType}Message`
delete uploadData.media
Object.assign(obj[key], { ...uploadData })
Object.assign(obj[key], { ...uploadData, media: undefined })
return obj
}
@@ -153,12 +156,12 @@ export const prepareWAMessageMedia = async(
(async() => {
try {
if(requiresThumbnailComputation) {
uploadData.jpegThumbnail = await generateThumbnail(bodyPath, mediaType as any, options)
uploadData.jpegThumbnail = await generateThumbnail(bodyPath!, mediaType as any, options)
logger?.debug('generated thumbnail')
}
if(requiresDurationComputation) {
uploadData.seconds = await getAudioDuration(bodyPath)
uploadData.seconds = await getAudioDuration(bodyPath!)
logger?.debug('computed audio duration')
}
} catch(error) {
@@ -177,8 +180,6 @@ export const prepareWAMessageMedia = async(
}
)
delete uploadData.media
const obj = WAProto.Message.fromObject({
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject(
{
@@ -189,13 +190,14 @@ export const prepareWAMessageMedia = async(
fileSha256,
fileLength,
mediaKeyTimestamp: unixTimestampSeconds(),
...uploadData
...uploadData,
media: undefined
}
)
})
if(cacheableKey) {
logger.debug({ cacheableKey }, 'set cache')
logger?.debug({ cacheableKey }, 'set cache')
options.mediaCache!.set(cacheableKey, WAProto.Message.encode(obj).finish())
}
@@ -232,8 +234,8 @@ export const generateForwardMessageContent = (
}
// hacky copy
content = normalizeMessageContent(message.message)
content = proto.Message.decode(proto.Message.encode(content).finish())
content = normalizeMessageContent(content)
content = proto.Message.decode(proto.Message.encode(content!).finish())
let key = Object.keys(content)[0] as MessageType
@@ -428,8 +430,8 @@ export const generateWAMessageFromContent = (
if(quoted) {
const participant = quoted.key.fromMe ? userJid : (quoted.participant || quoted.key.participant || quoted.key.remoteJid)
let quotedMsg = normalizeMessageContent(quoted.message)
const msgType = getContentType(quotedMsg)
let quotedMsg = normalizeMessageContent(quoted.message)!
const msgType = getContentType(quotedMsg)!
// strip any redundant properties
quotedMsg = proto.Message.fromObject({ [msgType]: quotedMsg[msgType] })
@@ -439,7 +441,7 @@ export const generateWAMessageFromContent = (
}
const contextInfo: proto.IContextInfo = message[key].contextInfo || { }
contextInfo.participant = jidNormalizedUser(participant)
contextInfo.participant = jidNormalizedUser(participant!)
contextInfo.stanzaId = quoted.key.id
contextInfo.quotedMessage = quotedMsg
@@ -521,7 +523,7 @@ export const getContentType = (content: WAProto.IMessage | undefined) => {
* @param content
* @returns
*/
export const normalizeMessageContent = (content: WAMessageContent | undefined): WAMessageContent => {
export const normalizeMessageContent = (content: WAMessageContent | null | undefined): WAMessageContent | undefined => {
content = content?.ephemeralMessage?.message?.viewOnceMessage?.message ||
content?.ephemeralMessage?.message ||
content?.viewOnceMessage?.message ||
@@ -614,13 +616,13 @@ export const aggregateMessageKeysNotFromMe = (keys: proto.IMessageKey[]) => {
const uqKey = `${remoteJid}:${participant || ''}`
if(!keyMap[uqKey]) {
keyMap[uqKey] = {
jid: remoteJid,
participant,
jid: remoteJid!,
participant: participant!,
messageIds: []
}
}
keyMap[uqKey].messageIds.push(id)
keyMap[uqKey].messageIds.push(id!)
}
}
@@ -650,7 +652,7 @@ export const downloadMediaMessage = async(
if(ctx) {
if(axios.isAxiosError(error)) {
// check if the message requires a reupload
if(REUPLOAD_REQUIRED_STATUS.includes(error.response?.status)) {
if(REUPLOAD_REQUIRED_STATUS.includes(error.response?.status!)) {
ctx.logger.info({ key: message.key }, 'sending reupload media request...')
// request reupload
message = await ctx.reuploadRequest(message)
@@ -670,9 +672,9 @@ export const downloadMediaMessage = async(
}
const contentType = getContentType(mContent)
const mediaType = contentType.replace('Message', '') as MediaType
const media = mContent[contentType]
if(typeof media !== 'object' || !('url' in media)) {
const mediaType = contentType?.replace('Message', '') as MediaType
const media = mContent[contentType!]
if(!media || typeof media !== 'object' || !('url' in media)) {
throw new Boom(`"${contentType}" message is not a media message`)
}
@@ -691,7 +693,7 @@ export const downloadMediaMessage = async(
}
/** Checks whether the given message is a media message; if it is returns the inner content */
export const assertMediaContent = (content: proto.IMessage) => {
export const assertMediaContent = (content: proto.IMessage | null | undefined) => {
content = extractMessageContent(content)
const mediaContent = content?.documentMessage
|| content?.imageMessage