From 4b13045d1c58c6330a4d651949b8eeb125a07962 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Mon, 10 Apr 2023 15:08:15 -0300 Subject: [PATCH] fix: possibility to use the External Provider URI with query (#4956) * [FIX] Possibility to use the External Provider URI with query * minor tweak --------- Co-authored-by: Gleidson Daniel Silva --- app/lib/methods/helpers/getAvatarUrl.test.ts | 32 ++++++++++++++++++++ app/lib/methods/helpers/getAvatarUrl.ts | 5 ++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 app/lib/methods/helpers/getAvatarUrl.test.ts diff --git a/app/lib/methods/helpers/getAvatarUrl.test.ts b/app/lib/methods/helpers/getAvatarUrl.test.ts new file mode 100644 index 000000000..e778e1450 --- /dev/null +++ b/app/lib/methods/helpers/getAvatarUrl.test.ts @@ -0,0 +1,32 @@ +import { formatUrl } from './getAvatarUrl'; + +jest.mock('react-native', () => ({ PixelRatio: { get: () => 1 } })); + +describe('formatUrl function', () => { + test('formats the default URL to get the user avatar', () => { + const url = 'https://mobile.rocket.chat/avatar/reinaldoneto'; + const size = 30; + const query = '&extraparam=true'; + const expected = 'https://mobile.rocket.chat/avatar/reinaldoneto?format=png&size=30&extraparam=true'; + const result = formatUrl(url, size, query); + expect(result).toEqual(expected); + }); + + test('formats an external provider URI to get the user avatar', () => { + const url = 'https://open.rocket.chat/avatar/reinaldoneto'; + const size = 30; + const query = undefined; + const expected = 'https://open.rocket.chat/avatar/reinaldoneto?format=png&size=30'; + const result = formatUrl(url, size, query); + expect(result).toEqual(expected); + }); + + test('formats an external provider URI that already includes a query to get the user avatar', () => { + const url = 'https://open.rocket.chat/avatar?rcusername=reinaldoneto'; + const size = 30; + const query = undefined; + const expected = 'https://open.rocket.chat/avatar?rcusername=reinaldoneto&format=png&size=30'; + const result = formatUrl(url, size, query); + expect(result).toEqual(expected); + }); +}); diff --git a/app/lib/methods/helpers/getAvatarUrl.ts b/app/lib/methods/helpers/getAvatarUrl.ts index 2a9e682e7..222e7d2a8 100644 --- a/app/lib/methods/helpers/getAvatarUrl.ts +++ b/app/lib/methods/helpers/getAvatarUrl.ts @@ -4,7 +4,10 @@ import { SubscriptionType } from '../../../definitions'; import { IAvatar } from '../../../containers/Avatar/interfaces'; import { compareServerVersion } from './compareServerVersion'; -const formatUrl = (url: string, size: number, query?: string) => `${url}?format=png&size=${PixelRatio.get() * size}${query}`; +export const formatUrl = (url: string, size: number, query?: string) => { + const hasQuestionMark = /\/[^\/?]+\?/.test(url); + return `${url}${hasQuestionMark ? '&' : '?'}format=png&size=${PixelRatio.get() * size}${query || ''}`; +}; export const getAvatarURL = ({ type,