Mercurial > prosody-modules
annotate mod_muc_media_metadata/mod_muc_media_metadata.lua @ 4548:c50fac0f3cbf
mod_rest: Fix type of response status codes in openapi spec
https://spec.openapis.org/oas/v3.0.1#responsesObject
> This field MUST be enclosed in quotation marks for compatibility
> between JSON and YAML.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 25 Apr 2021 16:45:06 +0200 |
parents | d4537f045a78 |
children |
rev | line source |
---|---|
3682
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local async = require "util.async"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local promise = require "util.promise"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local http = require "net.http"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local st = require "util.stanza"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
3686
2573d143621f
mod_muc_media_metadata: Update namespace
Matthew Wild <mwild1@gmail.com>
parents:
3685
diff
changeset
|
6 local xmlns_metadata = "xmpp:prosody.im/protocol/media-metadata#0" |
3682
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local fetch_headers = { |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 bytes = "content-length"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 type = "content-type"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 etag = "etag"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 blurhash = "blurhash"; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 }; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local function fetch_media_metadata(url) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 return promise.new(function (resolve) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 http.request(url, { method = "HEAD" }, function (body, code, response) --luacheck: ignore 212/body |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 if code == 200 then |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local metadata = {}; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 for metadata_name, header_name in pairs(fetch_headers) do |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 metadata[metadata_name] = response.headers[header_name]; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 resolve(metadata); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 else |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 resolve(nil); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local function metadata_to_tag(metadata) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if not metadata then return; end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local metadata_tag = st.stanza("metadata", { xmlns = xmlns_metadata }); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 for k, v in pairs(metadata) do |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 metadata_tag:text_tag(k, v) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 return metadata_tag; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 module:hook("muc-occupant-groupchat", function (event) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 local stanza = event.stanza; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 local promises; |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 for oob in stanza:childtags("x", "jabber:x:oob") do |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if not promises then promises = {}; end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local url = oob:get_child_text("url"); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 local p = fetch_media_metadata(url) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 :next(metadata_to_tag) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 :next(function (metadata_tag) |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 oob:add_child(metadata_tag); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 table.insert(promises, p); |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 if not promises then return; end |
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
3688
d4537f045a78
mod_muc_media_metadata: Actually consensus is on async.wait after all
Matthew Wild <mwild1@gmail.com>
parents:
3686
diff
changeset
|
60 async.wait(promise.all(promises)); |
3682
47e1c94fb6fb
mod_muc_media_metadata: Module to automatically fetch metadata for posted media
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end); |