Poll WIP (#231):

* mod_muc_poll
* feature detection and create poll button in Converse
This commit is contained in:
John Livingston
2024-06-27 19:56:12 +02:00
parent 241065e683
commit b792588364
9 changed files with 171 additions and 1 deletions

View File

@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
export const XMLNS_POLL = 'http://jabber.org/protocol/muc#x-poll'

View File

@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
import { _converse, converse } from '../../../src/headless/core.js'
import { getHeadingButtons } from './utils.js'
// import { XMLNS_POLL } from './constants.js'
converse.plugins.add('livechat-converse-poll', {
dependencies: ['converse-muc', 'converse-disco'],
initialize () {
// _converse.api.listen.on('chatRoomInitialized', muc => {
// muc.features.on('change:' + XMLNS_POLL, () => {
// // TODO: refresh headingbuttons?
// })
// })
// adding the poll actions in the MUC heading buttons:
_converse.api.listen.on('getHeadingButtons', getHeadingButtons)
}
})

View File

@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
import { XMLNS_POLL } from './constants.js'
import { converse, _converse, api } from '../../../src/headless/core.js'
import { __ } from 'i18n'
const $iq = converse.env.$iq
function _fetchPollForm (mucModel) {
return api.sendIQ(
$iq({
to: mucModel.get('jid'),
type: 'get'
}).c('query', { xmlns: XMLNS_POLL })
)
}
export function getHeadingButtons (view, buttons) {
const muc = view.model
if (muc.get('type') !== _converse.CHATROOMS_TYPE) {
// only on MUC.
return buttons
}
if (!muc.features?.get?.(XMLNS_POLL)) {
// Poll feature not available (can happen if the chat is remote, and the plugin not up to date)
return buttons
}
const myself = muc.getOwnOccupant()
if (!myself || !['admin', 'owner'].includes(myself.get('affiliation'))) {
return buttons
}
// Adding a "New poll" button.
buttons.unshift({
// eslint-disable-next-line no-undef
i18n_text: __(LOC_new_poll),
handler: async (ev) => {
ev.preventDefault()
const r = await _fetchPollForm(muc)
// FIXME
console.info('Received poll form', r)
},
a_class: '',
icon_class: 'fa-list-check', // FIXME
name: 'muc-create-poll'
})
return buttons
}