2022-06-21 19:23:55 +00:00
|
|
|
import { PixelRatio } from 'react-native';
|
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
import { SubscriptionType } from '../../../definitions';
|
|
|
|
import { IAvatar } from '../../../containers/Avatar/interfaces';
|
|
|
|
import { compareServerVersion } from './compareServerVersion';
|
2020-11-03 17:33:03 +00:00
|
|
|
|
2023-04-10 18:08:15 +00:00
|
|
|
export const formatUrl = (url: string, size: number, query?: string) => {
|
|
|
|
const hasQuestionMark = /\/[^\/?]+\?/.test(url);
|
|
|
|
return `${url}${hasQuestionMark ? '&' : '?'}format=png&size=${PixelRatio.get() * size}${query || ''}`;
|
|
|
|
};
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
export const getAvatarURL = ({
|
2021-09-13 20:41:05 +00:00
|
|
|
type,
|
2022-06-06 15:33:36 +00:00
|
|
|
text = '',
|
2021-09-13 20:41:05 +00:00
|
|
|
size = 25,
|
2022-06-06 15:33:36 +00:00
|
|
|
userId,
|
|
|
|
token,
|
2021-09-13 20:41:05 +00:00
|
|
|
avatar,
|
|
|
|
server,
|
|
|
|
avatarETag,
|
|
|
|
rid,
|
|
|
|
blockUnauthenticatedAccess,
|
2022-04-19 00:52:02 +00:00
|
|
|
serverVersion,
|
2023-06-16 19:21:40 +00:00
|
|
|
avatarExternalProviderUrl,
|
2023-10-17 19:28:02 +00:00
|
|
|
roomAvatarExternalProviderUrl,
|
|
|
|
cdnPrefix
|
2022-01-12 12:54:04 +00:00
|
|
|
}: IAvatar): string => {
|
2020-10-30 13:51:04 +00:00
|
|
|
let room;
|
2022-01-12 12:54:04 +00:00
|
|
|
if (type === SubscriptionType.DIRECT) {
|
2020-10-30 13:51:04 +00:00
|
|
|
room = text;
|
2023-06-16 19:21:40 +00:00
|
|
|
if (avatarExternalProviderUrl) {
|
|
|
|
const externalUri = avatarExternalProviderUrl.trim().replace(/\/+$/, '').replace('{username}', room);
|
2022-04-19 00:52:02 +00:00
|
|
|
return formatUrl(`${externalUri}`, size);
|
|
|
|
}
|
2023-06-16 19:21:40 +00:00
|
|
|
} else if (rid && compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.8.0') && roomAvatarExternalProviderUrl) {
|
|
|
|
const externalUri = roomAvatarExternalProviderUrl.trim().replace(/\/+$/, '').replace('{roomId}', rid);
|
|
|
|
return formatUrl(`${externalUri}`, size);
|
2022-02-07 18:44:04 +00:00
|
|
|
} else if (rid && !compareServerVersion(serverVersion, 'lowerThan', '3.6.0')) {
|
2021-09-13 20:41:05 +00:00
|
|
|
room = `room/${rid}`;
|
2020-10-30 13:51:04 +00:00
|
|
|
} else {
|
2021-09-13 20:41:05 +00:00
|
|
|
room = `@${text}`;
|
2020-10-30 13:51:04 +00:00
|
|
|
}
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
let query = '';
|
2022-06-06 15:33:36 +00:00
|
|
|
if (userId && token && blockUnauthenticatedAccess) {
|
|
|
|
query += `&rc_token=${token}&rc_uid=${userId}`;
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
|
|
|
if (avatarETag) {
|
2021-09-13 20:41:05 +00:00
|
|
|
query += `&etag=${avatarETag}`;
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:28:02 +00:00
|
|
|
if (cdnPrefix) {
|
|
|
|
server = cdnPrefix.trim().replace(/\/+$/, '');
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
if (avatar) {
|
2020-10-30 13:12:02 +00:00
|
|
|
if (avatar.startsWith('http')) {
|
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return formatUrl(`${server}${avatar}`, size, query);
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return formatUrl(`${server}/avatar/${room}`, size, query);
|
2020-03-30 19:50:27 +00:00
|
|
|
};
|