Nicer message deletion + catch profile picture update errors

This commit is contained in:
Adhiraj Singh
2020-12-18 12:35:24 +05:30
parent ae0f3be3d3
commit ccab24cdb2
4 changed files with 17 additions and 9 deletions

View File

@@ -51,6 +51,11 @@ WAConnectionTest('Groups', (conn) => {
it('should send a message on the group', async () => { it('should send a message on the group', async () => {
await sendAndRetreiveMessage(conn, 'Hello!', MessageType.text, {}, gid) await sendAndRetreiveMessage(conn, 'Hello!', MessageType.text, {}, gid)
}) })
it('should delete a message on the group', async () => {
const message = await sendAndRetreiveMessage(conn, 'Hello!', MessageType.text, {}, gid)
await delay(1500)
await conn.deleteMessage(message.key)
})
it('should quote a message on the group', async () => { it('should quote a message on the group', async () => {
const {messages} = await conn.loadMessages (gid, 100) const {messages} = await conn.loadMessages (gid, 100)
const quotableMessage = messages.find (m => m.message) const quotableMessage = messages.find (m => m.message)

View File

@@ -291,7 +291,7 @@ export class WAConnection extends Base {
// profile picture updates // profile picture updates
this.on('CB:Cmd,type:picture', async json => { this.on('CB:Cmd,type:picture', async json => {
const jid = whatsappID(json[1].jid) const jid = whatsappID(json[1].jid)
const imgUrl = await this.getProfilePicture(jid) const imgUrl = await this.getProfilePicture(jid).catch(() => '')
const contact = this.contacts[jid] const contact = this.contacts[jid]
if (contact) contact.imgUrl = imgUrl if (contact) contact.imgUrl = imgUrl

View File

@@ -290,14 +290,17 @@ export class WAConnection extends Base {
* @param id the person or group where you're trying to delete the message * @param id the person or group where you're trying to delete the message
* @param messageKey key of the message you want to delete * @param messageKey key of the message you want to delete
*/ */
async deleteMessage (id: string, messageKey: WAMessageKey) { async deleteMessage (k: string | WAMessageKey, messageKey?: WAMessageKey) {
if (typeof k === 'object') {
messageKey = k
}
const json: WAMessageContent = { const json: WAMessageContent = {
protocolMessage: { protocolMessage: {
key: messageKey, key: messageKey,
type: WAMessageProto.ProtocolMessage.ProtocolMessageType.REVOKE type: WAMessageProto.ProtocolMessage.ProtocolMessageType.REVOKE
} }
} }
const waMessage = this.prepareMessageFromContent (id, json, {}) const waMessage = this.prepareMessageFromContent (messageKey.remoteJid, json, {})
await this.relayWAMessage (waMessage) await this.relayWAMessage (waMessage)
return waMessage return waMessage
} }

View File

@@ -10,6 +10,7 @@ import { Agent } from 'https'
import Decoder from '../Binary/Decoder' import Decoder from '../Binary/Decoder'
import { MessageType, HKDFInfoKeys, MessageOptions, WAChat, WAMessageContent, BaileysError, WAMessageProto, TimedOutError, CancelledError, WAGenericMediaMessage, WAMessage, WAMessageKey } from './Constants' import { MessageType, HKDFInfoKeys, MessageOptions, WAChat, WAMessageContent, BaileysError, WAMessageProto, TimedOutError, CancelledError, WAGenericMediaMessage, WAMessage, WAMessageKey } from './Constants'
import KeyedDB from '@adiwajshing/keyed-db' import KeyedDB from '@adiwajshing/keyed-db'
import { Response } from 'node-fetch'
const platformMap = { const platformMap = {
'aix': 'AIX', 'aix': 'AIX',
@@ -274,7 +275,7 @@ export async function generateThumbnail(buffer: Buffer, mediaType: MessageType,
* Decode a media message (video, image, document, audio) & return decrypted buffer * Decode a media message (video, image, document, audio) & return decrypted buffer
* @param message the media message you want to decode * @param message the media message you want to decode
*/ */
export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchRequest: (host: string, method: string) => any) { export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchRequest: (host: string, method: string) => Promise<Response>) {
/* /*
One can infer media type from the key in the message One can infer media type from the key in the message
it is usually written as [mediaType]Message. Eg. imageMessage, audioMessage etc. it is usually written as [mediaType]Message. Eg. imageMessage, audioMessage etc.
@@ -287,7 +288,7 @@ export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchR
throw new BaileysError('cannot decode text message', message) throw new BaileysError('cannot decode text message', message)
} }
if (type === MessageType.location || type === MessageType.liveLocation) { if (type === MessageType.location || type === MessageType.liveLocation) {
return new Buffer(message[type].jpegThumbnail) return Buffer.from(message[type].jpegThumbnail)
} }
let messageContent: WAGenericMediaMessage let messageContent: WAGenericMediaMessage
if (message.productMessage) { if (message.productMessage) {
@@ -300,11 +301,10 @@ export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchR
// download the message // download the message
const fetched = await fetchRequest(messageContent.url, 'GET') const fetched = await fetchRequest(messageContent.url, 'GET')
const buffer = await fetched.buffer() if (fetched.status >= 400) {
throw new BaileysError ('Invalid code (' + fetched.status + ') returned. File has possibly been deleted from WA servers. Run `client.updateMediaMessage()` to refresh the url', {status: fetched.status})
if (buffer.length <= 10) {
throw new BaileysError ('Empty buffer returned. File has possibly been deleted from WA servers. Run `client.updateMediaMessage()` to refresh the url', {status: 404})
} }
const buffer = await fetched.buffer()
const decryptedMedia = (type: MessageType) => { const decryptedMedia = (type: MessageType) => {
// get the keys to decrypt the message // get the keys to decrypt the message