Annotate

mod_muc_badge/mod_muc_badge.lua @ 3571:f5ea0b886c7c

mod_storage_xmlarchive: Limit search to smallest time range in case of inexact match This should improve performance in case the exact days in the 'start' and 'end' range are missing from the index.
author Kim Alvefur <zash@zash.se>
date Fri, 03 May 2019 18:59:38 +0200
parent 3286:4d1f6d47c889
child 4982:8a4b17e2e984
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- MIT License
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- Copyright (c) 2018 Kim Alvefur
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- Permission is hereby granted, free of charge, to any person obtaining a copy
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- of this software and associated documentation files (the "Software"), to deal
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- in the Software without restriction, including without limitation the rights
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- copies of the Software, and to permit persons to whom the Software is
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 -- furnished to do so, subject to the following conditions:
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 -- The above copyright notice and this permission notice shall be included in
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 -- all copies or substantial portions of the Software.
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 module:depends"http";
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local jid_prep = require "util.jid".prep;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 -- Support both old and new MUC code
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local mod_muc = module:depends"muc";
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local rooms = rawget(mod_muc, "rooms");
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 function (jid)
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 return rooms[jid];
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- I believe the origins of this template to be in the public domain as per
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 -- https://github.com/badges/shields/blob/master/LICENSE.md
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 local template = module:get_option_string("badge_template", [[
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
38 <?xml version="1.0"?>
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
39 <svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="20">
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
40 <linearGradient id="b" x2="0" y2="100%">
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
41 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
42 <stop offset="1" stop-opacity=".1"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
43 </linearGradient>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
44 <clipPath id="a">
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
45 <rect width="{width}" height="20" rx="3" fill="#fff"/>
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
46 </clipPath>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
47 <g clip-path="url(#a)">
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
48 <path fill="#555" d="M0 0h{labelwidth}v20H0z"/>
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
49 <path fill="#fe7d37" d="M{labelwidth} 0h{countwidth}v20H{labelwidth}z"/>
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
50 <path fill="url(#b)" d="M0 0h{width}v20H0z"/>
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
51 </g>
3284
29a4d8b63fb1 mod_muc_badge: Preserve spaces in SVG, fixes displaying nicer borders.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3173
diff changeset
52 <g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11" xml:space="preserve">
3286
4d1f6d47c889 mod_muc_badge: Reduce the opacity of the text shadows.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3285
diff changeset
53 <text fill="#010101" fill-opacity=".3" y="14" x="0" textLength="{labelwidth}">{label}</text>
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
54 <text y="13" x="0" textLength="{labelwidth}">{label}</text>
3286
4d1f6d47c889 mod_muc_badge: Reduce the opacity of the text shadows.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3285
diff changeset
55 <text fill="#010101" fill-opacity=".3" y="14" x="{labelwidth}" textLength="{countwidth}">{number}</text>
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
56 <text y="13" x="{labelwidth}" textLength="{countwidth}">{number}</text>
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
57 </g>
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 </svg>
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 ]]);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 template = assert(require "util.template"(template));
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local number = module:get_option_string("badge_count", "%d online");
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
63 local charwidth = 7;
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 module:provides("http", {
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 route = {
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 ["GET /*"] = function (event, path)
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 local jid = jid_prep(path);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 if not jid then return end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local room = get_room_from_jid(jid);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 if not room then return end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if (room.get_hidden or room.is_hidden)(room) then return end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 local count = 0;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 for _ in pairs(room._occupants) do
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 count = count + 1;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
3285
38892f9f3fe9 mod_muc_badge: Add spaces around each part of the text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3284
diff changeset
79 local badge_label = (" %s "):format(room:get_name());
38892f9f3fe9 mod_muc_badge: Add spaces around each part of the text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3284
diff changeset
80 local badge_count = (" %s "):format(string.format(number, count));
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 local response = event.response;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 response.headers.content_type = "image/svg+xml";
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 local svg = [[<?xml version="1.0"?>]] ..
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 tostring(template.apply({
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
86 label = badge_label;
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
87 number = badge_count;
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
88 width = ("%d"):format( (#badge_label + #badge_count) * charwidth );
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
89 labelwidth = ("%d"):format( #badge_label * charwidth );
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
90 countwidth = ("%d"):format( #badge_count * charwidth );
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 }));
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 return svg;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 }
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 });