Pārlūkot izejas kodu

feat: Optimizes speaker stats to skip occupants with 0 talk time.

Skips adding focus user and skips sending an empty message.
Keep sending participant info for those left the room, to keep existing behaviour.
master
damencho 4 gadus atpakaļ
vecāks
revīzija
e6cdeb31ff

+ 23
- 18
resources/prosody-plugins/mod_speakerstats_component.lua Parādīt failu

6
 local st = require "util.stanza";
6
 local st = require "util.stanza";
7
 local socket = require "socket";
7
 local socket = require "socket";
8
 local json = require "util.json";
8
 local json = require "util.json";
9
+local um_is_admin = require "core.usermanager".is_admin;
9
 
10
 
10
 -- we use async to detect Prosody 0.10 and earlier
11
 -- we use async to detect Prosody 0.10 and earlier
11
 local have_async = pcall(require, "util.async");
12
 local have_async = pcall(require, "util.async");
22
 
23
 
23
 log("info", "Starting speakerstats for %s", muc_component_host);
24
 log("info", "Starting speakerstats for %s", muc_component_host);
24
 
25
 
26
+local function is_admin(jid)
27
+    return um_is_admin(jid, module.host);
28
+end
29
+
25
 -- receives messages from client currently connected to the room
30
 -- receives messages from client currently connected to the room
26
 -- clients indicates their own dominant speaker events
31
 -- clients indicates their own dominant speaker events
27
 function on_message(event)
32
 function on_message(event)
126
 
131
 
127
 -- Create SpeakerStats object for the joined user
132
 -- Create SpeakerStats object for the joined user
128
 function occupant_joined(event)
133
 function occupant_joined(event)
129
-    local room = event.room;
134
+    local occupant, room = event.occupant, event.room;
130
 
135
 
131
-    if is_healthcheck_room(room.jid) then
136
+    if is_healthcheck_room(room.jid) or is_admin(occupant.bare_jid) then
132
         return;
137
         return;
133
     end
138
     end
134
 
139
 
145
                 -- skip reporting those without a nick('dominantSpeakerId')
150
                 -- skip reporting those without a nick('dominantSpeakerId')
146
                 -- and skip focus if sneaked into the table
151
                 -- and skip focus if sneaked into the table
147
                 if values.nick ~= nil and values.nick ~= 'focus' then
152
                 if values.nick ~= nil and values.nick ~= 'focus' then
148
-                    local resultSpeakerStats = {};
149
-                    local totalDominantSpeakerTime
150
-                        = values.totalDominantSpeakerTime;
151
-
152
-                    -- before sending we need to calculate current dominant speaker
153
-                    -- state
154
-                    if values:isDominantSpeaker() then
155
-                        local timeElapsed = math.floor(
156
-                            socket.gettime()*1000 - values._dominantSpeakerStart);
157
-                        totalDominantSpeakerTime = totalDominantSpeakerTime
158
-                            + timeElapsed;
153
+                    local totalDominantSpeakerTime = values.totalDominantSpeakerTime;
154
+                    if totalDominantSpeakerTime > 0 or room:get_occupant_jid(jid) == nil then
155
+                        -- before sending we need to calculate current dominant speaker state
156
+                        if values:isDominantSpeaker() then
157
+                            local timeElapsed = math.floor(socket.gettime()*1000 - values._dominantSpeakerStart);
158
+                            totalDominantSpeakerTime = totalDominantSpeakerTime + timeElapsed;
159
+                        end
160
+
161
+                        users_json[values.nick] =  {
162
+                            displayName = values.displayName,
163
+                            totalDominantSpeakerTime = totalDominantSpeakerTime
164
+                        };
159
                     end
165
                     end
160
-
161
-                    resultSpeakerStats.displayName = values.displayName;
162
-                    resultSpeakerStats.totalDominantSpeakerTime
163
-                        = totalDominantSpeakerTime;
164
-                    users_json[values.nick] = resultSpeakerStats;
165
                 end
166
                 end
166
             end
167
             end
167
 
168
 
169
+            if next(users_json) == nil then
170
+                return;
171
+            end
172
+
168
             local body_json = {};
173
             local body_json = {};
169
             body_json.type = 'speakerstats';
174
             body_json.type = 'speakerstats';
170
             body_json.users = users_json;
175
             body_json.users = users_json;

Notiek ielāde…
Atcelt
Saglabāt