|
@@ -14,39 +14,39 @@ local is_healthcheck_room = util.is_healthcheck_room;
|
14
|
14
|
-- and that the message type pertains to the polls feature.
|
15
|
15
|
-- If yes, returns the parsed message. Otherwise, returns nil.
|
16
|
16
|
local function get_poll_message(stanza)
|
17
|
|
- if stanza.attr.type ~= "groupchat" then
|
18
|
|
- return nil;
|
19
|
|
- end
|
20
|
|
- local json_data = stanza:get_child_text("json-message", "http://jitsi.org/jitmeet");
|
21
|
|
- if json_data == nil then
|
22
|
|
- return nil;
|
23
|
|
- end
|
24
|
|
- local data = json.decode(json_data);
|
25
|
|
- if not data or (data.type ~= "new-poll" and data.type ~= "answer-poll") then
|
26
|
|
- return nil;
|
27
|
|
- end
|
28
|
|
- return data;
|
|
17
|
+ if stanza.attr.type ~= "groupchat" then
|
|
18
|
+ return nil;
|
|
19
|
+ end
|
|
20
|
+ local json_data = stanza:get_child_text("json-message", "http://jitsi.org/jitmeet");
|
|
21
|
+ if json_data == nil then
|
|
22
|
+ return nil;
|
|
23
|
+ end
|
|
24
|
+ local data = json.decode(json_data);
|
|
25
|
+ if not data or (data.type ~= "new-poll" and data.type ~= "answer-poll") then
|
|
26
|
+ return nil;
|
|
27
|
+ end
|
|
28
|
+ return data;
|
29
|
29
|
end
|
30
|
30
|
|
31
|
31
|
-- Logs a warning and returns true if a room does not
|
32
|
32
|
-- have poll data associated with it.
|
33
|
33
|
local function check_polls(room)
|
34
|
|
- if room.polls == nil then
|
35
|
|
- module:log("warn", "no polls data in room");
|
36
|
|
- return true;
|
37
|
|
- end
|
38
|
|
- return false;
|
|
34
|
+ if room.polls == nil then
|
|
35
|
+ module:log("warn", "no polls data in room");
|
|
36
|
+ return true;
|
|
37
|
+ end
|
|
38
|
+ return false;
|
39
|
39
|
end
|
40
|
40
|
|
41
|
41
|
-- Sets up poll data in new rooms.
|
42
|
42
|
module:hook("muc-room-created", function(event)
|
43
|
|
- local room = event.room;
|
44
|
|
- if is_healthcheck_room(room.jid) then return end
|
45
|
|
- module:log("debug", "setting up polls in room %s", room.jid);
|
46
|
|
- room.polls = {
|
47
|
|
- by_id = {};
|
48
|
|
- order = {};
|
49
|
|
- };
|
|
43
|
+ local room = event.room;
|
|
44
|
+ if is_healthcheck_room(room.jid) then return end
|
|
45
|
+ module:log("debug", "setting up polls in room %s", room.jid);
|
|
46
|
+ room.polls = {
|
|
47
|
+ by_id = {};
|
|
48
|
+ order = {};
|
|
49
|
+ };
|
50
|
50
|
end);
|
51
|
51
|
|
52
|
52
|
-- Keeps track of the current state of the polls in each room,
|
|
@@ -54,19 +54,19 @@ end);
|
54
|
54
|
-- and updating the room poll data accordingly.
|
55
|
55
|
-- This mirrors the client-side poll update logic.
|
56
|
56
|
module:hook("message/bare", function(event)
|
57
|
|
- local data = get_poll_message(event.stanza);
|
58
|
|
- if data == nil then return end
|
|
57
|
+ local data = get_poll_message(event.stanza);
|
|
58
|
+ if data == nil then return end
|
59
|
59
|
|
60
|
|
- local room = muc.get_room_from_jid(event.stanza.attr.to);
|
|
60
|
+ local room = muc.get_room_from_jid(event.stanza.attr.to);
|
61
|
61
|
|
62
|
|
- if data.type == "new-poll" then
|
63
|
|
- if check_polls(room) then return end
|
|
62
|
+ if data.type == "new-poll" then
|
|
63
|
+ if check_polls(room) then return end
|
64
|
64
|
|
65
|
65
|
local answers = {}
|
66
|
66
|
local compactAnswers = {}
|
67
|
|
- for _, name in ipairs(data.answers) do
|
|
67
|
+ for i, name in ipairs(data.answers) do
|
68
|
68
|
table.insert(answers, { name = name, voters = {} });
|
69
|
|
- table.insert(compactAnswers, {name = name});
|
|
69
|
+ table.insert(compactAnswers, { key = i, name = name});
|
70
|
70
|
end
|
71
|
71
|
|
72
|
72
|
local poll = {
|
|
@@ -81,78 +81,79 @@ module:hook("message/bare", function(event)
|
81
|
81
|
table.insert(room.polls.order, poll)
|
82
|
82
|
|
83
|
83
|
local pollData = {
|
84
|
|
- event = event,
|
85
|
|
- room = room,
|
86
|
|
- poll = {
|
|
84
|
+ event = event,
|
|
85
|
+ room = room,
|
|
86
|
+ poll = {
|
87
|
87
|
pollId = data.pollId,
|
88
|
88
|
senderId = data.senderId,
|
89
|
89
|
senderName = data.senderName,
|
90
|
90
|
question = data.question,
|
91
|
91
|
answers = compactAnswers
|
92
|
|
- }
|
|
92
|
+ }
|
93
|
93
|
}
|
94
|
94
|
|
95
|
95
|
module:fire_event("poll-created", pollData);
|
96
|
96
|
|
97
|
|
- elseif data.type == "answer-poll" then
|
98
|
|
- if check_polls(room) then return end
|
99
|
|
-
|
100
|
|
- local poll = room.polls.by_id[data.pollId];
|
101
|
|
- if poll == nil then
|
102
|
|
- module:log("warn", "answering inexistent poll");
|
103
|
|
- return;
|
104
|
|
- end
|
105
|
|
-
|
106
|
|
- local answers = {};
|
107
|
|
- for i, value in ipairs(data.answers) do
|
108
|
|
- table.insert(answers, {
|
109
|
|
- value = value,
|
110
|
|
- name = poll.answers[i].name,
|
111
|
|
- });
|
112
|
|
- poll.answers[i].voters[data.voterId] = value and data.voterName or nil;
|
113
|
|
- end
|
114
|
|
- local answerData = {
|
115
|
|
- event = event,
|
116
|
|
- room = room,
|
117
|
|
- pollId = poll.id,
|
118
|
|
- voterName = data.voterName,
|
119
|
|
- voterId = data.voterId,
|
120
|
|
- answers = answers
|
121
|
|
- }
|
122
|
|
-
|
123
|
|
- module:fire_event("answer-poll", answerData);
|
124
|
|
- end
|
|
97
|
+ elseif data.type == "answer-poll" then
|
|
98
|
+ if check_polls(room) then return end
|
|
99
|
+
|
|
100
|
+ local poll = room.polls.by_id[data.pollId];
|
|
101
|
+ if poll == nil then
|
|
102
|
+ module:log("warn", "answering inexistent poll");
|
|
103
|
+ return;
|
|
104
|
+ end
|
|
105
|
+
|
|
106
|
+ local answers = {};
|
|
107
|
+ for i, value in ipairs(data.answers) do
|
|
108
|
+ table.insert(answers, {
|
|
109
|
+ key = i,
|
|
110
|
+ value = value,
|
|
111
|
+ name = poll.answers[i].name,
|
|
112
|
+ });
|
|
113
|
+ poll.answers[i].voters[data.voterId] = value and data.voterName or nil;
|
|
114
|
+ end
|
|
115
|
+ local answerData = {
|
|
116
|
+ event = event,
|
|
117
|
+ room = room,
|
|
118
|
+ pollId = poll.id,
|
|
119
|
+ voterName = data.voterName,
|
|
120
|
+ voterId = data.voterId,
|
|
121
|
+ answers = answers
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ module:fire_event("answer-poll", answerData);
|
|
125
|
+ end
|
125
|
126
|
end);
|
126
|
127
|
|
127
|
128
|
-- Sends the current poll state to new occupants after joining a room.
|
128
|
129
|
module:hook("muc-occupant-joined", function(event)
|
129
|
|
- local room = event.room;
|
130
|
|
- if is_healthcheck_room(room.jid) then return end
|
131
|
|
- if room.polls == nil or #room.polls.order == 0 then
|
132
|
|
- return
|
133
|
|
- end
|
134
|
|
-
|
135
|
|
- local data = {
|
136
|
|
- type = "old-polls",
|
137
|
|
- polls = {},
|
138
|
|
- };
|
139
|
|
- for i, poll in ipairs(room.polls.order) do
|
140
|
|
- data.polls[i] = {
|
141
|
|
- id = poll.id,
|
142
|
|
- senderId = poll.sender_id,
|
143
|
|
- senderName = poll.sender_name,
|
144
|
|
- question = poll.question,
|
145
|
|
- answers = poll.answers
|
146
|
|
- };
|
147
|
|
- end
|
148
|
|
-
|
149
|
|
- local stanza = st.message({
|
150
|
|
- from = room.jid,
|
151
|
|
- to = event.occupant.jid
|
152
|
|
- })
|
153
|
|
- :tag("json-message", { xmlns = "http://jitsi.org/jitmeet" })
|
154
|
|
- :text(json.encode(data))
|
155
|
|
- :up();
|
156
|
|
-
|
157
|
|
- room:route_stanza(stanza);
|
|
130
|
+ local room = event.room;
|
|
131
|
+ if is_healthcheck_room(room.jid) then return end
|
|
132
|
+ if room.polls == nil or #room.polls.order == 0 then
|
|
133
|
+ return
|
|
134
|
+ end
|
|
135
|
+
|
|
136
|
+ local data = {
|
|
137
|
+ type = "old-polls",
|
|
138
|
+ polls = {},
|
|
139
|
+ };
|
|
140
|
+ for i, poll in ipairs(room.polls.order) do
|
|
141
|
+ data.polls[i] = {
|
|
142
|
+ id = poll.id,
|
|
143
|
+ senderId = poll.sender_id,
|
|
144
|
+ senderName = poll.sender_name,
|
|
145
|
+ question = poll.question,
|
|
146
|
+ answers = poll.answers
|
|
147
|
+ };
|
|
148
|
+ end
|
|
149
|
+
|
|
150
|
+ local stanza = st.message({
|
|
151
|
+ from = room.jid,
|
|
152
|
+ to = event.occupant.jid
|
|
153
|
+ })
|
|
154
|
+ :tag("json-message", { xmlns = "http://jitsi.org/jitmeet" })
|
|
155
|
+ :text(json.encode(data))
|
|
156
|
+ :up();
|
|
157
|
+
|
|
158
|
+ room:route_stanza(stanza);
|
158
|
159
|
end);
|