comparison mod_http_muc_log/mod_http_muc_log.lua @ 5104:d4b0a995e5e3

mod_http_muc_log: Move CSS and JS out of template This allows applying different cache and security policies to the static resources, while reducing the size of all pages.
author Kim Alvefur <zash@zash.se>
date Sun, 04 Dec 2022 22:10:07 +0100
parents f36d15107c15
children 7bce75e74f86
comparison
equal deleted inserted replaced
5103:4fb922aa0ace 5104:d4b0a995e5e3
37 <p>Tried to open <b>{filename}</b></p>\ 37 <p>Tried to open <b>{filename}</b></p>\
38 <pre>{error}</pre>", 38 <pre>{error}</pre>",
39 { module = module.name, filename = template_filename, error = err }); 39 { module = module.name, filename = template_filename, error = err });
40 end 40 end
41 end 41 end
42
43 local resources = module:get_option_path(module.name .. "_resources", "static");
42 44
43 -- local base_url = module:http_url() .. '/'; -- TODO: Generate links in a smart way 45 -- local base_url = module:http_url() .. '/'; -- TODO: Generate links in a smart way
44 local get_link do 46 local get_link do
45 local link, path = { path = '/' }, { "", "", is_directory = true }; 47 local link, path = { path = '/' }, { "", "", is_directory = true };
46 function get_link(room, date) 48 function get_link(room, date)
246 -- Phew, all wrangled, all that's left is rendering it with the template 248 -- Phew, all wrangled, all that's left is rendering it with the template
247 249
248 response.headers.content_type = "text/html; charset=utf-8"; 250 response.headers.content_type = "text/html; charset=utf-8";
249 local room_obj = get_room(room); 251 local room_obj = get_room(room);
250 return render(template, { 252 return render(template, {
253 static = "../@static";
251 room = room_obj._data; 254 room = room_obj._data;
252 jid = room_obj.jid; 255 jid = room_obj.jid;
253 jid_node = jid_split(room_obj.jid); 256 jid_node = jid_split(room_obj.jid);
254 hide_presence = hide_presence(request); 257 hide_presence = hide_presence(request);
255 presence_available = presence_logged; 258 presence_available = presence_logged;
465 end 468 end
466 469
467 response.headers.content_type = "text/html; charset=utf-8"; 470 response.headers.content_type = "text/html; charset=utf-8";
468 local room_obj = get_room(room); 471 local room_obj = get_room(room);
469 return render(template, { 472 return render(template, {
473 static = "../@static";
470 date = date; 474 date = date;
471 room = room_obj._data; 475 room = room_obj._data;
472 jid = room_obj.jid; 476 jid = room_obj.jid;
473 jid_node = jid_split(room_obj.jid); 477 jid_node = jid_split(room_obj.jid);
474 hide_presence = hide_presence(request); 478 hide_presence = hide_presence(request);
515 return a.jid < b.jid; 519 return a.jid < b.jid;
516 end); 520 end);
517 521
518 response.headers.content_type = "text/html; charset=utf-8"; 522 response.headers.content_type = "text/html; charset=utf-8";
519 return render(template, { 523 return render(template, {
524 static = "./@static";
520 title = module:get_option_string("name", "Prosody Chatrooms"); 525 title = module:get_option_string("name", "Prosody Chatrooms");
521 jid = module.host; 526 jid = module.host;
522 hide_presence = hide_presence(request); 527 hide_presence = hide_presence(request);
523 presence_available = presence_logged; 528 presence_available = presence_logged;
524 rooms = room_list; 529 rooms = room_list;
525 dates = {}; -- COMPAT util.interpolation {nil|func#...} bug 530 dates = {}; -- COMPAT util.interpolation {nil|func#...} bug
526 }); 531 });
532 end
533
534 local serve_static
535 do
536 if prosody.process_type == "prosody" then
537 -- Prosody >= 0.12
538 local http_files = require "net.http.files";
539 serve = http_files.serve;
540 else
541 -- Prosody <= 0.11
542 serve = module:depends "http_files".serve;
543 end
544 local mime_map = module:shared("/*/http_files/mime").types or { css = "text/css"; js = "application/javascript" };
545 serve_static = serve({ path = resources; mime_map = mime_map });
527 end 546 end
528 547
529 module:provides("http", { 548 module:provides("http", {
530 title = module:get_option_string("name", "Chatroom logs"); 549 title = module:get_option_string("name", "Chatroom logs");
531 route = { 550 route = {
533 ["GET /*"] = logs_page; 552 ["GET /*"] = logs_page;
534 -- mod_http only supports one wildcard so logs_page will dispatch to years_page if the path contains no date 553 -- mod_http only supports one wildcard so logs_page will dispatch to years_page if the path contains no date
535 -- thus: 554 -- thus:
536 -- GET /room --> years_page (via logs_page) 555 -- GET /room --> years_page (via logs_page)
537 -- GET /room/yyyy-mm-dd --> logs_page (for real) 556 -- GET /room/yyyy-mm-dd --> logs_page (for real)
557
558 ["GET /@static/*"] = serve_static;
559 -- There are not many ASCII characters that are safe to use in URLs but not
560 -- valid in JID localparts, '@' seemed the only option.
538 }; 561 };
539 }); 562 });
540 563