mod_http_muc_log/mod_http_muc_log.lua
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Mon, 17 Feb 2020 21:50:55 +0100
changeset 3897 3f20b7c88afb
parent 3896 96a2e5097fc4
child 4038 a359972d246e
permissions -rw-r--r--
mod_http_muc_log: Expose JID localpart to the template
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
     1
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
     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 it = require"util.iterators";
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
     6
local url = require"socket.url";
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
     7
local os_time, os_date = os.time, os.date;
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
     8
local httplib = require "util.http";
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
     9
local render_funcs = {};
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
    10
local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape, render_funcs);
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
3586
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
    32
local use_oob = module:get_option_boolean(module.name .. "_show_images", false);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
module:depends"http";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
    35
local template;
1582
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    36
do
3067
a9fa98e28697 mod_http_muc_log: Use separate variable for filename and file handle
Kim Alvefur <zash@zash.se>
parents: 3066
diff changeset
    37
	local template_filename = module:get_option_string(module.name .. "_template", module.name .. ".html");
3070
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    38
	local template_file, err = module:load_resource(template_filename);
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    39
	if template_file then
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    40
		template, err = template_file:read("*a");
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    41
		template_file:close();
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    42
	end
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    43
	if not template then
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    44
		module:log("error", "Error loading template: %s", err);
3490
887ce59cf396 mod_http_muc_log: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3489
diff changeset
    45
		template = render("<h1>mod_{module} could not read the template</h1>\
887ce59cf396 mod_http_muc_log: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3489
diff changeset
    46
		<p>Tried to open <b>{filename}</b></p>\
887ce59cf396 mod_http_muc_log: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3489
diff changeset
    47
		<pre>{error}</pre>",
3070
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    48
			{ module = module.name, filename = template_filename, error = err });
cefc375d0929 mod_http_muc_log: Handle errors while loading template file in a more graceful manner
Kim Alvefur <zash@zash.se>
parents: 3068
diff changeset
    49
	end
1582
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    50
end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
    52
-- local base_url = module:http_url() .. '/'; -- TODO: Generate links in a smart way
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    53
local get_link do
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    54
	local link, path = { path = '/' }, { "", "", is_directory = true };
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    55
	function get_link(room, date)
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    56
		path[1], path[2] = room, date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    57
		path.is_directory = not date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    58
		link.path = url.build_path(path);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    59
		return url.build(link);
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
end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
3597
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    63
local function get_absolute_link(room, date)
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    64
	local link = url.parse(module:http_url());
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    65
	local path = url.parse_path(link.path);
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    66
	if room then
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    67
		table.insert(path, room);
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    68
		if date then
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    69
			table.insert(path, date)
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    70
			path.is_directory = false;
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    71
		else
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    72
			path.is_directory = true;
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    73
		end
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    74
	end
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    75
	link.path = url.build_path(path)
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    76
	return url.build(link)
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    77
end
0b670831ace3 mod_http_muc_log: Correctly build absolute URLs
Kim Alvefur <zash@zash.se>
parents: 3596
diff changeset
    78
3063
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
    79
-- Whether room can be joined by anyone
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
    80
local function open_room(room) -- : boolean
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
	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
    82
		room = get_room(room);
3065
25eecbb195d9 mod_http_muc_log: Add comment about argument to policy function
Kim Alvefur <zash@zash.se>
parents: 3064
diff changeset
    83
		-- assumed to be a room object otherwise
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    84
	end
3061
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    85
	if not room then
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    86
		return nil;
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    87
	end
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    88
3063
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
    89
	if (room.get_members_only or room.is_members_only)(room) then
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
    90
		return false;
3061
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    91
	end
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    92
3063
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
    93
	if room:get_password() then
3061
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    94
		return false;
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    95
	end
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    96
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    97
	return true;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    98
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    99
3600
6e529f53b3c3 mod_http_muc_log: Allow specifying the default view
Kim Alvefur <zash@zash.se>
parents: 3599
diff changeset
   100
-- Can be set to "latest"
6e529f53b3c3 mod_http_muc_log: Allow specifying the default view
Kim Alvefur <zash@zash.se>
parents: 3599
diff changeset
   101
local default_view = module:get_option_string(module.name .. "_default_view", nil);
6e529f53b3c3 mod_http_muc_log: Allow specifying the default view
Kim Alvefur <zash@zash.se>
parents: 3599
diff changeset
   102
