Mercurial > prosody-modules
comparison mod_pubsub_subscription/mod_pubsub_subscription.lua @ 6305:1c0e9486ba23
mod_pubsub_subscription: Fix sometimes not subscribing at startup, improve logs
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 31 Jul 2025 15:12:51 +0100 |
| parents | 6dca425eea15 |
| children |
comparison
equal
deleted
inserted
replaced
| 6304:6e2458b69e33 | 6305:1c0e9486ba23 |
|---|---|
| 29 local item = item_event.item; | 29 local item = item_event.item; |
| 30 assert(item.service, "pubsub subscription item MUST have a 'service' field."); | 30 assert(item.service, "pubsub subscription item MUST have a 'service' field."); |
| 31 assert(item.node, "pubsub subscription item MUST have a 'node' field."); | 31 assert(item.node, "pubsub subscription item MUST have a 'node' field."); |
| 32 item.from = item.from or module.host; | 32 item.from = item.from or module.host; |
| 33 | 33 |
| 34 local already_subscibed = false; | 34 local already_subscribed = false; |
| 35 for _ in active_subscriptions:iter(item.service, item.node, item.from, nil) do -- luacheck: ignore 512 | 35 for _ in active_subscriptions:iter(item.service, item.node, item.from, nil) do -- luacheck: ignore 512 |
| 36 already_subscibed = true; | 36 already_subscribed = true; |
| 37 break | 37 break |
| 38 end | 38 end |
| 39 | 39 |
| 40 item._id = uuid.generate(); | 40 item._id = uuid.generate(); |
| 41 local iq_id = "pubsub-sub-"..id.short(); | 41 local iq_id = "pubsub-sub-"..id.short(); |
| 42 pending_subscription:set(iq_id, item._id); | 42 pending_subscription:set(iq_id, item._id); |
| 43 active_subscriptions:set(item.service, item.node, item.from, item._id, item); | 43 active_subscriptions:set(item.service, item.node, item.from, item._id, item); |
| 44 | 44 |
| 45 if not already_subscibed then | 45 if not already_subscribed then |
| 46 module:send(st.iq({ type = "set", id = iq_id, from = item.from, to = item.service }) | 46 local function send_subscribe() |
| 47 :tag("pubsub", { xmlns = xmlns_pubsub }) | 47 module:log("debug", "%s is subscribing to %s:%s", item.from, item.service, item.node); |
| 48 :tag("subscribe", { jid = item.from, node = item.node })); | 48 module:send(st.iq({ type = "set", id = iq_id, from = item.from, to = item.service }) |
| 49 :tag("pubsub", { xmlns = xmlns_pubsub }) | |
| 50 :tag("subscribe", { jid = item.from, node = item.node })); | |
| 51 end | |
| 52 | |
| 53 if prosody.start_time then | |
| 54 send_subscribe(); | |
| 55 else | |
| 56 module:log("debug", "%s will subscribe to %s:%s after server startup", item.from, item.service, item.node); | |
| 57 module:hook_global("server-started", send_subscribe); | |
| 58 end | |
| 59 else | |
| 60 module:log("debug", "%s is already subscribed to %s:%s", item.from, item.service, item.node); | |
| 49 end | 61 end |
| 50 end | 62 end |
| 51 | 63 |
| 52 for _, event_name in ipairs(valid_events) do | 64 for _, event_name in ipairs(valid_events) do |
| 53 module:hook("pubsub-event/host/"..event_name, function (event) | 65 module:hook("pubsub-event/host/"..event_name, function (event) |
| 63 pcall(cb, event); | 75 pcall(cb, event); |
| 64 end | 76 end |
| 65 end); | 77 end); |
| 66 end | 78 end |
| 67 | 79 |
| 68 function handle_iq(context, event) | 80 function handle_iq(context, event) --luacheck: ignore 212/context |
| 69 local stanza = event.stanza; | 81 local stanza = event.stanza; |
| 70 local service = stanza.attr.from; | 82 local service = stanza.attr.from; |
| 71 | 83 |
| 72 if not stanza.attr.id then return end -- shouldn't be possible | 84 if not stanza.attr.id then return end -- shouldn't be possible |
| 73 if not stanza.attr.id:match("^pubsub%-sub%-") then return end | 85 if not stanza.attr.id:match("^pubsub%-sub%-") then return end |
