Skip to content

Commit be28fd0

Browse files
committed
webview: Implement voting on polls.
Related: zulip#3205
1 parent 740724e commit be28fd0

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
@@ -132,6 +132,13 @@ type WebViewOutboundEventTimeDetails = {|
132132
originalText: string,
133133
|};
134134

135+
type WebViewOutboundEventVote = {|
136+
type: 'vote',
137+
messageId: number,
138+
key: string,
139+
vote: number,
140+
|};
141+
135142
export type WebViewOutboundEvent =
136143
| WebViewOutboundEventReady
137144
| WebViewOutboundEventScroll
@@ -146,7 +153,8 @@ export type WebViewOutboundEvent =
146153
| WebViewOutboundEventWarn
147154
| WebViewOutboundEventError
148155
| WebViewOutboundEventMention
149-
| WebViewOutboundEventTimeDetails;
156+
| WebViewOutboundEventTimeDetails
157+
| WebViewOutboundEventVote;
150158

151159
// TODO: Consider completing this and making it exact, once
152160
// `MessageList`'s props are type-checked.
@@ -313,6 +321,19 @@ export const handleWebViewOutboundEvent = (
313321
break;
314322
}
315323

324+
case 'vote': {
325+
api.sendSubmessage(
326+
props.backgroundData.auth,
327+
event.messageId,
328+
JSON.stringify({
329+
type: 'vote',
330+
key: event.key,
331+
vote: event.vote,
332+
}),
333+
);
334+
break;
335+
}
336+
316337
case 'debug':
317338
console.debug(props, event); // eslint-disable-line
318339
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)