Comparison

mod_http_muc_log/mod_http_muc_log.lua @ 2590:63dd3e525f13

mod_http_muc_log: Add some comments
author Kim Alvefur <zash@zash.se>
date Wed, 01 Mar 2017 20:29:40 +0100
parent 2236:86bc6e1d9d4d
child 2591:3e1a85c5194c
comparison
equal deleted inserted replaced
2589:89a3f049689f 2590:63dd3e525f13
48 link.path = url.build_path(path); 48 link.path = url.build_path(path);
49 return url.build(link); 49 return url.build(link);
50 end 50 end
51 end 51 end
52 52
53 local function public_room(room) 53 local function public_room(room) -- : boolean
54 if type(room) == "string" then 54 if type(room) == "string" then
55 room = get_room(room); 55 room = get_room(room);
56 end 56 end
57 return (room 57 return (room
58 and not (room.get_hidden or room.is_hidden)(room) 58 and not (room.get_hidden or room.is_hidden)(room)
61 end 61 end
62 62
63 local function sort_Y(a,b) return a.year > b.year end 63 local function sort_Y(a,b) return a.year > b.year end
64 local function sort_m(a,b) return a.n > b.n end 64 local function sort_m(a,b) return a.n > b.n end
65 65
66 -- Time zone hack?
66 local t_diff = os_time(os_date("*t")) - os_time(os_date("!*t")); 67 local t_diff = os_time(os_date("*t")) - os_time(os_date("!*t"));
67 local function time(t) 68 local function time(t)
68 return os_time(t) + t_diff; 69 return os_time(t) + t_diff;
69 end 70 end
70 71
72 -- Fetch one item
71 local function find_once(room, query, retval) 73 local function find_once(room, query, retval)
72 if query then query.limit = 1; else query = { limit = 1 }; end 74 if query then query.limit = 1; else query = { limit = 1 }; end
73 local iter, err = archive:find(room, query); 75 local iter, err = archive:find(room, query);
74 if not iter then return iter, err; end 76 if not iter then return iter, err; end
75 if retval then 77 if retval then
76 return select(retval, iter()); 78 return select(retval, iter());
77 end 79 end
78 return iter(); 80 return iter();
79 end 81 end
80 82
83 -- Produce the calendar view
81 local function years_page(event, path) 84 local function years_page(event, path)
82 local response = event.response; 85 local response = event.response;
83 86
84 local room = nodeprep(path:match("^(.*)/$")); 87 local room = nodeprep(path:match("^(.*)/$"));
85 if not room or not public_room(room) then return end 88 if not room or not public_room(room) then return end
86 89
90 -- Collect each date that has messages
91 -- convert it to a year / month / day tree
87 local date_list = archive.dates and archive:dates(room); 92 local date_list = archive.dates and archive:dates(room);
88 local dates = mt.new(); 93 local dates = mt.new();
89 if date_list then 94 if date_list then
90 for _, date in ipairs(date_list) do 95 for _, date in ipairs(date_list) do
91 local when = datetime.parse(date.."T00:00:00Z"); 96 local when = datetime.parse(date.."T00:00:00Z");
92 local t = os_date("!*t", when); 97 local t = os_date("!*t", when);
93 dates:set(t.year, t.month, t.day, when); 98 dates:set(t.year, t.month, t.day, when);
94 end 99 end
95 else 100 else
101 -- Collect date the hard way
96 module:log("debug", "Find all dates with messages"); 102 module:log("debug", "Find all dates with messages");
97 local next_day; 103 local next_day;
98 repeat 104 repeat
99 local when = find_once(room, { start = next_day; }, 3); 105 local when = find_once(room, { start = next_day; }, 3);
100 if not when then break; end 106 if not when then break; end
104 until not next_day; 110 until not next_day;
105 end 111 end
106 112
107 local years = {}; 113 local years = {};
108 114
115 -- Wrangle Y/m/d tree into year / month / week / day tree for calendar view
109 for current_year, months_t in pairs(dates.data) do 116 for current_year, months_t in pairs(dates.data) do
110 local t = { year = current_year, month = 1, day = 1 }; 117 local t = { year = current_year, month = 1, day = 1 };
111 local months = { }; 118 local months = { };
112 local year = { year = current_year, months = months }; 119 local year = { year = current_year, months = months };
113 years[#years+1] = year; 120 years[#years+1] = year;
138 end 145 end
139 table.sort(year, sort_m); 146 table.sort(year, sort_m);
140 end 147 end
141 table.sort(years, sort_Y); 148 table.sort(years, sort_Y);
142 149
150 -- Phew, all wrangled, all that's left is rendering it with the template
151
143 response.headers.content_type = "text/html; charset=utf-8"; 152 response.headers.content_type = "text/html; charset=utf-8";
144 return render(template, { 153 return render(template, {
145 title = get_room(room):get_name(); 154 title = get_room(room):get_name();
146 jid = get_room(room).jid; 155 jid = get_room(room).jid;
147 years = years; 156 years = years;
149 { href = "../", rel = "up", text = "Back to room list" }, 158 { href = "../", rel = "up", text = "Back to room list" },
150 }; 159 };
151 }); 160 });
152 end 161 end
153 162
163 -- Produce the chat log view
154 local function logs_page(event, path) 164 local function logs_page(event, path)
155 local response = event.response; 165 local response = event.response;
156 166
167 -- FIXME In the year, 252525, if MUC is still alive,
168 -- if Prosody can survive... Enjoy this Y10k bug
157 local room, date = path:match("^(.-)/(%d%d%d%d%-%d%d%-%d%d)$"); 169 local room, date = path:match("^(.-)/(%d%d%d%d%-%d%d%-%d%d)$");
158 room = nodeprep(room); 170 room = nodeprep(room);
159 if not room then 171 if not room then
160 return years_page(event, path); 172 return years_page(event, path);
161 end 173 end