mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-01-31 21:43:33 +03:00
Shrink the size of the feature detection change
This commit is contained in:
parent
839023cefc
commit
ad0ab0c845
5 changed files with 57 additions and 76 deletions
|
@ -24,7 +24,7 @@ function ListAddEdit({ list, onClose }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [editMode]);
|
}, [editMode]);
|
||||||
const supportsExclusive = supports('@mastodon/list-exclusive');
|
const supportsExclusive = supports('@mastodon/list-exclusive') || supports('@gotosocial/list-exclusive');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class="sheet">
|
<div class="sheet">
|
||||||
|
|
9
src/data/features.json
Normal file
9
src/data/features.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"@mastodon/edit-media-attributes": ">=4.1",
|
||||||
|
"@mastodon/list-exclusive": ">=4.2",
|
||||||
|
"@gotosocial/list-exclusive": ">=0.17",
|
||||||
|
"@mastodon/filtered-notifications": "~4.3 || >=4.3",
|
||||||
|
"@mastodon/fetch-multiple-statuses": "~4.3 || >=4.3",
|
||||||
|
"@mastodon/trending-link-posts": "~4.3 || >=4.3",
|
||||||
|
"@mastodon/grouped-notifications": "~4.3 || >=4.3"
|
||||||
|
}
|
|
@ -89,29 +89,26 @@ export async function initInstance(client, instance) {
|
||||||
configuration: { urls: { streaming } = {} } = {},
|
configuration: { urls: { streaming } = {} } = {},
|
||||||
} = info;
|
} = info;
|
||||||
|
|
||||||
let nodeInfo;
|
// GoToSocial requires we get the NodeInfo to identify server type
|
||||||
|
// spec: https://github.com/jhass/nodeinfo
|
||||||
try {
|
try {
|
||||||
if (uri || domain) {
|
if (uri || domain) {
|
||||||
let urlBase = uri || `https://${domain}`;
|
let urlBase = uri || `https://${domain}`;
|
||||||
const wellKnownResponse = await fetch(`${urlBase}/.well-known/nodeinfo`);
|
const wellKnown = await (await fetch(`${urlBase}/.well-known/nodeinfo`)).json();
|
||||||
if (wellKnownResponse.ok) {
|
if (Array.isArray(wellKnown?.links)) {
|
||||||
const wellKnown = await wellKnownResponse.json();
|
const nodeInfoUrl = wellKnown.links.find(
|
||||||
if (wellKnown && Array.isArray(wellKnown.links)) {
|
(link) => typeof link.rel === 'string' &&
|
||||||
const nodeInfoUrl = wellKnown.links.find(
|
link.rel.startsWith('http://nodeinfo.diaspora.software/ns/schema/')
|
||||||
(link) => typeof link.rel === 'string' &&
|
)?.href;
|
||||||
link.rel.startsWith('http://nodeinfo.diaspora.software/ns/schema/')
|
if (nodeInfoUrl && nodeInfoUrl.startsWith(urlBase)) {
|
||||||
)?.href;
|
const nodeInfo = await (await fetch(nodeInfoUrl)).json();
|
||||||
if (nodeInfoUrl && nodeInfoUrl.startsWith(urlBase)) {
|
if (typeof nodeInfo?.software?.name === 'string') {
|
||||||
const nodeInfoResponse = await fetch(nodeInfoUrl);
|
info.software_name = nodeInfo.software.name;
|
||||||
nodeInfo = await nodeInfoResponse.json();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
if (nodeInfo) {
|
|
||||||
info.nodeInfo = nodeInfo;
|
|
||||||
}
|
|
||||||
console.log(info);
|
console.log(info);
|
||||||
|
|
||||||
const instances = store.local.getJSON('instances') || {};
|
const instances = store.local.getJSON('instances') || {};
|
||||||
|
|
|
@ -163,5 +163,5 @@ export function getVapidKey() {
|
||||||
|
|
||||||
export function isMediaFirstInstance() {
|
export function isMediaFirstInstance() {
|
||||||
const instance = getCurrentInstance();
|
const instance = getCurrentInstance();
|
||||||
return instance.nodeInfo?.software?.name === 'pixelfed';
|
return /pixelfed/i.test(instance?.version);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,82 +1,57 @@
|
||||||
import { satisfies } from 'compare-versions';
|
import { satisfies } from 'compare-versions';
|
||||||
|
|
||||||
|
import features from '../data/features.json';
|
||||||
|
|
||||||
import { getCurrentInstance } from './store-utils';
|
import { getCurrentInstance } from './store-utils';
|
||||||
|
|
||||||
|
// Non-semver(?) UA string detection
|
||||||
|
const containPixelfed = /pixelfed/i;
|
||||||
|
const notContainPixelfed = /^(?!.*pixelfed).*$/i;
|
||||||
|
const containPleroma = /pleroma/i;
|
||||||
|
const containAkkoma = /akkoma/i;
|
||||||
|
const containGTS = /gotosocial/i;
|
||||||
const platformFeatures = {
|
const platformFeatures = {
|
||||||
'@mastodon/edit-media-attributes': [['mastodon', '>=4.1']],
|
'@mastodon/lists': notContainPixelfed,
|
||||||
'@mastodon/list-exclusive': [
|
'@mastodon/filters': notContainPixelfed,
|
||||||
['mastodon', '>=4.2'],
|
'@mastodon/mentions': notContainPixelfed,
|
||||||
['gotosocial', '>=0.17'],
|
'@mastodon/trending-hashtags': notContainPixelfed,
|
||||||
],
|
'@mastodon/trending-links': notContainPixelfed,
|
||||||
'@mastodon/filtered-notifications': [['mastodon', '>=4.3']],
|
'@mastodon/post-bookmark': notContainPixelfed,
|
||||||
'@mastodon/fetch-multiple-statuses': [['mastodon', '>=4.3']],
|
'@mastodon/post-edit': notContainPixelfed,
|
||||||
'@mastodon/trending-link-posts': [['mastodon', '>=4.3']],
|
'@mastodon/profile-edit': notContainPixelfed,
|
||||||
'@mastodon/grouped-notifications': [['mastodon', '>=4.3']],
|
'@mastodon/profile-private-note': notContainPixelfed,
|
||||||
'@mastodon/lists': [['!pixelfed']],
|
'@pixelfed/trending': containPixelfed,
|
||||||
'@mastodon/filters': [['!pixelfed']],
|
'@pixelfed/home-include-reblogs': containPixelfed,
|
||||||
'@mastodon/mentions': [['!pixelfed']],
|
'@pixelfed/global-feed': containPixelfed,
|
||||||
'@mastodon/trending-hashtags': [['!pixelfed']],
|
'@pleroma/local-visibility-post': containPleroma,
|
||||||
'@mastodon/trending-links': [['!pixelfed']],
|
'@akkoma/local-visibility-post': containAkkoma,
|
||||||
'@mastodon/post-bookmark': [['!pixelfed']],
|
|
||||||
'@mastodon/post-edit': [['!pixelfed']],
|
|
||||||
'@mastodon/profile-edit': [['!pixelfed']],
|
|
||||||
'@mastodon/profile-private-note': [['!pixelfed']],
|
|
||||||
'@pixelfed/trending': [['pixelfed']],
|
|
||||||
'@pixelfed/home-include-reblogs': [['pixelfed']],
|
|
||||||
'@pixelfed/global-feed': [['pixelfed']],
|
|
||||||
'@pleroma/local-visibility-post': [['pleroma']],
|
|
||||||
'@akkoma/local-visibility-post': [['akkoma']],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const supportsCache = {};
|
const supportsCache = {};
|
||||||
|
|
||||||
function supports(feature) {
|
function supports(feature) {
|
||||||
const specs = platformFeatures[feature];
|
|
||||||
if (!specs) return false;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let { version, domain, nodeInfo } = getCurrentInstance();
|
let { version, domain, software_name } = getCurrentInstance();
|
||||||
|
|
||||||
const key = `${domain}-${feature}`;
|
const key = `${domain}-${feature}`;
|
||||||
if (supportsCache[key]) return supportsCache[key];
|
if (supportsCache[key]) return supportsCache[key];
|
||||||
|
|
||||||
let software = 'mastodon';
|
if (platformFeatures[feature]) {
|
||||||
if (
|
return (supportsCache[key] = platformFeatures[feature].test(version));
|
||||||
nodeInfo && nodeInfo.software && typeof nodeInfo.software.version === 'string'
|
|
||||||
&& typeof nodeInfo.software.name === 'string'
|
|
||||||
) {
|
|
||||||
software = nodeInfo.software.name.toLowerCase();
|
|
||||||
version = nodeInfo.software.version;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const isSupported = specs.some((spec) => versionSatisfies(software, version, spec));
|
const range = features[feature];
|
||||||
return (supportsCache[key] = isSupported);
|
if (!range) return false;
|
||||||
|
return (supportsCache[key] = (
|
||||||
|
containGTS.test(feature) === containGTS.test(software_name)
|
||||||
|
&& satisfies(version, range, {
|
||||||
|
includePrerelease: true,
|
||||||
|
loose: true,
|
||||||
|
})
|
||||||
|
));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function versionSatisfies(software, version, [softwareSpec, versionSpec]) {
|
|
||||||
let softwareMatches;
|
|
||||||
|
|
||||||
// Inverted spec, like !pixelfed
|
|
||||||
if (softwareSpec.startsWith('!')) {
|
|
||||||
softwareMatches = software !== softwareSpec.slice(1);
|
|
||||||
} else {
|
|
||||||
softwareMatches = (
|
|
||||||
software === softwareSpec || (
|
|
||||||
// Hometown inherits Mastodon features
|
|
||||||
software === 'hometown' && softwareSpec === 'mastodon'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return softwareMatches && (
|
|
||||||
versionSpec == null || satisfies(version, versionSpec, {
|
|
||||||
includePrerelease: true,
|
|
||||||
loose: true,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default supports;
|
export default supports;
|
||||||
|
|
Loading…
Add table
Reference in a new issue