Simpler chat modifications

This commit is contained in:
Adhiraj
2020-08-23 16:49:15 +05:30
parent ea36aabb6c
commit d7e3312b1d
4 changed files with 38 additions and 28 deletions

View File

@@ -287,15 +287,14 @@ await conn.modifyChat (jid, ChatModification.archive) // archive chat
await conn.modifyChat (jid, ChatModification.unarchive) // unarchive chat await conn.modifyChat (jid, ChatModification.unarchive) // unarchive chat
const response = await conn.modifyChat (jid, ChatModification.pin) // pin the 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, 8*60*60*1000) // mute for 8 hours
await conn.modifyChat (jid, ChatModification.mute, {stamp: mutedate}) // mute
setTimeout (() => { setTimeout (() => {
conn.modifyChat (jid, ChatModification.unmute, {stamp: mutedate}) conn.modifyChat (jid, ChatModification.unmute)
}, 5000) // unmute after 5 seconds }, 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. **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.

View File

@@ -8,7 +8,6 @@
"keywords": [ "keywords": [
"whatsapp", "whatsapp",
"js-whatsapp", "js-whatsapp",
"reverse engineer",
"whatsapp-api", "whatsapp-api",
"whatsapp-web", "whatsapp-web",
"whatsapp", "whatsapp",
@@ -33,22 +32,22 @@
"dependencies": { "dependencies": {
"@adiwajshing/keyed-db": "^0.1.2", "@adiwajshing/keyed-db": "^0.1.2",
"curve25519-js": "0.0.4", "curve25519-js": "0.0.4",
"futoin-hkdf": "^1.3.2", "futoin-hkdf": "1.3.2",
"jimp": "^0.14.0", "jimp": "0.14.0",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"protobufjs": "^6.10.1", "protobufjs": "6.10.1",
"qrcode-terminal": "^0.12.0", "qrcode-terminal": "^0.12.0",
"ws": "^7.3.1" "ws": "^7.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/mocha": "^7.0.2", "@types/mocha": "7.0.2",
"@types/node": "^14.0.27", "@types/node": "14.0.27",
"@types/ws": "^7.2.6", "@types/ws": "^7.2.6",
"assert": "^2.0.0", "assert": "^2.0.0",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"mocha": "^8.1.1", "mocha": "^8.1.1",
"ts-node-dev": "^1.0.0-pre.57", "ts-node-dev": "^1.0.0-pre.57",
"typedoc": "^0.18.0", "typedoc": "0.18.0",
"typescript": "^3.9.7" "typescript": "3.9.7"
} }
} }

View File

@@ -89,15 +89,24 @@ WAConnectionTest('Misc', (conn) => {
await conn.modifyChat (testJid, ChatModification.unarchive) await conn.modifyChat (testJid, ChatModification.unarchive)
}) })
it('should pin & unpin a chat', async () => { 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 delay (2000)
await conn.modifyChat (testJid, ChatModification.unpin, {stamp: response.stamp}) await conn.modifyChat (testJid, ChatModification.unpin)
}) })
it('should mute & unmute a chat', async () => { it('should mute & unmute a chat', async () => {
const mutedate = new Date (new Date().getTime() + 8*60*60*1000) // 8 hours in the future const waitForEvent = new Promise (resolve => {
await conn.modifyChat (testJid, ChatModification.mute, {stamp: mutedate}) 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 delay (2000)
await conn.modifyChat (testJid, ChatModification.unmute, {stamp: mutedate}) await conn.modifyChat (testJid, ChatModification.unmute)
}) })
it('should return search results', async () => { it('should return search results', async () => {
const jids = [null, testJid] const jids = [null, testJid]

View File

@@ -128,35 +128,37 @@ export class WAConnection extends Base {
/** /**
* Modify a given chat (archive, pin etc.) * Modify a given chat (archive, pin etc.)
* @param jid the ID of the person/group you are modifiying * @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) jid = whatsappID (jid)
const chat = this.assertChatGet (jid)
let chatAttrs: Record<string, string> = {jid: jid} let chatAttrs: Record<string, string> = {jid: jid}
if ((type === ChatModification.unpin || type === ChatModification.unmute) && !options?.stamp) { if (type === ChatModification.mute && !durationMs) {
throw new Error('options.stamp must be set to the timestamp of the time of pinning/unpinning of the chat') 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) { switch (type) {
case ChatModification.pin: case ChatModification.pin:
case ChatModification.mute: case ChatModification.mute:
const strStamp = (unixTimestampSeconds() + Math.floor(durationMs/1000)).toString()
chatAttrs.type = type chatAttrs.type = type
chatAttrs[type] = strStamp chatAttrs[type] = strStamp
break break
case ChatModification.unpin: case ChatModification.unpin:
case ChatModification.unmute: case ChatModification.unmute:
chatAttrs.type = type.replace ('un', '') // replace 'unpin' with 'pin' chatAttrs.type = type.replace ('un', '') // replace 'unpin' with 'pin'
chatAttrs.previous = strStamp chatAttrs.previous = chat[type.replace ('un', '')]
break break
default: default:
chatAttrs.type = type chatAttrs.type = type
break 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 (chat) {
if (type.includes('un')) { if (type.includes('un')) {
type = type.replace ('un', '') as ChatModification type = type.replace ('un', '') as ChatModification
@@ -167,6 +169,7 @@ export class WAConnection extends Base {
this.emit ('chat-update', { jid, [type]: chat[type] }) this.emit ('chat-update', { jid, [type]: chat[type] })
} }
} }
return response return response
} }
} }