mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Chat ordering update
This commit is contained in:
@@ -55,7 +55,7 @@ export class WAConnection extends EventEmitter {
|
||||
/** Whether the phone is connected */
|
||||
phoneConnected: boolean = false
|
||||
/** key to use to order chats */
|
||||
chatOrderingKey = Utils.WA_CHAT_KEY
|
||||
chatOrderingKey = Utils.waChatKey(false)
|
||||
|
||||
/** log messages */
|
||||
shouldLogMessages = false
|
||||
@@ -63,7 +63,7 @@ export class WAConnection extends EventEmitter {
|
||||
|
||||
maxCachedMessages = 50
|
||||
|
||||
chats: KeyedDB<WAChat> = new KeyedDB (Utils.WA_CHAT_KEY, value => value.jid)
|
||||
chats = new KeyedDB (Utils.waChatKey(false), value => value.jid)
|
||||
contacts: { [k: string]: WAContact } = {}
|
||||
|
||||
/** Data structure of tokens & IDs used to establish one's identiy to WhatsApp Web */
|
||||
|
||||
@@ -167,7 +167,7 @@ export class WAConnection extends Base {
|
||||
* Must be called immediately after connect
|
||||
*/
|
||||
protected receiveChatsAndContacts(waitOnlyForLast: boolean) {
|
||||
const chats = new KeyedDB<WAChat>(this.chatOrderingKey, c => c.jid)
|
||||
const chats = new KeyedDB(this.chatOrderingKey, c => c.jid)
|
||||
const contacts = {}
|
||||
|
||||
let receivedContacts = false
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
WAMetric,
|
||||
WAFlag,
|
||||
} from '../WAConnection/Constants'
|
||||
import { generateProfilePicture, WA_CHAT_KEY, whatsappID, unixTimestampSeconds } from './Utils'
|
||||
import { generateProfilePicture, waChatKey, whatsappID, unixTimestampSeconds } from './Utils'
|
||||
import { Mutex } from './Mutex'
|
||||
|
||||
// All user related functions -- get profile picture, set status etc.
|
||||
@@ -96,7 +96,7 @@ export class WAConnection extends Base {
|
||||
* @param searchString optionally search for users
|
||||
* @returns the chats & the cursor to fetch the next page
|
||||
*/
|
||||
async loadChats (count: number, before: number | null, options: WALoadChatOptions = {}) {
|
||||
async loadChats (count: number, before: string | null, options: WALoadChatOptions = {}) {
|
||||
const chats = this.chats.paginated (before, count, options && (chat => (
|
||||
(typeof options?.custom !== 'function' || options?.custom(chat)) &&
|
||||
(typeof options?.searchString === 'undefined' || chat.name?.includes (options.searchString) || chat.jid?.startsWith(options.searchString))
|
||||
@@ -108,7 +108,7 @@ export class WAConnection extends Base {
|
||||
))
|
||||
)
|
||||
}
|
||||
const cursor = (chats[chats.length-1] && chats.length >= count) && this.chatOrderingKey (chats[chats.length-1])
|
||||
const cursor = (chats[chats.length-1] && chats.length >= count) && this.chatOrderingKey.key (chats[chats.length-1])
|
||||
return { chats, cursor }
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -207,7 +207,7 @@ export interface WAChat {
|
||||
name?: string
|
||||
|
||||
// Baileys added properties
|
||||
messages: KeyedDB<WAMessage>
|
||||
messages: KeyedDB<WAMessage, number>
|
||||
imgUrl?: string
|
||||
}
|
||||
export enum WAMetric {
|
||||
|
||||
@@ -24,18 +24,14 @@ export const Browsers = {
|
||||
/** The appropriate browser based on your OS & release */
|
||||
appropriate: browser => [ platformMap [platform()] || 'Ubuntu', browser, release() ] as [string, string, string]
|
||||
}
|
||||
function hashCode(s: string) {
|
||||
for(var i = 0, h = 0; i < s.length; i++)
|
||||
h = Math.imul(31, h) + s.charCodeAt(i) | 0;
|
||||
return h;
|
||||
}
|
||||
export const toNumber = (t: Long | number) => (t['low'] || t) as number
|
||||
|
||||
export const WA_CHAT_KEY = (c: WAChat) => ((c.t*100000) + (hashCode(c.jid)%100000))*-1 // -1 to sort descending
|
||||
export const WA_CHAT_KEY_PIN = (c: WAChat) => ((c.t*100000)*(c.pin ? 2 : 1) + (hashCode(c.jid)%100000))*-1 // -1 to sort descending
|
||||
|
||||
export const waChatKey = (pin: boolean) => ({
|
||||
key: (c: WAChat) => (pin ? (c.pin ? '1' : '0') : '') + c.t.toString(16).padStart(8, '0') + c.jid,
|
||||
compare: (k1: string, k2: string) => k2.localeCompare (k1)
|
||||
})
|
||||
export const WA_MESSAGE_KEY = (m: WAMessage) => toNumber (m.messageTimestamp)*1000 + (m['epoch'] || 0)%1000
|
||||
export const WA_MESSAGE_ID = (m: WAMessage) => GET_MESSAGE_ID (m.key)
|
||||
|
||||
export const GET_MESSAGE_ID = (key: WAMessageKey) => `${key.id}|${key.fromMe ? 1 : 0}`
|
||||
|
||||
export const whatsappID = (jid: string) => jid?.replace ('@c.us', '@s.whatsapp.net')
|
||||
|
||||
Reference in New Issue
Block a user