-
Notifications
You must be signed in to change notification settings - Fork 37
feat(web-next): implement remote follow UIRemote follow #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e5c65d5
feat(graphql): add lookupWebFinger query for remote follow
dodok8 e68ab2d
feat(web-next): add ViewerContext for auth state
dodok8 1af4801
feat(web-next): add RemoteFollowButton and onFollowed callback
dodok8 f0d5098
feat(web-next): add authorize_interaction page
dodok8 fdd9f70
feat(i18n): add translations for remote follow UI
dodok8 f747249
fix: apply codereview
dodok8 cec4eae
format: authorize-interaction
dodok8 d798b88
fix: add missing generated file
dodok8 2a99d49
Change Regex pattern to match client
dodok8 abccda9
Add protocol validation before open
dodok8 a2bb9bf
fix: add per-emoji try/catch in tag extraction loop
dodok8 c3f4f0f
Split authorize_interaction into two components to avoid unnecessary …
dodok8 e9b6c7f
Add isLoaded to ViewerContext to prevent FollowButton flash
dodok8 2bee533
Use named ViewerProviderProps interface
dodok8 380a354
Use URL scalar for WebFingerResult and add explicit return types
dodok8 33b0093
Remove extra text from zh-TW follow translation
dodok8 b942ddd
Rename lookupWebFinger query to lookupRemoteFollower with stronger types
dodok8 3f85acc
Tighten ActivityPub link matching in WebFinger lookup
dodok8 20f4608
Add acct: prefix strip
dodok8 460bb6d
Update translation source references after code changes
dodok8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| import { getNodeInfo } from "@fedify/fedify"; | ||
| import * as vocab from "@fedify/vocab"; | ||
| import { type Actor, isActor } from "@fedify/vocab"; | ||
| import { getLogger } from "@logtape/logtape"; | ||
| import { builder, type UserContext } from "./builder.ts"; | ||
|
|
||
| const logger = getLogger(["hackerspub", "graphql", "webfinger"]); | ||
|
|
||
| const FEDIVERSE_ID_REGEX = /^@?([^@]+)@([^@]+)$/; | ||
|
|
||
| interface WebfingerLink { | ||
| rel?: string; | ||
| type?: string; | ||
| href?: string; | ||
| template?: string; | ||
| } | ||
|
|
||
| const WebFingerResult = builder.simpleObject("WebFingerResult", { | ||
| fields: (t) => ({ | ||
| preferredUsername: t.string({ nullable: true }), | ||
| name: t.string({ nullable: true }), | ||
| summary: t.string({ nullable: true }), | ||
| url: t.string({ nullable: true }), | ||
| iconUrl: t.string({ nullable: true }), | ||
| handle: t.string({ nullable: true }), | ||
| domain: t.string({ nullable: true }), | ||
| software: t.string({ nullable: true }), | ||
| emojis: t.field({ type: "JSON", nullable: true }), | ||
| remoteFollowUrl: t.string({ nullable: true }), | ||
| }), | ||
| }); | ||
|
|
||
| async function buildWebFingerResult( | ||
| actorObject: Actor, | ||
| normalizedId: string, | ||
| domain: string, | ||
| remoteFollowUrl?: string, | ||
| ): Promise<{ | ||
| preferredUsername: string | null; | ||
| name: string | null; | ||
| summary: string | null; | ||
| url: string | null; | ||
| iconUrl: string | null; | ||
| handle: string | null; | ||
| domain: string | null; | ||
| software: string | null; | ||
| emojis: Record<string, string> | null; | ||
| remoteFollowUrl: string | null; | ||
| }> { | ||
| let software = "unknown"; | ||
| try { | ||
| const nodeInfo = await getNodeInfo(`https://${domain}`); | ||
| if (nodeInfo?.software?.name) { | ||
| software = nodeInfo.software.name.toLowerCase(); | ||
| } | ||
| } catch (error) { | ||
| logger.warn("Failed to get nodeinfo for {domain}: {error}", { | ||
| domain, | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| } | ||
|
|
||
| let iconUrl: string | null = null; | ||
| const icon = await actorObject.getIcon(); | ||
| if (icon) { | ||
| iconUrl = icon.url instanceof URL | ||
| ? icon.url.href | ||
| : icon.url?.href?.href ?? null; | ||
| } | ||
| if (!iconUrl && actorObject.iconId) { | ||
| iconUrl = actorObject.iconId.href; | ||
| } | ||
|
|
||
| const emojis: Record<string, string> = {}; | ||
| try { | ||
| for await (const tag of actorObject.getTags()) { | ||
| if (tag instanceof vocab.Emoji) { | ||
| if (tag.name == null) continue; | ||
| const emojiIcon = await tag.getIcon(); | ||
| if ( | ||
| emojiIcon?.url == null || | ||
| emojiIcon.url instanceof vocab.Link && emojiIcon.url.href == null | ||
| ) { | ||
| continue; | ||
| } | ||
| const emojiName = tag.name.toString(); | ||
| const raw = emojiIcon.url instanceof vocab.Link | ||
| ? emojiIcon.url.href!.href | ||
| : emojiIcon.url.href; | ||
| const u = new URL(raw); | ||
| if ( | ||
| (u.protocol === "http:" || u.protocol === "https:") && | ||
| !/[\'\"]/.test(raw) | ||
| ) { | ||
| emojis[emojiName] = u.href; | ||
| } | ||
| } | ||
| } | ||
| } catch (error) { | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.warn("Failed to extract custom emojis: {error}", { | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| preferredUsername: actorObject.preferredUsername?.toString() ?? null, | ||
| name: actorObject.name?.toString() ?? null, | ||
| summary: actorObject.summary?.toString() ?? null, | ||
| url: actorObject.url instanceof URL | ||
| ? actorObject.url.href | ||
| : actorObject.url?.toString() ?? null, | ||
| iconUrl, | ||
| handle: normalizedId, | ||
| domain, | ||
| software, | ||
| emojis: Object.keys(emojis).length > 0 ? emojis : null, | ||
| remoteFollowUrl: remoteFollowUrl ?? null, | ||
| }; | ||
| } | ||
|
|
||
| async function lookupWebFingerImpl( | ||
| ctx: UserContext, | ||
| fediverseId: string, | ||
| actorHandle: string, | ||
| ) { | ||
| const match = fediverseId.trim().match(FEDIVERSE_ID_REGEX); | ||
| if (!match) return null; | ||
|
|
||
| const [, username, domain] = match; | ||
| const normalizedId = `${username}@${domain}`; | ||
|
|
||
| logger.info("Looking up WebFinger for {fediverseId}", { | ||
| fediverseId: normalizedId, | ||
| }); | ||
|
|
||
| const webfingerResult = await ctx.fedCtx.lookupWebFinger( | ||
| `acct:${normalizedId}`, | ||
| ); | ||
| if (webfingerResult == null) return null; | ||
|
|
||
| const activityPubLink = webfingerResult.links?.find((link) => | ||
| link.type === "application/activity+json" || | ||
| (link.rel === "self" && link.type?.includes("activity")) | ||
dodok8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) as WebfingerLink | undefined; | ||
|
|
||
| if (!activityPubLink?.href) return null; | ||
|
|
||
| const remoteFollowLink = webfingerResult.links?.find((link) => | ||
| link.rel === "http://ostatus.org/schema/1.0/subscribe" | ||
| ) as WebfingerLink | undefined; | ||
|
|
||
| const remoteFollowUrl = remoteFollowLink?.template?.replace( | ||
| "{uri}", | ||
| encodeURIComponent(actorHandle), | ||
| ); | ||
dodok8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| const documentLoader = ctx.account == null | ||
| ? ctx.fedCtx.documentLoader | ||
| : await ctx.fedCtx.getDocumentLoader({ identifier: ctx.account.id }); | ||
|
|
||
| const actorObject = await ctx.fedCtx.lookupObject(activityPubLink.href, { | ||
| documentLoader, | ||
| }); | ||
|
|
||
| if (!isActor(actorObject)) { | ||
| throw new Error("Object is not an actor"); | ||
| } | ||
|
|
||
| return await buildWebFingerResult( | ||
| actorObject, | ||
| normalizedId, | ||
| domain, | ||
| remoteFollowUrl, | ||
| ); | ||
| } catch (error) { | ||
| logger.warn( | ||
| "ActivityPub lookup failed, using fallback: {error}", | ||
| { | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }, | ||
| ); | ||
|
|
||
| return { | ||
| preferredUsername: username, | ||
| name: username, | ||
| summary: null, | ||
| url: activityPubLink.href, | ||
| iconUrl: null, | ||
| handle: normalizedId, | ||
| domain, | ||
| software: "unknown", | ||
| emojis: null, | ||
| remoteFollowUrl: remoteFollowUrl ?? null, | ||
dodok8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| } | ||
|
|
||
| builder.queryFields((t) => ({ | ||
| lookupWebFinger: t.field({ | ||
| type: WebFingerResult, | ||
| nullable: true, | ||
| args: { | ||
| fediverseId: t.arg.string({ required: true }), | ||
| actorHandle: t.arg.string({ required: true }), | ||
| }, | ||
| async resolve(_, args, ctx) { | ||
| try { | ||
| return await lookupWebFingerImpl( | ||
dahlia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ctx, | ||
| args.fediverseId, | ||
| args.actorHandle, | ||
| ); | ||
| } catch (error) { | ||
| logger.error("WebFinger lookup error: {error}", { | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| return null; | ||
| } | ||
| }, | ||
| }), | ||
| })); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.