Software /
code /
prosody-modules
Annotate
mod_http_muc_log/mod_http_muc_log.lua @ 1581:9f6cd252d233
mod_http_muc_log: Revamp template system
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 02 Dec 2014 15:12:01 +0100 |
parent | 1580:63571115302f |
child | 1582:8e282eb0c70c |
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; |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
8 local url = require"socket.url"; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
9 local xml_escape = st.xml_escape; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
10 local t_concat = table.concat; |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
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
|
12 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
|
13 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 -- 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
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 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
|
23 function (jid) |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 return rooms[jid]; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 module:depends"http"; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
34 local function render(template, values) |
1555 | 35 --[[ DOC |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 {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
|
37 {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
|
38 {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
|
39 {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
|
40 ]] |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
41 return (template:gsub("%b{}", function (block) |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
42 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
|
43 local value = values[name]; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
44 if opt == '#' then |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
45 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
|
46 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
|
47 for i=1, #value do |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
48 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
|
49 end |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
50 return t_concat(out); |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
51 end |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
52 if value ~= nil then |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
53 if type(value) ~= "string" then |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
54 value = tostring(value); |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
55 end |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
56 if opt ~= '!' then |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
57 return xml_escape(value); |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
58 end |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
59 return value; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
60 elseif opt == '?' then |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
61 return block:sub(e+1, -2); |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
62 end |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
63 end)); |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
66 -- TODO Move template into a file |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
67 local template = [=[ |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 <!DOCTYPE html> |
1555 | 69 <html> |
70 <head> | |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 <meta charset="utf-8"> |
1563
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
72 <meta name="viewport" content="width=device-width, initial-scale=1"> |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 <title>{title}</title> |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 <style> |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
75 :link,:visited{text-decoration:none;color:#2e3436;text-decoration:none;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
76 :link:hover,:visited:hover{color:#3465a4;} |
1555 | 77 body{background-color:#eeeeec;margin:1ex 0;padding-bottom:3em;font-family:Arial,Helvetica,sans-serif;} |
78 ul,ol{padding:0;} | |
79 li{list-style:none;} | |
1563
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
80 hr{visibility:hidden;clear:both;} |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
81 br{clear:both;} |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
82 header,footer{margin:1ex 1em;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
83 footer{font-size:smaller;color:#babdb6;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
84 nav{font-size:large;margin:1ex 1ex;clear:both;line-height:1.5em;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
85 footer nav .up{display:none;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
86 @media screen and (min-width: 460px) { |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
87 nav {font-size:x-large;margin:1ex 1em;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
88 } |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
89 nav a{padding: 1ex;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
90 nav .up{font-size:smaller;display:block;clear:both;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
91 nav .prev{float:left;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
92 nav .next{float:right;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
93 nav .next::after{content:" →";} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
94 nav .prev::before{content:"← ";} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
95 nav :empty::after,nav :empty::before{content:""} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
96 .content{background-color:white;padding:1em;list-style-position:inside;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
97 .time{float:right;font-size:small;opacity:0.2;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
98 li:hover .time{opacity:1;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
99 .description{font-size:smaller;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
100 .body{white-space:pre-line;} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
101 .body::before,.body::after{content:"";} |
1555 | 102 .presence .verb{font-style:normal;color:#30c030;} |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
103 .unavailable .verb{color:#c03030;} |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 </style> |
1555 | 105 </head> |
106 <body> | |
1557
876af57a3983
mod_http_muc_log: Fix header margin
Kim Alvefur <zash@zash.se>
parents:
1556
diff
changeset
|
107 <header> |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
108 <h1 title="xmpp:{jid?}">{title}</h1> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
109 <nav>{links# |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
110 <a class="{rel?}" href="{href}" rel="{rel?}">{text}</a>} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
111 </nav> |
1555 | 112 </header> |
113 <hr> | |
114 <div class="content"> | |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
115 <nav> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
116 <dl class="room-list"> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
117 {rooms# |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
118 <dt class="name"><a href="{href}">{name}</a></dt> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
119 <dd class="description">{description?}</dd>} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
120 </dl> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
121 <ul class="dates">{dates# |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
122 <li><a href="{date}">{date}</a></li>} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
123 </ul> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
124 </nav> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
125 <ol class="chat-logs">{lines# |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
126 <li class="{st_name} {st_type?}" id="{key}"> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
127 <a class="time" href="#{key}"><time datetime="{datetime}">{time}</time></a> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
128 <b class="nick">{nick}</b> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
129 <em class="verb">{verb?}</em> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
130 <q class="body">{body?}</q> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
131 </li>} |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
132 </ol> |
1555 | 133 </div> |
134 <hr> | |
135 <footer> | |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
136 <nav>{links# |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
137 <a class="{rel?}" href="{href}" rel="{rel?}">{text}</a>} |
1555 | 138 </nav> |
139 <br> | |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
140 <div class="powered-by">Prosody</div> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
141 </footer> |
1555 | 142 <script> |
1563
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
143 /* |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
144 * Local timestamps |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
145 */ |
1555 | 146 (function () { |
147 var timeTags = document.getElementsByTagName("time"); | |
1563
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
148 var i = 0, tag, date; |
1555 | 149 while(timeTags[i]) { |
1563
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
150 tag = timeTags[i++]; |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
151 if(date = tag.getAttribute("datetime")) { |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
152 date = new Date(date); |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
153 tag.textContent = date.toLocaleTimeString(); |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
154 tag.setAttribute("title", date.toString()); |
49ba05d75119
mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents:
1562
diff
changeset
|
155 } |
1555 | 156 } |
157 })(); | |
158 </script> | |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
159 </body> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
160 </html> |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
161 ]=]; |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
163 local base_url = module:http_url() .. '/'; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
164 local get_link do |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
165 local link, path = { path = '/' }, { "", "", is_directory = true }; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
166 function get_link(room, date) |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
167 path[1], path[2] = room, date; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
168 path.is_directory = not date; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
169 link.path = url.build_path(path); |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
170 return url.build(link); |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
171 end |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
172 end |
1549
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 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
|
175 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
|
176 room = get_room(room); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 -- 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
|
185 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
|
186 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
|
187 |
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
|
188 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
|
189 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
|
190 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 local dates, i = {}, 1; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 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
|
193 local next_day; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 repeat |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 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
|
196 ["start"] = next_day; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 limit = 1; |
1553
1398d2bbcd42
mod_http_muc_log: List only dates with messages
Kim Alvefur <zash@zash.se>
parents:
1552
diff
changeset
|
198 with = "message<groupchat"; |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 }) |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 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
|
201 next_day = nil; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 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
|
203 next_day = datetime.date(when); |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
204 dates[i], i = { |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 date = next_day; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 }, i + 1; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 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
|
208 break; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 until not next_day; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 |
1579
9e784ddac236
mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents:
1578
diff
changeset
|
212 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
|
213 return render(template, { |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
214 title = get_room(room):get_name(); |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
215 jid = get_room(room).jid; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
216 dates = dates; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
217 links = { |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
218 { 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
|
219 }; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
220 }); |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 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
|
224 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
|
225 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 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
|
227 room = nodeprep(room); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 if not room then |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 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
|
230 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 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
|
232 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 local logs, i = {}, 1; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 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
|
235 ["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
|
236 ["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
|
237 -- with = "message<groupchat"; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 }); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 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
|
240 |
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
|
241 local first, last; |
1555 | 242 local verb, subject, body; |
243 for key, item, when in iter do | |
244 body = item:get_child_text("body"); | |
245 subject = item:get_child_text("subject"); | |
246 verb = nil; | |
247 if subject then | |
1578
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
248 verb, body = "set the topic to", subject; |
1555 | 249 elseif body and body:sub(1,4) == "/me " then |
250 verb, body = body:sub(5), nil; | |
251 elseif item.name == "presence" then | |
252 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
|
253 end |
1578
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
254 if body or verb then |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
255 logs[i], i = { |
1578
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
256 key = key; |
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
257 datetime = datetime.datetime(when); |
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
258 time = datetime.time(when); |
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
259 verb = verb; |
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
260 body = body; |
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
261 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
|
262 st_name = item.name; |
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
263 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
|
264 }, i + 1; |
a68ec7a2dc02
mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents:
1577
diff
changeset
|
265 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
|
266 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
|
267 last = key; |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 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
|
269 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
|
270 |
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
|
271 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
|
272 local prev_when = ""; |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 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
|
275 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
|
276 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
|
277 limit = 1; |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 }) 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
|
279 next_when = datetime.date(when); |
1550
1b2823b41f7f
mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents:
1549
diff
changeset
|
280 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
|
281 end |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 |
1550
1b2823b41f7f
mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents:
1549
diff
changeset
|
283 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
|
284 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
|
285 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
|
286 limit = 1; |
1550
1b2823b41f7f
mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents:
1549
diff
changeset
|
287 reverse = true; |
1b2823b41f7f
mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents:
1549
diff
changeset
|
288 }) 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
|
289 prev_when = datetime.date(when); |
1550
1b2823b41f7f
mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents:
1549
diff
changeset
|
290 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
|
291 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 |
1579
9e784ddac236
mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents:
1578
diff
changeset
|
293 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
|
294 return render(template, { |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
295 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
|
296 jid = get_room(room).jid; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
297 lines = logs; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
298 links = { |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
299 { href = "./", rel = "up", text = "Back to date list" }, |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
300 { 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
|
301 { 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
|
302 }; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
303 }); |
1549
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 local function list_rooms(event) |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
307 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
|
308 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
|
309 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
|
310 if public_room(room) then |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
311 room_list[i], i = { |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
312 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
|
313 name = room:get_name(); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
314 description = room:get_description(); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
315 }, i + 1; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 end |
1576
91b91052e0e8
mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents:
1575
diff
changeset
|
318 |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
319 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
|
320 return render(template, { |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
321 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
|
322 jid = module.host; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
323 rooms = room_list; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
324 }); |
1549
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 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 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
|
328 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 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
|
330 return function (event, path) |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 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
|
332 local ckey = path or ""; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 local cached = cache[ckey]; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 if cached then |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 local etag = cached.etag; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 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
|
338 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
|
339 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
|
340 return 304; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 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
|
343 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
|
344 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
|
345 return cached[1]; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 local start = gettime(); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 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
|
350 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
|
351 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 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
|
353 local etag = uuid(); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 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
|
355 response.headers.etag = etag; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 cache[ckey] = cached; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 |
1579
9e784ddac236
mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents:
1578
diff
changeset
|
359 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
|
360 return render; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 end |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
363 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 -- 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
|
365 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
|
366 local room = event.room; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
367 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
|
368 local today = datetime.date(); |
1581
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
369 cache[get_link(room_name)] = nil; |
9f6cd252d233
mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents:
1580
diff
changeset
|
370 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
|
371 end); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
373 module:provides("http", { |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
374 route = { |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
375 ["GET /"] = list_rooms; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
376 ["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
|
377 }; |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
378 }); |
f9f8bf82ece7
mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 |