From 8d720bd00002dbb8e25d9ab94e1e23b86f49d4e8 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Wed, 8 Mar 2023 17:35:54 -0300 Subject: [PATCH] [FIX] Possibility to use the External Provider URI with query --- app/lib/methods/helpers/getAvatarUrl.test.ts | 27 ++++++++++++++++++++ app/lib/methods/helpers/getAvatarUrl.ts | 5 +++- 2 files changed, 31 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..8cc48e60a --- /dev/null +++ b/app/lib/methods/helpers/getAvatarUrl.test.ts @@ -0,0 +1,27 @@ +import { formatUrl } from './getAvatarUrl'; + +jest.mock('react-native', () => ({ PixelRatio: { get: () => 1 } })); + +describe('Test the formatURL', () => { + it('format as default to get the avatar from an user', () => { + const url = 'https://mobile.rocket.chat/avatar/reinaldoneto'; + const size = 30; + const query = '&extraparam=true'; + const expectedResult = `https://mobile.rocket.chat/avatar/reinaldoneto?format=png&size=30&extraparam=true`; + expect(formatUrl(url, size, query)).toBe(expectedResult); + }); + it('format external provider uri to get the avatar from an user', () => { + const url = 'https://open.rocket.chat/avatar/reinaldoneto'; + const size = 30; + const query = undefined; + const expectedResult = `https://open.rocket.chat/avatar/reinaldoneto?format=png&size=30`; + expect(formatUrl(url, size, query)).toBe(expectedResult); + }); + it('format external provider uri that already adds a query to get the avatar from an user', () => { + const url = 'https://open.rocket.chat/avatar?rcusername=reinaldoneto'; + const size = 30; + const query = undefined; + const expectedResult = `https://open.rocket.chat/avatar?rcusername=reinaldoneto&format=png&size=30`; + expect(formatUrl(url, size, query)).toBe(expectedResult); + }); +}); 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,