diff --git a/src/Tests/Common.ts b/src/Tests/Common.ts index f9099bf..619eba0 100644 --- a/src/Tests/Common.ts +++ b/src/Tests/Common.ts @@ -1,4 +1,4 @@ -import { WAConnection, MessageLogLevel, MessageOptions, MessageType, unixTimestampSeconds, toNumber, GET_MESSAGE_ID, WA_MESSAGE_KEY } from '../WAConnection/WAConnection' +import { WAConnection, MessageLogLevel, MessageOptions, MessageType, unixTimestampSeconds, toNumber, GET_MESSAGE_ID, waMessageKey } from '../WAConnection/WAConnection' import * as assert from 'assert' import {promises as fs} from 'fs' @@ -36,7 +36,7 @@ export const WAConnectionTest = (name: string, func: (conn: WAConnection) => voi export const assertChatDBIntegrity = (conn: WAConnection) => { conn.chats.all ().forEach (chat => ( assert.deepEqual ( - [...chat.messages.all()].sort ((m1, m2) => WA_MESSAGE_KEY(m1)-WA_MESSAGE_KEY(m2)), + [...chat.messages.all()].sort ((m1, m2) => waMessageKey.compare(waMessageKey.key(m1), waMessageKey.key(m2))), chat.messages.all() ) )) diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index 80ed547..00bc120 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -231,7 +231,7 @@ export class WAConnection extends Base { chat.jid = Utils.whatsappID (chat.jid) chat.t = +chat.t chat.count = +chat.count - chat.messages = new KeyedDB (Utils.WA_MESSAGE_KEY, Utils.WA_MESSAGE_ID) + chat.messages = new KeyedDB (Utils.waMessageKey, Utils.WA_MESSAGE_ID) // chats data (log json to see what it looks like) !chats.get (chat.jid) && chats.insert (chat) diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 919d958..2b40efc 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -1,7 +1,7 @@ import * as QR from 'qrcode-terminal' import { WAConnection as Base } from './3.Connect' import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, MessageLogLevel, PresenceUpdate, BaileysEvent, DisconnectReason, WANode, WAOpenResult, Presence, AuthenticationCredentials } from './Constants' -import { whatsappID, unixTimestampSeconds, isGroupID, toNumber, GET_MESSAGE_ID, WA_MESSAGE_ID, WA_MESSAGE_KEY } from './Utils' +import { whatsappID, unixTimestampSeconds, isGroupID, toNumber, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey } from './Utils' import KeyedDB from '@adiwajshing/keyed-db' import { Mutex } from './Mutex' @@ -185,7 +185,7 @@ export class WAConnection extends Base { const chat: WAChat = { jid: jid, t: unixTimestampSeconds(), - messages: new KeyedDB(WA_MESSAGE_KEY, WA_MESSAGE_ID), + messages: new KeyedDB(waMessageKey, WA_MESSAGE_ID), count: 0, modify_tag: '', spam: 'false', diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index dbbd6e7..6937620 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -207,7 +207,7 @@ export interface WAChat { name?: string // Baileys added properties - messages: KeyedDB + messages: KeyedDB imgUrl?: string } export enum WAMetric { diff --git a/src/WAConnection/Utils.ts b/src/WAConnection/Utils.ts index 3cebe77..049c0c9 100644 --- a/src/WAConnection/Utils.ts +++ b/src/WAConnection/Utils.ts @@ -29,9 +29,11 @@ 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 waMessageKey = { + key: (m: WAMessage) => toNumber (m.messageTimestamp).toString(16) + (10000 + (m['epoch'] || 0)).toString(16), + compare: (k1: string, k2: string) => k1.localeCompare (k2) +} 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')