3068
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
   103
module:hook("muc-disco#info", function (event)
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
   104
	local room = event.room;
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
   105
	if open_room(room) then
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
   106
		table.insert(event.form, { name = "muc#roominfo_logs", type="text-single" });
3600
6e529f53b3c3 mod_http_muc_log: Allow specifying the default view
Kim Alvefur <zash@zash.se>
parents: 3599
diff changeset
   107
		event.formdata["muc#roominfo_logs"] = get_absolute_link(jid_split(event.room.jid), default_view);
3068
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
   108
	end
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
   109
end);
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
   110
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   111
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: 1609
diff changeset
   112
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: 1609
diff changeset
   113
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   114
-- Time zone hack?
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   115
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: 1609
diff changeset
   116
local function time(t)
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   117
	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: 1609
diff changeset
   118
end
2840
52a7c0f6aea1 mod_http_muc_log: Add a function for rounding timestamps down to midnight
Kim Alvefur <zash@zash.se>
parents: 2830
diff changeset
   119
local function date_floor(t)
52a7c0f6aea1 mod_http_muc_log: Add a function for rounding timestamps down to midnight
Kim Alvefur <zash@zash.se>
parents: 2830
diff changeset
   120
	return t - t % 86400;
52a7c0f6aea1 mod_http_muc_log: Add a function for rounding timestamps down to midnight
Kim Alvefur <zash@zash.se>
parents: 2830
diff changeset
   121
end
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   122
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   123
-- Fetch one item
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   124
local function find_once(room, query, retval)
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   125
	if query then query.limit = 1; else query = { limit = 1 }; end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   126
	local iter, err = archive:find(room, query);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   127
	if not iter then return iter, err; end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   128
	if retval then
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   129
		return select(retval, iter());
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   130
	end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   131
	return iter();
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   132
end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   133
2830
ed26608920d4 mod_http_muc_log: Switch to an inaccurate but O(1) method of producing the calendar view
Kim Alvefur <zash@zash.se>
parents: 2767
diff changeset
   134
local lazy = module:get_option_boolean(module.name .. "_lazy_calendar", true);
ed26608920d4 mod_http_muc_log: Switch to an inaccurate but O(1) method of producing the calendar view
Kim Alvefur <zash@zash.se>
parents: 2767
diff changeset
   135
3601
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   136
local presence_logged = module:get_option_boolean("muc_log_presences", false);
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   137
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   138
local function hide_presence(request)
3601
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   139
	if not presence_logged then
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   140
		return false;
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   141
	end
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   142
	if request.url.query then
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   143
		local data = httplib.formdecode(request.url.query);
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   144
		if data then
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   145
			return data.p == "h"
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   146
		end
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   147
	end
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   148
	return false;
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   149
end
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   150
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   151
local function get_dates(room) --> { integer, ... }
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   152
	local date_list = archive.dates and archive:dates(room);
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   153
	if date_list then
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   154
		for i = 1, #date_list do
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   155
			date_list[i] = datetime.parse(date_list[i].."T00:00:00Z");
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   156
		end
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   157
		return date_list;
3063
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
   158
	end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   160
	if lazy then
2830
ed26608920d4 mod_http_muc_log: Switch to an inaccurate but O(1) method of producing the calendar view
Kim Alvefur <zash@zash.se>
parents: 2767
diff changeset
   161
		-- Lazy with many false positives
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   162
		date_list = {};
2830
ed26608920d4 mod_http_muc_log: Switch to an inaccurate but O(1) method of producing the calendar view
Kim Alvefur <zash@zash.se>
parents: 2767
diff changeset
   163
		local first_day = find_once(room, nil, 3);
ed26608920d4 mod_http_muc_log: Switch to an inaccurate but O(1) method of producing the calendar view
Kim Alvefur <zash@zash.se>
parents: 2767
diff changeset
   164
		local last_day = find_once(room, { reverse = true }, 3);
2843
7738d7158dd0 mod_http_muc_log: Return a 404 page if there are no messages to derive a time span from
Kim Alvefur <zash@zash.se>
parents: 2842
diff changeset
   165
		if first_day and last_day then
7738d7158dd0 mod_http_muc_log: Return a 404 page if there are no messages to derive a time span from
Kim Alvefur <zash@zash.se>
parents: 2842
diff changeset
   166
			first_day = date_floor(first_day);
