feat: group mentions + improved user mentions (#2602)

* added logic to mention groups + fixed user mentions

* usage example updated

* docs fixed

* window.injectToFunction clarifications

* usage example clarifications

* fix: typo

* docs: added missed docs
This commit is contained in:
alechkos 2024-01-11 10:33:10 +02:00 • committed by GitHub
commit e0d955f130
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 164 additions and 25 deletions

View file

@ -810,6 +810,13 @@ class Client extends EventEmitter {
return result;
}
/**
* An object representing mentions of groups
* @typedef {Object} GroupMention
* @property {string} subject - The name of a group to mention (can be custom)
* @property {string} id - The group ID, e.g.: 'XXXXXXXXXX@g.us'
*/
/**
* Message options.
* @typedef {Object} MessageSendOptions
@ -822,7 +829,8 @@ class Client extends EventEmitter {
* @property {boolean} [parseVCards=true] - Automatically parse vCards and send them as contacts
* @property {string} [caption] - Image or video caption
* @property {string} [quotedMessageId] - Id of the message that is being quoted (or replied to)
* @property {Contact[]} [mentions] - Contacts that are being mentioned in the message
* @property {GroupMention[]} [groupMentions] - An array of object that handle group mentions
* @property {string[]} [mentions] - User IDs to mention in the message
* @property {boolean} [sendSeen=true] - Mark the conversation as seen after sending the message
* @property {string} [stickerAuthor=undefined] - Sets the author of the sticker, (if sendMediaAsSticker is true).
* @property {string} [stickerName=undefined] - Sets the name of the sticker, (if sendMediaAsSticker is true).
@ -839,10 +847,16 @@ class Client extends EventEmitter {
* @returns {Promise<Message>} Message that was just sent
*/
async sendMessage(chatId, content, options = {}) {
if (options.mentions && options.mentions.some(possiblyContact => possiblyContact instanceof Contact)) {
console.warn('Mentions with an array of Contact are now deprecated. See more at https://github.com/pedroslopez/whatsapp-web.js/pull/2166.');
options.mentions = options.mentions.map(a => a.id._serialized);
if (options.mentions) {
!Array.isArray(options.mentions) && (options.mentions = [options.mentions]);
if (options.mentions.some((possiblyContact) => possiblyContact instanceof Contact)) {
console.warn('Mentions with an array of Contact are now deprecated. See more at https://github.com/pedroslopez/whatsapp-web.js/pull/2166.');
options.mentions = options.mentions.map((a) => a.id._serialized);
}
}
options.groupMentions && !Array.isArray(options.groupMentions) && (options.groupMentions = [options.groupMentions]);
let internalOptions = {
linkPreview: options.linkPreview === false ? undefined : true,
sendAudioAsVoice: options.sendAudioAsVoice,
@ -852,7 +866,8 @@ class Client extends EventEmitter {
caption: options.caption,
quotedMessageId: options.quotedMessageId,
parseVCards: options.parseVCards === false ? false : true,
mentionedJidList: Array.isArray(options.mentions) ? options.mentions : [],
mentionedJidList: options.mentions || [],
groupMentions: options.groupMentions,
extraOptions: options.extra
};

View file

@ -187,14 +187,32 @@ class Message extends Base {
} : undefined;
/**
* Indicates the mentions in the message body.
* @type {Array<string>}
* @typedef {Object} Mention
* @property {string} server
* @property {string} user
* @property {string} _serialized
*/
this.mentionedIds = [];
if (data.mentionedJidList) {
this.mentionedIds = data.mentionedJidList;
}
/**
* Indicates the mentions in the message body.
* @type {Mention[]}
*/
this.mentionedIds = data.mentionedJidList || [];
/**
* @typedef {Object} GroupMention
* @property {string} groupSubject The name of the group
* @property {Object} groupJid The group ID
* @property {string} groupJid.server
* @property {string} groupJid.user
* @property {string} groupJid._serialized
*/
/**
* Indicates whether there are group mentions in the message body
* @type {GroupMention[]}
*/
this.groupMentions = data.groupMentions || [];
/**
* Order ID for message type ORDER
@ -341,6 +359,14 @@ class Message extends Base {
async getMentions() {
return await Promise.all(this.mentionedIds.map(async m => await this.client.getContactById(m)));
}
/**
* Returns groups mentioned in this message
* @returns {Promise<GroupChat[]|[]>}
*/
async getGroupMentions() {
return await Promise.all(this.groupMentions.map(async (m) => await this.client.getChatById(m.groupJid._serialized)));
}
/**
* Returns the quoted message, if any
@ -649,12 +675,20 @@ class Message extends Base {
* @returns {Promise<?Message>}
*/
async edit(content, options = {}) {
if (options.mentions && options.mentions.some(possiblyContact => possiblyContact instanceof Contact)) {
options.mentions = options.mentions.map(a => a.id._serialized);
if (options.mentions) {
!Array.isArray(options.mentions) && (options.mentions = [options.mentions]);
if (options.mentions.some((possiblyContact) => possiblyContact instanceof Contact)) {
console.warn('Mentions with an array of Contact are now deprecated. See more at https://github.com/pedroslopez/whatsapp-web.js/pull/2166.');
options.mentions = options.mentions.map((a) => a.id._serialized);
}
}
options.groupMentions && !Array.isArray(options.groupMentions) && (options.groupMentions = [options.groupMentions]);
let internalOptions = {
linkPreview: options.linkPreview === false ? undefined : true,
mentionedJidList: Array.isArray(options.mentions) ? options.mentions : [],
mentionedJidList: options.mentions || [],
groupMentions: options.groupMentions,
extraOptions: options.extra
};

View file

@ -58,12 +58,12 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.LidUtils = window.mR.findModule('getCurrentLid')[0];
window.Store.WidToJid = window.mR.findModule('widToUserJid')[0];
window.Store.JidToWid = window.mR.findModule('userJidToUserWid')[0];
window.Store.getMsgInfo = (window.mR.findModule('sendQueryMsgInfo')[0] || {}).sendQueryMsgInfo || window.mR.findModule('queryMsgInfo')[0].queryMsgInfo;
window.Store.pinUnpinMsg = window.mR.findModule('sendPinInChatMsg')[0].sendPinInChatMsg;
/* eslint-disable no-undef, no-cond-assign */
window.Store.QueryExist = ((m = window.mR.findModule('queryExists')[0]) ? m.queryExists : window.mR.findModule('queryExist')[0].queryWidExists);
window.Store.ReplyUtils = (m = window.mR.findModule('canReplyMsg')).length > 0 && m[0];
window.Store.getMsgInfo = (window.mR.findModule('sendQueryMsgInfo')[0] || {}).sendQueryMsgInfo || window.mR.findModule('queryMsgInfo')[0].queryMsgInfo;
/* eslint-enable no-undef, no-cond-assign */
window.Store.Settings = {
@ -195,7 +195,22 @@ exports.LoadUtils = () => {
}
if (options.mentionedJidList) {
options.mentionedJidList = options.mentionedJidList.map(cId => window.Store.Contact.get(cId).id);
options.mentionedJidList = await Promise.all(
options.mentionedJidList.map(async (id) => {
const wid = window.Store.WidFactory.createWid(id);
if (await window.Store.QueryExist(wid)) {
return wid;
}
})
);
options.mentionedJidList = options.mentionedJidList.filter(Boolean);
}
if (options.groupMentions) {
options.groupMentions = options.groupMentions.map((e) => ({
groupSubject: e.subject,
groupJid: window.Store.WidFactory.createWid(e.id)
}));
}
let locationOptions = {};
@ -368,7 +383,22 @@ exports.LoadUtils = () => {
delete options.extraOptions;
if (options.mentionedJidList) {
options.mentionedJidList = options.mentionedJidList.map(cId => window.Store.Contact.get(cId).id);
options.mentionedJidList = await Promise.all(
options.mentionedJidList.map(async (id) => {
const wid = window.Store.WidFactory.createWid(id);
if (await window.Store.QueryExist(wid)) {
return wid;
}
})
);
options.mentionedJidList = options.mentionedJidList.filter(Boolean);
}
if (options.groupMentions) {
options.groupMentions = options.groupMentions.map((e) => ({
groupSubject: e.subject,
groupJid: window.Store.WidFactory.createWid(e.id)
}));
}
if (options.linkPreview) {