annotate mod_http_muc_log/mod_http_muc_log.lua @ 1606:2c8b985ebde5

mod_http_muc_log: Switch to a calendar view for selecting dates
author Kim Alvefur <zash@zash.se>
date Tue, 10 Feb 2015 15:00:10 +0100
parents c8a51d1bc96d
children c427de617ada
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";
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
2 local mt = require"util.multitable";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 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
4 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
5 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
6 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
7 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
8 local gettime = require"socket".gettime;
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
9 local url = require"socket.url";
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
10 local xml_escape = st.xml_escape;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
11 local t_concat = table.concat;
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
12 local os_time, os_date = os.time, os.date;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
1571
eed7db9f3157 mod_mam_muc, mod_http_muc_log: Change store name from 'archive2' to 'muc_log' to distinguish it from personal MAM archives. Old data will require migration.
Kim Alvefur <zash@zash.se>
parents: 1564
diff changeset
14 local archive = module:open_store("muc_log", "archive");
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 -- 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
17 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
18 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
19 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
20 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
21 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
22 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
23 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 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
25 function (jid)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return rooms[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 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
30 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
31 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
32 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 module:depends"http";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
36 local function render(template, values)
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
37 --[[ DOC
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 {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
39 {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
40 {name?} is optional and is replaced with an empty string if no value exists
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
41 {name# sub-template } renders a sub-template using an array of values
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 ]]
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
43 return (template:gsub("%b{}", function (block)
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
44 local name, opt, e = block:sub(2, -2):match("([%a_][%w_]*)(%p?)()");
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
45 local value = values[name];
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
46 if opt == '#' then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
47 if not value or not value[1] then return ""; end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
48 local out, subtpl = {}, block:sub(e+1, -2);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
49 for i=1, #value do
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
50 out[i] = render(subtpl, value[i]);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
52 return t_concat(out);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
53 end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
54 if value ~= nil then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
55 if type(value) ~= "string" then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
56 value = tostring(value);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
57 end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
58 if opt ~= '!' then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
59 return xml_escape(value);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
60 end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
61 return value;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
62 elseif opt == '?' then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
63 return block:sub(e+1, -2);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
64 end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
65 end));
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67
1582
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
68 local template = "Could not load template"
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
69 do
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
70 local template_file = module:get_option_string(module.name .. "_template", module.name .. ".html");
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
71 template_file = assert(module:load_resource(template_file));
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
72 template = template_file:read("*a");
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
73 template_file:close();
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
74 end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
76 local base_url = module:http_url() .. '/';
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
77 local get_link do
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
78 local link, path = { path = '/' }, { "", "", is_directory = true };
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
79 function get_link(room, date)
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
80 path[1], path[2] = room, date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
81 path.is_directory = not date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
82 link.path = url.build_path(path);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
83 return url.build(link);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
84 end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
85 end
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 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
88 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
89 room = get_room(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end
1575
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
91 return (room
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
92 and not (room.get_hidden or room.is_hidden)(room)
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
93 and not (room.get_members_only or room.is_members_only)(room)
1580
63571115302f mod_http_muc_log: List rooms with logging explicitly enabled
Kim Alvefur <zash@zash.se>
parents: 1579
diff changeset
94 and room._data.logging == true);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
97 local function sort_Y(a,b) return a.year > b.year end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
98 local function sort_m(a,b) return a.n > b.n end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
99
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
100 local t_diff = os_time(os_date("*t")) - os_time(os_date("!*t"));
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
101 local function time(t)
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
102 return os_time(t) + t_diff;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
103 end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
104
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
105 local function years_page(event, path)
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 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
107
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
108 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
109 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
110
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
111 local dates = mt.new();
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 module:log("debug", "Find all dates with messages");
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
113 local next_day, t;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 repeat
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local iter = archive:find(room, {
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
116 start = next_day;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 limit = 1;
1553
1398d2bbcd42 mod_http_muc_log: List only dates with messages
Kim Alvefur <zash@zash.se>
parents: 1552
diff changeset
118 with = "message<groupchat";
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 if not iter then break end
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
121 next_day = nil;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 for key, message, when in iter do
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
123 t = os_date("!*t", when);
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
124 dates:set(t.year, t.month, t.day, when );
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
125 next_day = when + (86400 - (when % 86400));
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 break;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 end
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
128 until not next_day;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
129
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
130 local year, years;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
131 local month, months;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
132 local week, weeks;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
133 local days;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
134 local tmp, n;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
135
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
136 years = {};
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
137
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
138 for Y, m in pairs(dates.data) do
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
139 t = { year = Y, month = 1, day = 1 };
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
140 months = { };
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
141 year = { year = Y, months = months };
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
142 years[#years+1] = year;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
143 for m, d in pairs(m) do
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
144 t.day = 1;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
145 t.month = m;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
146 tmp = os_date("!*t", time(t));
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
147 days = {};
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
148 week = { days = days }
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
149 weeks = { week };
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
150 month = { year = year.year, month = os_date("!%B", time(t)), n = m, weeks = weeks };
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
151 months[#months+1] = month;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
152 n = 1;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
153 for i=1, (tmp.wday+5)%7 do
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
154 days[n], n = {}, n+1;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
155 end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
156 for i = 1, 31 do
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
157 t.day = i;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
158 tmp = os_date("!*t", time(t));
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
159 if tmp.month ~= m then break end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
160 if i > 1 and tmp.wday == 2 then
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
161 days = {};
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
162 weeks[#weeks+1] = { days = days };
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
163 n = 1;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
164 end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
165 if d[i] then
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
166 days[n], n = { wday = tmp.wday, links = {{ href = datetime.date(d[i]), day = i }} }, n+1;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
167 else
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
168 days[n], n = { wday = tmp.wday, plain = i }, n+1;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
169 end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
170 end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
171 end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
172 table.sort(year, sort_m);
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
173 end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
174 table.sort(years, sort_Y);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
176 response.headers.content_type = "text/html; charset=utf-8";
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
177 return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
178 title = get_room(room):get_name();
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
179 jid = get_room(room).jid;
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
180 years = years;
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
181 links = {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
182 { href = "../", rel = "up", text = "Back to room list" },
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
183 };
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
184 });
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 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
188 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
189
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 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
191 room = nodeprep(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 if not room then
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
193 return years_page(event, path);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 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
196
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 local logs, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 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
199 ["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
200 ["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
201 -- with = "message<groupchat";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 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
204
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
205 local first, last;
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
206 local verb, subject, body;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
207 for key, item, when in iter do
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
208 body = item:get_child_text("body");
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
209 subject = item:get_child_text("subject");
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
210 verb = nil;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
211 if subject then
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
212 verb, body = "set the topic to", subject;
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
213 elseif body and body:sub(1,4) == "/me " then
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
214 verb, body = body:sub(5), nil;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
215 elseif item.name == "presence" then
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
216 verb = item.attr.type == "unavailable" and "has left" or "has joined";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 end
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
218 if body or verb then
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
219 logs[i], i = {
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
220 key = key;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
221 datetime = datetime.datetime(when);
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
222 time = datetime.time(when);
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
223 verb = verb;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
224 body = body;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
225 nick = select(3, jid_split(item.attr.from));
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
226 st_name = item.name;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
227 st_type = item.attr.type;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
228 }, i + 1;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
229 end
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
230 first = first or key;
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
231 last = key;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 end
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
233 if i == 1 then return end -- No items
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
235 local next_when = "";
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
236 local prev_when = "";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 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
239 for key, message, when in archive:find(room, {
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
240 after = last;
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
241 limit = 1;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 }) do
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
243 next_when = datetime.date(when);
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
244 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
245 end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
247 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
248 for key, message, when in archive:find(room, {
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
249 before = first;
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
250 limit = 1;
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
251 reverse = true;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
252 }) do
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
253 prev_when = datetime.date(when);
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
254 module:log("debug", "Previous message: %s", datetime.datetime(when));
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
257 response.headers.content_type = "text/html; charset=utf-8";
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
258 return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
259 title = ("%s - %s"):format(get_room(room):get_name(), date);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
260 jid = get_room(room).jid;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
261 lines = logs;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
262 links = {
1606
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1605
diff changeset
263 { href = "./", rel = "up", text = "Back to calendar" },
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
264 { href = prev_when, rel = "prev", text = prev_when},
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
265 { href = next_when, rel = "next", text = next_when},
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
266 };
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
267 });
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 local function list_rooms(event)
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
271 local request, response = event.request, event.response;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 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
273 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
274 if public_room(room) then
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
275 room_list[i], i = {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
276 href = get_link(jid_split(room.jid), nil);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 name = room:get_name();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 description = room:get_description();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 }, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 end
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
282
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
283 response.headers.content_type = "text/html; charset=utf-8";
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
284 return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
285 title = module:get_option_string("name", "Prosody Chatrooms");
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
286 jid = module.host;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
287 rooms = room_list;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
288 });
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 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
292
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 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
294 return function (event, path)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 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
296 local ckey = path or "";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 local cached = cache[ckey];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 if cached then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 local etag = cached.etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 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
302 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
303 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
304 return 304;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 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
307 response.headers.etag = etag;
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
308 response.headers.content_type = "text/html; charset=utf-8";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 return cached[1];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 local start = gettime();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 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
314 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
315
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 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
317 local etag = uuid();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 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
319 response.headers.etag = etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 cache[ckey] = cached;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
323 response.headers.content_type = "text/html; charset=utf-8";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 return render;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 -- 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
329 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
330 local room = event.room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 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
332 local today = datetime.date();
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
333 cache[get_link(room_name)] = nil;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
334 cache[get_link(room_name, today)] = nil;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 end);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 module:provides("http", {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 route = {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 ["GET /"] = list_rooms;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 ["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
341 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343