Browse Source

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 3 years ago
parent
commit
e6cdeb31ff
1 changed files with 23 additions and 18 deletions
  1. 23
    18
      resources/prosody-plugins/mod_speakerstats_component.lua

+ 23
- 18
resources/prosody-plugins/mod_speakerstats_component.lua View File

@@ -6,6 +6,7 @@ local ext_events = module:require "ext_events"
6 6
 local st = require "util.stanza";
7 7
 local socket = require "socket";
8 8
 local json = require "util.json";
9
+local um_is_admin = require "core.usermanager".is_admin;
9 10
 
10 11
 -- we use async to detect Prosody 0.10 and earlier
11 12
 local have_async = pcall(require, "util.async");
@@ -22,6 +23,10 @@ end
22 23
 
23 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 30
 -- receives messages from client currently connected to the room
26 31
 -- clients indicates their own dominant speaker events
27 32
 function on_message(event)
@@ -126,9 +131,9 @@ end
126 131
 
127 132
 -- Create SpeakerStats object for the joined user
128 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 137
         return;
133 138
     end
134 139
 
@@ -145,26 +150,26 @@ function occupant_joined(event)
145 150
                 -- skip reporting those without a nick('dominantSpeakerId')
146 151
                 -- and skip focus if sneaked into the table
147 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 165
                     end
160
-
161
-                    resultSpeakerStats.displayName = values.displayName;
162
-                    resultSpeakerStats.totalDominantSpeakerTime
163
-                        = totalDominantSpeakerTime;
164
-                    users_json[values.nick] = resultSpeakerStats;
165 166
                 end
166 167
             end
167 168
 
169
+            if next(users_json) == nil then
170
+                return;
171
+            end
172
+
168 173
             local body_json = {};
169 174
             body_json.type = 'speakerstats';
170 175
             body_json.users = users_json;

Loading…
Cancel
Save