Skip to content

Commit 3dc3d70

Browse files
committed
webview: Implement voting on polls.
Related: zulip#3205
1 parent 32c5b35 commit 3dc3d70

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/webview/handleOutboundEvents.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ type WebViewOutboundEventTimeDetails = {|
131131
originalText: string,
132132
|};
133133

134+
type WebViewOutboundEventVote = {|
135+
type: 'vote',
136+
messageId: number,
137+
key: string,
138+
vote: number,
139+
|};
140+
134141
export type WebViewOutboundEvent =
135142
| WebViewOutboundEventReady
136143
| WebViewOutboundEventScroll
@@ -145,7 +152,8 @@ export type WebViewOutboundEvent =
145152
| WebViewOutboundEventWarn
146153
| WebViewOutboundEventError
147154
| WebViewOutboundEventMention
148-
| WebViewOutboundEventTimeDetails;
155+
| WebViewOutboundEventTimeDetails
156+
| WebViewOutboundEventVote;
149157

150158
// TODO: Consider completing this and making it exact, once
151159
// `MessageList`'s props are type-checked.
@@ -311,6 +319,19 @@ export const handleWebViewOutboundEvent = (
311319
break;
312320
}
313321

322+
case 'vote': {
323+
api.sendSubmessage(
324+
props.backgroundData.auth,
325+
event.messageId,
326+
JSON.stringify({
327+
type: 'vote',
328+
key: event.key,
329+
vote: event.vote,
330+
}),
331+
);
332+
break;
333+
}
334+
314335
case 'debug':
315336
console.debug(props, event); // eslint-disable-line
316337
break;

src/webview/js/generatedEs3.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,26 @@ var compiledWebviewJs = (function (exports) {
993993
return;
994994
}
995995
996+
if (target.matches('.poll-vote')) {
997+
var _messageElement = target.closest('.message');
998+
999+
if (!_messageElement) {
1000+
throw new Error('Message element not found');
1001+
}
1002+
1003+
var current_vote = requireAttribute(target, 'data-voted') === 'true';
1004+
var vote = current_vote ? -1 : 1;
1005+
sendMessage({
1006+
type: 'vote',
1007+
messageId: requireNumericAttribute(_messageElement, 'data-msg-id'),
1008+
key: requireAttribute(target, 'data-key'),
1009+
vote: vote
1010+
});
1011+
target.setAttribute('data-voted', (!current_vote).toString());
1012+
target.innerText = (parseInt(target.innerText, 10) + vote).toString();
1013+
return;
1014+
}
1015+
9961016
if (target.matches('time')) {
9971017
var originalText = requireAttribute(target, 'original-text');
9981018
sendMessage({

src/webview/js/js.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,27 @@ documentBody.addEventListener('click', (e: MouseEvent) => {
918918
return;
919919
}
920920

921+
if (target.matches('.poll-vote')) {
922+
const messageElement = target.closest('.message');
923+
if (!messageElement) {
924+
throw new Error('Message element not found');
925+
}
926+
// This duplicates some logic from PollData.handle.vote.outbound in
927+
// @zulip/shared/js/poll_data.js, but it's much simpler to just duplicate
928+
// it than it is to thread a callback all the way over here.
929+
const current_vote = requireAttribute(target, 'data-voted') === 'true';
930+
const vote = current_vote ? -1 : 1;
931+
sendMessage({
932+
type: 'vote',
933+
messageId: requireNumericAttribute(messageElement, 'data-msg-id'),
934+
key: requireAttribute(target, 'data-key'),
935+
vote,
936+
});
937+
target.setAttribute('data-voted', (!current_vote).toString());
938+
target.innerText = (parseInt(target.innerText, 10) + vote).toString();
939+
return;
940+
}
941+
921942
if (target.matches('time')) {
922943
const originalText = requireAttribute(target, 'original-text');
923944
sendMessage({

0 commit comments

Comments
 (0)