feat(polls) fix spacing and send answer identifier
This commit is contained in:
parent
4c46396e6a
commit
68eb0795eb
|
@ -14,39 +14,39 @@ local is_healthcheck_room = util.is_healthcheck_room;
|
||||||
-- and that the message type pertains to the polls feature.
|
-- and that the message type pertains to the polls feature.
|
||||||
-- If yes, returns the parsed message. Otherwise, returns nil.
|
-- If yes, returns the parsed message. Otherwise, returns nil.
|
||||||
local function get_poll_message(stanza)
|
local function get_poll_message(stanza)
|
||||||
if stanza.attr.type ~= "groupchat" then
|
if stanza.attr.type ~= "groupchat" then
|
||||||
return nil;
|
return nil;
|
||||||
end
|
end
|
||||||
local json_data = stanza:get_child_text("json-message", "http://jitsi.org/jitmeet");
|
local json_data = stanza:get_child_text("json-message", "http://jitsi.org/jitmeet");
|
||||||
if json_data == nil then
|
if json_data == nil then
|
||||||
return nil;
|
return nil;
|
||||||
end
|
end
|
||||||
local data = json.decode(json_data);
|
local data = json.decode(json_data);
|
||||||
if not data or (data.type ~= "new-poll" and data.type ~= "answer-poll") then
|
if not data or (data.type ~= "new-poll" and data.type ~= "answer-poll") then
|
||||||
return nil;
|
return nil;
|
||||||
end
|
end
|
||||||
return data;
|
return data;
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Logs a warning and returns true if a room does not
|
-- Logs a warning and returns true if a room does not
|
||||||
-- have poll data associated with it.
|
-- have poll data associated with it.
|
||||||
local function check_polls(room)
|
local function check_polls(room)
|
||||||
if room.polls == nil then
|
if room.polls == nil then
|
||||||
module:log("warn", "no polls data in room");
|
module:log("warn", "no polls data in room");
|
||||||
return true;
|
return true;
|
||||||
end
|
end
|
||||||
return false;
|
return false;
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sets up poll data in new rooms.
|
-- Sets up poll data in new rooms.
|
||||||
module:hook("muc-room-created", function(event)
|
module:hook("muc-room-created", function(event)
|
||||||
local room = event.room;
|
local room = event.room;
|
||||||
if is_healthcheck_room(room.jid) then return end
|
if is_healthcheck_room(room.jid) then return end
|
||||||
module:log("debug", "setting up polls in room %s", room.jid);
|
module:log("debug", "setting up polls in room %s", room.jid);
|
||||||
room.polls = {
|
room.polls = {
|
||||||
by_id = {};
|
by_id = {};
|
||||||
order = {};
|
order = {};
|
||||||
};
|
};
|
||||||
end);
|
end);
|
||||||
|
|
||||||
-- Keeps track of the current state of the polls in each room,
|
-- Keeps track of the current state of the polls in each room,
|
||||||
|
@ -54,19 +54,19 @@ end);
|
||||||
-- and updating the room poll data accordingly.
|
-- and updating the room poll data accordingly.
|
||||||
-- This mirrors the client-side poll update logic.
|
-- This mirrors the client-side poll update logic.
|
||||||
module:hook("message/bare", function(event)
|
module:hook("message/bare", function(event)
|
||||||
local data = get_poll_message(event.stanza);
|
local data = get_poll_message(event.stanza);
|
||||||
if data == nil then return end
|
if data == nil then return end
|
||||||
|
|
||||||
local room = muc.get_room_from_jid(event.stanza.attr.to);
|
local room = muc.get_room_from_jid(event.stanza.attr.to);
|
||||||
|
|
||||||
if data.type == "new-poll" then
|
if data.type == "new-poll" then
|
||||||
if check_polls(room) then return end
|
if check_polls(room) then return end
|
||||||
|
|
||||||
local answers = {}
|
local answers = {}
|
||||||
local compactAnswers = {}
|
local compactAnswers = {}
|
||||||
for _, name in ipairs(data.answers) do
|
for i, name in ipairs(data.answers) do
|
||||||
table.insert(answers, { name = name, voters = {} });
|
table.insert(answers, { name = name, voters = {} });
|
||||||
table.insert(compactAnswers, {name = name});
|
table.insert(compactAnswers, { key = i, name = name});
|
||||||
end
|
end
|
||||||
|
|
||||||
local poll = {
|
local poll = {
|
||||||
|
@ -81,78 +81,79 @@ module:hook("message/bare", function(event)
|
||||||
table.insert(room.polls.order, poll)
|
table.insert(room.polls.order, poll)
|
||||||
|
|
||||||
local pollData = {
|
local pollData = {
|
||||||
event = event,
|
event = event,
|
||||||
room = room,
|
room = room,
|
||||||
poll = {
|
poll = {
|
||||||
pollId = data.pollId,
|
pollId = data.pollId,
|
||||||
senderId = data.senderId,
|
senderId = data.senderId,
|
||||||
senderName = data.senderName,
|
senderName = data.senderName,
|
||||||
question = data.question,
|
question = data.question,
|
||||||
answers = compactAnswers
|
answers = compactAnswers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module:fire_event("poll-created", pollData);
|
module:fire_event("poll-created", pollData);
|
||||||
|
|
||||||
elseif data.type == "answer-poll" then
|
elseif data.type == "answer-poll" then
|
||||||
if check_polls(room) then return end
|
if check_polls(room) then return end
|
||||||
|
|
||||||
local poll = room.polls.by_id[data.pollId];
|
local poll = room.polls.by_id[data.pollId];
|
||||||
if poll == nil then
|
if poll == nil then
|
||||||
module:log("warn", "answering inexistent poll");
|
module:log("warn", "answering inexistent poll");
|
||||||
return;
|
return;
|
||||||
end
|
end
|
||||||
|
|
||||||
local answers = {};
|
local answers = {};
|
||||||
for i, value in ipairs(data.answers) do
|
for i, value in ipairs(data.answers) do
|
||||||
table.insert(answers, {
|
table.insert(answers, {
|
||||||
value = value,
|
key = i,
|
||||||
name = poll.answers[i].name,
|
value = value,
|
||||||
});
|
name = poll.answers[i].name,
|
||||||
poll.answers[i].voters[data.voterId] = value and data.voterName or nil;
|
});
|
||||||
end
|
poll.answers[i].voters[data.voterId] = value and data.voterName or nil;
|
||||||
local answerData = {
|
end
|
||||||
event = event,
|
local answerData = {
|
||||||
room = room,
|
event = event,
|
||||||
pollId = poll.id,
|
room = room,
|
||||||
voterName = data.voterName,
|
pollId = poll.id,
|
||||||
voterId = data.voterId,
|
voterName = data.voterName,
|
||||||
answers = answers
|
voterId = data.voterId,
|
||||||
}
|
answers = answers
|
||||||
|
}
|
||||||
|
|
||||||
module:fire_event("answer-poll", answerData);
|
module:fire_event("answer-poll", answerData);
|
||||||
end
|
end
|
||||||
end);
|
end);
|
||||||
|
|
||||||
-- Sends the current poll state to new occupants after joining a room.
|
-- Sends the current poll state to new occupants after joining a room.
|
||||||
module:hook("muc-occupant-joined", function(event)
|
module:hook("muc-occupant-joined", function(event)
|
||||||
local room = event.room;
|
local room = event.room;
|
||||||
if is_healthcheck_room(room.jid) then return end
|
if is_healthcheck_room(room.jid) then return end
|
||||||
if room.polls == nil or #room.polls.order == 0 then
|
if room.polls == nil or #room.polls.order == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local data = {
|
local data = {
|
||||||
type = "old-polls",
|
type = "old-polls",
|
||||||
polls = {},
|
polls = {},
|
||||||
};
|
};
|
||||||
for i, poll in ipairs(room.polls.order) do
|
for i, poll in ipairs(room.polls.order) do
|
||||||
data.polls[i] = {
|
data.polls[i] = {
|
||||||
id = poll.id,
|
id = poll.id,
|
||||||
senderId = poll.sender_id,
|
senderId = poll.sender_id,
|
||||||
senderName = poll.sender_name,
|
senderName = poll.sender_name,
|
||||||
question = poll.question,
|
question = poll.question,
|
||||||
answers = poll.answers
|
answers = poll.answers
|
||||||
};
|
};
|
||||||
end
|
end
|
||||||
|
|
||||||
local stanza = st.message({
|
local stanza = st.message({
|
||||||
from = room.jid,
|
from = room.jid,
|
||||||
to = event.occupant.jid
|
to = event.occupant.jid
|
||||||
})
|
})
|
||||||
:tag("json-message", { xmlns = "http://jitsi.org/jitmeet" })
|
:tag("json-message", { xmlns = "http://jitsi.org/jitmeet" })
|
||||||
:text(json.encode(data))
|
:text(json.encode(data))
|
||||||
:up();
|
:up();
|
||||||
|
|
||||||
room:route_stanza(stanza);
|
room:route_stanza(stanza);
|
||||||
end);
|
end);
|
||||||
|
|
Loading…
Reference in New Issue