Skip to content

Commit

Permalink
Use Fedify's NodeInfo client
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Oct 29, 2024
1 parent 37bd4d8 commit 49a624b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/credential-providers": "^3.577.0",
"@fedify/fedify": "^1.1.2",
"@fedify/fedify": "^1.2.0-dev.467",
"@fedify/markdown-it-hashtag": "0.2.0",
"@fedify/markdown-it-mention": "^0.1.1",
"@fedify/postgres": "0.1.0",
Expand Down
40 changes: 11 additions & 29 deletions src/federation/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
Emoji,
Link,
PropertyValue,
type ResourceDescriptor,
formatSemVer,
getActorHandle,
getActorTypeName,
getNodeInfo,
isActor,
lookupObject,
} from "@fedify/fedify";
Expand Down Expand Up @@ -101,7 +102,9 @@ export async function persistAccount(
emojis[tag.name.toString()] = href;
}
}
const nodeInfo = await getNodeInfo(actor.id);
const nodeInfo = await getNodeInfo(actor.id, {

This comment has been minimized.

Copy link
@ThisIsMissEm

ThisIsMissEm Oct 29, 2024

Contributor

You could quite possibly cache this at domain/server level, and then just link that to the Actor / Account.

This comment has been minimized.

Copy link
@dahlia

dahlia Oct 30, 2024

Author Owner

Fixed in 31df1b4!

parse: "best-effort",
});
const values: Omit<schema.NewAccount, "id" | "iri"> = {
type: getActorTypeName(actor),
name: actor?.name?.toString() ?? actor?.preferredUsername?.toString() ?? "",
Expand All @@ -122,8 +125,12 @@ export async function persistAccount(
postsCount: (await actor.getOutbox(opts))?.totalItems ?? 0,
successorId,
aliases: actor?.aliasIds?.map((alias) => alias.href) ?? [],
software: nodeInfo?.software?.name ?? null,
softwareVersion: nodeInfo?.software?.version ?? null,
software: nodeInfo?.software.name ?? null,
softwareVersion:
nodeInfo?.software == null ||
formatSemVer(nodeInfo.software.version) === "0.0.0"
? null
: formatSemVer(nodeInfo.software.version),
fieldHtmls,
published: toDate(actor.published),
};
Expand Down Expand Up @@ -305,28 +312,3 @@ export async function updateAccountStats(
),
);
}

interface NodeInfo {
software?: {
name?: string;
version?: string;
};
}

// TODO: Turn this logic into a utility function in Fedify:
async function getNodeInfo(url: URL): Promise<NodeInfo | null> {
try {
const wellKnownUrl = new URL("/.well-known/nodeinfo", url);
const wkResponse = await fetch(wellKnownUrl);
const rd: ResourceDescriptor = await wkResponse.json();
const link = rd.links?.find(
(link) => link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0",
);
if (link == null) return null;
const nodeInfoUrl = link.href;
const niResponse = await fetch(nodeInfoUrl);
return await niResponse.json();
} catch {
return null;
}
}

0 comments on commit 49a624b

Please sign in to comment.