Skip to content

Commit

Permalink
Store nodeInfo separately + other feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
graue committed Oct 11, 2024
1 parent ad0ab0c commit 22f0703
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
40 changes: 22 additions & 18 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export async function initInstance(client, instance) {
}
__BENCHMARK.end('fetch-instance');
if (!info) return;
console.log(info);
const {
// v1
uri,
Expand All @@ -89,6 +90,21 @@ export async function initInstance(client, instance) {
configuration: { urls: { streaming } = {} } = {},
} = info;

const instances = store.local.getJSON('instances') || {};
if (uri || domain) {
instances[
(domain || uri)
.replace(/^https?:\/\//, '')
.replace(/\/+$/, '')
.toLowerCase()
] = info;
}
if (instance) {
instances[instance.toLowerCase()] = info;
}
store.local.setJSON('instances', instances);

let nodeInfo;
// GoToSocial requires we get the NodeInfo to identify server type
// spec: https://github.com/jhass/nodeinfo
try {
Expand All @@ -101,29 +117,17 @@ export async function initInstance(client, instance) {
link.rel.startsWith('http://nodeinfo.diaspora.software/ns/schema/')
)?.href;
if (nodeInfoUrl && nodeInfoUrl.startsWith(urlBase)) {
const nodeInfo = await (await fetch(nodeInfoUrl)).json();
if (typeof nodeInfo?.software?.name === 'string') {
info.software_name = nodeInfo.software.name;
}
nodeInfo = await (await fetch(nodeInfoUrl)).json();
}
}
}
} catch (e) {}
console.log(info);

const instances = store.local.getJSON('instances') || {};
if (uri || domain) {
instances[
(domain || uri)
.replace(/^https?:\/\//, '')
.replace(/\/+$/, '')
.toLowerCase()
] = info;
const nodeInfos = store.local.getJSON('nodeInfos') || {};
if (nodeInfo) {
nodeInfos[instance.toLowerCase()] = nodeInfo;
}
if (instance) {
instances[instance.toLowerCase()] = info;
}
store.local.setJSON('instances', instances);
store.local.setJSON('nodeInfos', nodeInfos);

// This is a weird place to put this but here's updating the masto instance with the streaming API URL set in the configuration
// Reason: Streaming WebSocket URL may change, unlike the standard API REST URLs
const supportsWebSocket = 'WebSocket' in window;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/store-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ export function getCurrentInstance() {
}
}

let currentNodeInfo = null;
export function getCurrentNodeInfo() {
if (currentNodeInfo) return currentNodeInfo;
try {
const account = getCurrentAccount();
const nodeInfos = store.local.getJSON('nodeInfos') || {};
const instanceURL = account.instanceURL.toLowerCase();
return (currentNodeInfo = (nodeInfos[instanceURL] || {}));
} catch (e) {
console.error(e);
return {};
}
}

// Massage these instance configurations to match the Mastodon API
// - Pleroma
function getInstanceConfiguration(instance) {
Expand Down
26 changes: 17 additions & 9 deletions src/utils/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { satisfies } from 'compare-versions';

import features from '../data/features.json';

import { getCurrentInstance } from './store-utils';
import { getCurrentInstance, getCurrentNodeInfo } from './store-utils';

// Non-semver(?) UA string detection
const containPixelfed = /pixelfed/i;
Expand Down Expand Up @@ -31,7 +31,13 @@ const supportsCache = {};

function supports(feature) {
try {
let { version, domain, software_name } = getCurrentInstance();
let { version, domain } = getCurrentInstance();
let softwareName = getCurrentNodeInfo()?.software?.name || 'mastodon';

if (softwareName === 'hometown') {
// Hometown is a Mastodon fork and inherits its features
softwareName = 'mastodon';
}

const key = `${domain}-${feature}`;
if (supportsCache[key]) return supportsCache[key];
Expand All @@ -42,13 +48,15 @@ function supports(feature) {

const range = features[feature];
if (!range) return false;
return (supportsCache[key] = (
containGTS.test(feature) === containGTS.test(software_name)
&& satisfies(version, range, {
includePrerelease: true,
loose: true,
})
));

// '@mastodon/blah' => 'mastodon'
const featureSoftware = feature.match(/^@([a-z]+)\//)[1];

const doesSoftwareMatch = featureSoftware === softwareName.toLowerCase();
return (supportsCache[key] = doesSoftwareMatch && satisfies(version, range, {
includePrerelease: true,
loose: true,
}));
} catch (e) {
return false;
}
Expand Down

0 comments on commit 22f0703

Please sign in to comment.