7738d7158dd0 mod_http_muc_log: Return a 404 page if there are no messages to derive a time span from
Kim Alvefur <zash@zash.se>
parents: 2842
diff changeset
   167
			last_day = date_floor(last_day);
7738d7158dd0 mod_http_muc_log: Return a 404 page if there are no messages to derive a time span from
Kim Alvefur <zash@zash.se>
parents: 2842
diff changeset
   168
			for when = first_day, last_day, 86400 do
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   169
				table.insert(date_list, when);
2843
7738d7158dd0 mod_http_muc_log: Return a 404 page if there are no messages to derive a time span from
Kim Alvefur <zash@zash.se>
parents: 2842
diff changeset
   170
			end
7738d7158dd0 mod_http_muc_log: Return a 404 page if there are no messages to derive a time span from
Kim Alvefur <zash@zash.se>
parents: 2842
diff changeset
   171
		else
7738d7158dd0 mod_http_muc_log: Return a 404 page if there are no messages to derive a time span from
Kim Alvefur <zash@zash.se>
parents: 2842
diff changeset
   172
			return; -- 404
2830
ed26608920d4 mod_http_muc_log: Switch to an inaccurate but O(1) method of producing the calendar view
Kim Alvefur <zash@zash.se>
parents: 2767
diff changeset
   173
		end
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   174
		return date_list;
