This patch adds optional feature to send history upon a new connection
to the EventSource node of a client.

Also instead of sending JSON object as it was in the original version,
patch changes the module to send actual XMPP stanzas via EventSource
because they contain more information (like timestamps) and this way it
is up to the client software to parse and use any part of the stanza.
This feels better than making assumptions about what data the client
wants to get.

-- 
You received this message because you are subscribed to the Google Groups 
"prosody-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to prosody-dev+unsubscr...@googlegroups.com.
To post to this group, send email to prosody-dev@googlegroups.com.
Visit this group at https://groups.google.com/group/prosody-dev.
For more options, visit https://groups.google.com/d/optout.
# HG changeset patch
# User Senya <se...@kinetiksoft.com>
# Date 1519155241 -7200
#      Tue Feb 20 21:34:01 2018 +0200
# Node ID 5ba6fc4c7791fd8f642b56107daa4eb9582fd4f6
# Parent  7c16afc70d11f3bb2a0999da261f912519b2f72b
mod_muc_eventsource: history push support

diff -r 7c16afc70d11 -r 5ba6fc4c7791 mod_muc_eventsource/README.markdown
--- a/mod_muc_eventsource/README.markdown	Mon Feb 19 22:17:38 2018 +0000
+++ b/mod_muc_eventsource/README.markdown	Tue Feb 20 21:34:01 2018 +0200
@@ -67,12 +67,21 @@
 Hosts'](https://prosody.im/doc/http#virtual_hosts) section of our HTTP
 documentation.
 
+The module also supports pushing room history to the EventSource client upon
+connect. This behavior is enabled by following configuration:
+
+    Component "muc.example.org" "muc"
+      modules_enabled = { "muc_eventsource" }
+      muc_eventsource = {
+          push_history = true;
+      }
+
 Compatibility
 -------------
 
   ------- --------------
-  0.10    ?
-  0.9     ?
+  0.10    Doesn't work
+  0.9     Doesn't work
   0.8     Doesn't work
   Trunk   Works
   ------- --------------
diff -r 7c16afc70d11 -r 5ba6fc4c7791 mod_muc_eventsource/mod_muc_eventsource.lua
--- a/mod_muc_eventsource/mod_muc_eventsource.lua	Mon Feb 19 22:17:38 2018 +0000
+++ b/mod_muc_eventsource/mod_muc_eventsource.lua	Tue Feb 20 21:34:01 2018 +0200
@@ -3,6 +3,8 @@
 local jid_split = require "util.jid".split;
 local json = require "util.json";
 
+local options = module:get_option("muc_eventsource", {});
+local do_push_history = options.push_history;
 local streams = {};
 
 function client_closed(response)
@@ -14,6 +16,33 @@
 	end
 end
 
+local function get_room_from_jid(node, host)
+  local jid = node .. "@" .. host;
+	local component = hosts[host];
+	if component then
+		local muc = component.modules.muc
+		if muc and rawget(muc,"get_room_from_jid") then
+			-- We're running >0.10 (new MUC API)
+			return muc.get_room_from_jid(jid);
+		else
+			return
+		end
+	end
+end
+
+function push_history(room_name, conn)
+	local event = {
+		room = get_room_from_jid(room_name, module:get_host());
+		next_stanza = function() end;
+	};
+
+	module:fire_event("muc-get-history", event);
+
+	for msg in event.next_stanza, event do
+		conn:write("data: "..tostring(msg).."\n\n");
+	end
+end
+
 function serve_stream(event, node)
 	module:log("debug", "Client subscribed to: %s", node);
 
@@ -38,6 +67,10 @@
 	end
 	clientlist[response] = response.conn;
 
+	if do_push_history then
+		push_history(node, response.conn);
+	end
+
 	return true;
 end
 
@@ -52,13 +85,7 @@
 	if not body then
 		return;
 	end
-	local nick = select(3, jid_split(stanza.attr.from));
-	-- Encode body and broadcast to eventsource subscribers
-	local json_data = json.encode({
-		nick = nick;
-		body = body;
-	});
-	local data = "data: "..json_data:gsub("\n", "\ndata: \n").."\n\n";
+	local data = "data: "..tostring(stanza).."\n\n";
 	for response, conn in pairs(clientlist) do
 		conn:write(data);
 	end

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to