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 socket = require "socket";
local json = require "util.json";
local um_is_admin = require "core.usermanager".is_admin;
-- we use async to detect Prosody 0.10 and earlier
local have_async = pcall(require, "util.async");
@ -22,6 +23,10 @@ end
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
-- clients indicates their own dominant speaker events
function on_message(event)
@ -126,9 +131,9 @@ end
-- Create SpeakerStats object for the joined user
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;
end
@ -145,26 +150,26 @@ function occupant_joined(event)
-- skip reporting those without a nick('dominantSpeakerId')
-- and skip focus if sneaked into the table
if values.nick ~= nil and values.nick ~= 'focus' then
local resultSpeakerStats = {};
local totalDominantSpeakerTime
= values.totalDominantSpeakerTime;
local totalDominantSpeakerTime = values.totalDominantSpeakerTime;
if totalDominantSpeakerTime > 0 or room:get_occupant_jid(jid) == nil then
-- before sending we need to calculate current dominant speaker state
if values:isDominantSpeaker() then
local timeElapsed = math.floor(socket.gettime()*1000 - values._dominantSpeakerStart);
totalDominantSpeakerTime = totalDominantSpeakerTime + timeElapsed;
end
-- before sending we need to calculate current dominant speaker
-- state
if values:isDominantSpeaker() then
local timeElapsed = math.floor(
socket.gettime()*1000 - values._dominantSpeakerStart);
totalDominantSpeakerTime = totalDominantSpeakerTime
+ timeElapsed;
users_json[values.nick] = {
displayName = values.displayName,
totalDominantSpeakerTime = totalDominantSpeakerTime
};
end
resultSpeakerStats.displayName = values.displayName;
resultSpeakerStats.totalDominantSpeakerTime
= totalDominantSpeakerTime;
users_json[values.nick] = resultSpeakerStats;
end
end
if next(users_json) == nil then
return;
end
local body_json = {};
body_json.type = 'speakerstats';
body_json.users = users_json;