Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vote_update event #3207

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix vote_update event
  • Loading branch information
Bene committed Jul 18, 2024
commit 64cd50e414482e15d3a903f4271949b20ff53319
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
ClientInfo: require('./src/structures/ClientInfo'),
Location: require('./src/structures/Location'),
Poll: require('./src/structures/Poll'),
PollVote: require('./src/structures/PollVote'),
ProductMetadata: require('./src/structures/ProductMetadata'),
List: require('./src/structures/List'),
Buttons: require('./src/structures/Buttons'),
Expand Down
36 changes: 22 additions & 14 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { LoadUtils } = require('./util/Injected/Utils');
const ChatFactory = require('./factories/ChatFactory');
const ContactFactory = require('./factories/ContactFactory');
const WebCacheFactory = require('./webCache/WebCacheFactory');
const { ClientInfo, Message, MessageMedia, Contact, Location, Poll, GroupNotification, Label, Call, Buttons, List, Reaction } = require('./structures');
const { ClientInfo, Message, MessageMedia, Contact, Location, Poll, PollVote, GroupNotification, Label, Call, Buttons, List, Reaction } = require('./structures');
const NoAuth = require('./authStrategies/NoAuth');

/**
Expand Down Expand Up @@ -683,6 +683,16 @@ class Client extends EventEmitter {
*/
this.emit(Events.MESSAGE_CIPHERTEXT, new Message(this, msg));
});

await this.pupPage.exposeFunction('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 this.pupPage.evaluate(() => {
Expand Down Expand Up @@ -710,6 +720,11 @@ class Client extends EventEmitter {
});
window.Store.Chat.on('change:unreadCount', (chat) => {window.onChatUnreadCountEvent(chat);});

window.Store.PollVote.on('add', (vote) => {
const pollVoteModel = window.WWebJS.getPollVoteModel(vote);
pollVoteModel && window.onPollVoteEvent(pollVoteModel);
});

if (window.compareWwebVersions(window.Debug.VERSION, '>=', '2.3000.1014111620')) {
const module = window.Store.AddonReactionTable;
const ogMethod = module.bulkUpsert;
Expand Down Expand Up @@ -777,7 +792,7 @@ class Client extends EventEmitter {
/**
* Closes the client
*/
async destroy() {
async destroy() {
await this.pupBrowser.close();
await this.authStrategy.destroy();
}
Expand All @@ -789,6 +804,7 @@ class Client extends EventEmitter {
await this.pupPage.evaluate(() => {
return window.Store.AppState.logout();
});

await this.pupBrowser.close();

let maxDelay = 0;
Expand Down Expand Up @@ -1399,18 +1415,10 @@ class Client extends EventEmitter {

try {
createGroupResult = await window.Store.GroupUtils.createGroup(
{
'memberAddMode': options.memberAddMode === undefined ? true : options.memberAddMode,
'membershipApprovalMode': options.membershipApprovalMode === undefined ? false : options.membershipApprovalMode,
'announce': options.announce === undefined ? true : options.announce,
'ephemeralDuration': messageTimer,
'full': undefined,
'parentGroupId': parentGroupWid,
'restrict': options.restrict === undefined ? true : options.restrict,
'thumb': undefined,
'title': title,
},
participantWids
title,
participantWids,
messageTimer,
parentGroupWid
);
} catch (err) {
return 'CreateGroupError: An unknown error occupied while creating a group';
Expand Down
61 changes: 61 additions & 0 deletions src/structures/PollVote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const Message = require('./Message');
const Base = require('./Base');

/**
* Selected poll option structure
* @typedef {Object} SelectedPollOption
* @property {number} id The local selected or deselected option ID
* @property {string} name The option name
*/

/**
* Represents a Poll Vote on WhatsApp
* @extends {Base}
*/
class PollVote extends Base {
constructor(client, data) {
super(client);

if (data) this._patch(data);
}

_patch(data) {
/**
* The person who voted
* @type {string}
*/
this.voter = data.sender;

/**
* The selected poll option(s)
* If it's an empty array, the user hasn't selected any options on the poll,
* may occur when they deselected all poll options
* @type {SelectedPollOption[]}
*/
this.selectedOptions =
data.selectedOptionLocalIds.length > 0
? data.selectedOptionLocalIds.map((e) => ({
name: data.parentMessage.pollOptions.find((x) => x.localId === e).name,
localId: e
}))
: [];

/**
* Timestamp the option was selected or deselected at
* @type {number}
*/
this.interractedAtTs = data.senderTimestampMs;

/**
* The poll creation message associated with the poll vote
* @type {Message}
*/
this.parentMessage = new Message(this.client, data.parentMessage);

return super._patch(data);
}
}

module.exports = PollVote;
1 change: 1 addition & 0 deletions src/structures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ module.exports = {
Payment: require('./Payment'),
Reaction: require('./Reaction'),
Poll: require('./Poll'),
PollVote: require('./PollVote'),
};
3 changes: 2 additions & 1 deletion src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ exports.Events = {
STATE_CHANGED: 'change_state',
BATTERY_CHANGED: 'change_battery',
INCOMING_CALL: 'call',
REMOTE_SESSION_SAVED: 'remote_session_saved'
REMOTE_SESSION_SAVED: 'remote_session_saved',
VOTE_UPDATE: 'vote_update',
};

/**
Expand Down
9 changes: 9 additions & 0 deletions src/util/Injected/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,15 @@ exports.LoadUtils = () => {
return msg;
};

window.WWebJS.getPollVoteModel = (vote) => {
const _vote = vote.serialize();
if (vote.parentMsgKey) {
const msg = window.Store.Msg.get(vote.parentMsgKey);
msg && (_vote.parentMessage = window.WWebJS.getMessageModel(msg));
return _vote;
}
return null;
};

window.WWebJS.getChatModel = async chat => {

Expand Down
Loading