project: Move to ESM Modules
This commit is contained in:
+41
-32
@@ -5,7 +5,7 @@ import { promises as fs } from 'fs'
|
||||
import { type Transform } from 'stream'
|
||||
import { proto } from '../../WAProto'
|
||||
import { MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults'
|
||||
import {
|
||||
import type {
|
||||
AnyMediaMessageContent,
|
||||
AnyMessageContent,
|
||||
DownloadableMessage,
|
||||
@@ -13,27 +13,25 @@ import {
|
||||
MessageContentGenerationOptions,
|
||||
MessageGenerationOptions,
|
||||
MessageGenerationOptionsFromContent,
|
||||
MessageType,
|
||||
MessageUserReceipt,
|
||||
WAMediaUpload,
|
||||
WAMessage,
|
||||
WAMessageContent,
|
||||
WAMessageStatus,
|
||||
WAProto,
|
||||
WATextMessage
|
||||
} from '../Types'
|
||||
import { WAMessageStatus, WAProto } from '../Types'
|
||||
import { isJidGroup, isJidNewsletter, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary'
|
||||
import { sha256 } from './crypto'
|
||||
import { generateMessageIDV2, getKeyAuthor, unixTimestampSeconds } from './generics'
|
||||
import { ILogger } from './logger'
|
||||
import type { ILogger } from './logger'
|
||||
import {
|
||||
downloadContentFromMessage,
|
||||
encryptedStream,
|
||||
generateThumbnail,
|
||||
getAudioDuration,
|
||||
getAudioWaveform,
|
||||
getRawMediaUploadData,
|
||||
MediaDownloadOptions
|
||||
getRawMediaUploadData,
|
||||
type MediaDownloadOptions
|
||||
} from './messages-media'
|
||||
|
||||
type MediaUploadData = {
|
||||
@@ -86,14 +84,14 @@ export const generateLinkPreviewIfRequired = async (
|
||||
try {
|
||||
const urlInfo = await getUrlInfo(url)
|
||||
return urlInfo
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
// ignore if fails
|
||||
logger?.warn({ trace: error.stack }, 'url generation failed')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const assertColor = async color => {
|
||||
const assertColor = async (color: any) => {
|
||||
let assertedColor
|
||||
if (typeof color === 'number') {
|
||||
assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1
|
||||
@@ -127,10 +125,10 @@ export const prepareWAMessageMedia = async (
|
||||
|
||||
const uploadData: MediaUploadData = {
|
||||
...message,
|
||||
media: message[mediaType]
|
||||
media: (message as any)[mediaType]
|
||||
}
|
||||
delete uploadData[mediaType]
|
||||
|
||||
delete (uploadData as any)[mediaType]
|
||||
// check if cacheable + generate cache key
|
||||
const cacheableKey =
|
||||
typeof uploadData.media === 'object' &&
|
||||
'url' in uploadData.media &&
|
||||
@@ -154,7 +152,7 @@ export const prepareWAMessageMedia = async (
|
||||
const obj = WAProto.Message.decode(mediaBuff)
|
||||
const key = `${mediaType}Message`
|
||||
|
||||
Object.assign(obj[key], { ...uploadData, media: undefined })
|
||||
Object.assign(obj[key as keyof proto.Message]!, { ...uploadData, media: undefined })
|
||||
|
||||
return obj
|
||||
}
|
||||
@@ -262,7 +260,7 @@ export const prepareWAMessageMedia = async (
|
||||
logger?.debug('computed backgroundColor audio status')
|
||||
}
|
||||
} catch (error) {
|
||||
logger?.warn({ trace: error.stack }, 'failed to obtain extra info')
|
||||
logger?.warn({ trace: (error as any).stack }, 'failed to obtain extra info')
|
||||
}
|
||||
})()
|
||||
]).finally(async () => {
|
||||
@@ -279,7 +277,7 @@ export const prepareWAMessageMedia = async (
|
||||
})
|
||||
|
||||
const obj = WAProto.Message.fromObject({
|
||||
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
||||
[`${mediaType}Message`]: MessageTypeProto[mediaType as keyof typeof MessageTypeProto].fromObject({
|
||||
url: mediaUrl,
|
||||
directPath,
|
||||
mediaKey,
|
||||
@@ -335,9 +333,9 @@ export const generateForwardMessageContent = (message: WAMessage, forceForward?:
|
||||
content = normalizeMessageContent(content)
|
||||
content = proto.Message.decode(proto.Message.encode(content!).finish())
|
||||
|
||||
let key = Object.keys(content)[0] as MessageType
|
||||
let key = Object.keys(content)[0] as keyof proto.IMessage
|
||||
|
||||
let score = content[key].contextInfo?.forwardingScore || 0
|
||||
let score = (content?.[key] as { contextInfo: proto.IContextInfo })?.contextInfo?.forwardingScore || 0
|
||||
score += message.key.fromMe && !forceForward ? 0 : 1
|
||||
if (key === 'conversation') {
|
||||
content.extendedTextMessage = { text: content[key] }
|
||||
@@ -346,10 +344,11 @@ export const generateForwardMessageContent = (message: WAMessage, forceForward?:
|
||||
key = 'extendedTextMessage'
|
||||
}
|
||||
|
||||
const key_ = content?.[key] as { contextInfo: proto.IContextInfo }
|
||||
if (score > 0) {
|
||||
content[key].contextInfo = { forwardingScore: score, isForwarded: true }
|
||||
key_.contextInfo = { forwardingScore: score, isForwarded: true }
|
||||
} else {
|
||||
content[key].contextInfo = {}
|
||||
key_.contextInfo = {}
|
||||
}
|
||||
|
||||
return content
|
||||
@@ -403,7 +402,7 @@ export const generateWAMessageContent = async (
|
||||
}
|
||||
|
||||
if (contactLen === 1) {
|
||||
m.contactMessage = WAProto.Message.ContactMessage.fromObject(message.contacts.contacts[0])
|
||||
m.contactMessage = WAProto.Message.ContactMessage.fromObject(message.contacts.contacts[0]!)
|
||||
} else {
|
||||
m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.fromObject(message.contacts)
|
||||
}
|
||||
@@ -541,9 +540,12 @@ export const generateWAMessageContent = async (
|
||||
}
|
||||
|
||||
if ('mentions' in message && message.mentions?.length) {
|
||||
const [messageType] = Object.keys(m)
|
||||
m[messageType].contextInfo = m[messageType] || {}
|
||||
m[messageType].contextInfo.mentionedJid = message.mentions
|
||||
const messageType = Object.keys(m)[0]! as Exclude<keyof proto.IMessage, 'conversation'>
|
||||
const key = m[messageType]
|
||||
if ('contextInfo' in key!) {
|
||||
key.contextInfo = key.contextInfo || {}
|
||||
key.contextInfo.mentionedJid = message.mentions
|
||||
}
|
||||
}
|
||||
|
||||
if ('edit' in message) {
|
||||
@@ -558,9 +560,11 @@ export const generateWAMessageContent = async (
|
||||
}
|
||||
|
||||
if ('contextInfo' in message && !!message.contextInfo) {
|
||||
const [messageType] = Object.keys(m)
|
||||
m[messageType] = m[messageType] || {}
|
||||
m[messageType].contextInfo = message.contextInfo
|
||||
const messageType = Object.keys(m)[0]! as Exclude<keyof proto.IMessage, 'conversation'>
|
||||
const key = m[messageType]
|
||||
if ('contextInfo' in key!) {
|
||||
key.contextInfo = { ...key.contextInfo, ...message.contextInfo }
|
||||
}
|
||||
}
|
||||
|
||||
return WAProto.Message.fromObject(m)
|
||||
@@ -578,7 +582,7 @@ export const generateWAMessageFromContent = (
|
||||
}
|
||||
|
||||
const innerMessage = normalizeMessageContent(message)!
|
||||
const key: string = getContentType(innerMessage)!
|
||||
const key = getContentType(innerMessage)! as Exclude<keyof proto.IMessage, 'conversation'>
|
||||
const timestamp = unixTimestampSeconds(options.timestamp)
|
||||
const { quoted, userJid } = options
|
||||
|
||||
@@ -597,7 +601,8 @@ export const generateWAMessageFromContent = (
|
||||
delete quotedContent.contextInfo
|
||||
}
|
||||
|
||||
const contextInfo: proto.IContextInfo = innerMessage[key].contextInfo || {}
|
||||
const contextInfo: proto.IContextInfo =
|
||||
('contextInfo' in innerMessage[key]! && innerMessage[key]?.contextInfo) || {}
|
||||
contextInfo.participant = jidNormalizedUser(participant!)
|
||||
contextInfo.stanzaId = quoted.key.id
|
||||
contextInfo.quotedMessage = quotedMsg
|
||||
@@ -608,7 +613,10 @@ export const generateWAMessageFromContent = (
|
||||
contextInfo.remoteJid = quoted.key.remoteJid
|
||||
}
|
||||
|
||||
innerMessage[key].contextInfo = contextInfo
|
||||
if (contextInfo && innerMessage[key]) {
|
||||
/* @ts-ignore */
|
||||
innerMessage[key].contextInfo = contextInfo
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
@@ -621,8 +629,9 @@ export const generateWAMessageFromContent = (
|
||||
// newsletters don't support ephemeral messages
|
||||
!isJidNewsletter(jid)
|
||||
) {
|
||||
/* @ts-ignore */
|
||||
innerMessage[key].contextInfo = {
|
||||
...(innerMessage[key].contextInfo || {}),
|
||||
...((innerMessage[key] as any).contextInfo || {}),
|
||||
expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
|
||||
//ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
|
||||
}
|
||||
@@ -653,7 +662,7 @@ export const generateWAMessage = async (jid: string, content: AnyMessageContent,
|
||||
}
|
||||
|
||||
/** Get the key to access the true type of content */
|
||||
export const getContentType = (content: WAProto.IMessage | undefined) => {
|
||||
export const getContentType = (content: proto.IMessage | undefined) => {
|
||||
if (content) {
|
||||
const keys = Object.keys(content)
|
||||
const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage')
|
||||
@@ -837,7 +846,7 @@ export function getAggregateVotesInPollMessage(
|
||||
data = voteHashMap[hash]
|
||||
}
|
||||
|
||||
voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId))
|
||||
voteHashMap[hash]!.voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user