make webfinger server stuff more readable

This commit is contained in:
Johann150 2023-12-16 09:59:51 +01:00
parent bed6a1e2d8
commit c67ff44207
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -80,26 +80,33 @@ router.get('/.well-known/oauth-authorization-server', oauth);
router.get('/.well-known/openid-configuration', oauth);
router.get(webFingerPath, async ctx => {
const fromId = (id: User['id']): FindOptionsWhere<User> => ({
id,
host: IsNull(),
isSuspended: false,
});
const fromAcct = (acct_str: string): FindOptionsWhere<User> | number => {
const acct = Acct.parse(acct_str);
if (!acct.host || acct.host === config.host.toLowerCase()) {
return {
usernameLower: acct.username,
host: IsNull(),
isSuspended: false,
};
} else {
return 422;
}
};
const generateQuery = (resource: string): FindOptionsWhere<User> | number =>
resource.startsWith(`${config.url.toLowerCase()}/users/`) ?
fromId(resource.split('/').pop()!) :
fromAcct(Acct.parse(
resource.startsWith(`${config.url.toLowerCase()}/@`) ? resource.split('/').pop()! :
resource.startsWith('acct:') ? resource.slice('acct:'.length) :
resource));
const fromAcct = (acct: Acct.Acct): FindOptionsWhere<User> | number =>
!acct.host || acct.host === config.host.toLowerCase() ? {
usernameLower: acct.username,
host: IsNull(),
isSuspended: false,
} : 422;
const generateQuery = (resource: string): FindOptionsWhere<User> | number => {
if (resource.startsWith(`${config.url.toLowerCase()}/users/`)) {
return {
id: resource.split('/').pop()!,
host: IsNull(),
isSuspended: false,
};
} else if (resource.startsWith(`${config.url.toLowerCase()}/@`)) {
return fromAcct(resource.split('/').pop()!);
} else if (resource.startsWith("acct:")) {
return fromAcct(resource.slice('acct:'.length));
}
return fromAcct(resource);
};
let resource = ctx.query.resource;