From eb350e8d6cd87cbc8d8575043517a58cd66b8748 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 27 Oct 2020 16:44:54 +0900 Subject: [PATCH] Better favicon detection --- src/services/fetch-instance-metadata.ts | 39 +++++++++++++++---------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/services/fetch-instance-metadata.ts b/src/services/fetch-instance-metadata.ts index 487421816..c1caeef6e 100644 --- a/src/services/fetch-instance-metadata.ts +++ b/src/services/fetch-instance-metadata.ts @@ -9,14 +9,16 @@ import { URL } from 'url'; const logger = new Logger('metadata', 'cyan'); -export async function fetchInstanceMetadata(instance: Instance): Promise { +export async function fetchInstanceMetadata(instance: Instance, force = false): Promise { const unlock = await getFetchInstanceMetadataLock(instance.host); - const _instance = await Instances.findOne({ host: instance.host }); - const now = Date.now(); - if (_instance && _instance.infoUpdatedAt && (now - _instance.infoUpdatedAt.getTime() < 1000 * 60 * 60 * 24)) { - unlock(); - return; + if (!force) { + const _instance = await Instances.findOne({ host: instance.host }); + const now = Date.now(); + if (_instance && _instance.infoUpdatedAt && (now - _instance.infoUpdatedAt.getTime() < 1000 * 60 * 60 * 24)) { + unlock(); + return; + } } logger.info(`Fetching metadata of ${instance.host} ...`); @@ -29,7 +31,7 @@ export async function fetchInstanceMetadata(instance: Instance): Promise { ]); const [favicon, icon, themeColor, name, description] = await Promise.all([ - fetchFaviconUrl(instance).catch(() => null), + fetchFaviconUrl(instance, dom).catch(() => null), fetchIconUrl(instance, dom, manifest).catch(() => null), getThemeColor(dom, manifest).catch(() => null), getSiteName(info, dom, manifest).catch(() => null), @@ -150,10 +152,17 @@ async function fetchManifest(instance: Instance): Promise | return manifest; } -async function fetchFaviconUrl(instance: Instance): Promise { - logger.info(`Fetching favicon URL of ${instance.host} ...`); - +async function fetchFaviconUrl(instance: Instance, doc: DOMWindow['document'] | null): Promise { const url = 'https://' + instance.host; + + if (doc) { + const href = doc.querySelector('link[rel="icon"]')?.getAttribute('href'); + + if (href) { + return (new URL(href, url)).href; + } + } + const faviconUrl = url + '/favicon.ico'; const favicon = await fetch(faviconUrl, { @@ -169,6 +178,11 @@ async function fetchFaviconUrl(instance: Instance): Promise { } async function fetchIconUrl(instance: Instance, doc: DOMWindow['document'] | null, manifest: Record | null): Promise { + if (manifest && manifest.icons && manifest.icons.length > 0 && manifest.icons[0].src) { + const url = 'https://' + instance.host; + return (new URL(manifest.icons[0].src, url)).href; + } + if (doc) { const url = 'https://' + instance.host; @@ -183,11 +197,6 @@ async function fetchIconUrl(instance: Instance, doc: DOMWindow['document'] | nul } } - if (manifest && manifest.icons && manifest.icons.length > 0 && manifest.icons[0].src) { - const url = 'https://' + instance.host; - return (new URL(manifest.icons[0].src, url)).href; - } - return null; }