annotate mod_http_muc_log/mod_http_muc_log.lua @ 1554:8059b7cdaf17

mod_http_muc_log: Make MUC local code identical to mod_mam_muc
author Kim Alvefur <zash@zash.se>
date Sun, 09 Nov 2014 08:37:43 +0100
parents 1398d2bbcd42
children 2e51f70cd7ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require "util.stanza";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local datetime = require"util.datetime";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local jid_split = require"util.jid".split;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local nodeprep = require"util.encodings".stringprep.nodeprep;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local uuid = require"util.uuid".generate;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local it = require"util.iterators";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local gettime = require"socket".gettime;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local archive = module:open_store("archive2", "archive");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 -- Support both old and new MUC code
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local mod_muc = module:depends"muc";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local rooms = rawget(mod_muc, "rooms");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
1554
8059b7cdaf17 mod_http_muc_log: Make MUC local code identical to mod_mam_muc
Kim Alvefur <zash@zash.se>
parents: 1553
diff changeset
15 local new_muc = not rooms;
8059b7cdaf17 mod_http_muc_log: Make MUC local code identical to mod_mam_muc
Kim Alvefur <zash@zash.se>
parents: 1553
diff changeset
16 if new_muc then
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 rooms = module:shared"muc/rooms";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 function (jid)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return rooms[jid];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local function get_room(name)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local jid = name .. '@' .. module.host;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return get_room_from_jid(jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 module:depends"http";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local function template(data)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local _doc = [[
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 Like util.template, but deals with plain text
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 Returns a closure that is called with a table of values
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 {name} is substituted for values["name"] and is XML escaped
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 {name!} is substituted without XML escaping
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 {name?} is optional and is replaced with an empty string if no value exists
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 ]]
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 return function(values)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 return (data:gsub("{([^!}]-)(%p?)}", function (name, opt)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local value = values[name];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if value then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if opt ~= "!" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return st.xml_escape(value);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 return value;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 elseif opt == "?" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return "";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end));
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 local base = template[[
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 <!DOCTYPE html>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 <meta charset="utf-8">
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 <title>{title}</title>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 <style>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 body { margin: 1ex 1em; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 ul { padding: 0; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 li.action dt, li.action dd { display: inline-block; margin-left: 0;}
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 li.action dd { margin-left: 1ex;}
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 li { list-style: none; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 li:hover { background: #eee; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 li time { float: right; font-size: small; opacity: 0.2; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 li:hover time { opacity: 1; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 li.join , li.leave { color: green; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 li.join dt, li.leave dt { color: green; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 nav { font-size: x-large; margin: 1ex 2em; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 nav a { text-decoration: none; }
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 </style>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 <h1>{title}</h1>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 {body!}
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 ]]
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local dates_template = template(base{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 title = "Logs for room {room}";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 body = [[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
79 <base href="{room}/">
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
80 <nav>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
81 <a href="..">↑</a>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
82 </nav>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
83 <ul>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
84 {lines!}</ul>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
85 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 })
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local date_line_template = template[[
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 <li><a href="{date}">{date}</a></li>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 ]];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 local page_template = template(base{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 title = "Logs for room {room} on {date}";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 body = [[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
95 <nav>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
96 <a class="prev" href="{prev}">←</a>
1552
81b3599c02fb mod_http_muc_log: Have URL of date list page end with a slash
Kim Alvefur <zash@zash.se>
parents: 1550
diff changeset
97 <a class="up" href=".">↑</a>
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
98 <a class="next" href="{next}">→</a>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
99 </nav>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
100 <ul>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
101 {logs!}
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
102 </ul>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
103 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 local line_templates = {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 ["message<groupchat"] = template[[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
108 <li id="{key}" class="{st_name}"><a href="#{key}"><time>{time}</time></a><dl><dt>{nick}</dt><dd>{body}</dd></dl></li>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
109 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 ["message<groupchat<subject"] = template[[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
111 <li id="{key}" class="{st_name} action subject"><a href="#{key}"><time>{time}</time></a><dl><dt>{nick}</dt><dd>changed subject to {subject}</dd></dl></li>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
112 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 ["presence"] = template[[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
114 <li id="{key}" class="action join"><a href="#{key}"><time>{time}</time></a><dl><dt>{nick}</dt><dd>joined</dd></dl></li>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
115 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 ["presence<unavailable"] = template[[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
117 <li id="{key}" class="action leave"><a href="#{key}"><time>{time}</time></a><dl><dt>{nick}</dt><dd>left</dd></dl></li>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
118 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 local room_list_template = template(base{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 title = "Rooms on {host}";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 body = [[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
124 <dl>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
125 {rooms!}
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
126 </dl>
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
127 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 local room_item_template = template[[
1552
81b3599c02fb mod_http_muc_log: Have URL of date list page end with a slash
Kim Alvefur <zash@zash.se>
parents: 1550
diff changeset
131 <dt><a href="{room}/">{name}</a></dt>
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 <dd>{description?}</dd>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 ]];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 local function public_room(room)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 if type(room) == "string" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 room = get_room(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 return room and not room:get_hidden() and not room:get_members_only() and room._data.logging ~= false;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 -- FIXME Invent some more efficient API for this
1552
81b3599c02fb mod_http_muc_log: Have URL of date list page end with a slash
Kim Alvefur <zash@zash.se>
parents: 1550
diff changeset
143 local function dates_page(event, path)
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 local request, response = event.request, event.response;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
1552
81b3599c02fb mod_http_muc_log: Have URL of date list page end with a slash
Kim Alvefur <zash@zash.se>
parents: 1550
diff changeset
146 local room = nodeprep(path:match("^(.*)/$"));
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 if not room or not public_room(room) then return end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 local dates, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 module:log("debug", "Find all dates with messages");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 local next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 repeat
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 local iter = archive:find(room, {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 ["start"] = next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 limit = 1;
1553
1398d2bbcd42 mod_http_muc_log: List only dates with messages
Kim Alvefur <zash@zash.se>
parents: 1552
diff changeset
156 with = "message<groupchat";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 })
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 if not iter then break end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 next_day = nil;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 for key, message, when in iter do
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 next_day = datetime.date(when);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 dates[i], i = date_line_template{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 date = next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 }, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 next_day = datetime.parse(next_day .. "T23:59:59Z") + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 break;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 until not next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 return dates_template{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 room = room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 lines = table.concat(dates);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 local function logs_page(event, path)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 local request, response = event.request, event.response;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 local room, date = path:match("^(.-)/(%d%d%d%d%-%d%d%-%d%d)$");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 room = nodeprep(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 if not room then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 return dates_page(event, path);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 if not public_room(room) then return end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 local logs, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local iter, err = archive:find(room, {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 ["start"] = datetime.parse(date.."T00:00:00Z");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 ["end"] = datetime.parse(date.."T23:59:59Z");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 limit = math.huge;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 -- with = "message<groupchat";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 if not iter then return 500; end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 local templ, typ;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 for key, message, when in iter do
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 templ = message.name;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 local typ = message.attr.type;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 if typ then templ = templ .. '<' .. typ; end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 local subject = message:get_child_text("subject");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 if subject then templ = templ .. '<subject'; end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 templ = line_templates[templ];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 if templ then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 logs[i], i = templ {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 key = key;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 time = datetime.time(when);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 nick = select(3, jid_split(message.attr.from));
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 body = message:get_child_text("body");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 subject = subject;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 st_name = message.name;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 st_type = message.attr.type;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 }, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 else
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 module:log("debug", "No template for %s", tostring(message));
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 local next_when = datetime.parse(date.."T12:00:00Z") + 86400;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 local prev_when = datetime.parse(date.."T12:00:00Z") - 86400;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 module:log("debug", "Find next date with messages");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 for key, message, when in archive:find(room, {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 ["start"] = datetime.parse(date.."T00:00:00Z") + 86401;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 limit = math.huge;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 }) do
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
226 next_when = when;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
227 module:log("debug", "Next message: %s", datetime.datetime(when));
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
228 break;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
229 end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
231 module:log("debug", "Find prev date with messages");
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
232 for key, message, when in archive:find(room, {
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
233 ["end"] = datetime.parse(date.."T00:00:00Z") - 1;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
234 limit = math.huge;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
235 reverse = true;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
236 }) do
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
237 prev_when = when;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
238 module:log("debug", "Previous message: %s", datetime.datetime(when));
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
239 break;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 return page_template{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 room = room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 date = date;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 logs = table.concat(logs);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 next = datetime.date(next_when);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 prev = datetime.date(prev_when);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 local function list_rooms(event)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 local room_list, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 for room in each_room() do
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 if public_room(room) then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 room_list[i], i = room_item_template {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 room = jid_split(room.jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 name = room:get_name();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 description = room:get_description();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 subject = room:get_subject();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 }, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 return room_list_template {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 host = module.host;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 rooms = table.concat(room_list);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 local cache = setmetatable({}, {__mode = 'v'});
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 local function with_cache(f)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 return function (event, path)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 local request, response = event.request, event.response;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 local ckey = path or "";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 local cached = cache[ckey];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 if cached then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 local etag = cached.etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 local if_none_match = request.headers.if_none_match;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 if etag == if_none_match then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 module:log("debug", "Client cache hit");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 return 304;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 module:log("debug", "Server cache hit");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 response.headers.etag = etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 return cached[1];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 local start = gettime();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 local render = f(event, path);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 module:log("debug", "Rendering took %dms", math.floor( (gettime() - start) * 1000 + 0.5));
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 if type(render) == "string" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 local etag = uuid();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 cached = { render, etag = etag, date = datetime.date() };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 response.headers.etag = etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 cache[ckey] = cached;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 return render;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 -- How is cache invalidation a hard problem? ;)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 module:hook("muc-broadcast-message", function (event)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 local room = event.room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 local room_name = jid_split(room.jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 local today = datetime.date();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 cache[ room_name .. "/" .. today ] = nil;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 if cache[room_name] and cache[room_name].date ~= today then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 cache[room_name] = nil;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 end);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 module:log("info", module:http_url());
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 module:provides("http", {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 route = {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 ["GET /"] = list_rooms;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 ["GET /*"] = with_cache(logs_page);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322