comparison mod_http_pep_avatar/mod_http_pep_avatar.lua @ 3425:461429e0db58

mod_http_pep_avatar: Provides PEP avatars via HTTP
author Kim Alvefur <zash@zash.se>
date Thu, 03 Jan 2019 11:46:51 +0100
parents mod_atom/mod_atom.lua@6ae875c98daf
children 213679266dcb
comparison
equal deleted inserted replaced
3424:6ae875c98daf 3425:461429e0db58
1 -- HTTP Access to PEP Avatar
2 -- By Kim Alvefur <zash@zash.se>
3
4 local mod_pep = module:depends"pep";
5
6 local um = require "core.usermanager";
7 local nodeprep = require "util.encodings".stringprep.nodeprep;
8 local base64_decode = require "util.encodings".base64.decode;
9
10 module:depends("http")
11 module:provides("http", {
12 route = {
13 ["GET /*"] = function (event, user)
14 local request, response = event.request, event.response;
15 local actor = request.ip;
16
17 user = nodeprep(user);
18 if not user then return 400; end
19 if not um.user_exists(user, module.host) then
20 return 404;
21 end
22
23 local pep_service = mod_pep.get_pep_service(user);
24
25 local ok, avatar_hash, avatar_meta = pep_service:get_last_item("urn:xmpp:avatar:metadata", actor);
26
27 if not ok or not avatar_hash then
28 return 404;
29 end
30
31 if avatar_hash == request.headers.if_none_match then
32 return 304;
33 end
34
35 local data_ok, avatar_data = pep_service:get_items("urn:xmpp:avatar:data", actor, avatar_hash);
36 if not data_ok or type(avatar_data) ~= "table" or not avatar_data[avatar_hash] then
37 return 404;
38 end
39
40 response.headers.etag = avatar_hash;
41
42 local info = avatar_meta.tags[1]:get_child("info");
43 response.headers.content_type = info and info.attr.type or "application/octet-stream";
44
45 local data = avatar_data[avatar_hash];
46 return base64_decode(data.tags[1]:get_text());
47 end;
48 }
49 });