Unarchive tests

This commit is contained in:
Adhiraj Singh
2020-12-31 11:18:47 +05:30
parent 763ac66551
commit 05bae6b1e7
4 changed files with 82 additions and 52 deletions

View File

@@ -119,9 +119,37 @@ WAConnectionTest('Misc', conn => {
}
})
it('should archive & unarchive', async () => {
// wait for chats
await new Promise(resolve => (
conn.once('chats-received', ({ }) => resolve(undefined))
))
const idx = conn.chats.all().findIndex(chat => chat.jid === testJid)
await conn.modifyChat (testJid, ChatModification.archive)
const idx2 = conn.chats.all().findIndex(chat => chat.jid === testJid)
assert.ok(idx < idx2) // should move further down the array
await delay (2000)
await conn.modifyChat (testJid, ChatModification.unarchive)
const idx3 = conn.chats.all().findIndex(chat => chat.jid === testJid)
assert.strictEqual(idx, idx3) // should be back there
})
it('should archive & unarchive on new message', async () => {
// wait for chats
await new Promise(resolve => (
conn.once('chats-received', ({ }) => resolve(undefined))
))
const idx = conn.chats.all().findIndex(chat => chat.jid === testJid)
await conn.modifyChat (testJid, ChatModification.archive)
const idx2 = conn.chats.all().findIndex(chat => chat.jid === testJid)
assert.ok(idx < idx2) // should move further down the array
await delay (2000)
await sendAndRetreiveMessage(conn, 'test', MessageType.text)
// should be unarchived
const idx3 = conn.chats.all().findIndex(chat => chat.jid === testJid)
assert.strictEqual(idx, idx3) // should be back there
})
it('should pin & unpin a chat', async () => {
await conn.modifyChat (testJid, ChatModification.pin)

View File

@@ -266,7 +266,7 @@ export class WAConnection extends Base {
return 'clear'
},
'archive': () => {
chat.archive = 'true'
this.chats.updateKey(chat, chat => chat.archive = 'true')
return 'archive'
},
'unarchive': () => {
@@ -282,7 +282,7 @@ export class WAConnection extends Base {
if (func) {
const property = func ()
this.emit ('chat-update', { jid, [property]: chat[property] || null })
this.emit ('chat-update', { jid, [property]: chat[property] || 'false' })
}
})
// profile picture updates
@@ -508,8 +508,7 @@ export class WAConnection extends Base {
}
} else if (!messages.get(WA_MESSAGE_ID(message))) { // if the message is not already there
const last = messages.all().slice(-1)
const lastEpoch = ((last && last[0]) && last[0]['epoch']) || 0
const lastEpoch = (messages.last && messages.last['epoch']) || 0
message['epoch'] = lastEpoch+1
messages.insert (message)
@@ -518,7 +517,7 @@ export class WAConnection extends Base {
}
// only update if it's an actual message
if (message.message && !ephemeralProtocolMsg) {
this.chats.updateKey(chat, chat => {
this.chats.update(chat.jid, chat => {
chat.t = +toNumber(message.messageTimestamp)
chatUpdate.t = chat.t
// a new message unarchives the chat

View File

@@ -1,10 +1,10 @@
import {WAConnection as Base} from './6.MessagesSend'
import { MessageType, WAMessageKey, MessageInfo, WAMessageContent, WAMetric, WAFlag, WANode, WAMessage, WAMessageProto, ChatModification, BaileysError, WAChatIndex, WAChat } from './Constants'
import { whatsappID, delay, toNumber, unixTimestampSeconds, GET_MESSAGE_ID, WA_MESSAGE_ID, isGroupID, newMessagesDB } from './Utils'
import { whatsappID, delay, toNumber, unixTimestampSeconds, GET_MESSAGE_ID, isGroupID, newMessagesDB } from './Utils'
import { Mutex } from './Mutex'
export class WAConnection extends Base {
@Mutex ()
async loadAllUnreadMessages () {
const tasks = this.chats.all()
@@ -453,14 +453,16 @@ export class WAConnection extends Base {
chat.messages = chat.messages.filter(m => m.starred)
}
}
if (type.includes('un')) {
type = type.replace ('un', '') as ChatModification
delete chat[type.replace('un','')]
this.emit ('chat-update', { jid, [type]: false })
} else {
chat[type] = chatAttrs[type] || 'true'
this.emit ('chat-update', { jid, [type]: chat[type] })
}
this.chats.update(jid, chat => {
if (type.includes('un')) {
type = type.replace ('un', '') as ChatModification
delete chat[type.replace('un','')]
this.emit ('chat-update', { jid, [type]: false })
} else {
chat[type] = chatAttrs[type] || 'true'
this.emit ('chat-update', { jid, [type]: chat[type] })
}
})
}
return response
}