Fix updating remote user host for remote AP implementations that don't support webfinger uri queries

This commit is contained in:
Laura Hausmann 2023-09-10 19:22:45 +02:00
parent 558b58a0cd
commit 06be4e108b
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
3 changed files with 26 additions and 4 deletions

View File

@ -48,7 +48,7 @@ import Resolver from "../resolver.js";
import { extractApHashtags } from "./tag.js"; import { extractApHashtags } from "./tag.js";
import { resolveNote, extractEmojis } from "./note.js"; import { resolveNote, extractEmojis } from "./note.js";
import { resolveImage } from "./image.js"; import { resolveImage } from "./image.js";
import { getSubjectHostFromUri } from "@/remote/resolve-user.js" import { getSubjectHostFromUri, getSubjectHostFromRemoteUser } from "@/remote/resolve-user.js"
const logger = apLogger; const logger = apLogger;
@ -425,11 +425,13 @@ export async function createPerson(
* @param uri URI of Person * @param uri URI of Person
* @param resolver Resolver * @param resolver Resolver
* @param hint Hint of Person object (If this value is a valid Person, it is used for updating without Remote resolve) * @param hint Hint of Person object (If this value is a valid Person, it is used for updating without Remote resolve)
* @param userHint Hint of IRemoteUser object, used for updating user information for remotes that only support webfinger with acct: query
*/ */
export async function updatePerson( export async function updatePerson(
uri: string, uri: string,
resolver?: Resolver | null, resolver?: Resolver | null,
hint?: IObject, hint?: IObject,
userHint?: IRemoteUser,
): Promise<void> { ): Promise<void> {
if (typeof uri !== "string") throw new Error("uri is not string"); if (typeof uri !== "string") throw new Error("uri is not string");
@ -452,10 +454,10 @@ export async function updatePerson(
const person = validateActor(object, uri); const person = validateActor(object, uri);
const host = await getSubjectHostFromUri(uri);
logger.info(`Updating the Person: ${person.id}`); logger.info(`Updating the Person: ${person.id}`);
const host = await getSubjectHostFromUri(uri) ?? await getSubjectHostFromRemoteUser(userHint);
// Fetch avatar and header image // Fetch avatar and header image
const [avatar, banner] = await Promise.all( const [avatar, banner] = await Promise.all(
[person.icon, person.image].map((img) => [person.icon, person.image].map((img) =>

View File

@ -187,6 +187,26 @@ export async function getSubjectHostFromUri(uri: string): Promise<string | null>
} }
} }
export async function getSubjectHostFromAcct(acct: string): Promise<string | null> {
try {
const res = await resolveUserWebFinger(acct.toLowerCase());
const finalAcct = subjectToAcct(res.subject);
const m = finalAcct.match(/^([^@]+)@(.*)/);
if (!m) {
return null;
}
return m[2];
}
catch {
return null;
}
}
export async function getSubjectHostFromRemoteUser(user: IRemoteUser | undefined): Promise<string | null> {
return user ? getSubjectHostFromAcct(`${user.username}@${user.host}`) : null;
}
async function resolveUserWebFinger(acctLower: string, recurse: boolean = true): Promise<{ async function resolveUserWebFinger(acctLower: string, recurse: boolean = true): Promise<{
subject: string, subject: string,
self: { self: {

View File

@ -18,5 +18,5 @@ export const paramDef = {
export default define(meta, paramDef, async (ps) => { export default define(meta, paramDef, async (ps) => {
const user = await getRemoteUser(ps.userId); const user = await getRemoteUser(ps.userId);
await updatePerson(user.uri!); await updatePerson(user.uri!, undefined, undefined, user);
}); });