2020-11-03 17:33:03 +00:00
|
|
|
import semver from 'semver';
|
|
|
|
|
|
|
|
import reduxStore from '../lib/createStore';
|
|
|
|
|
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-10-30 15:54:02 +00:00
|
|
|
type, text, size, user = {}, avatar, server, avatarETag, rid, blockUnauthenticatedAccess
|
2020-03-30 19:50:27 +00:00
|
|
|
}) => {
|
2020-11-03 17:33:03 +00:00
|
|
|
const { version: serverVersion } = reduxStore.getState().server;
|
|
|
|
const isLegacy = serverVersion && semver.lt(semver.coerce(serverVersion), '3.6.0');
|
|
|
|
|
2020-10-30 13:51:04 +00:00
|
|
|
let room;
|
|
|
|
if (type === 'd') {
|
|
|
|
room = text;
|
2020-11-03 17:33:03 +00:00
|
|
|
} else if (rid && !isLegacy) {
|
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:51:04 +00:00
|
|
|
const uriSize = size > 100 ? size : 100;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatUrl(`${ server }${ avatar }`, uriSize, query);
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
return formatUrl(`${ server }/avatar/${ room }`, uriSize, query);
|
2020-03-30 19:50:27 +00:00
|
|
|
};
|