string based key for messages

This commit is contained in:
Adhiraj
2020-10-05 01:40:13 +05:30
parent 64c9b7449e
commit 6560fc1756
5 changed files with 10 additions and 8 deletions

View File

@@ -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()
)
))

View File

@@ -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)

View File

@@ -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',

View File

@@ -207,7 +207,7 @@ export interface WAChat {
name?: string
// Baileys added properties
messages: KeyedDB<WAMessage, number>
messages: KeyedDB<WAMessage, string>
imgUrl?: string
}
export enum WAMetric {

View File

@@ -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')