Browse Source

Updates time to be in ms and sends update of stats when user joins.

master
damencho 7 years ago
parent
commit
7a09befd87
1 changed files with 48 additions and 3 deletions
  1. 48
    3
      resources/prosody-plugins/mod_speakerstats_component.lua

+ 48
- 3
resources/prosody-plugins/mod_speakerstats_component.lua View File

1
 local get_room_from_jid = module:require "util".get_room_from_jid;
1
 local get_room_from_jid = module:require "util".get_room_from_jid;
2
 local jid_resource = require "util.jid".resource;
2
 local jid_resource = require "util.jid".resource;
3
 local ext_events = module:require "ext_events"
3
 local ext_events = module:require "ext_events"
4
+local st = require "util.stanza";
5
+local socket = require "socket";
6
+local json = require "util.json";
4
 
7
 
5
 local muc_component_host = module:get_option_string("muc_component");
8
 local muc_component_host = module:get_option_string("muc_component");
6
 if muc_component_host == nil then
9
 if muc_component_host == nil then
82
         "set isDominant %s for %s", tostring(isNowDominantSpeaker), self.nick);
85
         "set isDominant %s for %s", tostring(isNowDominantSpeaker), self.nick);
83
 
86
 
84
     if not self._isDominantSpeaker and isNowDominantSpeaker then
87
     if not self._isDominantSpeaker and isNowDominantSpeaker then
85
-        self._dominantSpeakerStart = os.time();
88
+        self._dominantSpeakerStart = socket.gettime()*1000;
86
     elseif self._isDominantSpeaker and not isNowDominantSpeaker then
89
     elseif self._isDominantSpeaker and not isNowDominantSpeaker then
87
-        local now = os.time();
88
-        local timeElapsed = now - (self._dominantSpeakerStart or 0);
90
+        local now = socket.gettime()*1000;
91
+        local timeElapsed = math.floor(now - (self._dominantSpeakerStart or 0));
89
 
92
 
90
         self.totalDominantSpeakerTime
93
         self.totalDominantSpeakerTime
91
             = self.totalDominantSpeakerTime + timeElapsed;
94
             = self.totalDominantSpeakerTime + timeElapsed;
109
     local nick = jid_resource(occupant.nick);
112
     local nick = jid_resource(occupant.nick);
110
 
113
 
111
     if room.speakerStats then
114
     if room.speakerStats then
115
+        -- lets send the current speaker stats to that user, so he can update
116
+        -- its local stats
117
+        if next(room.speakerStats) ~= nil then
118
+            local users_json = {};
119
+            for jid, values in pairs(room.speakerStats) do
120
+                -- skip reporting those without a nick('dominantSpeakerId')
121
+                -- and skip focus if sneaked into the table
122
+                if values.nick ~= nil and values.nick ~= 'focus' then
123
+                    local resultSpeakerStats = {};
124
+                    local totalDominantSpeakerTime
125
+                        = values.totalDominantSpeakerTime;
126
+
127
+                    -- before sending we need to calculate current dominant speaker
128
+                    -- state
129
+                    if values._dominantSpeakerStart ~= nil then
130
+                        local timeElapsed = math.floor(
131
+                            socket.gettime()*1000
132
+                                - (values._dominantSpeakerStart or 0));
133
+                        totalDominantSpeakerTime = totalDominantSpeakerTime
134
+                            + timeElapsed;
135
+                    end
136
+
137
+                    resultSpeakerStats.displayName = values.displayName;
138
+                    resultSpeakerStats.totalDominantSpeakerTime
139
+                        = totalDominantSpeakerTime;
140
+                    users_json[values.nick] = resultSpeakerStats;
141
+                end
142
+            end
143
+
144
+            local body_json = {};
145
+            body_json.type = 'speakerstats';
146
+            body_json.users = users_json;
147
+
148
+            local stanza = st.message({
149
+                from = module.host;
150
+                to = occupant.jid; })
151
+            :tag("json-message", {xmlns='http://jitsi.org/jitmeet'})
152
+            :text(json.encode(body_json)):up();
153
+
154
+            room:route_stanza(stanza);
155
+        end
156
+
112
         room.speakerStats[occupant.jid] = new_SpeakerStats(nick);
157
         room.speakerStats[occupant.jid] = new_SpeakerStats(nick);
113
     end
158
     end
114
 end
159
 end

Loading…
Cancel
Save