diff --git a/src/Client.js b/src/Client.js index 18d3bea..fa07986 100644 --- a/src/Client.js +++ b/src/Client.js @@ -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; diff --git a/src/structures/PollVote.js b/src/structures/PollVote.js index 4c7eb02..2d75ad6 100644 --- a/src/structures/PollVote.js +++ b/src/structures/PollVote.js @@ -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); } } diff --git a/src/util/Injected/Store.js b/src/util/Injected/Store.js index ba4043b..848b08f 100644 --- a/src/util/Injected/Store.js +++ b/src/util/Injected/Store.js @@ -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'); diff --git a/src/util/Injected/Utils.js b/src/util/Injected/Utils.js index d615945..bce694d 100644 --- a/src/util/Injected/Utils.js +++ b/src/util/Injected/Utils.js @@ -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);