Poll WIP (#231):

* backend: custom xml
This commit is contained in:
John Livingston 2024-07-01 12:36:32 +02:00
parent 5e6fd50c49
commit c5b0176e95
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
3 changed files with 42 additions and 1 deletions

View File

@ -3,7 +3,15 @@
-- FIXME: create a XEP to standardize this, and remove the "x-".
local xmlns_poll = "http://jabber.org/protocol/muc#x-poll";
local xmlns_poll_message = "http://jabber.org/protocol/muc#x-poll-message";
local poll_message_tag = "x-poll";
local poll_question_tag = "x-poll-question";
local poll_choice_tag = "x-poll-choice";
return {
xmlns_poll = xmlns_poll;
xmlns_poll_message = xmlns_poll_message;
poll_message_tag = poll_message_tag;
poll_question_tag = poll_question_tag;
poll_choice_tag = poll_choice_tag;
};

View File

@ -6,6 +6,10 @@ local st = require "util.stanza";
local timer = require "util.timer";
local xmlns_occupant_id = "urn:xmpp:occupant-id:0";
local xmlns_replace = "urn:xmpp:message-correct:0";
local xmlns_poll_message = module:require("constants").xmlns_poll_message;
local poll_message_tag = module:require("constants").poll_message_tag;
local poll_question_tag = module:require("constants").poll_question_tag;
local poll_choice_tag = module:require("constants").poll_choice_tag;
local mod_muc = module:depends"muc";
local get_room_from_jid = mod_muc.get_room_from_jid;
@ -35,7 +39,7 @@ local function build_poll_message(room, message_id, is_end_message)
local choice, label = choice_desc.number, choice_desc.label;
content = content .. choice .. ': ' .. label;
if total > 0 then
local nb = current_poll.votes_by_choices["" .. choice] or 0;
local nb = current_poll.votes_by_choices[choice] or 0;
local percent = string.format("%.2f", nb * 100 / total);
content = content .. " (" .. nb .. "/" .. total .. " = " .. percent .. "%)";
end
@ -57,6 +61,33 @@ local function build_poll_message(room, message_id, is_end_message)
id = current_poll.occupant_id
}):up();
-- now we must add some custom XML data, so that compatible clients can display the poll as they want:
-- <x-poll xmlns="http://jabber.org/protocol/muc#x-poll-message" id="I9UWyoxsz4BN" votes="1" over="">
-- <x-poll-question>Poll question</x-poll-question>
-- <x-poll-choice choice="1" votes="0">Choice 1 label</x-poll-choice>
-- <x-poll-choice choice="2" votes="1">Choice 2 label</x-poll-choice>
-- <x-poll-choice choice="3" votes="0">Choice 3 label</x-poll-choice>
-- <x-poll-choice choice="4" votes="0">Choice 4 label</x-poll-choice>
-- </x-poll>
local message_attrs = {
xmlns = xmlns_poll_message,
id = current_poll.poll_id,
votes = "" .. total
};
if current_poll.already_ended then
message_attrs["over"] = "";
end
msg:tag(poll_message_tag, message_attrs):text_tag(poll_question_tag, current_poll["muc#roompoll_question"], {});
for _, choice_desc in ipairs(current_poll.choices_ordered) do
local choice, label = choice_desc.number, choice_desc.label;
local nb = current_poll.votes_by_choices[choice] or 0;
msg:text_tag(poll_choice_tag, label, {
votes = "" .. nb,
choice = choice
});
end
msg:up();
return msg;
end

View File

@ -1,6 +1,7 @@
-- SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
-- SPDX-License-Identifier: AGPL-3.0-only
local id = require "util.id";
local st = require "util.stanza";
local get_time = require "util.time".now;
local timer = require "util.timer";
@ -95,6 +96,7 @@ end
local function create_poll(room, fields, occupant)
module:log("debug", "Creating a new poll for room %s, by %s", room.jid, occupant.bare_jid);
room._data.current_poll = fields;
room._data.current_poll.poll_id = id.short();
room._data.current_poll.end_timestamp = get_time() + (60 * fields["muc#roompoll_duration"]);
room._data.current_poll.votes_by_occupant = {};
room._data.current_poll.votes_by_choices = {};