mod_atom/mod_atom.lua
author marc0s <marcos.devera@quobis.com>
Fri, 29 Mar 2019 17:06:18 +0100
changeset 3504 e86315c9b5c4
parent 3436 61368aec97d6
child 3577 5dd505c39c4b
permissions -rw-r--r--
offline_hints: discard no-store hinted messages at mod_offline This module will hook `message/offline/handle` and will skip offline storage for messages hinted with `no-store` or `no-permanent-store`.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2298
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- HTTP Access to PEP -> microblog
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
3245
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
     4
local mod_pep = module:depends"pep";
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
     5
3428
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3427
diff changeset
     6
local um = require "core.usermanager";
2298
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;
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local st = require "util.stanza";
3436
61368aec97d6 mod_atom: Apply nodeprep via redirect for cache-friendlyness
Kim Alvefur <zash@zash.se>
parents: 3428
diff changeset
     9
local urlencode = require "util.http".urlencode;
2298
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
3245
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    11
module:depends("http")
2298
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 = {
3245
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    14
		["GET /*"] = function (event, user)
3427
4a8fa0364f35 mod_atom: Unpack event object
Kim Alvefur <zash@zash.se>
parents: 3280
diff changeset
    15
			local request, response = event.request, event.response;
4a8fa0364f35 mod_atom: Unpack event object
Kim Alvefur <zash@zash.se>
parents: 3280
diff changeset
    16
			local actor = request.ip;
3245
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    17
3436
61368aec97d6 mod_atom: Apply nodeprep via redirect for cache-friendlyness
Kim Alvefur <zash@zash.se>
parents: 3428
diff changeset
    18
			local prepped = nodeprep(user);
61368aec97d6 mod_atom: Apply nodeprep via redirect for cache-friendlyness
Kim Alvefur <zash@zash.se>
parents: 3428
diff changeset
    19
			if not prepped then return 400; end
61368aec97d6 mod_atom: Apply nodeprep via redirect for cache-friendlyness
Kim Alvefur <zash@zash.se>
parents: 3428
diff changeset
    20
			if prepped ~= user then
61368aec97d6 mod_atom: Apply nodeprep via redirect for cache-friendlyness
Kim Alvefur <zash@zash.se>
parents: 3428
diff changeset
    21
				response.headers.location = module:http_url() .. "/" .. urlencode(prepped);
61368aec97d6 mod_atom: Apply nodeprep via redirect for cache-friendlyness
Kim Alvefur <zash@zash.se>
parents: 3428
diff changeset
    22
				return 302;
61368aec97d6 mod_atom: Apply nodeprep via redirect for cache-friendlyness
Kim Alvefur <zash@zash.se>
parents: 3428
diff changeset
    23
			end
3428
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3427
diff changeset
    24
			if not um.user_exists(user, module.host) then
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3427
diff changeset
    25
				return 404;
6ae875c98daf mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents: 3427
diff changeset
    26
			end
3245
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    27
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    28
			local pubsub_service = mod_pep.get_pep_service(user);
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    29
			local ok, items = pubsub_service:get_items("urn:xmpp:microblog:0", actor);
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    30
			if ok then
3427
4a8fa0364f35 mod_atom: Unpack event object
Kim Alvefur <zash@zash.se>
parents: 3280
diff changeset
    31
				response.headers.content_type = "application/xml";
3276
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3245
diff changeset
    32
				local feed = st.stanza("feed", { xmlns = "http://www.w3.org/2005/Atom" })
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3245
diff changeset
    33
					:text_tag("generator", "Prosody", { uri = "xmpp:prosody.im", version = prosody.version })
3278
2acfb45fd9ec mod_atom: Expose title and description from node metadata in feed
Kim Alvefur <zash@zash.se>
parents: 3277
diff changeset
    34
					:text_tag("title", pubsub_service.nodes["urn:xmpp:microblog:0"].config.title or "Microblog feed")
2acfb45fd9ec mod_atom: Expose title and description from node metadata in feed
Kim Alvefur <zash@zash.se>
parents: 3277
diff changeset
    35
					:text_tag("subtitle", pubsub_service.nodes["urn:xmpp:microblog:0"].config.description)
3276
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3245
diff changeset
    36
					:tag("author")
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3245
diff changeset
    37
						:text_tag("name", user)
3277
78888f4ec7e3 mod_atom: Add node as portablecontacts preferredUsername
Kim Alvefur <zash@zash.se>
parents: 3276
diff changeset
    38
						:text_tag("preferredUsername", user, { xmlns = "http://portablecontacts.net/spec/1.0" });
3279
25b4cad8fee4 mod_atom: Include User Nickname (if configured to be public)
Kim Alvefur <zash@zash.se>
parents: 3278
diff changeset
    39
				local ok, _, nick = pubsub_service:get_last_item("http://jabber.org/protocol/nick", actor);
25b4cad8fee4 mod_atom: Include User Nickname (if configured to be public)
Kim Alvefur <zash@zash.se>
parents: 3278
diff changeset
    40
				if ok and nick then
25b4cad8fee4 mod_atom: Include User Nickname (if configured to be public)
Kim Alvefur <zash@zash.se>
parents: 3278
diff changeset
    41
					feed:text_tag("displayName", nick.tags[1][1], { xmlns = "http://portablecontacts.net/spec/1.0" });
25b4cad8fee4 mod_atom: Include User Nickname (if configured to be public)
Kim Alvefur <zash@zash.se>
parents: 3278
diff changeset
    42
				end
3276
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3245
diff changeset
    43
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3245
diff changeset
    44
				feed:reset();
119e22ccd64a mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents: 3245
diff changeset
    45
3245
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    46
				for i = #items, 1, -1 do
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    47
					feed:add_direct_child(items[items[i]].tags[1]);
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    48
				end
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    49
				return tostring(feed);
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    50
			elseif items == "forbidden" then
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    51
				return 403;
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    52
			elseif items == "item-not-found" then
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    53
				return 404;
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    54
			end
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    55
		end;
4b52cafd5811 mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents: 2298
diff changeset
    56
	}
2298
4915b8223b07 mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    57
});