From 2ac3560afa1cfd5ebceee2b5f38d4c8b9679f9e8 Mon Sep 17 00:00:00 2001 From: Christopher Vollick <0@psycoti.ca> Date: Mon, 23 Jan 2023 18:02:34 -0500 Subject: [PATCH] Add PubSub Helpers to BlatherNotify Since we think we may end up struturing a few scripts to push things into a PubSub node, I wanted to make that as easy as possible. So the way this works is that at startup we can give it a pubsub node and server, and then we can call `BlatherNotify.publish` from then on giving it the thing we want to publish and it'll just push it to the node we configured. Easy. But if we run into a situation where we want to publish to 2 or more different pubsub nodes in different situations, we can instead call `BlatherNotify.pubsub(node, server)` and it'll return us a non-global thing we can call publish on which will publish to that node specifically. --- lib/blather_notify.rb | 48 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/blather_notify.rb b/lib/blather_notify.rb index 81deb18cf6dfd88bf1a691ce2607a0c45168f26d..309f9e59eb05c0fd614b68b64c556345cd9b9c3f 100644 --- a/lib/blather_notify.rb +++ b/lib/blather_notify.rb @@ -7,13 +7,45 @@ require "timeout" module BlatherNotify extend Blather::DSL + class PubSub + class Address + attr_reader :node, :server + + def initialize(node:, server:) + @node = node + @server = server + end + + def to_uri + "xmpp:#{@server}?;node=#{@node}" + end + end + + def initialize(blather, addr) + @blather = blather + @addr = addr + end + + def publish(xml) + @blather.write_with_promise( + Blather::Stanza::PubSub::Publish.new( + @addr.server, + @addr.node, + :set, + xml + ) + ) + end + end + @ready = Queue.new when_ready { @ready << :ready } - def self.start(jid, password) + def self.start(jid, password, default_pubsub_addr: nil) # workqueue_count MUST be 0 or else Blather uses threads! setup(jid, password, nil, nil, nil, nil, workqueue_count: 0) + set_default_pubsub(default_pubsub_addr) EM.error_handler(&method(:panic)) @@ -81,4 +113,18 @@ module BlatherNotify write_with_promise(command(command_node, iq.sessionid, form: form)) end end + + def self.pubsub(addr) + PubSub.new(self, addr) + end + + def self.set_default_pubsub(addr) + @default_pubsub = addr && pubsub(addr) + end + + def self.publish(xml) + raise "No default pubsub set!" unless @default_pubsub + + @default_pubsub.publish(xml) + end end