Cleaner upserts

This commit is contained in:
Adhiraj Singh
2020-12-18 16:49:28 +05:30
parent ccab24cdb2
commit b6a8d02038
9 changed files with 172 additions and 175 deletions

View File

@@ -79,7 +79,7 @@ describe('Test Connect', () => {
conn.on ('connection-phone-change', ({connected}) => {
if (connected === expect) {
conn.removeAllListeners ('connection-phone-change')
resolve ()
resolve(undefined)
}
})
})
@@ -234,7 +234,7 @@ describe ('Reconnects', () => {
if (closes >= 1) {
conn.removeAllListeners ('close')
conn.removeAllListeners ('connecting')
resolve ()
resolve(undefined)
}
})
conn.on ('connecting', () => {
@@ -249,7 +249,7 @@ describe ('Reconnects', () => {
await new Promise (resolve => {
conn.on ('open', () => {
conn.removeAllListeners ('open')
resolve ()
resolve(undefined)
})
})

View File

@@ -38,7 +38,7 @@ WAConnectionTest('Groups', (conn) => {
conn.chats.get(jid).metadata.desc,
newDesc
)
resolve ()
resolve(undefined)
}
})
))
@@ -74,7 +74,7 @@ WAConnectionTest('Groups', (conn) => {
if (jid === gid) {
assert.strictEqual(name, subject)
assert.strictEqual(conn.chats.get(jid).name, subject)
resolve ()
resolve(undefined)
}
})
})
@@ -91,7 +91,7 @@ WAConnectionTest('Groups', (conn) => {
if (jid === gid) {
assert.strictEqual (announce, 'true')
assert.strictEqual(conn.chats.get(gid).metadata.announce, announce)
resolve ()
resolve(undefined)
}
})
})
@@ -116,7 +116,7 @@ WAConnectionTest('Groups', (conn) => {
whatsappID(jid) === whatsappID(participants[0]) && isAdmin
)),
)
resolve()
resolve(undefined)
}
})
@@ -137,7 +137,7 @@ WAConnectionTest('Groups', (conn) => {
conn.chats.get(jid).metadata.participants.find(p => whatsappID(p.jid) === whatsappID(participants[0])),
undefined
)
resolve ()
resolve(undefined)
}
})
})
@@ -152,7 +152,7 @@ WAConnectionTest('Groups', (conn) => {
conn.once ('chat-update', ({jid, read_only}) => {
if (jid === gid) {
assert.strictEqual (read_only, 'true')
resolve ()
resolve(undefined)
}
})
})
@@ -166,7 +166,7 @@ WAConnectionTest('Groups', (conn) => {
conn.once ('chat-update', ({jid, archive}) => {
if (jid === gid) {
assert.strictEqual (archive, 'true')
resolve ()
resolve(undefined)
}
})
})
@@ -178,7 +178,7 @@ WAConnectionTest('Groups', (conn) => {
conn.once ('chat-update', (chat) => {
if (chat.jid === gid) {
assert.strictEqual (chat['delete'], 'true')
resolve ()
resolve(undefined)
}
})
})

View File

@@ -15,7 +15,7 @@ WAConnectionTest('Messages', conn => {
await new Promise(resolve => conn.once('message-status-update', update => {
if (update.ids.includes(message.key.id)) {
assert.strictEqual(update.type, WA_MESSAGE_STATUS_TYPE.SERVER_ACK)
resolve()
resolve(undefined)
}
}))

View File

@@ -25,7 +25,7 @@ WAConnectionTest('Misc', conn => {
if (jid === conn.user.jid) {
assert.strictEqual (status, newStatus)
conn.removeAllListeners ('user-status-update')
resolve ()
resolve(undefined)
}
})
})
@@ -79,7 +79,7 @@ WAConnectionTest('Misc', conn => {
conn.once ('chat-update', ({jid: tJid, count}) => {
if (jid === tJid) {
assert.ok (count < 0)
resolve ()
resolve(undefined)
}
})
})
@@ -107,7 +107,7 @@ WAConnectionTest('Misc', conn => {
if (jid === testJid ) {
assert.ok (mute)
conn.removeAllListeners ('chat-update')
resolve ()
resolve(undefined)
}
})
})
@@ -161,7 +161,7 @@ WAConnectionTest('Misc', conn => {
it('should detect overlaps and clear messages accordingly', async () => {
// wait for chats
await new Promise(resolve => (
conn.once('chats-received', ({ hasReceivedLastMessage }) => hasReceivedLastMessage && resolve())
conn.once('chats-received', ({ hasReceivedLastMessage }) => hasReceivedLastMessage && resolve(undefined))
))
conn.maxCachedMessages = 100
@@ -188,7 +188,7 @@ WAConnectionTest('Misc', conn => {
missing.count
)
assert.strictEqual(missing.count, oldCount)
resolve()
resolve(undefined)
}
})
))
@@ -214,7 +214,7 @@ WAConnectionTest('Misc', conn => {
if (jid === testJid && typeof ephemeral !== 'undefined') {
assert.strictEqual(!!(+ephemeral), ephemeralOn)
assert.strictEqual(!!(+chat.ephemeral), ephemeralOn)
resolve()
resolve(undefined)
conn.removeAllListeners('chat-update')
}
})

