Poll WIP (#231):

* fix backend.
This commit is contained in:
John Livingston 2024-07-01 12:04:03 +02:00
parent b741959312
commit 5e6fd50c49
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
2 changed files with 21 additions and 5 deletions

View File

@ -10,7 +10,7 @@ local xmlns_replace = "urn:xmpp:message-correct:0";
local mod_muc = module:depends"muc";
local get_room_from_jid = mod_muc.get_room_from_jid;
local debounce_delay = 10; -- number of seconds during which we must group votes to avoid flood.
local debounce_delay = 5; -- number of seconds during which we must group votes to avoid flood.
local scheduled_updates = {};
-- construct the poll message stanza
@ -31,10 +31,11 @@ local function build_poll_message(room, message_id, is_end_message)
for choice, nb in pairs(current_poll.votes_by_choices) do
total = total + nb;
end
for choice, label in pairs(current_poll.choices) do
for _, choice_desc in ipairs(current_poll.choices_ordered) do
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
@ -76,6 +77,11 @@ local function send_poll_update_message(room)
if not room._data.current_poll then
return nil;
end
if room._data.current_poll.already_ended then
module:log("debug", "Cancelling the update message for room %s poll, because already_ended==true.", room.jid);
return nil;
end
module:log("debug", "Sending an update message for room %s poll", room.jid);
local message_id = id.medium(); -- generate a new id
local msg = build_poll_message(room, message_id, false);

View File

@ -98,7 +98,7 @@ local function create_poll(room, fields, occupant)
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 = {};
room._data.current_poll.choices = {};
room._data.current_poll.choices_ordered = {}; -- choices labels with numerical index, so we can have correct order
room._data.current_poll.already_ended = false;
room._data.current_poll.occupant_bare_jid = occupant.bare_jid;
room._data.current_poll.occupant_nick = occupant.nick;
@ -108,10 +108,20 @@ local function create_poll(room, fields, occupant)
if c then
if fields["muc#roompoll_choice" .. c]:find("%S") then
room._data.current_poll.votes_by_choices[c] = 0;
room._data.current_poll.choices[c] = fields["muc#roompoll_choice" .. c];
table.insert(room._data.current_poll.choices_ordered, {
number = c;
label = fields["muc#roompoll_choice" .. c];
});
end
end
end
table.sort(room._data.current_poll.choices_ordered, function(a, b)
if a.number == b.number then
return 0;
end
return tonumber(a.number) < tonumber(b.number);
end);
room._data.current_poll.start_message_id = poll_start_message(room);
schedule_poll_end(room.jid, room._data.current_poll.end_timestamp);
end