Mangane/app/soapbox/utils/features.js

74 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-05-17 21:44:33 +02:00
// Detect backend features to conditionally render elements
2020-09-28 04:41:11 +02:00
import gte from 'semver/functions/gte';
import lt from 'semver/functions/lt';
2021-08-23 21:14:47 +02:00
import { List as ImmutableList, Map as ImmutableMap } from 'immutable';
import { createSelector } from 'reselect';
2020-05-17 21:44:33 +02:00
const any = arr => arr.some(Boolean);
// For uglification
export const MASTODON = 'Mastodon';
export const PLEROMA = 'Pleroma';
export const getFeatures = createSelector([
instance => parseVersion(instance.get('version')),
instance => instance.getIn(['pleroma', 'metadata', 'features'], ImmutableList()),
2021-08-23 21:14:47 +02:00
instance => instance.getIn(['pleroma', 'metadata', 'federation'], ImmutableMap()),
], (v, features, federation) => {
2020-05-17 21:44:33 +02:00
return {
bookmarks: any([
v.software === MASTODON && gte(v.compatVersion, '3.1.0'),
v.software === PLEROMA && gte(v.version, '0.9.9'),
]),
2021-09-18 21:36:42 +02:00
lists: any([
v.software === MASTODON && gte(v.compatVersion, '2.1.0'),
v.software === PLEROMA && gte(v.version, '0.9.9'),
]),
suggestions: v.software === MASTODON && gte(v.compatVersion, '2.4.3'),
suggestionsV2: v.software === MASTODON && gte(v.compatVersion, '3.4.0'),
trends: v.software === MASTODON && gte(v.compatVersion, '3.0.0'),
mediaV2: any([
v.software === MASTODON && gte(v.compatVersion, '3.1.3'),
// Even though Pleroma supports these endpoints, it has disadvantages
// v.software === PLEROMA && gte(v.version, '2.1.0'),
]),
directTimeline: any([
v.software === MASTODON && lt(v.compatVersion, '3.0.0'),
v.software === PLEROMA && gte(v.version, '0.9.9'),
]),
conversations: any([
v.software === MASTODON && gte(v.compatVersion, '2.6.0'),
v.software === PLEROMA && gte(v.version, '0.9.9'),
]),
emojiReacts: v.software === PLEROMA && gte(v.version, '2.0.0'),
emojiReactsRGI: v.software === PLEROMA && gte(v.version, '2.2.49'),
attachmentLimit: v.software === PLEROMA ? Infinity : 4,
focalPoint: v.software === MASTODON && gte(v.compatVersion, '2.3.0'),
importAPI: v.software === PLEROMA,
importMutes: v.software === PLEROMA && gte(v.version, '2.2.0'),
2021-08-23 21:14:47 +02:00
emailList: features.includes('email_list'),
chats: v.software === PLEROMA && gte(v.version, '2.1.0'),
scopes: v.software === PLEROMA ? 'read write follow push admin' : 'read write follow push',
2021-08-23 21:14:47 +02:00
federating: federation.get('enabled', true), // Assume true unless explicitly false
richText: v.software === PLEROMA,
securityAPI: v.software === PLEROMA,
settingsStore: v.software === PLEROMA,
accountAliasesAPI: v.software === PLEROMA,
resetPasswordAPI: v.software === PLEROMA,
exposableReactions: features.includes('exposable_reactions'),
accountSubscriptions: v.software === PLEROMA && gte(v.version, '1.0.0'),
unrestrictedLists: v.software === PLEROMA,
2021-09-18 22:50:29 +02:00
accountByUsername: v.software === PLEROMA,
2020-05-17 21:44:33 +02:00
};
});
2020-05-17 21:44:33 +02:00
export const parseVersion = version => {
const regex = /^([\w\.]*)(?: \(compatible; ([\w]*) (.*)\))?$/;
const match = regex.exec(version);
2020-05-17 21:44:33 +02:00
return {
software: match[2] || MASTODON,
2020-05-17 21:44:33 +02:00
version: match[3] || match[1],
compatVersion: match[1],
};
};