|
|
|
@ -4,7 +4,6 @@ import fetch from 'node-fetch';
|
|
|
|
|
import tinycolor from 'tinycolor2';
|
|
|
|
|
import { DAY } from '@/const.js';
|
|
|
|
|
import { getJson, getHtml, getAgentByUrl } from '@/misc/fetch.js';
|
|
|
|
|
import { Instance } from '@/models/entities/instance.js';
|
|
|
|
|
import { Instances } from '@/models/index.js';
|
|
|
|
|
import { getFetchInstanceMetadataLock } from '@/misc/app-lock.js';
|
|
|
|
|
import { createUpdateInstanceJob } from '@/queue/index.js';
|
|
|
|
@ -12,16 +11,16 @@ import Logger from './logger.js';
|
|
|
|
|
|
|
|
|
|
const logger = new Logger('metadata');
|
|
|
|
|
|
|
|
|
|
export async function fetchInstanceMetadata(instance: Instance, force = false): Promise<void> {
|
|
|
|
|
const unlock = await getFetchInstanceMetadataLock(instance.host);
|
|
|
|
|
export async function fetchInstanceMetadata(instance_host: string, force = false): Promise<void> {
|
|
|
|
|
const unlock = await getFetchInstanceMetadataLock(instance_host);
|
|
|
|
|
|
|
|
|
|
const instance = await Instances.findOneBy({ host: instance_host });
|
|
|
|
|
if (!force) {
|
|
|
|
|
const _instance = await Instances.findOneBy({ host: instance.host });
|
|
|
|
|
if (_instance && _instance.infoUpdatedAt) {
|
|
|
|
|
const threshold = Date.now() - _instance.infoUpdatedAt.getTime();
|
|
|
|
|
if (instance && !instance.isNotResponding && instance.infoUpdatedAt) {
|
|
|
|
|
const threshold = Date.now() - instance.infoUpdatedAt.getTime();
|
|
|
|
|
if (threshold > DAY) {
|
|
|
|
|
// queue a job to deal with it later
|
|
|
|
|
createUpdateInstanceJob(instance.host);
|
|
|
|
|
createUpdateInstanceJob(instance_host);
|
|
|
|
|
}
|
|
|
|
|
// if the instance is already known and force is not specified,
|
|
|
|
|
// never update the data in the same thread
|
|
|
|
@ -30,24 +29,24 @@ export async function fetchInstanceMetadata(instance: Instance, force = false):
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info(`Fetching metadata of ${instance.host} ...`);
|
|
|
|
|
logger.info(`Fetching metadata of ${instance_host} ...`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const [info, dom, manifest] = await Promise.all([
|
|
|
|
|
fetchNodeinfo(instance).catch(() => null),
|
|
|
|
|
fetchDom(instance).catch(() => null),
|
|
|
|
|
fetchManifest(instance).catch(() => null),
|
|
|
|
|
fetchNodeinfo(instance_host).catch(() => null),
|
|
|
|
|
fetchDom(instance_host).catch(() => null),
|
|
|
|
|
fetchManifest(instance_host).catch(() => null),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const [favicon, icon, themeColor, name, description] = await Promise.all([
|
|
|
|
|
fetchFaviconUrl(instance, dom).catch(() => null),
|
|
|
|
|
fetchIconUrl(instance, dom, manifest).catch(() => null),
|
|
|
|
|
fetchFaviconUrl(instance_host, dom).catch(() => null),
|
|
|
|
|
fetchIconUrl(instance_host, dom, manifest).catch(() => null),
|
|
|
|
|
getThemeColor(info, dom, manifest).catch(() => null),
|
|
|
|
|
getSiteName(info, dom, manifest).catch(() => null),
|
|
|
|
|
getDescription(info, dom, manifest).catch(() => null),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
logger.succ(`Successfuly fetched metadata of ${instance.host}`);
|
|
|
|
|
logger.succ(`Successfuly fetched metadata of ${instance_host}`);
|
|
|
|
|
|
|
|
|
|
const updates = {
|
|
|
|
|
infoUpdatedAt: new Date(),
|
|
|
|
@ -69,9 +68,9 @@ export async function fetchInstanceMetadata(instance: Instance, force = false):
|
|
|
|
|
|
|
|
|
|
await Instances.update(instance.id, updates);
|
|
|
|
|
|
|
|
|
|
logger.succ(`Successfuly updated metadata of ${instance.host}`);
|
|
|
|
|
logger.succ(`Successfuly updated metadata of ${instance_host}`);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error(`Failed to update metadata of ${instance.host}: ${e}`);
|
|
|
|
|
logger.error(`Failed to update metadata of ${instance_host}: ${e}`);
|
|
|
|
|
} finally {
|
|
|
|
|
unlock();
|
|
|
|
|
}
|
|
|
|
@ -96,11 +95,11 @@ type NodeInfo = {
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function fetchNodeinfo(instance: Instance): Promise<NodeInfo> {
|
|
|
|
|
logger.info(`Fetching nodeinfo of ${instance.host} ...`);
|
|
|
|
|
async function fetchNodeinfo(instance_host: string): Promise<NodeInfo> {
|
|
|
|
|
logger.info(`Fetching nodeinfo of ${instance_host} ...`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const wellknown = await getJson('https://' + instance.host + '/.well-known/nodeinfo')
|
|
|
|
|
const wellknown = await getJson('https://' + instance_host + '/.well-known/nodeinfo')
|
|
|
|
|
.catch(e => {
|
|
|
|
|
if (e.statusCode === 404) {
|
|
|
|
|
throw new Error('No nodeinfo provided');
|
|
|
|
@ -129,21 +128,21 @@ async function fetchNodeinfo(instance: Instance): Promise<NodeInfo> {
|
|
|
|
|
throw new Error(e.statusCode || e.message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
logger.succ(`Successfuly fetched nodeinfo of ${instance.host}`);
|
|
|
|
|
logger.succ(`Successfuly fetched nodeinfo of ${instance_host}`);
|
|
|
|
|
|
|
|
|
|
return info as NodeInfo;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
const message = e instanceof Error ? e.message : e;
|
|
|
|
|
logger.error(`Failed to fetch nodeinfo of ${instance.host}: ${message}`);
|
|
|
|
|
logger.error(`Failed to fetch nodeinfo of ${instance_host}: ${message}`);
|
|
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchDom(instance: Instance): Promise<DOMWindow['document']> {
|
|
|
|
|
logger.info(`Fetching HTML of ${instance.host} ...`);
|
|
|
|
|
async function fetchDom(instance_host: string): Promise<DOMWindow['document']> {
|
|
|
|
|
logger.info(`Fetching HTML of ${instance_host} ...`);
|
|
|
|
|
|
|
|
|
|
const url = 'https://' + instance.host;
|
|
|
|
|
const url = 'https://' + instance_host;
|
|
|
|
|
|
|
|
|
|
const html = await getHtml(url);
|
|
|
|
|
|
|
|
|
@ -153,8 +152,8 @@ async function fetchDom(instance: Instance): Promise<DOMWindow['document']> {
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchManifest(instance: Instance): Promise<Record<string, unknown> | null> {
|
|
|
|
|
const url = 'https://' + instance.host;
|
|
|
|
|
async function fetchManifest(instance_host: string): Promise<Record<string, unknown> | null> {
|
|
|
|
|
const url = 'https://' + instance_host;
|
|
|
|
|
|
|
|
|
|
const manifestUrl = url + '/manifest.json';
|
|
|
|
|
|
|
|
|
@ -163,8 +162,8 @@ async function fetchManifest(instance: Instance): Promise<Record<string, unknown
|
|
|
|
|
return manifest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchFaviconUrl(instance: Instance, doc: DOMWindow['document'] | null): Promise<string | null> {
|
|
|
|
|
const url = 'https://' + instance.host;
|
|
|
|
|
async function fetchFaviconUrl(instance_host: string, doc: DOMWindow['document'] | null): Promise<string | null> {
|
|
|
|
|
const url = 'https://' + instance_host;
|
|
|
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
|
// https://github.com/misskey-dev/misskey/pull/8220#issuecomment-1025104043
|
|
|
|
@ -190,19 +189,19 @@ async function fetchFaviconUrl(instance: Instance, doc: DOMWindow['document'] |
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchIconUrl(instance: Instance, doc: DOMWindow['document'] | null, manifest: Record<string, any> | null): Promise<string | null> {
|
|
|
|
|
async function fetchIconUrl(instance_host: string, doc: DOMWindow['document'] | null, manifest: Record<string, any> | null): Promise<string | null> {
|
|
|
|
|
if (manifest && manifest.icons && manifest.icons.length > 0 && manifest.icons[0].src) {
|
|
|
|
|
const url = 'https://' + instance.host;
|
|
|
|
|
const url = 'https://' + instance_host;
|
|
|
|
|
return (new URL(manifest.icons[0].src, url)).href;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
|
const url = 'https://' + instance.host;
|
|
|
|
|
const url = 'https://' + instance_host;
|
|
|
|
|
|
|
|
|
|
// https://github.com/misskey-dev/misskey/pull/8220#issuecomment-1025104043
|
|
|
|
|
const links = Array.from(doc.getElementsByTagName('link')).reverse();
|
|
|
|
|
// https://github.com/misskey-dev/misskey/pull/8220/files/0ec4eba22a914e31b86874f12448f88b3e58dd5a#r796487559
|
|
|
|
|
const href =
|
|
|
|
|
const href =
|
|
|
|
|
[
|
|
|
|
|
links.find(link => link.relList.contains('apple-touch-icon-precomposed'))?.href,
|
|
|
|
|
links.find(link => link.relList.contains('apple-touch-icon'))?.href,
|
|
|
|
|