Added method to block/unblock user (close #233)

This commit is contained in:
Edgard
2020-12-13 16:16:58 -03:00
committed by Edgard Messias
parent 282fdb1bd3
commit 0bf0ec6490
6 changed files with 96 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import {WAConnection as Base} from './4.Events'
import { Presence, WABroadcastListInfo, WAProfilePictureChange, WALoadChatOptions, WAChatIndex } from './Constants'
import { Presence, WABroadcastListInfo, WAProfilePictureChange, WALoadChatOptions, WAChatIndex, BlocklistUpdate } from './Constants'
import {
WAMessage,
WANode,
@@ -159,4 +159,44 @@ export class WAConnection extends Base {
}
return response
}
/**
* Add or remove user from blocklist
* @param jid the ID of the person who you are blocking/unblocking
* @param type type of operation
*/
@Mutex (jid => jid)
async blockUser (jid: string, type: 'add' | 'remove' = 'add') {
jid.replace('@s.whatsapp.net', '@c.us')
const tag = this.generateMessageTag()
const json: WANode = [
'block',
{
type: type,
},
[
['user', { jid }, null]
],
]
const result = await this.setQuery ([json], [WAMetric.block, WAFlag.ignore], tag)
if (result.status === 200) {
if (type === 'add') {
this.blocklist.push(jid)
} else {
const index = this.blocklist.indexOf(jid);
if (index !== -1) {
this.blocklist.splice(index, 1);
}
}
// Blocklist update event
const update: BlocklistUpdate = { added: [], removed: [] }
let key = type === 'add' ? 'added' : 'removed'
update[key] = [ jid ]
this.emit('blocklist-update', update)
}
return result
}
}