Mercurial > prosody-hg
comparison plugins/mod_pep.lua @ 10037:e01f38acde74
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 30 May 2019 21:44:35 +0200 |
| parents | fdc42f685557 045209b41b3a |
| children | 0bc291a5734a |
comparison
equal
deleted
inserted
replaced
| 10035:386f085820e6 | 10037:e01f38acde74 |
|---|---|
| 89 }; | 89 }; |
| 90 end | 90 end |
| 91 return data, err; | 91 return data, err; |
| 92 end | 92 end |
| 93 function store:set(node, data) | 93 function store:set(node, data) |
| 94 if data then | |
| 95 -- Save the data without subscriptions | |
| 96 local subscribers = {}; | |
| 97 for jid, sub in pairs(data.subscribers) do | |
| 98 if type(sub) ~= "table" or not sub.presence then | |
| 99 subscribers[jid] = sub; | |
| 100 end | |
| 101 end | |
| 102 data = { | |
| 103 name = data.name; | |
| 104 config = data.config; | |
| 105 affiliations = data.affiliations; | |
| 106 subscribers = subscribers; | |
| 107 }; | |
| 108 end | |
| 109 return node_config:set(username, node, data); | 94 return node_config:set(username, node, data); |
| 110 end | 95 end |
| 111 function store:users() | 96 function store:users() |
| 112 return pairs(known_nodes:get(username) or {}); | 97 return pairs(known_nodes:get(username) or {}); |
| 113 end | 98 end |
| 157 | 142 |
| 158 if item then | 143 if item then |
| 159 message:add_child(item); | 144 message:add_child(item); |
| 160 end | 145 end |
| 161 | 146 |
| 147 local broadcast_to = {}; | |
| 162 for jid in pairs(jids) do | 148 for jid in pairs(jids) do |
| 149 broadcast_to[jid] = true; | |
| 150 end | |
| 151 | |
| 152 local service_recipients = recipients[username]; | |
| 153 if service_recipients then | |
| 154 local service = services[username]; | |
| 155 for recipient, nodes in pairs(service_recipients) do | |
| 156 if nodes:contains(node) and service:may(node, recipient, "subscribe") then | |
| 157 broadcast_to[recipient] = true; | |
| 158 end | |
| 159 end | |
| 160 end | |
| 161 | |
| 162 for jid in pairs(broadcast_to) do | |
| 163 module:log("debug", "Sending notification to %s from %s: %s", jid, user_bare, tostring(item)); | 163 module:log("debug", "Sending notification to %s from %s: %s", jid, user_bare, tostring(item)); |
| 164 message.attr.to = jid; | 164 message.attr.to = jid; |
| 165 module:send(message); | 165 module:send(message); |
| 166 end | 166 end |
| 167 end | 167 end |
| 168 return simple_broadcast; | 168 return simple_broadcast; |
| 169 end | |
| 170 | |
| 171 local function on_node_creation(event) | |
| 172 local service = event.service; | |
| 173 local node = event.node; | |
| 174 local username = service.config.pep_username; | |
| 175 | |
| 176 local service_recipients = recipients[username]; | |
| 177 if not service_recipients then return; end | |
| 178 | |
| 179 for recipient, nodes in pairs(service_recipients) do | |
| 180 if nodes:contains(node) then | |
| 181 service:add_subscription(node, recipient, recipient, { presence = true }); | |
| 182 end | |
| 183 end | |
| 184 end | 169 end |
| 185 | 170 |
| 186 function get_pep_service(username) | 171 function get_pep_service(username) |
| 187 module:log("debug", "get_pep_service(%q)", username); | 172 module:log("debug", "get_pep_service(%q)", username); |
| 188 local user_bare = jid_join(username, host); | 173 local user_bare = jid_join(username, host); |
| 238 end | 223 end |
| 239 services[username] = service; | 224 services[username] = service; |
| 240 module:add_item("pep-service", { service = service, jid = user_bare }); | 225 module:add_item("pep-service", { service = service, jid = user_bare }); |
| 241 return service; | 226 return service; |
| 242 end | 227 end |
| 243 | |
| 244 module:hook("item-added/pep-service", function (event) | |
| 245 local service = event.item.service; | |
| 246 module:hook_object_event(service.events, "node-created", on_node_creation); | |
| 247 end); | |
| 248 | 228 |
| 249 function handle_pubsub_iq(event) | 229 function handle_pubsub_iq(event) |
| 250 local origin, stanza = event.origin, event.stanza; | 230 local origin, stanza = event.origin, event.stanza; |
| 251 local service_name = origin.username; | 231 local service_name = origin.username; |
| 252 if stanza.attr.to ~= nil then | 232 if stanza.attr.to ~= nil then |
| 306 if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then | 286 if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then |
| 307 return; | 287 return; |
| 308 end | 288 end |
| 309 | 289 |
| 310 local service = get_pep_service(service_name); | 290 local service = get_pep_service(service_name); |
| 311 for node in current - nodes do | |
| 312 service:remove_subscription(node, recipient, recipient); | |
| 313 end | |
| 314 | 291 |
| 315 for node in nodes - current do | 292 for node in nodes - current do |
| 316 if service:add_subscription(node, recipient, recipient, { presence = true }) then | 293 if service:may(node, recipient, "subscribe") then |
| 317 resend_last_item(recipient, node, service); | 294 resend_last_item(recipient, node, service); |
| 318 end | 295 end |
| 319 end | 296 end |
| 320 | 297 |
| 321 if nodes == empty_set or nodes:empty() then | 298 if nodes == empty_set or nodes:empty() then |
