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.
This commit is contained in:
damencho 2021-06-21 12:23:17 +03:00 committed by Дамян Минков
parent 60db81f31c
commit e6cdeb31ff
1 changed files with 22 additions and 17 deletions

View File

@ -6,6 +6,7 @@ local ext_events = module:require "ext_events"
local st = require "util.stanza"; local st = require "util.stanza";
local socket = require "socket"; local socket = require "socket";
local json = require "util.json"; local json = require "util.json";
local um_is_admin = require "core.usermanager".is_admin;
-- we use async to detect Prosody 0.10 and earlier -- we use async to detect Prosody 0.10 and earlier
local have_async = pcall(require, "util.async"); local have_async = pcall(require, "util.async");
@ -22,6 +23,10 @@ end
log("info", "Starting speakerstats for %s", muc_component_host); log("info", "Starting speakerstats for %s", muc_component_host);
local function is_admin(jid)
return um_is_admin(jid, module.host);
end
-- receives messages from client currently connected to the room -- receives messages from client currently connected to the room
-- clients indicates their own dominant speaker events -- clients indicates their own dominant speaker events
function on_message(event) function on_message(event)
@ -126,9 +131,9 @@ end
-- Create SpeakerStats object for the joined user -- Create SpeakerStats object for the joined user
function occupant_joined(event) function occupant_joined(event)
local room = event.room; local occupant, room = event.occupant, event.room;
if is_healthcheck_room(room.jid) then if is_healthcheck_room(room.jid) or is_admin(occupant.bare_jid) then
return; return;
end end
@ -145,25 +150,25 @@ function occupant_joined(event)
-- skip reporting those without a nick('dominantSpeakerId') -- skip reporting those without a nick('dominantSpeakerId')
-- and skip focus if sneaked into the table -- and skip focus if sneaked into the table
if values.nick ~= nil and values.nick ~= 'focus' then if values.nick ~= nil and values.nick ~= 'focus' then
local resultSpeakerStats = {}; local totalDominantSpeakerTime = values.totalDominantSpeakerTime;
local totalDominantSpeakerTime if totalDominantSpeakerTime > 0 or room:get_occupant_jid(jid) == nil then
= values.totalDominantSpeakerTime; -- before sending we need to calculate current dominant speaker state
-- before sending we need to calculate current dominant speaker
-- state
if values:isDominantSpeaker() then if values:isDominantSpeaker() then
local timeElapsed = math.floor( local timeElapsed = math.floor(socket.gettime()*1000 - values._dominantSpeakerStart);
socket.gettime()*1000 - values._dominantSpeakerStart); totalDominantSpeakerTime = totalDominantSpeakerTime + timeElapsed;
totalDominantSpeakerTime = totalDominantSpeakerTime
+ timeElapsed;
end end
resultSpeakerStats.displayName = values.displayName; users_json[values.nick] = {
resultSpeakerStats.totalDominantSpeakerTime displayName = values.displayName,
= totalDominantSpeakerTime; totalDominantSpeakerTime = totalDominantSpeakerTime
users_json[values.nick] = resultSpeakerStats; };
end end
end end
end
if next(users_json) == nil then
return;
end
local body_json = {}; local body_json = {};
body_json.type = 'speakerstats'; body_json.type = 'speakerstats';