feat(polls): trigger events for poll created and answered (#10249)
Co-authored-by: alexbratu92 <alexbratu92@gmail.com>
This commit is contained in:
parent
4767ef497f
commit
4c46396e6a
|
@ -62,20 +62,37 @@ module:hook("message/bare", function(event)
|
||||||
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 = {}
|
||||||
for _, name in ipairs(data.answers) do
|
local compactAnswers = {}
|
||||||
table.insert(answers, { name = name, voters = {} });
|
for _, name in ipairs(data.answers) do
|
||||||
end
|
table.insert(answers, { name = name, voters = {} });
|
||||||
|
table.insert(compactAnswers, {name = name});
|
||||||
|
end
|
||||||
|
|
||||||
local poll = {
|
local poll = {
|
||||||
id = data.pollId,
|
id = data.pollId,
|
||||||
sender_id = data.senderId,
|
sender_id = data.senderId,
|
||||||
sender_name = data.senderName,
|
sender_name = data.senderName,
|
||||||
question = data.question,
|
question = data.question,
|
||||||
answers = answers
|
answers = answers
|
||||||
};
|
};
|
||||||
room.polls.by_id[data.pollId] = poll
|
|
||||||
table.insert(room.polls.order, poll)
|
room.polls.by_id[data.pollId] = poll
|
||||||
|
table.insert(room.polls.order, poll)
|
||||||
|
|
||||||
|
local pollData = {
|
||||||
|
event = event,
|
||||||
|
room = room,
|
||||||
|
poll = {
|
||||||
|
pollId = data.pollId,
|
||||||
|
senderId = data.senderId,
|
||||||
|
senderName = data.senderName,
|
||||||
|
question = data.question,
|
||||||
|
answers = compactAnswers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -86,9 +103,24 @@ module:hook("message/bare", function(event)
|
||||||
return;
|
return;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local answers = {};
|
||||||
for i, value in ipairs(data.answers) do
|
for i, value in ipairs(data.answers) do
|
||||||
|
table.insert(answers, {
|
||||||
|
value = value,
|
||||||
|
name = poll.answers[i].name,
|
||||||
|
});
|
||||||
poll.answers[i].voters[data.voterId] = value and data.voterName or nil;
|
poll.answers[i].voters[data.voterId] = value and data.voterName or nil;
|
||||||
end
|
end
|
||||||
|
local answerData = {
|
||||||
|
event = event,
|
||||||
|
room = room,
|
||||||
|
pollId = poll.id,
|
||||||
|
voterName = data.voterName,
|
||||||
|
voterId = data.voterId,
|
||||||
|
answers = answers
|
||||||
|
}
|
||||||
|
|
||||||
|
module:fire_event("answer-poll", answerData);
|
||||||
end
|
end
|
||||||
end);
|
end);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue