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

feat: vote_update event #2596

Merged
merged 48 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
88692e7
Merge branch 'add-polls' into polls-ext
Oct 9, 2023
55a386d
added listener to poll votes
Oct 9, 2023
52f79e1
added isUnvote property
Oct 9, 2023
075e45c
Merge branch 'main' into polls-ext
Oct 10, 2023
2867009
Merge branch 'main' into polls-ext
Oct 11, 2023
6817d18
Merge branch 'main' into polls-ext
Oct 11, 2023
fb7ade3
Merge branch 'main' into polls-ext
Oct 18, 2023
d68aac1
added option for custom message secret
Oct 19, 2023
fadc728
usage example updated
Oct 19, 2023
ec2f59a
Merge branch 'main' into polls-ext
Oct 21, 2023
667d488
added logic for catching removed votes
Oct 22, 2023
579bcb8
usage example updated
Oct 22, 2023
d6751cd
Merge branch 'main' into polls-ext
Oct 22, 2023
4c7e11e
fixed logic for poll vote events
Oct 24, 2023
6081db6
temp usage example
Oct 24, 2023
6d0bec6
No more `isCurrentState`
tuyuribr Oct 24, 2023
ba4995a
messageSecret converted to array
Oct 25, 2023
d5c1ec5
docs updated
Oct 25, 2023
a9fad78
we already have messageSecret in vote.parentMessage
alechkos Oct 25, 2023
1698df2
PollVote.selectedOptions changed to be similar to pollOptions
alechkos Oct 25, 2023
b4d6834
docs fixed
alechkos Oct 26, 2023
e7d8c39
window.injectToFunction clarifications
alechkos Nov 1, 2023
70e9729
minor clarifications
alechkos Nov 2, 2023
20d238e
Merge branch 'main' into polls-ext
alechkos Nov 24, 2023
dad71ed
Merge branch 'main' into polls-ext
alechkos Nov 26, 2023
8292c41
Merge branch 'main' into polls-ext
alechkos Dec 1, 2023
1155a87
fix: typo
alechkos Dec 1, 2023
64d5e84
Merge branch 'main' into polls-ext
alechkos Dec 1, 2023
176e0bb
Merge branch 'main' into polls-ext
alechkos Dec 5, 2023
eb71469
Merge branch 'main' into polls-ext
alechkos Dec 7, 2023
5f9ac13
Merge branch 'main' into polls-ext
alechkos Dec 12, 2023
eb7b300
Merge branch 'main' into polls-ext
alechkos Dec 12, 2023
22a3f9f
Merge branch 'main' into polls-ext
alechkos Dec 12, 2023
907f532
Merge branch 'main' into polls-ext
alechkos Dec 16, 2023
8c6c965
Merge branch 'main' into polls-ext
alechkos Dec 17, 2023
9ae8f99
Merge branch 'main' into polls-ext
alechkos Jan 2, 2024
2d83d60
Merge branch 'main' into polls-ext
alechkos Jan 11, 2024
46d1183
Merge branch 'main' into polls-ext
alechkos Jan 11, 2024
88dfb08
Merge branch 'main' into polls-ext
alechkos Jan 14, 2024
b359a51
Merge branch 'main' into polls-ext
alechkos Jan 18, 2024
05ce064
Merge branch 'main' into polls-ext
alechkos Mar 1, 2024
f3c9763
Merge branch 'main' into polls-ext
alechkos Apr 3, 2024
5d3d4dc
Merge branch 'main' into polls-ext
alechkos Apr 22, 2024
2c3313e
style: fix broken link in readme file
alechkos Apr 22, 2024
b42690a
Merge branch 'main' into polls-ext
alechkos Apr 27, 2024
61c2eae
Merge branch 'main' into polls-ext
alechkos Apr 28, 2024
a92a95b
Merge branch 'main' into polls-ext
alechkos Apr 29, 2024
bee5d1b
refactor: restore default obj prop
alechkos Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,9 @@ client.on('group_membership_request', async (notification) => {
/** You can approve or reject the newly appeared membership request: */
await client.approveGroupMembershipRequestss(notification.chatId, notification.author);
await client.rejectGroupMembershipRequests(notification.chatId, notification.author);
});
});

client.on('vote_update', (vote) => {
/** The vote that was affected: */
console.log(vote);
});
36 changes: 36 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ declare namespace WAWebJS {

/** Emitted when the RemoteAuth session is saved successfully on the external Database */
on(event: 'remote_session_saved', listener: () => void): this

/**
* Emitted when some poll option is selected or deselected,
* shows a user's current selected option(s) on the poll
*/
on(event: 'vote_update', listener: (
vote: PollVote
) => void): this
}

/** Current connection information */
Expand Down Expand Up @@ -1018,6 +1026,34 @@ declare namespace WAWebJS {
constructor(pollName: string, pollOptions: Array<string>, options?: PollSendOptions)
}

/** Represents a Poll Vote on WhatsApp */
export interface PollVote {
/** The person who voted */
voter: string;

/**
* 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
*/
selectedOptions: SelectedPollOption[];

/** Timestamp the option was selected or deselected at */
interractedAtTs: number;

/** The poll creation message associated with the poll vote */
parentMessage: Message;
}

/** Selected poll option structure */
export interface SelectedPollOption {
/** The local selected option ID */
id: number;

/** The option name */
name: string;
}

export interface Label {
/** Label name */
name: string,
Expand Down
17 changes: 16 additions & 1 deletion src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { ExposeStore, LoadUtils } = require('./util/Injected');
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 LegacySessionAuth = require('./authStrategies/LegacySessionAuth');
const NoAuth = require('./authStrategies/NoAuth');

Expand Down Expand Up @@ -54,6 +54,7 @@ const NoAuth = require('./authStrategies/NoAuth');
* @fires Client#contact_changed
* @fires Client#group_admin_changed
* @fires Client#group_membership_request
* @fires Client#vote_update
*/
class Client extends EventEmitter {
constructor(options = {}) {
Expand Down Expand Up @@ -685,6 +686,16 @@ class Client extends EventEmitter {
this.emit(Events.MESSAGE_CIPHERTEXT, new Message(this, msg));
});

await page.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 page.evaluate(() => {
window.Store.Msg.on('change', (msg) => { window.onChangeMessageEvent(window.WWebJS.getMessageModel(msg)); });
window.Store.Msg.on('change:type', (msg) => { window.onChangeMessageTypeEvent(window.WWebJS.getMessageModel(msg)); });
Expand All @@ -709,6 +720,10 @@ 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);
});

{
const module = window.Store.createOrUpdateReactionsModule;
Expand Down
1 change: 1 addition & 0 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class Message extends Base {
this.allowMultipleAnswers = Boolean(!data.pollSelectableOptionsCount);
this.pollInvalidated = data.pollInvalidated;
this.isSentCagPollCreation = data.isSentCagPollCreation;
this.messageSecret = Object.keys(data.messageSecret).map((key) => data.messageSecret[key]);
}

return super._patch(data);
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.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,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