Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add shield.svg api route to generate custom shields/badges #6565

Merged
merged 4 commits into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/rocketchat-api/server/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ RocketChat.settings.addGroup('General', function() {
this.add('API_Default_Count', 50, { type: 'int', public: false });
this.add('API_Allow_Infinite_Count', true, { type: 'boolean', public: false });
this.add('API_Enable_Direct_Message_History_EndPoint', false, { type: 'boolean', public: false });
this.add('API_Enable_Shields', true, { type: 'boolean', public: false });
this.add('API_Shield_Types', '*', { type: 'string', public: false, enableQuery: { _id: 'API_Enable_Shields', value: true } });
this.add('API_Enable_CORS', false, { type: 'boolean', public: false });
this.add('API_CORS_Origin', '*', { type: 'string', public: false, enableQuery: { _id: 'API_Enable_CORS', value: true } });
});
Expand Down
69 changes: 69 additions & 0 deletions packages/rocketchat-api/server/v1/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,72 @@ RocketChat.API.v1.addRoute('me', { authRequired: true }, {
]));
}
});

let onlineCache = 0;
let onlineCacheDate = 0;
const cacheInvalid = 60000; // 1 minute
RocketChat.API.v1.addRoute('shield.svg', { authRequired: false }, {
get() {
const { type, channel, name, icon } = this.queryParams;
if (!RocketChat.settings.get('API_Enable_Shields')) {
throw new Meteor.Error('error-endpoint-disabled', 'This endpoint is disabled', { route: '/api/v1/shields.svg' });
}
const types = RocketChat.settings.get('API_Shield_Types');
if (type && (types !== '*' && !types.split(',').map((t) => t.trim()).includes(type))) {
throw new Meteor.Error('error-shield-disabled', 'This shield type is disabled', { route: '/api/v1/shields.svg' });
}
const hideIcon = icon === 'false';
if (hideIcon && (!name || !name.trim())) {
return RocketChat.API.v1.failure('Name cannot be empty when icon is hidden');
}
let text;
switch (type) {
case 'online':
if (Date.now() - onlineCacheDate > cacheInvalid) {
onlineCache = RocketChat.models.Users.findUsersNotOffline().count();
onlineCacheDate = Date.now();
}
text = `${ onlineCache } ${ TAPi18n.__('Online') }`;
break;
case 'channel':
if (!channel) {
return RocketChat.API.v1.failure('Shield channel is required for type "channel"');
}
text = `#${ channel }`;
break;
default:
text = TAPi18n.__('Join_Chat').toUpperCase();
}
const iconSize = hideIcon ? 7 : 24;
const leftSize = name ? name.length * 6 + 7 + iconSize : iconSize;
const rightSize = text.length * 6 + 20;
const width = leftSize + rightSize;
const height = 20;
return {
headers: { 'Content-Type': 'image/svg+xml;charset=utf-8' },
body: `
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${ width }" height="${ height }">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="${ width }" height="${ height }" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h${ leftSize }v${ height }H0z"/>
<path fill="#4c1" d="M${ leftSize } 0h${ rightSize }v${ height }H${ leftSize }z"/>
<path fill="url(#b)" d="M0 0h${ width }v${ height }H0z"/>
</g>
${ hideIcon ? '' : '<image x="5" y="3" width="14" height="14" xlink:href="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfN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S03LjM2MmMtMjAuNjU5LDIxLjU0LTYzLjIwOSw1MC40OTYtMTA3LjMwNyw0OS40M2MtNS44MDYsOC44MDUtMTIuMTIxLDE2LjAwNi0xNy41NjIsMjEuODVjNTAsMTIuNjUxLDEwMS40NzMtMjYuNDgxLDEyNC44NjktNTQuNjI5YzIzLjAyMyw1LjQ4Niw0Ny45NDEsOC40OTUsNzMuOTU5LDguNDk1YzExNS45MTcsMCwyMTAuMDQ4LTU5LjU1LDIxMS41NTEtMTMzLjM2NEM0NjYuMDQ4LDMyMS43NjUsMzcxLjkxNywzNzMuMzczLDI1NiwzNzMuMzczeiIvPjwvZz48L3N2Zz4="/>' }
<g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
${ name ? `<text x="${ iconSize }" y="15" fill="#010101" fill-opacity=".3">${ name }</text>
<text x="${ iconSize }" y="14">${ name }</text>` : '' }
<text x="${ leftSize + 7 }" y="15" fill="#010101" fill-opacity=".3">${ text }</text>
<text x="${ leftSize + 7 }" y="14">${ text }</text>
</g>
</svg>
`.trim().replace(/\>[\s]+\</gm, '><')
};
}
});
5 changes: 5 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@
"API_Enable_CORS": "Enable CORS",
"API_Enable_Direct_Message_History_EndPoint": "Enable Direct Message History Endpoint",
"API_Enable_Direct_Message_History_EndPoint_Description": "This enables the `/api/v1/im.history.others` which allows the viewing of direct messages sent by other users that the caller is not part of.",
"API_Enable_Shields": "Enable Shields",
"API_Enable_Shields_Description": "Enable shields available at `/api/v1/shields.svg`",
"API_GitHub_Enterprise_URL": "Server URL",
"API_GitHub_Enterprise_URL_Description": "Example: http://domain.com (excluding trailing slash)",
"API_Gitlab_URL": "GitLab URL",
"API_Shield_Types": "Shield Types",
"API_Shield_Types_Description": "Types of shields to enable as a comma separated list, choose from `online`, `channel` or `*` for all",
"API_Token": "API Token",
"API_Upper_Count_Limit": "Max Record Amount",
"API_Upper_Count_Limit_Description": "What is the maximum number of records the REST API should return (when not unlimited)?",
Expand Down Expand Up @@ -782,6 +786,7 @@
"Jitsi_Enable_Channels": "Enable in Channels",
"join": "Join",
"Join_audio_call": "Join audio call",
"Join_Chat": "Join Chat",
"Join_default_channels": "Join default channels",
"Join_the_Community": "Join the Community",
"Join_the_given_channel": "Join the given channel",
Expand Down