annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
1 -- HTTP Access to PEP Avatar
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- By Kim Alvefur <zash@zash.se>
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
4 local mod_pep = module:depends"pep";
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
5
3424
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3423
diff changeset
6 local um = require "core.usermanager";
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local nodeprep = require "util.encodings".stringprep.nodeprep;
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
8 local base64_decode = require "util.encodings".base64.decode;
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
10 module:depends("http")
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 module:provides("http", {
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 route = {
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
13 ["GET /*"] = function (event, user)
3423
4a8fa0364f35 mod_atom: Unpack event object
Kim Alvefur <zash@zash.se>
parents: 3276
diff changeset
14 local request, response = event.request, event.response;
4a8fa0364f35 mod_atom: Unpack event object
Kim Alvefur <zash@zash.se>
parents: 3276
diff changeset
15 local actor = request.ip;
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
16
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
17 user = nodeprep(user);
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
18 if not user then return 400; end
3424
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3423
diff changeset
19 if not um.user_exists(user, module.host) then
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3423
diff changeset
20 return 404;
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3423
diff changeset
21 end
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
22
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
23 local pep_service = mod_pep.get_pep_service(user);
3272
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3241
diff changeset
24
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
25 local ok, avatar_hash, avatar_meta = pep_service:get_last_item("urn:xmpp:avatar:metadata", actor);
3272
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3241
diff changeset
26
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
27 if not ok or not avatar_hash then
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
28 return 404;
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
29 end
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
30
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
31 if avatar_hash == request.headers.if_none_match then
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
32 return 304;
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
33 end
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
34
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
35 local data_ok, avatar_data = pep_service:get_items("urn:xmpp:avatar:data", actor, avatar_hash);
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
36 if not data_ok or type(avatar_data) ~= "table" or not avatar_data[avatar_hash] then
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
37 return 404;
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
38 end
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
39
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
40 response.headers.etag = avatar_hash;
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
41
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
42 local info = avatar_meta.tags[1]:get_child("info");
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
43 response.headers.content_type = info and info.attr.type or "application/octet-stream";
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
44
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
45 local data = avatar_data[avatar_hash];
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
46 return base64_decode(data.tags[1]:get_text());
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
47 end;
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
48 }
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 });