fedimbed: try catch nodeinfo
This commit is contained in:
		
							parent
							
								
									5fb999ec5b
								
							
						
					
					
						commit
						d39bf5f2a4
					
				
					 1 changed files with 37 additions and 31 deletions
				
			
		| 
						 | 
				
			
			@ -30,12 +30,12 @@ const PATH_REGEX = {
 | 
			
		|||
  lemmy: /^\/post\/\d+\/?/,
 | 
			
		||||
  honk: /^\/u\/(.+?)\/h\/(.+?)\/?/,
 | 
			
		||||
  pixelfed: /^\/p\/(.+?)\/(.+?)\/?/,
 | 
			
		||||
  cohost: /^\/[A-Za-z0-9]+\/post\/\d+-[A-Za-z0-9-]+\/?/,
 | 
			
		||||
  //cohost: /^\/[A-Za-z0-9]+\/post\/\d+-[A-Za-z0-9-]+\/?/,
 | 
			
		||||
  bluesky: BSKY_POST_REGEX,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const PLATFORM_COLORS = {
 | 
			
		||||
  mastodon: 0x2791da,
 | 
			
		||||
  mastodon: 0x6363ff,
 | 
			
		||||
  pleroma: 0xfba457,
 | 
			
		||||
  akkoma: 0x593196,
 | 
			
		||||
  misskey: 0x99c203,
 | 
			
		||||
| 
						 | 
				
			
			@ -46,7 +46,7 @@ const PLATFORM_COLORS = {
 | 
			
		|||
  birdsitelive: 0x1da1f2,
 | 
			
		||||
  iceshrimp: 0x8e82f9, // YCbCr interpolated as the accent color is a gradient
 | 
			
		||||
  pixelfed: 0x10c5f8,
 | 
			
		||||
  cohost: 0x83254f,
 | 
			
		||||
  //cohost: 0x83254f,
 | 
			
		||||
  bluesky: 0x0085ff,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -67,6 +67,7 @@ const BSKY_DOMAINS = [
 | 
			
		|||
/*const TW_DOMAINS = [
 | 
			
		||||
  "tw.c7.pm",
 | 
			
		||||
  "tw.counter-strike.gay",
 | 
			
		||||
  "xcancel.com",
 | 
			
		||||
  "twitter.com",
 | 
			
		||||
  "x.com",
 | 
			
		||||
  "fxtwitter.com",
 | 
			
		||||
| 
						 | 
				
			
			@ -84,36 +85,41 @@ async function resolvePlatform(url) {
 | 
			
		|||
  const urlObj = new URL(url);
 | 
			
		||||
  if (domainCache.has(urlObj.hostname)) return domainCache.get(urlObj.hostname);
 | 
			
		||||
 | 
			
		||||
  const res = await fetch(urlObj.origin + "/.well-known/nodeinfo", {
 | 
			
		||||
    headers: {"User-Agent": FRIENDLY_USERAGENT},
 | 
			
		||||
  }).then((res) => res.text());
 | 
			
		||||
  try {
 | 
			
		||||
    const res = await fetch(urlObj.origin + "/.well-known/nodeinfo", {
 | 
			
		||||
      headers: {"User-Agent": FRIENDLY_USERAGENT},
 | 
			
		||||
    }).then((res) => res.text());
 | 
			
		||||
 | 
			
		||||
  if (!res.startsWith("{")) {
 | 
			
		||||
    logger.error("fedimbed", `No nodeinfo for "${urlObj.hostname}"???`);
 | 
			
		||||
    domainCache.set(urlObj.hostname, null);
 | 
			
		||||
    return null;
 | 
			
		||||
    if (!res.startsWith("{")) {
 | 
			
		||||
      logger.error("fedimbed", `No nodeinfo for "${urlObj.hostname}"???`);
 | 
			
		||||
      domainCache.set(urlObj.hostname, null);
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const probe = JSON.parse(res);
 | 
			
		||||
 | 
			
		||||
    if (!probe?.links) {
 | 
			
		||||
      logger.error("fedimbed", `No nodeinfo for "${urlObj.hostname}"???`);
 | 
			
		||||
      domainCache.set(urlObj.hostname, null);
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const nodeinfo = await fetch(probe.links[probe.links.length - 1].href, {
 | 
			
		||||
      headers: {"User-Agent": FRIENDLY_USERAGENT},
 | 
			
		||||
    }).then((res) => res.json());
 | 
			
		||||
 | 
			
		||||
    if (!nodeinfo?.software?.name) {
 | 
			
		||||
      logger.error("fedimbed", `Got nodeinfo for "${urlObj.hostname}", but missing software name.`);
 | 
			
		||||
      domainCache.set(urlObj.hostname, null);
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    domainCache.set(urlObj.hostname, nodeinfo.software.name);
 | 
			
		||||
    return nodeinfo.software.name;
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    logger.error("fedimbed", `Failed to get nodeinfo for "${url}": ${err}`);
 | 
			
		||||
    return "<nodeinfo failed>";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const probe = JSON.parse(res);
 | 
			
		||||
 | 
			
		||||
  if (!probe?.links) {
 | 
			
		||||
    logger.error("fedimbed", `No nodeinfo for "${urlObj.hostname}"???`);
 | 
			
		||||
    domainCache.set(urlObj.hostname, null);
 | 
			
		||||
    return null;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const nodeinfo = await fetch(probe.links[probe.links.length - 1].href, {
 | 
			
		||||
    headers: {"User-Agent": FRIENDLY_USERAGENT},
 | 
			
		||||
  }).then((res) => res.json());
 | 
			
		||||
 | 
			
		||||
  if (!nodeinfo?.software?.name) {
 | 
			
		||||
    logger.error("fedimbed", `Got nodeinfo for "${urlObj.hostname}", but missing software name.`);
 | 
			
		||||
    domainCache.set(urlObj.hostname, null);
 | 
			
		||||
    return null;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  domainCache.set(urlObj.hostname, nodeinfo.software.name);
 | 
			
		||||
  return nodeinfo.software.name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const keyId = "https://hf.c7.pm/actor#main-key";
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue