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,