Mercurial > prosody-modules
comparison mod_rest/mod_rest.lua @ 4954:e8a487c42b36
merge upstream
| author | Goffi <goffi@goffi.org> |
|---|---|
| date | Sat, 28 May 2022 16:43:04 +0200 |
| parents | e67cc71727ca |
| children | 537054999093 |
comparison
equal
deleted
inserted
replaced
| 4953:7d6ae8bb95dc | 4954:e8a487c42b36 |
|---|---|
| 65 username = id.medium():lower(); | 65 username = id.medium():lower(); |
| 66 host = module.host; | 66 host = module.host; |
| 67 } | 67 } |
| 68 end | 68 end |
| 69 end | 69 end |
| 70 | |
| 71 local function event_suffix(jid_to) | |
| 72 local node, _, resource = jid.split(jid_to); | |
| 73 if node then | |
| 74 if resource then | |
| 75 return '/full'; | |
| 76 else | |
| 77 return '/bare'; | |
| 78 end | |
| 79 else | |
| 80 return '/host'; | |
| 81 end | |
| 82 end | |
| 83 | |
| 70 | 84 |
| 71 -- TODO This ought to be handled some way other than duplicating this | 85 -- TODO This ought to be handled some way other than duplicating this |
| 72 -- core.stanza_router code here. | 86 -- core.stanza_router code here. |
| 73 local function compat_preevents(origin, stanza) --> boolean : handled | 87 local function compat_preevents(origin, stanza) --> boolean : handled |
| 74 local to = stanza.attr.to; | 88 local to = stanza.attr.to; |
| 354 return post_errors.new("iq_type"); | 368 return post_errors.new("iq_type"); |
| 355 elseif #payload.tags ~= 1 then | 369 elseif #payload.tags ~= 1 then |
| 356 return post_errors.new("iq_tags"); | 370 return post_errors.new("iq_tags"); |
| 357 end | 371 end |
| 358 | 372 |
| 359 return module:send_iq(payload, origin):next( | 373 -- special handling of multiple responses to MAM queries primarily from |
| 374 -- remote hosts, local go directly to origin.send() | |
| 375 local archive_event_name = "message"..event_suffix(from); | |
| 376 local archive_handler; | |
| 377 local archive_query = payload:get_child("query", "urn:xmpp:mam:2"); | |
| 378 if archive_query then | |
| 379 archive_handler = function(result_event) | |
| 380 if result_event.stanza:find("{urn:xmpp:mam:2}result/@queryid") == archive_query.attr.queryid then | |
| 381 origin.send(result_event.stanza); | |
| 382 return true; | |
| 383 end | |
| 384 end | |
| 385 module:hook(archive_event_name, archive_handler, 1); | |
| 386 end | |
| 387 | |
| 388 local p = module:send_iq(payload, origin):next( | |
| 360 function (result) | 389 function (result) |
| 361 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); | 390 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); |
| 362 response.headers.content_type = send_type; | 391 response.headers.content_type = send_type; |
| 392 if responses[1] then | |
| 393 local tail = responses[#responses]; | |
| 394 if tail.name ~= "iq" or tail.attr.from ~= result.stanza.attr.from or tail.attr.id ~= result.stanza.attr.id then | |
| 395 origin.send(result.stanza); | |
| 396 end | |
| 397 end | |
| 363 if responses[2] then | 398 if responses[2] then |
| 364 return encode(send_type, responses); | 399 return encode(send_type, responses); |
| 365 end | 400 end |
| 366 return encode(send_type, result.stanza); | 401 return encode(send_type, result.stanza); |
| 367 end, | 402 end, |
| 375 return encode(send_type, error.context.stanza); | 410 return encode(send_type, error.context.stanza); |
| 376 else | 411 else |
| 377 return error; | 412 return error; |
| 378 end | 413 end |
| 379 end); | 414 end); |
| 415 | |
| 416 if archive_handler then | |
| 417 p:finally(function () | |
| 418 module:unhook(archive_event_name, archive_handler); | |
| 419 end) | |
| 420 end | |
| 421 | |
| 422 return p; | |
| 380 else | 423 else |
| 381 function origin.send(stanza) | 424 function origin.send(stanza) |
| 382 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); | 425 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); |
| 383 response.headers.content_type = send_type; | 426 response.headers.content_type = send_type; |
| 384 response:send(encode(send_type, stanza)); | 427 response:send(encode(send_type, stanza)); |
