fix: return poll vote reactions (#3694)
* wip: return poll vote reactions * wip: add module * wip: return poll vote reactions * wip: return poll vote reactions * Update Client.js * Update PollVote.js * wip: use async getMessagesById * Update Client.js * style: clean shit * style: reformat code --------- Co-authored-by: tuyuribr <45042245+tuyuribr@users.noreply.github.com>
This commit is contained in:
parent
6d3078fd77
commit
2ac982ee1c
4 changed files with 63 additions and 27 deletions
|
|
@ -706,14 +706,15 @@ class Client extends EventEmitter {
|
|||
this.emit(Events.MESSAGE_CIPHERTEXT, new Message(this, msg));
|
||||
});
|
||||
|
||||
await exposeFunctionIfAbsent(this.pupPage, 'onPollVoteEvent', (vote) => {
|
||||
const _vote = new PollVote(this, vote);
|
||||
/**
|
||||
* Emitted when some poll option is selected or deselected,
|
||||
* shows a user's current selected option(s) on the poll
|
||||
* @event Client#vote_update
|
||||
*/
|
||||
this.emit(Events.VOTE_UPDATE, _vote);
|
||||
await exposeFunctionIfAbsent(this.pupPage, 'onPollVoteEvent', (votes) => {
|
||||
for (const vote of votes) {
|
||||
/**
|
||||
* Emitted when some poll option is selected or deselected,
|
||||
* shows a user's current selected option(s) on the poll
|
||||
* @event Client#vote_update
|
||||
*/
|
||||
this.emit(Events.VOTE_UPDATE, new PollVote(this, vote));
|
||||
}
|
||||
});
|
||||
|
||||
await this.pupPage.evaluate(() => {
|
||||
|
|
@ -740,10 +741,6 @@ class Client extends EventEmitter {
|
|||
}
|
||||
});
|
||||
window.Store.Chat.on('change:unreadCount', (chat) => {window.onChatUnreadCountEvent(chat);});
|
||||
window.Store.PollVote.on('add', async (vote) => {
|
||||
const pollVoteModel = await window.WWebJS.getPollVoteModel(vote);
|
||||
pollVoteModel && window.onPollVoteEvent(pollVoteModel);
|
||||
});
|
||||
|
||||
if (window.compareWwebVersions(window.Debug.VERSION, '>=', '2.3000.1014111620')) {
|
||||
const module = window.Store.AddonReactionTable;
|
||||
|
|
@ -761,6 +758,39 @@ class Client extends EventEmitter {
|
|||
|
||||
return ogMethod(...args);
|
||||
}).bind(module);
|
||||
|
||||
const pollVoteModule = window.Store.AddonPollVoteTable;
|
||||
const ogPollVoteMethod = pollVoteModule.bulkUpsert;
|
||||
|
||||
pollVoteModule.bulkUpsert = (async (...args) => {
|
||||
const votes = await Promise.all(args[0].map(async vote => {
|
||||
const msgKey = vote.id;
|
||||
const parentMsgKey = vote.pollUpdateParentKey;
|
||||
const timestamp = vote.t / 1000;
|
||||
const sender = vote.author ?? vote.from;
|
||||
const senderUserJid = sender._serialized;
|
||||
|
||||
let parentMessage = window.Store.Msg.get(parentMsgKey._serialized);
|
||||
if (!parentMessage) {
|
||||
const fetched = await window.Store.Msg.getMessagesById([parentMsgKey._serialized]);
|
||||
parentMessage = fetched?.messages?.[0] || null;
|
||||
}
|
||||
|
||||
return {
|
||||
...vote,
|
||||
msgKey,
|
||||
sender,
|
||||
parentMsgKey,
|
||||
senderUserJid,
|
||||
timestamp,
|
||||
parentMessage
|
||||
};
|
||||
}));
|
||||
|
||||
window.onPollVoteEvent(votes);
|
||||
|
||||
return ogPollVoteMethod.apply(pollVoteModule, args);
|
||||
}).bind(pollVoteModule);
|
||||
} else {
|
||||
const module = window.Store.createOrUpdateReactionsModule;
|
||||
const ogMethod = module.createOrUpdateReactions;
|
||||
|
|
|
|||
|
|
@ -34,14 +34,22 @@ class PollVote extends Base {
|
|||
* may occur when they deselected all poll options
|
||||
* @type {SelectedPollOption[]}
|
||||
*/
|
||||
this.selectedOptions =
|
||||
data.selectedOptionLocalIds.length > 0
|
||||
? data.selectedOptionLocalIds.map((e) => ({
|
||||
if (data.selectedOptionLocalIds.length > 0) {
|
||||
if(data.parentMessage) { // temporary failsafe
|
||||
this.selectedOptions = data.selectedOptionLocalIds.map((e) => ({
|
||||
name: data.parentMessage.pollOptions.find((x) => x.localId === e).name,
|
||||
localId: e
|
||||
}))
|
||||
: [];
|
||||
|
||||
}));
|
||||
} else {
|
||||
this.selectedOptions = data.selectedOptionLocalIds.map((e) => ({
|
||||
name: undefined,
|
||||
localId: e
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
this.selectedOptions = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp the option was selected or deselected at
|
||||
* @type {number}
|
||||
|
|
@ -54,6 +62,12 @@ class PollVote extends Base {
|
|||
*/
|
||||
this.parentMessage = new Message(this.client, data.parentMessage);
|
||||
|
||||
/**
|
||||
* The poll creation message id
|
||||
* @type {Object}
|
||||
*/
|
||||
this.parentMsgKey = data.parentMsgKey;
|
||||
|
||||
return super._patch(data);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,6 +96,7 @@ exports.ExposeStore = () => {
|
|||
window.Store.DeviceList = window.require('WAWebApiDeviceList');
|
||||
window.Store.HistorySync = window.require('WAWebSendNonMessageDataRequest');
|
||||
window.Store.AddonReactionTable = window.require('WAWebAddonReactionTableMode').reactionTableMode;
|
||||
window.Store.AddonPollVoteTable = window.require('WAWebAddonPollVoteTableMode').pollVoteTableMode;
|
||||
window.Store.PinnedMsgUtils = window.require('WAWebPinInChatSchema');
|
||||
window.Store.ChatGetters = window.require('WAWebChatGetters');
|
||||
window.Store.PinnedMsgUtils = window.require('WAWebSendPinMessageAction');
|
||||
|
|
|
|||
|
|
@ -523,15 +523,6 @@ exports.LoadUtils = () => {
|
|||
return msg;
|
||||
};
|
||||
|
||||
window.WWebJS.getPollVoteModel = async (vote) => {
|
||||
const _vote = vote.serialize();
|
||||
if (!vote.parentMsgKey) return null;
|
||||
const msg =
|
||||
window.Store.Msg.get(vote.parentMsgKey) || (await window.Store.Msg.getMessagesById([vote.parentMsgKey]))?.messages?.[0];
|
||||
msg && (_vote.parentMessage = window.WWebJS.getMessageModel(msg));
|
||||
return _vote;
|
||||
};
|
||||
|
||||
window.WWebJS.getChat = async (chatId, { getAsModel = true } = {}) => {
|
||||
const isChannel = /@\w*newsletter\b/.test(chatId);
|
||||
const chatWid = window.Store.WidFactory.createWid(chatId);
|
||||
|
|
|
|||
Loading…
Reference in a new issue