2021-03-18 13:33:35 +00:00
|
|
|
import { compareServerVersion, methods } from '../lib/utils';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { SubscriptionType } from '../definitions/ISubscription';
|
|
|
|
import { IAvatar } from '../containers/Avatar/interfaces';
|
2020-11-03 17:33:03 +00:00
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const formatUrl = (url: string, size: number, query: string) => `${url}?format=png&size=${size}${query}`;
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
export const avatarURL = ({
|
2021-09-13 20:41:05 +00:00
|
|
|
type,
|
|
|
|
text,
|
|
|
|
size = 25,
|
|
|
|
user = {},
|
|
|
|
avatar,
|
|
|
|
server,
|
|
|
|
avatarETag,
|
|
|
|
rid,
|
|
|
|
blockUnauthenticatedAccess,
|
|
|
|
serverVersion
|
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;
|
2021-09-13 20:41:05 +00:00
|
|
|
} else if (rid && !compareServerVersion(serverVersion, '3.6.0', methods.lowerThan)) {
|
|
|
|
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
|
|
|
const { id, token } = user;
|
|
|
|
let query = '';
|
2020-10-30 15:54:02 +00:00
|
|
|
if (id && token && blockUnauthenticatedAccess) {
|
2021-09-13 20:41:05 +00:00
|
|
|
query += `&rc_token=${token}&rc_uid=${id}`;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|