server: refactor resolveSelf to just return the webfinger href

Since the href seems to be the only attribute that is used, and I didn't
want to add a full type definition this was the easier option.
This commit is contained in:
Johann150 2022-12-03 23:45:10 +01:00
parent b2fb4bffca
commit 77bfe74645
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -47,7 +47,7 @@ export async function resolveUser(username: string, idnHost: string | null): Pro
const self = await resolveSelf(acctLower); const self = await resolveSelf(acctLower);
logger.succ(`return new remote user: ${chalk.magenta(acctLower)}`); logger.succ(`return new remote user: ${chalk.magenta(acctLower)}`);
return await createPerson(self.href); return await createPerson(self);
} }
// If user information is out of date, start over with webfinger // If user information is out of date, start over with webfinger
@ -60,13 +60,13 @@ export async function resolveUser(username: string, idnHost: string | null): Pro
logger.info(`try resync: ${acctLower}`); logger.info(`try resync: ${acctLower}`);
const self = await resolveSelf(acctLower); const self = await resolveSelf(acctLower);
if (user.uri !== self.href) { if (user.uri !== self) {
// if uri mismatch, Fix (user@host <=> AP's Person id(IRemoteUser.uri)) mapping. // if uri mismatch, Fix (user@host <=> AP's Person id(IRemoteUser.uri)) mapping.
logger.info(`uri missmatch: ${acctLower}`); logger.info(`uri missmatch: ${acctLower}`);
logger.info(`recovery missmatch uri for (username=${username}, host=${host}) from ${user.uri} to ${self.href}`); logger.info(`recovery missmatch uri for (username=${username}, host=${host}) from ${user.uri} to ${self}`);
// validate uri // validate uri
const uri = new URL(self.href); const uri = new URL(self);
if (uri.hostname !== host) { if (uri.hostname !== host) {
throw new Error('Invalid uri'); throw new Error('Invalid uri');
} }
@ -75,16 +75,16 @@ export async function resolveUser(username: string, idnHost: string | null): Pro
usernameLower, usernameLower,
host, host,
}, { }, {
uri: self.href, uri: self,
}); });
} else { } else {
logger.info(`uri is fine: ${acctLower}`); logger.info(`uri is fine: ${acctLower}`);
} }
await updatePerson(self.href); await updatePerson(self);
logger.info(`return resynced remote user: ${acctLower}`); logger.info(`return resynced remote user: ${acctLower}`);
return await Users.findOneBy({ uri: self.href }).then(u => { return await Users.findOneBy({ uri: self }).then(u => {
if (u == null) { if (u == null) {
throw new Error('user not found'); throw new Error('user not found');
} else { } else {
@ -97,16 +97,21 @@ export async function resolveUser(username: string, idnHost: string | null): Pro
return user; return user;
} }
async function resolveSelf(acctLower: string) { /**
* Gets the Webfinger href matching rel="self".
*/
async function resolveSelf(acctLower: string): string {
logger.info(`WebFinger for ${chalk.yellow(acctLower)}`); logger.info(`WebFinger for ${chalk.yellow(acctLower)}`);
// get webfinger response for user
const finger = await webFinger(acctLower).catch(e => { const finger = await webFinger(acctLower).catch(e => {
logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: ${ e.statusCode || e.message }`); logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: ${ e.statusCode || e.message }`);
throw new Error(`Failed to WebFinger for ${acctLower}: ${ e.statusCode || e.message }`); throw new Error(`Failed to WebFinger for ${acctLower}: ${ e.statusCode || e.message }`);
}); });
const self = finger.links.find(link => link.rel != null && link.rel.toLowerCase() === 'self'); // try to find the rel="self" link
if (!self) { const self = finger.links.find(link => link.rel?.toLowerCase() === 'self');
if (!self?.href) {
logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: self link not found`); logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: self link not found`);
throw new Error('self link not found'); throw new Error('self link not found');
} }
return self; return self.href;
} }