Convert utils/features.ts to TypeScript
This commit is contained in:
parent
55c99f0207
commit
05f5045648
6 changed files with 46 additions and 21 deletions
|
@ -23,6 +23,7 @@ describe('normalizeInstance()', () => {
|
||||||
description: '',
|
description: '',
|
||||||
description_limit: 1500,
|
description_limit: 1500,
|
||||||
email: '',
|
email: '',
|
||||||
|
feature_quote: false,
|
||||||
fedibird_capabilities: [],
|
fedibird_capabilities: [],
|
||||||
invites_enabled: false,
|
invites_enabled: false,
|
||||||
languages: [],
|
languages: [],
|
||||||
|
|
|
@ -35,6 +35,7 @@ export const InstanceRecord = ImmutableRecord({
|
||||||
description: '',
|
description: '',
|
||||||
description_limit: 1500,
|
description_limit: 1500,
|
||||||
email: '',
|
email: '',
|
||||||
|
feature_quote: false,
|
||||||
fedibird_capabilities: ImmutableList(),
|
fedibird_capabilities: ImmutableList(),
|
||||||
invites_enabled: false,
|
invites_enabled: false,
|
||||||
languages: ImmutableList(),
|
languages: ImmutableList(),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
import { InstanceRecord } from 'soapbox/normalizers';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
parseVersion,
|
parseVersion,
|
||||||
|
@ -28,7 +28,7 @@ describe('parseVersion', () => {
|
||||||
describe('getFeatures', () => {
|
describe('getFeatures', () => {
|
||||||
describe('emojiReacts', () => {
|
describe('emojiReacts', () => {
|
||||||
it('is true for Pleroma 2.0+', () => {
|
it('is true for Pleroma 2.0+', () => {
|
||||||
const instance = ImmutableMap({
|
const instance = InstanceRecord({
|
||||||
version: '2.7.2 (compatible; Pleroma 2.0.5-6-ga36eb5ea-plerasstodon+dev)',
|
version: '2.7.2 (compatible; Pleroma 2.0.5-6-ga36eb5ea-plerasstodon+dev)',
|
||||||
});
|
});
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
|
@ -36,7 +36,7 @@ describe('getFeatures', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('is false for Pleroma < 2.0', () => {
|
it('is false for Pleroma < 2.0', () => {
|
||||||
const instance = ImmutableMap({
|
const instance = InstanceRecord({
|
||||||
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
||||||
});
|
});
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
|
@ -44,7 +44,7 @@ describe('getFeatures', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('is false for Mastodon', () => {
|
it('is false for Mastodon', () => {
|
||||||
const instance = ImmutableMap({ version: '3.1.4' });
|
const instance = InstanceRecord({ version: '3.1.4' });
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
expect(features.emojiReacts).toBe(false);
|
expect(features.emojiReacts).toBe(false);
|
||||||
});
|
});
|
||||||
|
@ -52,19 +52,19 @@ describe('getFeatures', () => {
|
||||||
|
|
||||||
describe('suggestions', () => {
|
describe('suggestions', () => {
|
||||||
it('is true for Mastodon 2.4.3+', () => {
|
it('is true for Mastodon 2.4.3+', () => {
|
||||||
const instance = ImmutableMap({ version: '2.4.3' });
|
const instance = InstanceRecord({ version: '2.4.3' });
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
expect(features.suggestions).toBe(true);
|
expect(features.suggestions).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('is false for Mastodon < 2.4.3', () => {
|
it('is false for Mastodon < 2.4.3', () => {
|
||||||
const instance = ImmutableMap({ version: '2.4.2' });
|
const instance = InstanceRecord({ version: '2.4.2' });
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
expect(features.suggestions).toBe(false);
|
expect(features.suggestions).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('is false for Pleroma', () => {
|
it('is false for Pleroma', () => {
|
||||||
const instance = ImmutableMap({
|
const instance = InstanceRecord({
|
||||||
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
||||||
});
|
});
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
|
@ -74,19 +74,19 @@ describe('getFeatures', () => {
|
||||||
|
|
||||||
describe('trends', () => {
|
describe('trends', () => {
|
||||||
it('is true for Mastodon 3.0.0+', () => {
|
it('is true for Mastodon 3.0.0+', () => {
|
||||||
const instance = ImmutableMap({ version: '3.0.0' });
|
const instance = InstanceRecord({ version: '3.0.0' });
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
expect(features.trends).toBe(true);
|
expect(features.trends).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('is false for Mastodon < 3.0.0', () => {
|
it('is false for Mastodon < 3.0.0', () => {
|
||||||
const instance = ImmutableMap({ version: '2.4.3' });
|
const instance = InstanceRecord({ version: '2.4.3' });
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
expect(features.trends).toBe(false);
|
expect(features.trends).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('is false for Pleroma', () => {
|
it('is false for Pleroma', () => {
|
||||||
const instance = ImmutableMap({
|
const instance = InstanceRecord({
|
||||||
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
||||||
});
|
});
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
|
@ -96,13 +96,13 @@ describe('getFeatures', () => {
|
||||||
|
|
||||||
describe('focalPoint', () => {
|
describe('focalPoint', () => {
|
||||||
it('is true for Mastodon 2.3.0+', () => {
|
it('is true for Mastodon 2.3.0+', () => {
|
||||||
const instance = ImmutableMap({ version: '2.3.0' });
|
const instance = InstanceRecord({ version: '2.3.0' });
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
expect(features.focalPoint).toBe(true);
|
expect(features.focalPoint).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('is false for Pleroma', () => {
|
it('is false for Pleroma', () => {
|
||||||
const instance = ImmutableMap({
|
const instance = InstanceRecord({
|
||||||
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
|
||||||
});
|
});
|
||||||
const features = getFeatures(instance);
|
const features = getFeatures(instance);
|
||||||
|
|
|
@ -6,11 +6,13 @@ import lt from 'semver/functions/lt';
|
||||||
|
|
||||||
import { custom } from 'soapbox/custom';
|
import { custom } from 'soapbox/custom';
|
||||||
|
|
||||||
|
import type { Instance } from 'soapbox/types/entities';
|
||||||
|
|
||||||
// Import custom overrides, if exists
|
// Import custom overrides, if exists
|
||||||
const overrides = custom('features');
|
const overrides = custom('features');
|
||||||
|
|
||||||
// Truthy array convenience function
|
// Truthy array convenience function
|
||||||
const any = arr => arr.some(Boolean);
|
const any = (arr: Array<any>): boolean => arr.some(Boolean);
|
||||||
|
|
||||||
// For uglification
|
// For uglification
|
||||||
export const MASTODON = 'Mastodon';
|
export const MASTODON = 'Mastodon';
|
||||||
|
@ -18,12 +20,12 @@ export const PLEROMA = 'Pleroma';
|
||||||
export const MITRA = 'Mitra';
|
export const MITRA = 'Mitra';
|
||||||
export const TRUTHSOCIAL = 'TruthSocial';
|
export const TRUTHSOCIAL = 'TruthSocial';
|
||||||
|
|
||||||
export const getFeatures = createSelector([instance => instance], instance => {
|
const getInstanceFeatures = (instance: Instance) => {
|
||||||
const v = parseVersion(instance.get('version'));
|
const v = parseVersion(instance.get('version'));
|
||||||
const features = instance.getIn(['pleroma', 'metadata', 'features'], ImmutableList());
|
const features = instance.pleroma.getIn(['metadata', 'features'], ImmutableList()) as ImmutableList<string>;
|
||||||
const federation = instance.getIn(['pleroma', 'metadata', 'federation'], ImmutableMap());
|
const federation = instance.pleroma.getIn(['metadata', 'federation'], ImmutableMap()) as ImmutableMap<string, any>;
|
||||||
|
|
||||||
return Object.assign({
|
return {
|
||||||
media: true,
|
media: true,
|
||||||
privacyScopes: v.software !== TRUTHSOCIAL,
|
privacyScopes: v.software !== TRUTHSOCIAL,
|
||||||
spoilers: v.software !== TRUTHSOCIAL,
|
spoilers: v.software !== TRUTHSOCIAL,
|
||||||
|
@ -112,7 +114,7 @@ export const getFeatures = createSelector([instance => instance], instance => {
|
||||||
accountEndorsements: v.software === PLEROMA && gte(v.version, '2.4.50'),
|
accountEndorsements: v.software === PLEROMA && gte(v.version, '2.4.50'),
|
||||||
quotePosts: any([
|
quotePosts: any([
|
||||||
v.software === PLEROMA && gte(v.version, '2.4.50'),
|
v.software === PLEROMA && gte(v.version, '2.4.50'),
|
||||||
instance.get('feature_quote') === true,
|
instance.feature_quote === true,
|
||||||
]),
|
]),
|
||||||
birthdays: v.software === PLEROMA && gte(v.version, '2.4.50'),
|
birthdays: v.software === PLEROMA && gte(v.version, '2.4.50'),
|
||||||
ethereumLogin: v.software === MITRA,
|
ethereumLogin: v.software === MITRA,
|
||||||
|
@ -121,11 +123,26 @@ export const getFeatures = createSelector([instance => instance], instance => {
|
||||||
v.software === MASTODON && gte(v.compatVersion, '3.2.0'),
|
v.software === MASTODON && gte(v.compatVersion, '3.2.0'),
|
||||||
v.software === PLEROMA && gte(v.version, '2.4.50'),
|
v.software === PLEROMA && gte(v.version, '2.4.50'),
|
||||||
]),
|
]),
|
||||||
}, overrides);
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type Features = ReturnType<typeof getInstanceFeatures>;
|
||||||
|
|
||||||
|
export const getFeatures = createSelector([
|
||||||
|
(instance: Instance) => instance,
|
||||||
|
], (instance): Features => {
|
||||||
|
const features = getInstanceFeatures(instance);
|
||||||
|
return Object.assign(features, overrides) as Features;
|
||||||
});
|
});
|
||||||
|
|
||||||
export const parseVersion = version => {
|
interface Backend {
|
||||||
const regex = /^([\w\.]*)(?: \(compatible; ([\w]*) (.*)\))?$/;
|
software: string | null,
|
||||||
|
version: string,
|
||||||
|
compatVersion: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const parseVersion = (version: string): Backend => {
|
||||||
|
const regex = /^([\w.]*)(?: \(compatible; ([\w]*) (.*)\))?$/;
|
||||||
const match = regex.exec(version);
|
const match = regex.exec(version);
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
|
@ -74,6 +74,7 @@
|
||||||
"@types/react-helmet": "^6.1.5",
|
"@types/react-helmet": "^6.1.5",
|
||||||
"@types/react-router-dom": "^5.3.3",
|
"@types/react-router-dom": "^5.3.3",
|
||||||
"@types/react-toggle": "^4.0.3",
|
"@types/react-toggle": "^4.0.3",
|
||||||
|
"@types/semver": "^7.3.9",
|
||||||
"@types/uuid": "^8.3.4",
|
"@types/uuid": "^8.3.4",
|
||||||
"array-includes": "^3.0.3",
|
"array-includes": "^3.0.3",
|
||||||
"autoprefixer": "^10.4.2",
|
"autoprefixer": "^10.4.2",
|
||||||
|
|
|
@ -2169,6 +2169,11 @@
|
||||||
dependencies:
|
dependencies:
|
||||||
schema-utils "*"
|
schema-utils "*"
|
||||||
|
|
||||||
|
"@types/semver@^7.3.9":
|
||||||
|
version "7.3.9"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc"
|
||||||
|
integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ==
|
||||||
|
|
||||||
"@types/stack-utils@^2.0.0":
|
"@types/stack-utils@^2.0.0":
|
||||||
version "2.0.1"
|
version "2.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
|
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
|
||||||
|
|
Loading…
Reference in a new issue