annotate mod_pubsub_pivotaltracker/mod_pubsub_pivotaltracker.lua @ 5342:e28ba69b5307

mod_rest: Implement use of refresh tokens in rest.sh example Because having access tokens expire daily was becoming annoying. Now this is starting to be in dire need of refactoring.
author Kim Alvefur <zash@zash.se>
date Wed, 12 Apr 2023 11:24:50 +0200
parents 7dbde05b48a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
859
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module:depends("http");
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local lom = require "lxp.lom";
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local st = require "util.stanza";
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local json = require "util.json";
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local datetime = require "util.datetime".datetime;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local pubsub_service = module:depends("pubsub").service;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local node = module:get_option("pivotaltracker_node", "tracker");
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local stanza_mt = require "util.stanza".stanza_mt;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local function stanza_from_lom(lom)
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if lom.tag then
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local child_tags, attr = {}, {};
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local stanza = setmetatable({ name = lom.tag, attr = attr, tags = child_tags }, stanza_mt);
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 for i, attr_name in ipairs(lom.attr) do
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 attr[attr_name] = lom.attr[attr_name]
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 for i, child in ipairs(lom) do
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if child.tag then
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 child = stanza_from_lom(child);
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 child_tags[#child_tags+1] = child;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 stanza[i] = child;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return stanza;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 else
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 return lom;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 function handle_POST(event)
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local data = lom.parse(event.request.body);
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 859
diff changeset
35
859
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if not data then
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 return "Invalid XML. From you of all people...";
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 859
diff changeset
39
859
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 data = stanza_from_lom(data);
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 859
diff changeset
41
859
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if data.name ~= "activity" then
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return "Unrecognised XML element: "..data.name;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 859
diff changeset
45
859
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local activity_id = data:get_child("id"):get_text();
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local description = data:get_child("description"):get_text();
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local author_name = data:get_child("author"):get_text();
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local story = data:get_child("stories"):get_child("story");
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local story_link = story:get_child("url"):get_text();
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 859
diff changeset
51
859
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local ok, err = pubsub_service:publish(node, true, "activity", st.stanza("item", { id = "activity", xmlns = "http://jabber.org/protocol/pubsub" })
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" })
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 :tag("id"):text(activity_id):up()
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 :tag("title"):text(description):up()
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 :tag("link", { rel = "alternate", href = story_link }):up()
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 :tag("published"):text(datetime()):up()
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 :tag("author")
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 :tag("name"):text(author_name):up()
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 :up()
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 );
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 859
diff changeset
62
859
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 module:log("debug", "Handled POST: \n%s\n", tostring(event.request.body));
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 return "Thank you Pivotal!";
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 module:provides("http", {
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 route = {
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 POST = handle_POST;
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 };
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 });
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 function module.load()
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 if not pubsub_service.nodes[node] then
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local ok, err = pubsub_service:create(node, true);
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 if not ok then
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 module:log("error", "Error creating node: %s", err);
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 else
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 module:log("debug", "Node %q created", node);
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
9922e8cdf4a4 mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end