From 0a1d0b3bd0868b3f9ea511d7f1aeac3e8415bbbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Jovanovic=CC=81?= Date: Sun, 27 Aug 2017 22:16:08 +0200 Subject: [PATCH 1/6] Implement feature to load user avatar from external avatar provider --- packages/rocketchat-i18n/i18n/en.i18n.json | 1 + packages/rocketchat-lib/server/startup/settings.js | 4 ++++ packages/rocketchat-ui/getAvatarUrlFromUsername.js | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 9413c0c42a0e..c59826f45b65 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -29,6 +29,7 @@ "Accounts_AllowUserProfileChange": "Allow User Profile Change", "Accounts_AvatarResize": "Resize Avatars", "Accounts_AvatarSize": "Avatar Size", + "Accounts_ExternalSource": "Avatar External Provider URL", "Accounts_BlockedDomainsList": "Blocked Domains List", "Accounts_BlockedDomainsList_Description": "Comma-separated list of blocked domains", "Accounts_BlockedUsernameList": "Blocked Username List", diff --git a/packages/rocketchat-lib/server/startup/settings.js b/packages/rocketchat-lib/server/startup/settings.js index ec165002dc4d..981a6b2cab5a 100644 --- a/packages/rocketchat-lib/server/startup/settings.js +++ b/packages/rocketchat-lib/server/startup/settings.js @@ -177,6 +177,10 @@ RocketChat.settings.addGroup('Accounts', function() { value: true } }); + this.add('Accounts_ExternalSource', '', { + type: 'string', + 'public': true + }); return this.add('Accounts_SetDefaultAvatar', true, { type: 'boolean' diff --git a/packages/rocketchat-ui/getAvatarUrlFromUsername.js b/packages/rocketchat-ui/getAvatarUrlFromUsername.js index d9e17c396539..6ea8abe696d1 100644 --- a/packages/rocketchat-ui/getAvatarUrlFromUsername.js +++ b/packages/rocketchat-ui/getAvatarUrlFromUsername.js @@ -1,5 +1,11 @@ // TODO: remove global this.getAvatarUrlFromUsername = function(username) { + const externalSource = (RocketChat.settings.get('Accounts_ExternalSource') || '').trim().replace(/\/$/, ''); + if (externalSource.length) { + const url = externalSource.replace('{username}', username); + return `${ url }`; + } + const key = `avatar_random_${ username }`; const random = typeof Session !== 'undefined' ? Session.keys[key] : 0; if (username == null) { From 4725040d504816cd017c2c5a2567348d708de1de Mon Sep 17 00:00:00 2001 From: Milan Jovanovic Date: Thu, 24 Jan 2019 14:32:42 +0000 Subject: [PATCH 2/6] Avatar External source moved to utils package --- packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js index fb2653f65fc5..f326f1ec971a 100644 --- a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js +++ b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js @@ -2,6 +2,12 @@ import { Session } from 'meteor/session'; import { settings } from 'meteor/rocketchat:settings'; export const getAvatarUrlFromUsername = function(username) { + const externalSource = (RocketChat.settings.get('Accounts_ExternalSource') || '').trim().replace(/\/$/, ''); + if (externalSource.length) { + const url = externalSource.replace('{username}', username); + return `${ url }`; + } + const key = `avatar_random_${ username }`; const random = typeof Session !== 'undefined' && typeof Session.keys[key] !== 'undefined' ? Session.keys[key] : 0; if (username == null) { From 81b9039ace67c677d22f876498f8b28eb9dcc23e Mon Sep 17 00:00:00 2001 From: Milan Jovanovic Date: Thu, 24 Jan 2019 14:35:10 +0000 Subject: [PATCH 3/6] Changed settings name to be more descriptive --- packages/rocketchat-i18n/i18n/en.i18n.json | 2 +- packages/rocketchat-lib/server/startup/settings.js | 4 ++-- packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 781c2ed2d117..764187468802 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -43,7 +43,7 @@ "Accounts_AvatarCacheTime_description": "Number of seconds the http protocol is told to cache the avatar images.", "Accounts_AvatarResize": "Resize Avatars", "Accounts_AvatarSize": "Avatar Size", - "Accounts_ExternalSource": "Avatar External Provider URL", + "Accounts_AvatarExternalProviderUrl": "Avatar External Provider URL", "Accounts_BlockedDomainsList": "Blocked Domains List", "Accounts_BlockedDomainsList_Description": "Comma-separated list of blocked domains", "Accounts_BlockedUsernameList": "Blocked Username List", diff --git a/packages/rocketchat-lib/server/startup/settings.js b/packages/rocketchat-lib/server/startup/settings.js index 2539bfe3a367..ab9fe4376f9d 100644 --- a/packages/rocketchat-lib/server/startup/settings.js +++ b/packages/rocketchat-lib/server/startup/settings.js @@ -470,9 +470,9 @@ RocketChat.settings.addGroup('Accounts', function() { }, }); - this.add('Accounts_ExternalSource', '', { + this.add('Accounts_AvatarExternalProviderUrl', '', { type: 'string', - 'public': true + public: true, }); this.add('Accounts_AvatarCacheTime', 3600, { diff --git a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js index f326f1ec971a..7c42e8b328da 100644 --- a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js +++ b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js @@ -2,7 +2,7 @@ import { Session } from 'meteor/session'; import { settings } from 'meteor/rocketchat:settings'; export const getAvatarUrlFromUsername = function(username) { - const externalSource = (RocketChat.settings.get('Accounts_ExternalSource') || '').trim().replace(/\/$/, ''); + const externalSource = (RocketChat.settings.get('Accounts_AvatarExternalProviderUrl') || '').trim().replace(/\/$/, ''); if (externalSource.length) { const url = externalSource.replace('{username}', username); return `${ url }`; From f5e911c1d063e3583fe86aab54ba688424f2f19e Mon Sep 17 00:00:00 2001 From: Milan Jovanovic Date: Thu, 24 Jan 2019 14:40:34 +0000 Subject: [PATCH 4/6] removed accidentally added file --- .../rocketchat-ui/getAvatarUrlFromUsername.js | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 packages/rocketchat-ui/getAvatarUrlFromUsername.js diff --git a/packages/rocketchat-ui/getAvatarUrlFromUsername.js b/packages/rocketchat-ui/getAvatarUrlFromUsername.js deleted file mode 100644 index 6ea8abe696d1..000000000000 --- a/packages/rocketchat-ui/getAvatarUrlFromUsername.js +++ /dev/null @@ -1,23 +0,0 @@ -// TODO: remove global -this.getAvatarUrlFromUsername = function(username) { - const externalSource = (RocketChat.settings.get('Accounts_ExternalSource') || '').trim().replace(/\/$/, ''); - if (externalSource.length) { - const url = externalSource.replace('{username}', username); - return `${ url }`; - } - - const key = `avatar_random_${ username }`; - const random = typeof Session !== 'undefined' ? Session.keys[key] : 0; - if (username == null) { - return; - } - const cdnPrefix = (RocketChat.settings.get('CDN_PREFIX') || '').trim().replace(/\/$/, ''); - const pathPrefix = (__meteor_runtime_config__.ROOT_URL_PATH_PREFIX || '').trim().replace(/\/$/, ''); - let path = pathPrefix; - if (cdnPrefix) { - path = cdnPrefix + pathPrefix; - } else if (Meteor.isCordova) { - path = Meteor.absoluteUrl().replace(/\/$/, ''); - } - return `${ path }/avatar/${ encodeURIComponent(username) }?_dc=${ random }`; -}; From 9b5b862d9124c4b68cda5c6b21cfe0eff52b30d2 Mon Sep 17 00:00:00 2001 From: Milan Jovanovic Date: Thu, 24 Jan 2019 14:45:47 +0000 Subject: [PATCH 5/6] Fixed trailing spaces issue and how settings is referenced --- packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js index 7c42e8b328da..39e53debf2da 100644 --- a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js +++ b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js @@ -2,12 +2,11 @@ import { Session } from 'meteor/session'; import { settings } from 'meteor/rocketchat:settings'; export const getAvatarUrlFromUsername = function(username) { - const externalSource = (RocketChat.settings.get('Accounts_AvatarExternalProviderUrl') || '').trim().replace(/\/$/, ''); + const externalSource = (settings.get('Accounts_AvatarExternalProviderUrl') || '').trim().replace(/\/$/, ''); if (externalSource.length) { const url = externalSource.replace('{username}', username); return `${ url }`; } - const key = `avatar_random_${ username }`; const random = typeof Session !== 'undefined' && typeof Session.keys[key] !== 'undefined' ? Session.keys[key] : 0; if (username == null) { From a2f6fa5c5cd56941f5cd63bbf49e50ba383ed567 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 8 Feb 2019 10:01:27 -0200 Subject: [PATCH 6/6] Add setting description --- packages/rocketchat-i18n/i18n/en.i18n.json | 1 + packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index e5f2f84f5c8d..a83f532fbc96 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -44,6 +44,7 @@ "Accounts_AvatarResize": "Resize Avatars", "Accounts_AvatarSize": "Avatar Size", "Accounts_AvatarExternalProviderUrl": "Avatar External Provider URL", + "Accounts_AvatarExternalProviderUrl_Description": "Example: `https://acme.com/api/v1/{username}`", "Accounts_BlockedDomainsList": "Blocked Domains List", "Accounts_BlockedDomainsList_Description": "Comma-separated list of blocked domains", "Accounts_BlockedUsernameList": "Blocked Username List", diff --git a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js index 39e53debf2da..8606771343cc 100644 --- a/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js +++ b/packages/rocketchat-utils/lib/getAvatarUrlFromUsername.js @@ -3,9 +3,8 @@ import { settings } from 'meteor/rocketchat:settings'; export const getAvatarUrlFromUsername = function(username) { const externalSource = (settings.get('Accounts_AvatarExternalProviderUrl') || '').trim().replace(/\/$/, ''); - if (externalSource.length) { - const url = externalSource.replace('{username}', username); - return `${ url }`; + if (externalSource !== '') { + return externalSource.replace('{username}', username); } const key = `avatar_random_${ username }`; const random = typeof Session !== 'undefined' && typeof Session.keys[key] !== 'undefined' ? Session.keys[key] : 0;