mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Simpler chat modifications
This commit is contained in:
@@ -287,15 +287,14 @@ await conn.modifyChat (jid, ChatModification.archive) // archive chat
|
||||
await conn.modifyChat (jid, ChatModification.unarchive) // unarchive chat
|
||||
|
||||
const response = await conn.modifyChat (jid, ChatModification.pin) // pin the chat
|
||||
await conn.modifyChat (jid, ChatModification.unpin, {stamp: response.stamp})
|
||||
await conn.modifyChat (jid, ChatModification.unpin) // unpin it
|
||||
|
||||
const mutedate = new Date (new Date().getTime() + 8*60*60*1000) // mute for 8 hours in the future
|
||||
await conn.modifyChat (jid, ChatModification.mute, {stamp: mutedate}) // mute
|
||||
await conn.modifyChat (jid, ChatModification.mute, 8*60*60*1000) // mute for 8 hours
|
||||
setTimeout (() => {
|
||||
conn.modifyChat (jid, ChatModification.unmute, {stamp: mutedate})
|
||||
conn.modifyChat (jid, ChatModification.unmute)
|
||||
}, 5000) // unmute after 5 seconds
|
||||
|
||||
await conn.deleteChat (jid) // will delete the chat (can be a group or broadcast list)
|
||||
await conn.deleteChat (jid) // will delete the chat (can be a group or broadcast list as well)
|
||||
```
|
||||
|
||||
**Note:** to unmute or unpin a chat, one must pass the timestamp of the pinning or muting. This is returned by the pin & mute functions. This is also available in the `WAChat` objects of the respective chats, as a `mute` or `pin` property.
|
||||
|
||||
15
package.json
15
package.json
@@ -8,7 +8,6 @@
|
||||
"keywords": [
|
||||
"whatsapp",
|
||||
"js-whatsapp",
|
||||
"reverse engineer",
|
||||
"whatsapp-api",
|
||||
"whatsapp-web",
|
||||
"whatsapp",
|
||||
@@ -33,22 +32,22 @@
|
||||
"dependencies": {
|
||||
"@adiwajshing/keyed-db": "^0.1.2",
|
||||
"curve25519-js": "0.0.4",
|
||||
"futoin-hkdf": "^1.3.2",
|
||||
"jimp": "^0.14.0",
|
||||
"futoin-hkdf": "1.3.2",
|
||||
"jimp": "0.14.0",
|
||||
"node-fetch": "^2.6.0",
|
||||
"protobufjs": "^6.10.1",
|
||||
"protobufjs": "6.10.1",
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"ws": "^7.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mocha": "^7.0.2",
|
||||
"@types/node": "^14.0.27",
|
||||
"@types/mocha": "7.0.2",
|
||||
"@types/node": "14.0.27",
|
||||
"@types/ws": "^7.2.6",
|
||||
"assert": "^2.0.0",
|
||||
"dotenv": "^8.2.0",
|
||||
"mocha": "^8.1.1",
|
||||
"ts-node-dev": "^1.0.0-pre.57",
|
||||
"typedoc": "^0.18.0",
|
||||
"typescript": "^3.9.7"
|
||||
"typedoc": "0.18.0",
|
||||
"typescript": "3.9.7"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,15 +89,24 @@ WAConnectionTest('Misc', (conn) => {
|
||||
await conn.modifyChat (testJid, ChatModification.unarchive)
|
||||
})
|
||||
it('should pin & unpin a chat', async () => {
|
||||
const response = await conn.modifyChat (testJid, ChatModification.pin)
|
||||
await conn.modifyChat (testJid, ChatModification.pin)
|
||||
await delay (2000)
|
||||
await conn.modifyChat (testJid, ChatModification.unpin, {stamp: response.stamp})
|
||||
await conn.modifyChat (testJid, ChatModification.unpin)
|
||||
})
|
||||
it('should mute & unmute a chat', async () => {
|
||||
const mutedate = new Date (new Date().getTime() + 8*60*60*1000) // 8 hours in the future
|
||||
await conn.modifyChat (testJid, ChatModification.mute, {stamp: mutedate})
|
||||
const waitForEvent = new Promise (resolve => {
|
||||
conn.on ('chat-update', ({jid, mute}) => {
|
||||
if (jid === testJid ) {
|
||||
assert.ok (mute)
|
||||
conn.removeAllListeners ('chat-update')
|
||||
resolve ()
|
||||
}
|
||||
})
|
||||
})
|
||||
await conn.modifyChat (testJid, ChatModification.mute, 8*60*60*1000) // 8 hours in the future
|
||||
await waitForEvent
|
||||
await delay (2000)
|
||||
await conn.modifyChat (testJid, ChatModification.unmute, {stamp: mutedate})
|
||||
await conn.modifyChat (testJid, ChatModification.unmute)
|
||||
})
|
||||
it('should return search results', async () => {
|
||||
const jids = [null, testJid]
|
||||
|
||||
@@ -128,35 +128,37 @@ export class WAConnection extends Base {
|
||||
/**
|
||||
* Modify a given chat (archive, pin etc.)
|
||||
* @param jid the ID of the person/group you are modifiying
|
||||
* @param options.stamp the timestamp of pinning/muting the chat. Is required when unpinning/unmuting
|
||||
* @param durationMs only for muting, how long to mute the chat for
|
||||
*/
|
||||
async modifyChat (jid: string, type: ChatModification, options: {stamp: Date | string} = {stamp: new Date()}) {
|
||||
async modifyChat (jid: string, type: ChatModification, durationMs?: number) {
|
||||
jid = whatsappID (jid)
|
||||
const chat = this.assertChatGet (jid)
|
||||
|
||||
let chatAttrs: Record<string, string> = {jid: jid}
|
||||
if ((type === ChatModification.unpin || type === ChatModification.unmute) && !options?.stamp) {
|
||||
throw new Error('options.stamp must be set to the timestamp of the time of pinning/unpinning of the chat')
|
||||
if (type === ChatModification.mute && !durationMs) {
|
||||
throw new Error('duration must be set to the timestamp of the time of pinning/unpinning of the chat')
|
||||
}
|
||||
const strStamp = options.stamp &&
|
||||
(typeof options.stamp === 'string' ? options.stamp : unixTimestampSeconds(options.stamp).toString ())
|
||||
|
||||
durationMs = durationMs || 0
|
||||
switch (type) {
|
||||
case ChatModification.pin:
|
||||
case ChatModification.mute:
|
||||
const strStamp = (unixTimestampSeconds() + Math.floor(durationMs/1000)).toString()
|
||||
chatAttrs.type = type
|
||||
chatAttrs[type] = strStamp
|
||||
break
|
||||
case ChatModification.unpin:
|
||||
case ChatModification.unmute:
|
||||
chatAttrs.type = type.replace ('un', '') // replace 'unpin' with 'pin'
|
||||
chatAttrs.previous = strStamp
|
||||
chatAttrs.previous = chat[type.replace ('un', '')]
|
||||
break
|
||||
default:
|
||||
chatAttrs.type = type
|
||||
break
|
||||
}
|
||||
let response = await this.setQuery ([['chat', chatAttrs, null]]) as {status: number, stamp: string}
|
||||
response.stamp = strStamp
|
||||
|
||||
const chat = this.chats.get (jid)
|
||||
const response = await this.setQuery ([['chat', chatAttrs, null]])
|
||||
|
||||
if (chat) {
|
||||
if (type.includes('un')) {
|
||||
type = type.replace ('un', '') as ChatModification
|
||||
@@ -167,6 +169,7 @@ export class WAConnection extends Base {
|
||||
this.emit ('chat-update', { jid, [type]: chat[type] })
|
||||
}
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user