View File

@@ -87,7 +87,7 @@ export class WAConnection extends Base {
response = await this.waitForMessage('s2', true)
}
const newUser = await this.validateNewConnection(response[1]) // validate the connection
const newUser = this.validateNewConnection(response[1]) // validate the connection
if (newUser.jid !== this.user?.jid) {
isNewUser = true
// clear out old data

View File

@@ -29,7 +29,7 @@ export class WAConnection extends Base {
chat.count = +chat.count
chat.messages = newMessagesDB()
// chats data (log json to see what it looks like)
!chats.get (chat.jid) && chats.insert (chat)
chats.insert(chat)
})
this.logger.info (`received ${json[2].length} chats`)
@@ -80,6 +80,7 @@ export class WAConnection extends Base {
messages.reverse().forEach (([,, message]: ['message', null, WAMessage]) => {
const jid = message.key.remoteJid
const chat = this.chats.get(jid)
const mKeyID = WA_MESSAGE_ID(message)
if (chat) {
if (style === 'previous') {
@@ -96,15 +97,11 @@ export class WAConnection extends Base {
// hacky way to allow more previous messages
message['epoch'] = prevEpoch+1000
}
if (chat.messages.get(mKeyID)) {
chat.messages.delete(chat.messages.get(mKeyID))
if (chat.messages.upsert(message).length > 0) {
overlaps[jid] = { ...(overlaps[jid] || { requiresOverlap: true }), didOverlap: true }
}
chat.messages.insert (message)
}
updates[jid] = updates[jid] || newMessagesDB()
updates[jid].insert(message)
updates[jid].upsert(message)
lastMessages[jid] = mKeyID
} else if (!chat) this.logger.debug({ jid }, `chat not found`)
@@ -220,13 +217,12 @@ export class WAConnection extends Base {
const oldMessage = chat.messages.get (WA_MESSAGE_ID(message))
if (oldMessage) {
message['epoch'] = oldMessage['epoch']
chat.messages.delete (oldMessage)
chat.messages.insert (message)
const chatUpdate: Partial<WAChat> = { jid, messages: newMessagesDB([ message ]) }
this.emit ('chat-update', chatUpdate)
// emit deprecated
this.emit ('message-update', message)
if (chat.messages.upsert(message).length) {
const chatUpdate: Partial<WAChat> = { jid, messages: newMessagesDB([ message ]) }
this.emit ('chat-update', chatUpdate)
// emit deprecated
this.emit ('message-update', message)
}
} else {
this.logger.debug ({ unhandled: true }, 'received message update for non-present message from ' + jid)
}
@@ -407,26 +403,24 @@ export class WAConnection extends Base {
this.chatUpdatedMessage (update.ids, update.type, chat)
}
/** inserts an empty chat into the DB */
protected async chatAdd (jid: string, name?: string) {
if (this.chats.get (jid)) return
protected async chatAdd (jid: string, name?: string) {
const chat: WAChat = {
jid: jid,
jid,
name,
t: unixTimestampSeconds(),
messages: new KeyedDB(waMessageKey, WA_MESSAGE_ID),
messages: newMessagesDB(),
count: 0,
modify_tag: '',
spam: 'false',
name
spam: 'false'
}
this.chats.insert (chat)
if (this.loadProfilePicturesForChatsAutomatically) {
await this.setProfilePicture (chat)
}
this.emit ('chat-new', chat)
return chat
if(this.chats.insertIfAbsent (chat).length) {
if (this.loadProfilePicturesForChatsAutomatically) {
await this.setProfilePicture (chat)
}
this.emit ('chat-new', chat)
return chat
}
}
protected contactAddOrGet (jid: string) {
jid = whatsappID(jid)
@@ -506,7 +500,6 @@ export class WAConnection extends Base {
message['epoch'] = lastEpoch+1
messages.insert (message)
while (messages.length > this.maxCachedMessages) {
messages.delete (messages.all()[0]) // delete oldest messages
}
@@ -519,7 +512,6 @@ export class WAConnection extends Base {
chatUpdate.messages = newMessagesDB([ message ])
// emit deprecated
this.emit('message-new', message)
// check if the message is an action
if (message.messageStubType) {
const jid = chat.jid

View File

@@ -128,8 +128,8 @@ export class WAConnection extends Base {
fepoch -= 1
m['epoch'] = fepoch
if(chat.messages.length < this.maxCachedMessages && !chat.messages.get (WA_MESSAGE_ID(m))) {
chat.messages.insert(m)
if(chat.messages.length < this.maxCachedMessages) {
chat.messages.insertIfAbsent(m)
}
}
messages.unshift (...extra)