mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
string based key for messages
This commit is contained in:
@@ -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 * as assert from 'assert'
|
||||||
import {promises as fs} from 'fs'
|
import {promises as fs} from 'fs'
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ export const WAConnectionTest = (name: string, func: (conn: WAConnection) => voi
|
|||||||
export const assertChatDBIntegrity = (conn: WAConnection) => {
|
export const assertChatDBIntegrity = (conn: WAConnection) => {
|
||||||
conn.chats.all ().forEach (chat => (
|
conn.chats.all ().forEach (chat => (
|
||||||
assert.deepEqual (
|
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()
|
chat.messages.all()
|
||||||
)
|
)
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ export class WAConnection extends Base {
|
|||||||
chat.jid = Utils.whatsappID (chat.jid)
|
chat.jid = Utils.whatsappID (chat.jid)
|
||||||
chat.t = +chat.t
|
chat.t = +chat.t
|
||||||
chat.count = +chat.count
|
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 data (log json to see what it looks like)
|
||||||
!chats.get (chat.jid) && chats.insert (chat)
|
!chats.get (chat.jid) && chats.insert (chat)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import * as QR from 'qrcode-terminal'
|
import * as QR from 'qrcode-terminal'
|
||||||
import { WAConnection as Base } from './3.Connect'
|
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 { 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 KeyedDB from '@adiwajshing/keyed-db'
|
||||||
import { Mutex } from './Mutex'
|
import { Mutex } from './Mutex'
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ export class WAConnection extends Base {
|
|||||||
const chat: WAChat = {
|
const chat: WAChat = {
|
||||||
jid: jid,
|
jid: jid,
|
||||||
t: unixTimestampSeconds(),
|
t: unixTimestampSeconds(),
|
||||||
messages: new KeyedDB(WA_MESSAGE_KEY, WA_MESSAGE_ID),
|
messages: new KeyedDB(waMessageKey, WA_MESSAGE_ID),
|
||||||
count: 0,
|
count: 0,
|
||||||
modify_tag: '',
|
modify_tag: '',
|
||||||
spam: 'false',
|
spam: 'false',
|
||||||
|
|||||||
@@ -207,7 +207,7 @@ export interface WAChat {
|
|||||||
name?: string
|
name?: string
|
||||||
|
|
||||||
// Baileys added properties
|
// Baileys added properties
|
||||||
messages: KeyedDB<WAMessage, number>
|
messages: KeyedDB<WAMessage, string>
|
||||||
imgUrl?: string
|
imgUrl?: string
|
||||||
}
|
}
|
||||||
export enum WAMetric {
|
export enum WAMetric {
|
||||||
|
|||||||
@@ -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,
|
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)
|
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 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 GET_MESSAGE_ID = (key: WAMessageKey) => `${key.id}|${key.fromMe ? 1 : 0}`
|
||||||
|
|
||||||
export const whatsappID = (jid: string) => jid?.replace ('@c.us', '@s.whatsapp.net')
|
export const whatsappID = (jid: string) => jid?.replace ('@c.us', '@s.whatsapp.net')
|
||||||
|
|||||||
Reference in New Issue
Block a user