mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
Add lookup Mastodon API route
This commit is contained in:
parent
06f98c36bb
commit
59cd8c9bd1
|
|
@ -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":
|
||||||
|
|
|
||||||
59
server/api/api/v1/accounts/lookup/index.ts
Normal file
59
server/api/api/v1/accounts/lookup/index.ts
Normal 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,
|
||||||
|
);
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue