fix: call loadMessages multiple times

This commit is contained in:
Dayan C. Galiazzi
2020-09-07 13:01:40 -03:00
parent f9f109e134
commit 3d34a0e55c

View File

@@ -25,7 +25,7 @@ export class WAConnection extends Base {
async messageInfo (jid: string, messageID: string) { async messageInfo (jid: string, messageID: string) {
const query = ['query', {type: 'message_info', index: messageID, jid: jid, epoch: this.msgCount.toString()}, null] const query = ['query', {type: 'message_info', index: messageID, jid: jid, epoch: this.msgCount.toString()}, null]
const response = (await this.query ({json: query, binaryTags: [22, WAFlag.ignore], expect200: true}))[2] const response = (await this.query ({json: query, binaryTags: [22, WAFlag.ignore], expect200: true}))[2]
const info: MessageInfo = {reads: [], deliveries: []} const info: MessageInfo = {reads: [], deliveries: []}
if (response) { if (response) {
//console.log (response) //console.log (response)
@@ -52,7 +52,7 @@ export class WAConnection extends Base {
if (type === 'unread') await this.sendReadReceipt (jid, null, -2) if (type === 'unread') await this.sendReadReceipt (jid, null, -2)
else if (chat.count !== 0) { else if (chat.count !== 0) {
let messageID: string let messageID: string
let messages: WAMessage[] let messages: WAMessage[]
let cursor: any let cursor: any
@@ -77,7 +77,7 @@ export class WAConnection extends Base {
this.emit ('chat-update', {jid, count: chat.count}) this.emit ('chat-update', {jid, count: chat.count})
} }
/** /**
* Sends a read receipt for a given message; * Sends a read receipt for a given message;
* does not update the chat do @see chatRead * does not update the chat do @see chatRead
* @param jid the ID of the person/group whose message you want to mark read * @param jid the ID of the person/group whose message you want to mark read
* @param messageID optionally, the message ID * @param messageID optionally, the message ID
@@ -85,9 +85,9 @@ export class WAConnection extends Base {
*/ */
async sendReadReceipt(jid: string, messageID: string, count: number) { async sendReadReceipt(jid: string, messageID: string, count: number) {
const attributes = { const attributes = {
jid: jid, jid: jid,
count: count.toString(), count: count.toString(),
index: messageID, index: messageID,
owner: messageID ? 'false' : null owner: messageID ? 'false' : null
} }
const read = await this.setQuery ([['read', attributes, null]]) const read = await this.setQuery ([['read', attributes, null]])
@@ -125,16 +125,18 @@ export class WAConnection extends Base {
return (response[2] as WANode[])?.map(item => item[2] as WAMessage) || [] return (response[2] as WANode[])?.map(item => item[2] as WAMessage) || []
} }
const chat = this.chats.get (jid) const chat = this.chats.get (jid)
let messages: WAMessage[] let messages: WAMessage[]
if (!before && chat && mostRecentFirst) { if (!before && chat && mostRecentFirst) {
messages = chat.messages messages = chat.messages
if (messages.length < count) { const diff = count - messages.length
const extra = await retreive (count-messages.length, messages[0]?.key) if (diff < 0) {
messages.unshift (...extra) messages.splice(0, Math.abs(diff));
} else if (diff > 0) {
const extra = await retreive (diff, messages[0]?.key)
messages.unshift (...extra)
} }
} else messages = await retreive (count, before) } else messages = await retreive (count, before)
let cursor let cursor
if (messages[0]) cursor = { id: messages[0].key.id, fromMe: messages[0].key.fromMe } if (messages[0]) cursor = { id: messages[0].key.id, fromMe: messages[0].key.fromMe }
return {messages, cursor} return {messages, cursor}
@@ -200,7 +202,7 @@ export class WAConnection extends Base {
async messagesReceivedAfter (date: Date, onlyUnrespondedMessages = false) { async messagesReceivedAfter (date: Date, onlyUnrespondedMessages = false) {
const stamp = unixTimestampSeconds (date) const stamp = unixTimestampSeconds (date)
// find the index where the chat timestamp becomes greater // find the index where the chat timestamp becomes greater
const idx = this.chats.all ().findIndex (c => c.t < stamp) const idx = this.chats.all ().findIndex (c => c.t < stamp)
// all chats before that index -- i.e. all chats that were updated after that // all chats before that index -- i.e. all chats that were updated after that
const chats = this.chats.all ().slice (0, idx) const chats = this.chats.all ().slice (0, idx)
@@ -208,7 +210,7 @@ export class WAConnection extends Base {
await Promise.all ( await Promise.all (
chats.map (async chat => { chats.map (async chat => {
await this.findMessage (chat.jid, 5, m => { await this.findMessage (chat.jid, 5, m => {
if (toNumber(m.messageTimestamp) < stamp || (onlyUnrespondedMessages && m.key.fromMe)) return true if (toNumber(m.messageTimestamp) < stamp || (onlyUnrespondedMessages && m.key.fromMe)) return true
messages.push (m) messages.push (m)
}) })
}) })
@@ -228,7 +230,7 @@ export class WAConnection extends Base {
async generateLinkPreview (text: string) { async generateLinkPreview (text: string) {
const query = ['query', {type: 'url', url: text, epoch: this.msgCount.toString()}, null] const query = ['query', {type: 'url', url: text, epoch: this.msgCount.toString()}, null]
const response = await this.query ({json: query, binaryTags: [26, WAFlag.ignore], expect200: true}) const response = await this.query ({json: query, binaryTags: [26, WAFlag.ignore], expect200: true})
if (response[1]) response[1].jpegThumbnail = response[2] if (response[1]) response[1].jpegThumbnail = response[2]
const data = response[1] as WAUrlInfo const data = response[1] as WAUrlInfo
@@ -244,15 +246,15 @@ export class WAConnection extends Base {
/** /**
* Search WhatsApp messages with a given text string * Search WhatsApp messages with a given text string
* @param txt the search string * @param txt the search string
* @param inJid the ID of the chat to search in, set to null to search all chats * @param inJid the ID of the chat to search in, set to null to search all chats
* @param count number of results to return * @param count number of results to return
* @param page page number of results (starts from 1) * @param page page number of results (starts from 1)
*/ */
async searchMessages(txt: string, inJid: string | null, count: number, page: number) { async searchMessages(txt: string, inJid: string | null, count: number, page: number) {
const json = [ const json = [
'query', 'query',
{ {
epoch: this.msgCount.toString(), epoch: this.msgCount.toString(),
type: 'search', type: 'search',
search: txt, search: txt,
count: count.toString(), count: count.toString(),
@@ -305,9 +307,9 @@ export class WAConnection extends Base {
async forwardMessage(id: string, message: WAMessage, forceForward: boolean=false) { async forwardMessage(id: string, message: WAMessage, forceForward: boolean=false) {
const content = message.message const content = message.message
if (!content) throw new Error ('no content in message') if (!content) throw new Error ('no content in message')
let key = Object.keys(content)[0] let key = Object.keys(content)[0]
let score = content[key].contextInfo?.forwardingScore || 0 let score = content[key].contextInfo?.forwardingScore || 0
score += message.key.fromMe && !forceForward ? 0 : 1 score += message.key.fromMe && !forceForward ? 0 : 1
if (key === MessageType.text) { if (key === MessageType.text) {
@@ -318,8 +320,8 @@ export class WAConnection extends Base {
} }
if (score > 0) content[key].contextInfo = { forwardingScore: score, isForwarded: true } if (score > 0) content[key].contextInfo = { forwardingScore: score, isForwarded: true }
else content[key].contextInfo = {} else content[key].contextInfo = {}
const waMessage = this.prepareMessageFromContent (id, content, {}) const waMessage = this.prepareMessageFromContent (id, content, {})
await this.relayWAMessage (waMessage) await this.relayWAMessage (waMessage)
return waMessage return waMessage
} }