Skip to content

Commit d7da7c2

Browse files
committed
webview: Implement voting on polls.
Related: zulip#3205
1 parent d2323bd commit d7da7c2

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,27 @@ var compiledWebviewJs = (function (exports) {
980980
return;
981981
}
982982
983+
if (target.matches('.poll-vote')) {
984+
var _messageElement = target.closest('.message');
985+
986+
if (_messageElement) {
987+
var current_vote = requireAttribute(target, 'data-voted') === 'true';
988+
var vote = current_vote ? -1 : 1;
989+
sendMessage({
990+
type: 'vote',
991+
messageId: requireNumericAttribute(_messageElement, 'data-msg-id'),
992+
key: requireAttribute(target, 'data-key'),
993+
vote: vote
994+
});
995+
target.setAttribute('data-voted', (!current_vote).toString());
996+
target.innerText = (parseInt(target.innerText, 10) + vote).toString();
997+
} else {
998+
throw new Error('Message element not found');
999+
}
1000+
1001+
return;
1002+
}
1003+
9831004
if (target.matches('time')) {
9841005
var originalText = requireAttribute(target, 'original-text');
9851006
sendMessage({

src/webview/js/js.js

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

907+
if (target.matches('.poll-vote')) {
908+
const messageElement = target.closest('.message');
909+
if (!messageElement) {
910+
throw new Error('Message element not found');
911+
}
912+
// This duplicates some logic from PollData.handle.vote.outbound, but it's
913+
// much simpler to just duplicate it than it is to thread a callback all
914+
// the way over here.
915+
const current_vote = requireAttribute(target, 'data-voted') === 'true';
916+
const vote = current_vote ? -1 : 1;
917+
sendMessage({
918+
type: 'vote',
919+
messageId: requireNumericAttribute(messageElement, 'data-msg-id'),
920+
key: requireAttribute(target, 'data-key'),
921+
vote,
922+
});
923+
target.setAttribute('data-voted', (!current_vote).toString());
924+
target.innerText = (parseInt(target.innerText, 10) + vote).toString();
925+
return;
926+
}
927+
907928
if (target.matches('time')) {
908929
const originalText = requireAttribute(target, 'original-text');
909930
sendMessage({

0 commit comments

Comments
 (0)