浏览代码

Send poltergeist presence update reusing previous presences.

If we do not reuse previous presences we lose avatar and name and people joining after the poltergeist creation will not be updated with those values.
j8
damencho 7 年前
父节点
当前提交
4a9a8eec9a
共有 1 个文件被更改,包括 50 次插入4 次删除
  1. 50
    4
      resources/prosody-plugins/mod_muc_poltergeist.lua

+ 50
- 4
resources/prosody-plugins/mod_muc_poltergeist.lua 查看文件

182
 	return not not room:get_occupant_jid(poltergeist_component.."/"..nick);
182
 	return not not room:get_occupant_jid(poltergeist_component.."/"..nick);
183
 end
183
 end
184
 
184
 
185
+-- Returns the last presence of occupant
186
+-- @param room the room instance where to check for occupant
187
+-- @param nick the nick of the occupant
188
+-- @return presence of the occupant
189
+function get_presence(room, nick)
190
+    local occupant_jid
191
+        = room:get_occupant_jid(poltergeist_component.."/"..nick);
192
+    if (occupant_jid) then
193
+        return room:get_occupant_by_nick(occupant_jid):get_presence();
194
+    end
195
+
196
+    return nil;
197
+end
198
+
185
 -- Event handlers
199
 -- Event handlers
186
 
200
 
187
 --- Note: mod_muc and some of its sub-modules add event handlers between 0 and -100,
201
 --- Note: mod_muc and some of its sub-modules add event handlers between 0 and -100,
268
 
282
 
269
     local nick = string.sub(username, 0, 8);
283
     local nick = string.sub(username, 0, 8);
270
     if (have_poltergeist_occupant(room, nick)) then
284
     if (have_poltergeist_occupant(room, nick)) then
271
-        local update_presence = st.presence({
272
-            to = room.jid.."/"..nick,
273
-            from = poltergeist_component.."/"..nick
274
-        }):tag("status"):text(status):up();
285
+        local update_presence = get_presence(room, nick);
286
+
287
+        if (not update_presence) then
288
+            -- no presence found for occupant, create one
289
+            update_presence = st.presence({
290
+                to = room.jid.."/"..nick,
291
+                from = poltergeist_component.."/"..nick
292
+            });
293
+        else
294
+            -- update occupant presence with appropriate to and from
295
+            -- so we can send it again
296
+            update_presence = st.clone(update_presence);
297
+            update_presence.attr.to = room.jid.."/"..nick;
298
+            update_presence.attr.from = poltergeist_component.."/"..nick;
299
+        end
300
+
301
+        local once = false;
302
+        -- the status tag we will attach
303
+        local statusTag = st.stanza("status"):text(status);
304
+
305
+        -- if there is already a status tag replace it
306
+        update_presence:maptags(function (tag)
307
+            if tag.name == statusTag.name then
308
+                if not once then
309
+                    once = true;
310
+                    return statusTag;
311
+                else
312
+                    return nil;
313
+                end
314
+            end
315
+            return tag;
316
+        end);
317
+        if (not once) then
318
+            -- no status tag was repleced, attach it
319
+            update_presence:add_child(statusTag);
320
+        end
275
 
321
 
276
         room:handle_normal_presence(
322
         room:handle_normal_presence(
277
             prosody.hosts[poltergeist_component], update_presence);
323
             prosody.hosts[poltergeist_component], update_presence);

正在加载...
取消
保存