2021-03-18 13:33:35 +00:00
|
|
|
import { compareServerVersion, methods } from '../lib/utils';
|
2020-11-03 17:33:03 +00:00
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
const formatUrl = (url, size, query) => `${ url }?format=png&size=${ size }${ query }`;
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
export const avatarURL = ({
|
2020-11-04 16:53:44 +00:00
|
|
|
type, text, size, user = {}, avatar, server, avatarETag, rid, blockUnauthenticatedAccess, serverVersion
|
2020-03-30 19:50:27 +00:00
|
|
|
}) => {
|
2020-10-30 13:51:04 +00:00
|
|
|
let room;
|
|
|
|
if (type === 'd') {
|
|
|
|
room = text;
|
2021-03-18 13:33:35 +00:00
|
|
|
} else if (rid && !(compareServerVersion(serverVersion, '3.6.0', methods.lowerThan))) {
|
2020-10-30 13:51:04 +00:00
|
|
|
room = `room/${ rid }`;
|
|
|
|
} else {
|
|
|
|
room = `@${ text }`;
|
|
|
|
}
|
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) {
|
2020-10-30 13:12:02 +00:00
|
|
|
query += `&rc_token=${ token }&rc_uid=${ id }`;
|
|
|
|
}
|
|
|
|
if (avatarETag) {
|
|
|
|
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-02-01 17:18:55 +00:00
|
|
|
return formatUrl(`${ server }${ avatar }`, size, query);
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:18:55 +00:00
|
|
|
return formatUrl(`${ server }/avatar/${ room }`, size, query);
|
2020-03-30 19:50:27 +00:00
|
|
|
};
|