annotate mod_http_pep_avatar/mod_http_pep_avatar.lua @ 3810:91ff86fc3b20

mod_rest: Factor out payload parsing To make it easier to add new format parsers in a single place
author Kim Alvefur <zash@zash.se>
date Wed, 01 Jan 2020 15:43:47 +0100
parents 00bdecb12779
children 02d238799537
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;
3433
213679266dcb mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents: 3425
diff changeset
9 local urlencode = require "util.http".urlencode;
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
11 module:depends("http")
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 module:provides("http", {
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 route = {
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
14 ["GET /*"] = function (event, user)
3575
00bdecb12779 mod_http_pep_avatar: Return a message instead of 404 for base path
Kim Alvefur <zash@zash.se>
parents: 3433
diff changeset
15 if user == "" then
00bdecb12779 mod_http_pep_avatar: Return a message instead of 404 for base path
Kim Alvefur <zash@zash.se>
parents: 3433
diff changeset
16 return [[<h1>Hello from mod_http_pep_avatar</h1><p>This module provides access to public avatars of local users.</p>]];
00bdecb12779 mod_http_pep_avatar: Return a message instead of 404 for base path
Kim Alvefur <zash@zash.se>
parents: 3433
diff changeset
17 end;
00bdecb12779 mod_http_pep_avatar: Return a message instead of 404 for base path
Kim Alvefur <zash@zash.se>
parents: 3433
diff changeset
18
3423
4a8fa0364f35 mod_atom: Unpack event object
Kim Alvefur <zash@zash.se>
parents: 3276
diff changeset
19 local request, response = event.request, event.response;
4a8fa0364f35 mod_atom: Unpack event object
Kim Alvefur <zash@zash.se>
parents: 3276
diff changeset
20 local actor = request.ip;
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
21
3433
213679266dcb mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents: 3425
diff changeset
22 local prepped = nodeprep(user);
213679266dcb mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents: 3425
diff changeset
23 if not prepped then return 400; end
213679266dcb mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents: 3425
diff changeset
24 if prepped ~= user then
213679266dcb mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents: 3425
diff changeset
25 response.headers.location = module:http_url() .. "/" .. urlencode(prepped);
213679266dcb mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents: 3425
diff changeset
26 return 302;
213679266dcb mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents: 3425
diff changeset
27 end
3424
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3423
diff changeset
28 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
29 return 404;
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3423
diff changeset
30 end
3241
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
31
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
32 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
33
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
34 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
35
3425
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
36 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
37 return 404;
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
38 end
3425
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 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
41 return 304;
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
42 end
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
43
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
44 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
45 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
46 return 404;
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
47 end
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
48
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
49 response.headers.etag = avatar_hash;
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
50
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
51 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
52 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
53
461429e0db58 mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents: 3424
diff changeset
54 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
55 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
56 end;
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2294
diff changeset
57 }
2294
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 });