comparison mod_muc_badge/mod_muc_badge.lua @ 3141:774845606d73

mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
author Kim Alvefur <zash@zash.se>
date Sat, 23 Jun 2018 18:08:34 +0200
parents
children 8d2fe3b93c15
comparison
equal deleted inserted replaced
3140:11087a72990b 3141:774845606d73
1 -- MIT License
2 --
3 -- Copyright (c) 2018 Kim Alvefur
4 --
5 -- Permission is hereby granted, free of charge, to any person obtaining a copy
6 -- of this software and associated documentation files (the "Software"), to deal
7 -- in the Software without restriction, including without limitation the rights
8 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 -- copies of the Software, and to permit persons to whom the Software is
10 -- furnished to do so, subject to the following conditions:
11 --
12 -- The above copyright notice and this permission notice shall be included in
13 -- all copies or substantial portions of the Software.
14 --
15 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 module:depends"http";
24
25 local jid_prep = require "util.jid".prep;
26
27 -- Support both old and new MUC code
28 local mod_muc = module:depends"muc";
29 local rooms = rawget(mod_muc, "rooms");
30 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
31 function (jid)
32 return rooms[jid];
33 end
34
35 -- I believe the origins of this template to be in the public domain as per
36 -- https://github.com/badges/shields/blob/master/LICENSE.md
37 local template = module:get_option_string("badge_template", [[
38 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="144" height="20">
39 <linearGradient id="b" x2="0" y2="100%">
40 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
41 <stop offset="1" stop-opacity=".1"/>
42 </linearGradient>
43 <clipPath id="a">
44 <rect width="144" height="20" rx="3" fill="#fff"/>
45 </clipPath>
46 <g clip-path="url(#a)">
47 <path fill="#555" d="M0 0h69v20H0z"/>
48 <path fill="#fe7d37" d="M69 0h75v20H69z"/>
49 <path fill="url(#b)" d="M0 0h144v20H0z"/>
50 </g>
51 <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110">
52 <text x="355" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="590">{label}</text>
53 <text x="355" y="140" transform="scale(.1)" textLength="590">{label}</text>
54 <text x="1055" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="650">{number}</text>
55 <text x="1055" y="140" transform="scale(.1)" textLength="650">{number}</text>
56 </g>
57 </svg>
58 ]]);
59 template = assert(require "util.template"(template));
60
61 local label = module:get_option_string("badge_label", "Chatroom");
62 local number = module:get_option_string("badge_count", "%d online");
63
64 module:provides("http", {
65 route = {
66 ["GET /*"] = function (event, path)
67 local jid = jid_prep(path);
68 if not jid then return end
69
70 local room = get_room_from_jid(jid);
71 if not room then return end
72 if (room.get_hidden or room.is_hidden)(room) then return end
73
74 local count = 0;
75 for _ in pairs(room._occupants) do
76 count = count + 1;
77 end
78
79 local response = event.response;
80 response.headers.content_type = "image/svg+xml";
81 local svg = [[<?xml version="1.0"?>]] ..
82 tostring(template.apply({
83 label = label;
84 number = string.format(number, count);
85 }));
86 return svg;
87 end;
88 }
89 });