From e97c33cec5ad7a8b544a1b54c95d45c2ce27126c Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 5 Jul 2024 16:34:51 +0200 Subject: [PATCH] Polls: notifier on votes --- client/@types/global.d.ts | 2 ++ client/utils/conversejs.ts | 11 +++++++++++ .../custom/plugins/poll/components/poll-view.js | 13 +++++++++++-- languages/en.yml | 1 + languages/fr.yml | 1 + 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/client/@types/global.d.ts b/client/@types/global.d.ts index 1398da00..890ad8ab 100644 --- a/client/@types/global.d.ts +++ b/client/@types/global.d.ts @@ -128,3 +128,5 @@ declare const LOC_TOKEN_ACTION_CREATE: string declare const LOC_TOKEN_ACTION_REVOKE: string declare const LOC_TOKEN_DEFAULT_LABEL: string declare const LOC_TOKEN_ACTION_REVOKE_CONFIRM: string + +declare const LOC_POLL_VOTE_OK: string diff --git a/client/utils/conversejs.ts b/client/utils/conversejs.ts index 276b0bc5..89936971 100644 --- a/client/utils/conversejs.ts +++ b/client/utils/conversejs.ts @@ -17,6 +17,8 @@ declare global { } } +let pollListenerInitiliazed: boolean = false + /** * load the ConverseJS CSS. * @param url CSS url @@ -164,6 +166,15 @@ async function displayConverseJS ( } const converseJSParams: InitConverseJSParams = await (response).json() + if (!pollListenerInitiliazed) { + // First time we got here, initiliaze this event: + const i18nVoteOk = await clientOptions.peertubeHelpers.translate(LOC_POLL_VOTE_OK) + pollListenerInitiliazed = true + document.addEventListener('livechat-poll-vote', () => { + clientOptions.peertubeHelpers.notifier.success(i18nVoteOk) + }) + } + await loadConverseJS(converseJSParams) await window.initConverse(converseJSParams, chatIncludeMode, authHeader ?? null) } diff --git a/conversejs/custom/plugins/poll/components/poll-view.js b/conversejs/custom/plugins/poll/components/poll-view.js index 6f7410c4..9973aa99 100644 --- a/conversejs/custom/plugins/poll/components/poll-view.js +++ b/conversejs/custom/plugins/poll/components/poll-view.js @@ -47,7 +47,7 @@ export default class MUCPollView extends CustomElement { this.collapsed = !this.collapsed } - voteFor (choice) { + async voteFor (choice) { if (this.buttonDisabled) { return } const currentPoll = this.model?.get('current_poll') @@ -59,9 +59,18 @@ export default class MUCPollView extends CustomElement { this.buttonDisabled = true this.requestUpdate() - this.model.sendMessage({ + await this.model.sendMessage({ body: '!' + choice.choice }) + + // Dispatching an event. + // When in Peertube interface, this will open a Peertube notifier with a message. + // FIXME: we should only trigger this on the message echo or bounce, + // but seems ConverseJs does not provide any promise for that. + const event = new Event('livechat-poll-vote', { + bubbles: true + }) + this.dispatchEvent(event) } closePoll (ev) { diff --git a/languages/en.yml b/languages/en.yml index fd54f01a..08e9aaab 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -578,3 +578,4 @@ poll_vote_instructions_xmpp: "Send a message with an exclamation mark followed b poll_is_over: This poll is now over. poll_choice_invalid: This choice is not valid. poll_anonymous_vote_ok: Your vote is taken into account. Votes are anonymous, they will not be shown to other participants. +poll_vote_ok: Your vote has been taking into account, the counters will be updated in a moment. diff --git a/languages/fr.yml b/languages/fr.yml index a638dfc7..9bc36708 100644 --- a/languages/fr.yml +++ b/languages/fr.yml @@ -559,3 +559,4 @@ poll_vote_instructions: "Pour voter, cliquez sur votre choix, ou envoyez un mess poll_is_over: Ce sondage est à présent terminé. poll_choice_invalid: Ce choix n'est pas valide. poll_anonymous_vote_ok: Votre vote a été pris en compte. Les votes sont anonymes, ils ne seront pas montrés aux autres participant⋅es. +poll_vote_ok: Votre vote a été pris en compte, les compteurs seront mis à jour dans un instant.