1836
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   175
	end
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   176
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   177
	-- Collect date the hard way
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   178
	module:log("debug", "Find all dates with messages");
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   179
	date_list = {};
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   180
	local next_day;
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   181
	repeat
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   182
		local when = find_once(room, { start = next_day; }, 3);
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   183
		if not when then break; end
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   184
		table.insert(date_list, when);
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   185
		next_day = date_floor(when) + 86400;
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   186
	until not next_day;
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   187
	return date_list;
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   188
end
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   189
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   190
function render_funcs.calendarize(date_list)
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   191
	-- convert array of timestamps to a year / month / day tree
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   192
	local dates = mt.new();
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   193
	for _, when in ipairs(date_list) do
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   194
		local t = os_date("!*t", when);
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   195
		dates:set(t.year, t.month, t.day, when);
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   196
	end
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   197
	-- Wrangle Y/m/d tree into year / month / week / day tree for calendar view
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   198
	local years = {};
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   199
	for current_year, months_t in pairs(dates.data) do
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   200
		local t = { year = current_year, month = 1, day = 1 };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   201
		local months = { };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   202
		local year = { year = current_year, months = months };
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   203
		years[#years+1] = year;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   204
		for current_month, days_t in pairs(months_t) do
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   205
			t.day = 1;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   206
			t.month = current_month;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   207
			local tmp = os_date("!*t", time(t));
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   208
			local days = {};
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   209
			local week = { days = days }
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   210
			local weeks = { week };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   211
			local month = { year = year.year, month = os_date("!%B", time(t)), n = current_month, weeks = weeks };
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   212
			months[#months+1] = month;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   213
			local current_day = 1;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   214
			for _=1, (tmp.wday+5)%7 do
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   215
				days[current_day], current_day = {}, current_day+1;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   216
			end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   217
			for i = 1, 31 do
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   218
				t.day = i;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   219
				tmp = os_date("!*t", time(t));
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   220
				if tmp.month ~= current_month then break end
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   221
				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: 1609
diff changeset
   222
					days = {};
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   223
					weeks[#weeks+1] = { days = days };
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   224
					current_day = 1;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   225
				end
2689
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   226
				days[current_day] = {
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   227
					wday = tmp.wday, day = i, href = days_t[i] and datetime.date(days_t[i])
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   228
				};
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   229
				current_day = current_day+1;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   230
			end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   231
		end
3753
cb61f0e06de3 mod_http_muc_log: Fix sorting months
Kim Alvefur <zash@zash.se>
parents: 3726
diff changeset
   232
		table.sort(months, sort_m);
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   233
	end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   234
	table.sort(years, sort_Y);
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   235
	return years;
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   236
end
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   237
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   238
-- Produce the calendar view
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   239
local function years_page(event, path)
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   240
	local request, response = event.request, event.response;
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   241
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   242
	local room = nodeprep(path:match("^(.*)/$"));
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   243
	local is_open = open_room(room);
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   244
	if is_open == nil then
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   245
		return -- implicit 404
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   246
	elseif is_open == false then
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   247
		return 403;
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   248
	end
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   249
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   250
	local date_list = get_dates(room);
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   251
	if not date_list then
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   252
		return; -- 404
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   253
	end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   254
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   255
	-- Phew, all wrangled, all that's left is rendering it with the template
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
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";
3896
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   258
	local room_obj = get_room(room);
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   259
	return render(template, {
3896
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   260
		room = room_obj._data;
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   261
		jid = room_obj.jid;
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   262
		jid_node = jid_split(room_obj.jid);
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   263
		hide_presence = hide_presence(request);
3601
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   264
		presence_available = presence_logged;
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   265
		dates = date_list;
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   266
		links = {
2844
683a2f25223d mod_http_muc_log: Reword "back" links
Kim Alvefur <zash@zash.se>
parents: 2843
diff changeset
   267
			{ href = "../", rel = "up", text = "Room list" },
3722
cc6f7e2e4a59 mod_http_muc_log: Add arrow to 'latest' link like on other navigation
Kim Alvefur <zash@zash.se>
parents: 3719
diff changeset
   268
			{ href = "latest", rel = "last", text = "Latest" },
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   269
		};
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   270
	});
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   271
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   272
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   273
-- Produce the chat log view
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   274
local function logs_page(event, path)
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   275
	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
   276
3595
3f8383c5a045 mod_http_muc_log: Fix Y10k bug
Kim Alvefur <zash@zash.se>
parents: 3587
diff changeset
   277
	local room, date = path:match("^([^/]+)/([^/]*)/?$");
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   278
	room = nodeprep(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   279
	if not room then
3595
3f8383c5a045 mod_http_muc_log: Fix Y10k bug
Kim Alvefur <zash@zash.se>
parents: 3587
diff changeset
   280
		return 400;
3f8383c5a045 mod_http_muc_log: Fix Y10k bug
Kim Alvefur <zash@zash.se>
parents: 3587
diff changeset
   281
	elseif date == "" then
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   282
		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
   283
	end
3063
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
   284
	local is_open = open_room(room);
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
   285
	if is_open == nil then
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
   286
		return -- implicit 404
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
   287
	elseif is_open == false then
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
   288
		return 403;
14d8f2a9d5f4 mod_http_muc_log: Base read-access on whether anyone would be able to join (like MUC)
Kim Alvefur <zash@zash.se>
parents: 3062
diff changeset
   289
	end
3596
61a9c087730a mod_http_muc_log: Provide a redirect to 'latest' day with logs (fixes #1360)
Kim Alvefur <zash@zash.se>
parents: 3595
diff changeset
   290
	if date == "latest" then
61a9c087730a mod_http_muc_log: Provide a redirect to 'latest' day with logs (fixes #1360)
Kim Alvefur <zash@zash.se>
parents: 3595
diff changeset
   291
		local last_day = find_once(room, { reverse = true }, 3);
3599
00a848ede42d mod_http_muc_log: Preserve ?query part in redirect
Kim Alvefur <zash@zash.se>
parents: 3598
diff changeset
   292
		response.headers.location = url.build({ path = datetime.date(last_day), query = request.url.query });
3596
61a9c087730a mod_http_muc_log: Provide a redirect to 'latest' day with logs (fixes #1360)
Kim Alvefur <zash@zash.se>
parents: 3595
diff changeset
   293
		return 303;
61a9c087730a mod_http_muc_log: Provide a redirect to 'latest' day with logs (fixes #1360)
Kim Alvefur <zash@zash.se>
parents: 3595
diff changeset
   294
	end
2845
462dece0a3c2 mod_http_muc_log: Parse date out of path once
Kim Alvefur <zash@zash.se>
parents: 2844
diff changeset
   295
	local day_start = datetime.parse(date.."T00:00:00Z");
3595
3f8383c5a045 mod_http_muc_log: Fix Y10k bug
Kim Alvefur <zash@zash.se>
parents: 3587
diff changeset
   296
	if not day_start then
3f8383c5a045 mod_http_muc_log: Fix Y10k bug
Kim Alvefur <zash@zash.se>
parents: 3587
diff changeset
   297
		module:log("debug", "Invalid date format: %q", date);
3f8383c5a045 mod_http_muc_log: Fix Y10k bug
Kim Alvefur <zash@zash.se>
parents: 3587
diff changeset
   298
		return 400;
3f8383c5a045 mod_http_muc_log: Fix Y10k bug
Kim Alvefur <zash@zash.se>
parents: 3587
diff changeset
   299
	end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   300
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   301
	local logs, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   302
	local iter, err = archive:find(room, {
2845
462dece0a3c2 mod_http_muc_log: Parse date out of path once
Kim Alvefur <zash@zash.se>
parents: 2844
diff changeset
   303
		["start"] = day_start;
462dece0a3c2 mod_http_muc_log: Parse date out of path once
Kim Alvefur <zash@zash.se>
parents: 2844
diff changeset
   304
		["end"]   = day_start + 86399;
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   305
		["with"]  = hide_presence(request) and "message<groupchat" or nil;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   306
	});
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   307
	if not iter then
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   308
		module:log("warn", "Could not search archive: %s", err or "no error");
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   309
		return 500;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   310
	end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   311
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
   312
	local first, last;
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   313
	for key, item, when in iter do
3707
7244ff1d62a8 mod_http_muc_log: Expose xml:lang from each message/body #a11y
Kim Alvefur <zash@zash.se>
parents: 3706
diff changeset
   314
		local body_tag = item:get_child("body");
7244ff1d62a8 mod_http_muc_log: Expose xml:lang from each message/body #a11y
Kim Alvefur <zash@zash.se>
parents: 3706
diff changeset
   315
		local body = body_tag and body_tag:get_text();
2595
3e1a85c5194c mod_http_muc_log: Move scope of variables into loop
Kim Alvefur <zash@zash.se>
parents: 2594
diff changeset
   316
		local subject = item:get_child_text("subject");
3e1a85c5194c mod_http_muc_log: Move scope of variables into loop
Kim Alvefur <zash@zash.se>
parents: 2594
diff changeset
   317
		local verb = nil;
3707
7244ff1d62a8 mod_http_muc_log: Expose xml:lang from each message/body #a11y
Kim Alvefur <zash@zash.se>
parents: 3706
diff changeset
   318
		local lang = body_tag and body_tag.attr["xml:lang"] or item.attr["xml:lang"];
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   319
		if subject then
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   320
			verb, body = "set the topic to", subject;
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   321
		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
   322
			verb, body = body:sub(5), nil;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   323
		elseif item.name == "presence" then
3064
982668000163 mod_http_muc_log: Add a note about changing how presence is treated
Kim Alvefur <zash@zash.se>
parents: 3063
diff changeset
   324
			-- TODO Distinguish between join and presence update
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   325
			verb = item.attr.type == "unavailable" and "has left" or "has joined";
3707
7244ff1d62a8 mod_http_muc_log: Expose xml:lang from each message/body #a11y
Kim Alvefur <zash@zash.se>
parents: 3706
diff changeset
   326
			lang = "en";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   327
		end
3586
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   328
		local oob = use_oob and item:get_child("x", "jabber:x:oob");
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   329
		if body or verb or oob then
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   330
			local line = {
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   331
				key = key;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   332
				datetime = datetime.datetime(when);
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   333
				time = datetime.time(when);
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   334
				verb = verb;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   335
				body = body;
3707
7244ff1d62a8 mod_http_muc_log: Expose xml:lang from each message/body #a11y
Kim Alvefur <zash@zash.se>
parents: 3706
diff changeset
   336
				lang = lang;
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   337
				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
   338
				st_name = item.name;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   339
				st_type = item.attr.type;
3586
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   340
			};
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   341
			if oob then
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   342
				line.oob = {
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   343
					url = oob:get_child_text("url");
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   344
					desc = oob:get_child_text("desc");
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   345
				}
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   346
			end
444e2306c99a mod_http_muc_log: Add option to show OOB images
Kim Alvefur <zash@zash.se>
parents: 3582
diff changeset
   347
			logs[i], i = line, i + 1;
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   348
		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
   349
		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
   350
		last = key;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   351
	end
2850
7eb23a4e7fde mod_http_muc_log: Generate empty pages in lazy mode, so that one can navigate past quiet days
Kim Alvefur <zash@zash.se>
parents: 2849
diff changeset
   352
	if i == 1 and not lazy 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
   353
2240
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   354
	local next_when, prev_when = "", "";
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   355
	local date_list = archive.dates and archive:dates(room);
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   356
	if date_list then
2596
fb1987d4ac62 mod_http_muc_log: Rename loop variable to avoid name clash
Kim Alvefur <zash@zash.se>
parents: 2595
diff changeset
   357
		for j = 1, #date_list do
fb1987d4ac62 mod_http_muc_log: Rename loop variable to avoid name clash
Kim Alvefur <zash@zash.se>
parents: 2595
diff changeset
   358
			if date_list[j] == date then
fb1987d4ac62 mod_http_muc_log: Rename loop variable to avoid name clash
Kim Alvefur <zash@zash.se>
parents: 2595
diff changeset
   359
				next_when = date_list[j+1] or "";
fb1987d4ac62 mod_http_muc_log: Rename loop variable to avoid name clash
Kim Alvefur <zash@zash.se>
parents: 2595
diff changeset
   360
				prev_when = date_list[j-1] or "";
2240
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   361
				break;
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   362
			end
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   363
		end
2848
9fac07bba402 mod_http_muc_log: Lazy nex/prev link generation
Kim Alvefur <zash@zash.se>
parents: 2847
diff changeset
   364
	elseif lazy then
9fac07bba402 mod_http_muc_log: Lazy nex/prev link generation
Kim Alvefur <zash@zash.se>
parents: 2847
diff changeset
   365
		next_when = datetime.date(day_start + 86400);
9fac07bba402 mod_http_muc_log: Lazy nex/prev link generation
Kim Alvefur <zash@zash.se>
parents: 2847
diff changeset
   366
		prev_when = datetime.date(day_start - 86400);
2849
0de6ed2ae9bd mod_http_muc_log: Check that there are timestamps to work with
Kim Alvefur <zash@zash.se>
parents: 2848
diff changeset
   367
	elseif first and last then
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   368
2240
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   369
		module:log("debug", "Find next date with messages");
2597
b61b0ff1c0f9 mod_http_muc_log: Fix prev/next date finding
Kim Alvefur <zash@zash.se>
parents: 2596
diff changeset
   370
		next_when = find_once(room, { after = last }, 3);
2240
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   371
		if next_when then
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   372
			next_when = datetime.date(next_when);
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   373
			module:log("debug", "Next message: %s", next_when);
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   374
		end
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   375
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   376
		module:log("debug", "Find prev date with messages");
2597
b61b0ff1c0f9 mod_http_muc_log: Fix prev/next date finding
Kim Alvefur <zash@zash.se>
parents: 2596
diff changeset
   377
		prev_when = find_once(room, { before = first, reverse = true }, 3);
2240
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   378
		if prev_when then
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   379
			prev_when = datetime.date(prev_when);
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   380
			module:log("debug", "Previous message: %s", prev_when);
86bc6e1d9d4d mod_http_muc_log: Find next and previous date with date index if this is supported by the storage driver
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
   381
		end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   382
	end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   383
3694
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   384
	local links = {
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   385
		{ href = "./", rel = "up", text = "Calendar" },
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   386
	};
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   387
	if prev_when ~= "" then
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   388
		table.insert(links, { href = prev_when, rel = "prev", text = prev_when});
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   389
	end
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   390
	if next_when ~= "" then
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   391
		table.insert(links, { href = next_when, rel = "next", text = next_when});
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   392
	end
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   393
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
   394
	response.headers.content_type = "text/html; charset=utf-8";
3896
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   395
	local room_obj = get_room(room);
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   396
	return render(template, {
3718
04ff0de40ba9 mod_http_muc_log: Add date metadata to log pages
Kim Alvefur <zash@zash.se>
parents: 3707
diff changeset
   397
		date = date;
3896
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   398
		room = room_obj._data;
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   399
		jid = room_obj.jid;
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   400
		jid_node = jid_split(room_obj.jid);
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   401
		hide_presence = hide_presence(request);
3601
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   402
		presence_available = presence_logged;
3896
96a2e5097fc4 mod_http_muc_log: Reduce hashtable lookups
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3761
diff changeset
   403
		lang = room_obj.get_language and room_obj:get_language();
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   404
		lines = logs;
3694
8c0a6d4541d5 mod_http_muc_log: Wrap navigation in unorderded lists (thanks jonas’)
Kim Alvefur <zash@zash.se>
parents: 3601
diff changeset
   405
		links = links;
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   406
		dates = {}; -- COMPAT util.interpolation {nil|func#...} bug
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   407
	});
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   408
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   409
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   410
local function list_rooms(event)
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   411
	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
   412
	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
   413
	for room in each_room() do
3066
37a78e365b46 mod_http_muc_log: Fix inverted logic
Kim Alvefur <zash@zash.se>
parents: 3065
diff changeset
   414
		if not (room.get_hidden or room.is_hidden)(room) then
3897
3f20b7c88afb mod_http_muc_log: Expose JID localpart to the template
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3896
diff changeset
   415
			local localpart = jid_split(room.jid);
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   416
			room_list[i], i = {
3582
ea63dc0cc824 mod_http_muc_log: Sort room listing by jid for stable order
Kim Alvefur <zash@zash.se>
parents: 3564
diff changeset
   417
				jid = room.jid;
3897
3f20b7c88afb mod_http_muc_log: Expose JID localpart to the template
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3896
diff changeset
   418
				localpart = localpart;
3f20b7c88afb mod_http_muc_log: Expose JID localpart to the template
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3896
diff changeset
   419
				href = get_link(localpart, default_view);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   420
				name = room:get_name();
3706
caf27826c7b2 mod_http_muc_log: Use configured room language in room title/desc #a11y
Kim Alvefur <zash@zash.se>
parents: 3696
diff changeset
   421
				lang = room.get_language and room:get_language();
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   422
				description = room:get_description();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   423
			}, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   424
		end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   425
	end
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
   426
3582
ea63dc0cc824 mod_http_muc_log: Sort room listing by jid for stable order
Kim Alvefur <zash@zash.se>
parents: 3564
diff changeset
   427
	table.sort(room_list, function (a, b)
3587
a36412d4fafd mod_http_muc_log: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3586
diff changeset
   428
		return a.jid < b.jid;
3582
ea63dc0cc824 mod_http_muc_log: Sort room listing by jid for stable order
Kim Alvefur <zash@zash.se>
parents: 3564
diff changeset
   429
	end);
ea63dc0cc824 mod_http_muc_log: Sort room listing by jid for stable order
Kim Alvefur <zash@zash.se>
parents: 3564
diff changeset
   430
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   431
	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
   432
	return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   433
		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
   434
		jid = module.host;
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   435
		hide_presence = hide_presence(request);
3601
da7ec4ed6ddf mod_http_muc_log: Hide join/part controls if they are not stored by mod_muc_mam
Kim Alvefur <zash@zash.se>
parents: 3600
diff changeset
   436
		presence_available = presence_logged;
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   437
		rooms = room_list;
3754
9002c8a2165f mod_http_muc_log: Refactor calendarization of date list into a template filter BC
Kim Alvefur <zash@zash.se>
parents: 3753
diff changeset
   438
		dates = {}; -- COMPAT util.interpolation {nil|func#...} bug
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   439
	});
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   440
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   441
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   442
module:provides("http", {
3761
971417eedfee mod_http_muc_log: Set a http app title
Kim Alvefur <zash@zash.se>
parents: 3754
diff changeset
   443
	title = module:get_option_string("name", "Chatroom logs");
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   444
	route = {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   445
		["GET /"] = list_rooms;
2689
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   446
		["GET /*"] = logs_page;
3726
bdbbf11eac0c mod_http_muc_log: Add comment about the routing of years and logs pages
Kim Alvefur <zash@zash.se>
parents: 3722
diff changeset
   447
		-- mod_http only supports one wildcard so logs_page will dispatch to years_page if the path contains no date
bdbbf11eac0c mod_http_muc_log: Add comment about the routing of years and logs pages
Kim Alvefur <zash@zash.se>
parents: 3722
diff changeset
   448
		-- thus:
bdbbf11eac0c mod_http_muc_log: Add comment about the routing of years and logs pages
Kim Alvefur <zash@zash.se>
parents: 3722
diff changeset
   449
		-- GET /room --> years_page (via logs_page)
bdbbf11eac0c mod_http_muc_log: Add comment about the routing of years and logs pages
Kim Alvefur <zash@zash.se>
parents: 3722
diff changeset
   450
		-- GET /room/yyyy-mm-dd --> logs_page (for real)
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   451
	};
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   452
});
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   453