comparison mod_muc_badge/mod_muc_badge.lua @ 3173:cd23446eaff1

mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
author Kim Alvefur <zash@zash.se>
date Wed, 04 Jul 2018 23:04:22 +0200
parents 8d2fe3b93c15
children 29a4d8b63fb1
comparison
equal deleted inserted replaced
3172:8d2fe3b93c15 3173:cd23446eaff1
34 34
35 -- I believe the origins of this template to be in the public domain as per 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 36 -- https://github.com/badges/shields/blob/master/LICENSE.md
37 local template = module:get_option_string("badge_template", [[ 37 local template = module:get_option_string("badge_template", [[
38 <?xml version="1.0"?> 38 <?xml version="1.0"?>
39 <svg xmlns="http://www.w3.org/2000/svg" width="144" height="20"> 39 <svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="20">
40 <linearGradient id="b" x2="0" y2="100%"> 40 <linearGradient id="b" x2="0" y2="100%">
41 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> 41 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
42 <stop offset="1" stop-opacity=".1"/> 42 <stop offset="1" stop-opacity=".1"/>
43 </linearGradient> 43 </linearGradient>
44 <clipPath id="a"> 44 <clipPath id="a">
45 <rect width="144" height="20" rx="3" fill="#fff"/> 45 <rect width="{width}" height="20" rx="3" fill="#fff"/>
46 </clipPath> 46 </clipPath>
47 <g clip-path="url(#a)"> 47 <g clip-path="url(#a)">
48 <path fill="#555" d="M0 0h69v20H0z"/> 48 <path fill="#555" d="M0 0h{labelwidth}v20H0z"/>
49 <path fill="#fe7d37" d="M69 0h75v20H69z"/> 49 <path fill="#fe7d37" d="M{labelwidth} 0h{countwidth}v20H{labelwidth}z"/>
50 <path fill="url(#b)" d="M0 0h144v20H0z"/> 50 <path fill="url(#b)" d="M0 0h{width}v20H0z"/>
51 </g> 51 </g>
52 <g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> 52 <g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
53 <text fill="#010101" y="14" x="0" textLength="59">{label}</text> 53 <text fill="#010101" y="14" x="0" textLength="{labelwidth}">{label}</text>
54 <text y="13" x="0" textLength="59">{label}</text> 54 <text y="13" x="0" textLength="{labelwidth}">{label}</text>
55 <text fill="#010101" y="14" x="59" textLength="65">{number}</text> 55 <text fill="#010101" y="14" x="{labelwidth}" textLength="{countwidth}">{number}</text>
56 <text y="13" x="59" textLength="65">{number}</text> 56 <text y="13" x="{labelwidth}" textLength="{countwidth}">{number}</text>
57 </g> 57 </g>
58 </svg> 58 </svg>
59 ]]); 59 ]]);
60 template = assert(require "util.template"(template)); 60 template = assert(require "util.template"(template));
61 61
62 local label = module:get_option_string("badge_label", "Chatroom");
63 local number = module:get_option_string("badge_count", "%d online"); 62 local number = module:get_option_string("badge_count", "%d online");
63 local charwidth = 7;
64 64
65 module:provides("http", { 65 module:provides("http", {
66 route = { 66 route = {
67 ["GET /*"] = function (event, path) 67 ["GET /*"] = function (event, path)
68 local jid = jid_prep(path); 68 local jid = jid_prep(path);
74 74
75 local count = 0; 75 local count = 0;
76 for _ in pairs(room._occupants) do 76 for _ in pairs(room._occupants) do
77 count = count + 1; 77 count = count + 1;
78 end 78 end
79 local badge_label = room:get_name();
80 local badge_count = string.format(number, count);
79 81
80 local response = event.response; 82 local response = event.response;
81 response.headers.content_type = "image/svg+xml"; 83 response.headers.content_type = "image/svg+xml";
82 local svg = [[<?xml version="1.0"?>]] .. 84 local svg = [[<?xml version="1.0"?>]] ..
83 tostring(template.apply({ 85 tostring(template.apply({
84 label = label; 86 label = badge_label;
85 number = string.format(number, count); 87 number = badge_count;
88 width = ("%d"):format( (#badge_label + #badge_count) * charwidth );
89 labelwidth = ("%d"):format( #badge_label * charwidth );
90 countwidth = ("%d"):format( #badge_count * charwidth );
86 })); 91 }));
87 return svg; 92 return svg;
88 end; 93 end;
89 } 94 }
90 }); 95 });