mod_http_muc_log/mod_http_muc_log.lua
author Kim Alvefur <zash@zash.se>
Sun, 05 May 2019 11:01:17 +0200
changeset 3582 ea63dc0cc824
parent 3564 0ebb7112c102
child 3586 444e2306c99a
permissions -rw-r--r--
mod_http_muc_log: Sort room listing by jid for stable order It was silly that the list would be shuffled every time you reloaded the page.
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";
1754
3b839db88412 mod_http_muc_log: Template engine thing moved into util.interpolation
Kim Alvefur <zash@zash.se>
parents: 1675
diff changeset
     9
local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
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
    11
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
    12
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
-- 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
    14
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
    15
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
    16
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
    17
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
    18
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
    19
	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
    20
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
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
    22
	function (jid)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
		return rooms[jid];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
	end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
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
    27
	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
    28
	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
    29
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
module:depends"http";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
    33
local template;
1582
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    34
do
3067
a9fa98e28697 mod_http_muc_log: Use separate variable for filename and file handle
Kim Alvefur <zash@zash.se>
parents: 3066
diff changeset
    35
	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
    36
	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
    37
	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
    38
		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
    39
		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
    40
	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
    41
	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
    42
		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
    43
		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
    44
		<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
    45
		<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
    46
			{ 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
    47
	end
1582
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    48
end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
    50
-- 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
    51
local get_link do
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    52
	local link, path = { path = '/' }, { "", "", is_directory = true };
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    53
	function get_link(room, date)
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    54
		path[1], path[2] = room, date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    55
		path.is_directory = not date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    56
		link.path = url.build_path(path);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    57
		return url.build(link);
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
end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    60
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
    61
-- 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
    62
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
    63
	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
    64
		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
    65
		-- 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
    66
	end
3061
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    67
	if not room then
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    68
		return nil;
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    69
	end
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    70
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
    71
	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
    72
		return false;
3061
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    73
	end
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    74
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
    75
	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
    76
		return false;
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    77
	end
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    78
f69a2e97d912 mod_http_muc_log: Improve readability of policy function
Kim Alvefur <zash@zash.se>
parents: 2850
diff changeset
    79
	return true;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    80
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
3068
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
    82
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
    83
	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
    84
	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
    85
		table.insert(event.form, { name = "muc#roominfo_logs", type="text-single" });
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
    86
		event.formdata["muc#roominfo_logs"] = module:http_url() .. "/" .. get_link(jid_split(event.room.jid), nil);
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
    87
	end
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
    88
end);
ce61f1826f1f mod_http_muc_log: Include URL in disco#info for public rooms
Kim Alvefur <zash@zash.se>
parents: 3067
diff changeset
    89
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
    90
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
    91
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
    92
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
    93
-- 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
    94
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
    95
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
    96
	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
    97
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
    98
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
    99
	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
   100
end
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   101
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   102
-- Fetch one item
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   103
local function find_once(room, query, retval)
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   104
	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
   105
	local iter, err = archive:find(room, query);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   106
	if not iter then return iter, err; end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   107
	if retval then
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   108
		return select(retval, iter());
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   109
	end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   110
	return iter();
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   111
end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   112
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
   113
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
   114
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   115
local function hide_presence(request)
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   116
	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
   117
		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
   118
		if data then
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   119
			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
   120
		end
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   121
	end
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   122
	return false;
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   123
end
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   124
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   125
-- Produce the calendar view
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   126
local function years_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
   127
	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
   128
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
   129
	local room = nodeprep(path:match("^(.*)/$"));
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
   130
	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
   131
	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
   132
		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
   133
	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
   134
		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
   135
	end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   136
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   137
	-- Collect each date that has messages
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   138
	-- convert it to a year / month / day tree
1836
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   139
	local date_list = archive.dates and archive:dates(room);
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   140
	local dates = mt.new();
1836
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   141
	if date_list then
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   142
		for _, date in ipairs(date_list) do
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   143
			local when = datetime.parse(date.."T00:00:00Z");
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   144
			local t = os_date("!*t", when);
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   145
			dates:set(t.year, t.month, t.day, when);
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   146
		end
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
   147
	elseif lazy then
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
   148
		-- Lazy with many false positives
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
   149
		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
   150
		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
   151
		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
   152
			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
   153
			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
   154
			for when = first_day, last_day, 86400 do
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
   155
				local t = os_date("!*t", when);
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
   156
				dates:set(t.year, t.month, t.day, when);
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
   157
			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
   158
		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
   159
			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
   160
		end
1836
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   161
	else
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   162
		-- Collect date the hard way
1836
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   163
		module:log("debug", "Find all dates with messages");
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   164
		local next_day;
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   165
		repeat
2239
c5ee48e27d01 mod_http_muc_log: Link to next day with content, regardless of type (simplifes)
Kim Alvefur <zash@zash.se>
parents: 1836
diff changeset
   166
			local when = find_once(room, { start = next_day; }, 3);
1836
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   167
			if not when then break; end
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   168
			local t = os_date("!*t", when);
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   169
			dates:set(t.year, t.month, t.day, when );
2840
52a7c0f6aea1 mod_http_muc_log: Add a function for rounding timestamps down to midnight
Kim Alvefur <zash@zash.se>
parents: 2830
diff changeset
   170
			next_day = date_floor(when) + 86400;
1836
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   171
		until not next_day;
48125f2c358b mod_http_muc_log: If archive driver provides the dates method, use it
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
   172
	end
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   173
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   174
	local years = {};
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   175
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   176
	-- Wrangle Y/m/d tree into year / month / week / day tree for calendar view
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   177
	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
   178
		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
   179
		local months = { };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   180
		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
   181
		years[#years+1] = year;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   182
		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
   183
			t.day = 1;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   184
			t.month = current_month;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   185
			local tmp = os_date("!*t", time(t));
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   186
			local days = {};
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   187
			local week = { days = days }
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   188
			local weeks = { week };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   189
			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
   190
			months[#months+1] = month;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   191
			local current_day = 1;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   192
			for _=1, (tmp.wday+5)%7 do
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   193
				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
   194
			end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   195
			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
   196
				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
   197
				tmp = os_date("!*t", time(t));
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   198
				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
   199
				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
   200
					days = {};
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   201
					weeks[#weeks+1] = { days = days };
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   202
					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
   203
				end
2689
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   204
				days[current_day] = {
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   205
					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
   206
				};
cd5781ca782d mod_http_muc_log: Remove caching
Kim Alvefur <zash@zash.se>
parents: 2597
diff changeset
   207
				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
   208
			end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   209
		end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   210
		table.sort(year, sort_m);
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   211
	end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   212
	table.sort(years, sort_Y);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   213
2594
63dd3e525f13 mod_http_muc_log: Add some comments
Kim Alvefur <zash@zash.se>
parents: 2240
diff changeset
   214
	-- 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
   215
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
   216
	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
   217
	return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   218
		title = get_room(room):get_name();
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   219
		jid = get_room(room).jid;
3489
181561d0aae5 mod_http_muc_log: Add functionality for hiding joins and parts
Kim Alvefur <zash@zash.se>
parents: 3287
diff changeset
   220
		hide_presence = hide_presence(request);
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   221
		years = years;
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   222
		links = {
2844
683a2f25223d mod_http_muc_log: Reword "back" links
Kim Alvefur <zash@zash.se>
parents: 2843
diff changeset
   223
			{ href = "../", rel = "up", text = "Room list" },
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   224
		};
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   225
	});
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   226
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   227
2594