Add lookup Mastodon API route

This commit is contained in:
Jesse Wierzbinski 2024-04-09 20:09:57 -10:00
parent 06f98c36bb
commit 59cd8c9bd1
No known key found for this signature in database
2 changed files with 60 additions and 0 deletions

View file

@ -59,6 +59,7 @@ export const rawRoutes = {
"./server/api/api/v1/accounts/[id]/unfollow", "./server/api/api/v1/accounts/[id]/unfollow",
"/api/v1/accounts/[id]/unmute": "./server/api/api/v1/accounts/[id]/unmute", "/api/v1/accounts/[id]/unmute": "./server/api/api/v1/accounts/[id]/unmute",
"/api/v1/accounts/[id]/unpin": "./server/api/api/v1/accounts/[id]/unpin", "/api/v1/accounts/[id]/unpin": "./server/api/api/v1/accounts/[id]/unpin",
"/api/v1/accounts/lookup": "./server/api/api/v1/accounts/lookup/index",
"/api/v1/follow_requests/[account_id]/authorize": "/api/v1/follow_requests/[account_id]/authorize":
"./server/api/api/v1/follow_requests/[account_id]/authorize", "./server/api/api/v1/follow_requests/[account_id]/authorize",
"/api/v1/follow_requests/[account_id]/reject": "/api/v1/follow_requests/[account_id]/reject":

View file

@ -0,0 +1,59 @@
import { apiRoute, applyConfig } from "@api";
import { errorResponse, jsonResponse } from "@response";
import { client } from "~database/datasource";
import type { UserWithRelations } from "~database/entities/User";
import { resolveWebFinger, userToAPI } from "~database/entities/User";
import { userRelations } from "~database/entities/relations";
export const meta = applyConfig({
allowedMethods: ["GET"],
ratelimits: {
max: 30,
duration: 60,
},
route: "/api/v1/accounts/lookup",
auth: {
required: false,
oauthPermissions: [],
},
});
export default apiRoute<{
acct: string;
}>(async (req, matchedRoute, extraData) => {
const { acct } = extraData.parsedRequest;
if (!acct) {
return errorResponse("Invalid acct parameter", 400);
}
// Check if acct is matching format username@domain.com or @username@domain.com
const accountMatches = acct
?.trim()
.match(/@?[a-zA-Z0-9_]+(@[a-zA-Z0-9_.:]+)/g);
if (accountMatches) {
// Remove leading @ if it exists
if (accountMatches[0].startsWith("@")) {
accountMatches[0] = accountMatches[0].slice(1);
}
const [username, domain] = accountMatches[0].split("@");
const foundAccount = await resolveWebFinger(username, domain).catch(
(e) => {
console.error(e);
return null;
},
);
if (foundAccount) {
return jsonResponse(userToAPI(foundAccount));
}
return errorResponse("Account not found", 404);
}
return errorResponse(
"Acct parameter is not of format username@domain.com or @username@domain.com",
400,
);
});