mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
refactor: ♻️ Use native Hono return functions instead of custom ones
This commit is contained in:
parent
7e2f333945
commit
866692c1dc
12
app.ts
12
app.ts
|
|
@ -1,4 +1,4 @@
|
|||
import { errorResponse, jsonResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { sentry } from "@/sentry";
|
||||
import { Hono } from "@hono/hono";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
|
|
@ -76,8 +76,10 @@ export const appFactory = async () => {
|
|||
proxy?.headers.set("Cache-Control", "max-age=31536000");
|
||||
|
||||
if (!proxy || proxy.status === 404) {
|
||||
return errorResponse(
|
||||
"Route not found on proxy or API route. Are you using the correct HTTP method?",
|
||||
return context.json(
|
||||
{
|
||||
error: "Route not found on proxy or API route. Are you using the correct HTTP method?",
|
||||
},
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
|
@ -95,11 +97,11 @@ export const appFactory = async () => {
|
|||
return proxy;
|
||||
});
|
||||
|
||||
app.onError((error) => {
|
||||
app.onError((error, c) => {
|
||||
const serverLogger = getLogger("server");
|
||||
serverLogger.error`${error}`;
|
||||
sentry?.captureException(error);
|
||||
return jsonResponse(
|
||||
return c.json(
|
||||
{
|
||||
error: "A server error occured",
|
||||
name: error.name,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { errorResponse } from "@/response";
|
||||
import { createMiddleware } from "@hono/hono/factory";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
|
|
@ -8,7 +7,7 @@ export const agentBans = createMiddleware(async (context, next) => {
|
|||
|
||||
for (const agent of config.http.banned_user_agents) {
|
||||
if (new RegExp(agent).test(ua)) {
|
||||
return errorResponse("Forbidden", 403);
|
||||
return context.json({ error: "Forbidden" }, 403);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { errorResponse } from "@/response";
|
||||
import { createMiddleware } from "@hono/hono/factory";
|
||||
|
||||
export const boundaryCheck = createMiddleware(async (context, next) => {
|
||||
|
|
@ -7,8 +6,10 @@ export const boundaryCheck = createMiddleware(async (context, next) => {
|
|||
|
||||
if (contentType?.includes("multipart/form-data")) {
|
||||
if (!contentType.includes("boundary")) {
|
||||
return errorResponse(
|
||||
"You are sending a request with a multipart/form-data content type but without a boundary. Please include a boundary in the Content-Type header. For more information, visit https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data",
|
||||
return context.json(
|
||||
{
|
||||
error: "You are sending a request with a multipart/form-data content type but without a boundary. Please include a boundary in the Content-Type header. For more information, visit https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data",
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { errorResponse } from "@/response";
|
||||
import { sentry } from "@/sentry";
|
||||
import { createMiddleware } from "@hono/hono/factory";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
|
|
@ -19,7 +18,7 @@ export const ipBans = createMiddleware(async (context, next) => {
|
|||
for (const ip of config.http.banned_ips) {
|
||||
try {
|
||||
if (matches(ip, requestIp?.address)) {
|
||||
return errorResponse("Forbidden", 403);
|
||||
return context.json({ error: "Forbidden" }, 403);
|
||||
}
|
||||
} catch (e) {
|
||||
const logger = getLogger("server");
|
||||
|
|
@ -28,8 +27,8 @@ export const ipBans = createMiddleware(async (context, next) => {
|
|||
logger.error`${e}`;
|
||||
sentry?.captureException(e);
|
||||
|
||||
return errorResponse(
|
||||
`A server error occured: ${(e as Error).message}`,
|
||||
return context.json(
|
||||
{ error: `A server error occured: ${(e as Error).message}` },
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { errorResponse } from "@/response";
|
||||
import { createMiddleware } from "@hono/hono/factory";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
|
|
@ -7,8 +6,10 @@ export const urlCheck = createMiddleware(async (context, next) => {
|
|||
const baseUrl = new URL(config.http.base_url);
|
||||
|
||||
if (new URL(context.req.url).origin !== baseUrl.origin) {
|
||||
return errorResponse(
|
||||
`Request URL ${context.req.url} does not match base URL ${baseUrl.origin}`,
|
||||
return context.json(
|
||||
{
|
||||
error: `Request URL ${context.req.url} does not match base URL ${baseUrl.origin}`,
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { errorResponse, redirect } from "@/response";
|
||||
import { redirect } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq, or } from "drizzle-orm";
|
||||
import { SignJWT } from "jose";
|
||||
|
|
@ -159,7 +159,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!application) {
|
||||
return errorResponse("Invalid application", 400);
|
||||
return context.json({ error: "Invalid application" }, 400);
|
||||
}
|
||||
|
||||
const searchParams = new URLSearchParams({
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -42,13 +41,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -62,7 +61,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
}
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import ISO6391 from "iso-639-1";
|
||||
import { z } from "zod";
|
||||
|
|
@ -55,13 +54,13 @@ export default apiRoute((app) =>
|
|||
const { reblogs, notify, languages } = context.req.valid("json");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
let relationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -77,7 +76,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
}
|
||||
|
||||
return jsonResponse(relationship.toApi());
|
||||
return context.json(relationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -61,7 +60,7 @@ export default apiRoute((app) =>
|
|||
// TODO: Add follower/following privacy settings
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const { objects, link } = await Timeline.getUserTimeline(
|
||||
|
|
@ -75,7 +74,7 @@ export default apiRoute((app) =>
|
|||
context.req.url,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
await Promise.all(objects.map((object) => object.toApi())),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -58,7 +57,7 @@ export default apiRoute((app) =>
|
|||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
// TODO: Add follower/following privacy settings
|
||||
|
|
@ -74,7 +73,7 @@ export default apiRoute((app) =>
|
|||
context.req.url,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
await Promise.all(objects.map((object) => object.toApi())),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -40,10 +39,10 @@ export default apiRoute((app) =>
|
|||
const foundUser = await User.fromId(id);
|
||||
|
||||
if (!foundUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse(foundUser.toApi(user?.id === foundUser.id));
|
||||
return context.json(foundUser.toApi(user?.id === foundUser.id));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -54,13 +53,13 @@ export default apiRoute((app) =>
|
|||
const { notifications } = context.req.valid("json");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -74,7 +73,7 @@ export default apiRoute((app) =>
|
|||
mutingNotifications: notifications ?? true,
|
||||
});
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -47,13 +46,13 @@ export default apiRoute((app) =>
|
|||
const { comment } = context.req.valid("json");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -65,7 +64,7 @@ export default apiRoute((app) =>
|
|||
note: comment,
|
||||
});
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -42,13 +41,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -60,7 +59,7 @@ export default apiRoute((app) =>
|
|||
endorsed: true,
|
||||
});
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -38,18 +37,18 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const newUser = await otherUser.updateFromRemote();
|
||||
|
||||
return jsonResponse(newUser.toApi(false));
|
||||
return context.json(newUser.toApi(false));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -42,13 +41,13 @@ export default apiRoute((app) =>
|
|||
const { user: self } = context.req.valid("header");
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const oppositeRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -67,7 +66,7 @@ export default apiRoute((app) =>
|
|||
otherUser,
|
||||
);
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, gt, gte, isNull, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -75,7 +74,7 @@ export default apiRoute((app) =>
|
|||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const {
|
||||
|
|
@ -109,7 +108,7 @@ export default apiRoute((app) =>
|
|||
user?.id,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
await Promise.all(objects.map((note) => note.toApi(otherUser))),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -42,13 +41,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -62,7 +61,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
}
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -42,13 +41,13 @@ export default apiRoute((app) =>
|
|||
const { user: self } = context.req.valid("header");
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -57,10 +56,10 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (!(await self.unfollow(otherUser, foundRelationship))) {
|
||||
return errorResponse("Failed to unfollow user", 500);
|
||||
return context.json({ error: "Failed to unfollow user" }, 500);
|
||||
}
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -42,13 +41,13 @@ export default apiRoute((app) =>
|
|||
const { user: self } = context.req.valid("header");
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const user = await User.fromId(id);
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -63,7 +62,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
}
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -42,13 +41,13 @@ export default apiRoute((app) =>
|
|||
const { user: self } = context.req.valid("header");
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -62,7 +61,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
}
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, qsQuery } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { inArray } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -41,7 +40,7 @@ export default apiRoute((app) =>
|
|||
const { id: ids } = context.req.valid("query");
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const idFollowerRelationships =
|
||||
|
|
@ -60,7 +59,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (idFollowerRelationships.length === 0) {
|
||||
return jsonResponse([]);
|
||||
return context.json([]);
|
||||
}
|
||||
|
||||
// Find users that you follow in idFollowerRelationships
|
||||
|
|
@ -82,7 +81,7 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (relevantRelationships.length === 0) {
|
||||
return jsonResponse([]);
|
||||
return context.json([]);
|
||||
}
|
||||
|
||||
const finalUsers = await User.manyFromSql(
|
||||
|
|
@ -92,7 +91,7 @@ export default apiRoute((app) =>
|
|||
),
|
||||
);
|
||||
|
||||
return jsonResponse(finalUsers.map((o) => o.toApi()));
|
||||
return context.json(finalUsers.map((o) => o.toApi()));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -42,10 +41,10 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse(user.toApi());
|
||||
return context.json(user.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, jsonOrForm } from "@/api";
|
||||
import { jsonResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { tempmailDomains } from "@/tempmail";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
|
|
@ -52,7 +52,7 @@ export default apiRoute((app) =>
|
|||
context.req.valid("json");
|
||||
|
||||
if (!config.signups.registration) {
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
{
|
||||
error: "Registration is disabled",
|
||||
},
|
||||
|
|
@ -235,7 +235,7 @@ export default apiRoute((app) =>
|
|||
.join(", ")}`,
|
||||
)
|
||||
.join(", ");
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
{
|
||||
error: `Validation failed: ${errorsText}`,
|
||||
details: Object.fromEntries(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq } from "drizzle-orm";
|
||||
import {
|
||||
|
|
@ -50,7 +49,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!acct) {
|
||||
return errorResponse("Invalid acct parameter", 400);
|
||||
return context.json({ error: "Invalid acct parameter" }, 400);
|
||||
}
|
||||
|
||||
// Check if acct is matching format username@domain.com or @username@domain.com
|
||||
|
|
@ -86,10 +85,10 @@ export default apiRoute((app) =>
|
|||
const foundAccount = await User.resolve(uri);
|
||||
|
||||
if (foundAccount) {
|
||||
return jsonResponse(foundAccount.toApi());
|
||||
return context.json(foundAccount.toApi());
|
||||
}
|
||||
|
||||
return errorResponse("Account not found", 404);
|
||||
return context.json({ error: "Account not found" }, 404);
|
||||
}
|
||||
|
||||
let username = acct;
|
||||
|
|
@ -100,11 +99,11 @@ export default apiRoute((app) =>
|
|||
const account = await User.fromSql(eq(Users.username, username));
|
||||
|
||||
if (account) {
|
||||
return jsonResponse(account.toApi());
|
||||
return context.json(account.toApi());
|
||||
}
|
||||
|
||||
return errorResponse(
|
||||
`Account with username ${username} not found`,
|
||||
return context.json(
|
||||
{ error: `Account with username ${username} not found` },
|
||||
404,
|
||||
);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, qsQuery } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -41,7 +40,7 @@ export default apiRoute((app) =>
|
|||
const ids = Array.isArray(id) ? id : [id];
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const relationships = await Relationship.fromOwnerAndSubjects(
|
||||
|
|
@ -55,7 +54,7 @@ export default apiRoute((app) =>
|
|||
ids.indexOf(b.data.subjectId),
|
||||
);
|
||||
|
||||
return jsonResponse(relationships.map((r) => r.toApi()));
|
||||
return context.json(relationships.map((r) => r.toApi()));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq, ilike, not, or, sql } from "drizzle-orm";
|
||||
import {
|
||||
|
|
@ -80,7 +79,7 @@ export default apiRoute((app) =>
|
|||
const { user: self } = context.req.valid("header");
|
||||
|
||||
if (!self && following) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const [username, host] = q.replace(/^@/, "").split("@");
|
||||
|
|
@ -126,7 +125,7 @@ export default apiRoute((app) =>
|
|||
|
||||
const result = indexOfCorrectSort.map((index) => accounts[index]);
|
||||
|
||||
return jsonResponse(result.map((acct) => acct.toApi()));
|
||||
return context.json(result.map((acct) => acct.toApi()));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, jsonOrForm } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { sanitizedHtmlStrip } from "@/sanitization";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
|
|
@ -149,7 +148,7 @@ export default apiRoute((app) =>
|
|||
} = context.req.valid("json");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const self = user.data;
|
||||
|
|
@ -192,7 +191,10 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (existingUser) {
|
||||
return errorResponse("Username is already taken", 400);
|
||||
return context.json(
|
||||
{ error: "Username is already taken" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
self.username = username;
|
||||
|
|
@ -330,10 +332,10 @@ export default apiRoute((app) =>
|
|||
|
||||
const output = await User.fromId(self.id);
|
||||
if (!output) {
|
||||
return errorResponse("Couldn't edit user", 500);
|
||||
return context.json({ error: "Couldn't edit user" }, 500);
|
||||
}
|
||||
|
||||
return jsonResponse(output.toApi());
|
||||
return context.json(output.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["GET"],
|
||||
|
|
@ -24,10 +23,10 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
return jsonResponse(user.toApi(true));
|
||||
return context.json(user.toApi(true));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { apiRoute, applyConfig, handleZodError, jsonOrForm } from "@/api";
|
||||
import { randomString } from "@/math";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { db } from "~/drizzle/db";
|
||||
|
|
@ -67,7 +66,7 @@ export default apiRoute((app) =>
|
|||
.returning()
|
||||
)[0];
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
id: app.id,
|
||||
name: app.name,
|
||||
website: app.website,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { getFromToken } from "~/classes/functions/application";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
||||
|
|
@ -27,19 +26,19 @@ export default apiRoute((app) =>
|
|||
const { user, token } = context.req.valid("header");
|
||||
|
||||
if (!token) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const application = await getFromToken(token);
|
||||
|
||||
if (!application) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
name: application.name,
|
||||
website: application.website,
|
||||
vapid_key: application.vapidKey,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -50,7 +49,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { objects: blocks, link } = await Timeline.getUserTimeline(
|
||||
|
|
@ -64,7 +63,7 @@ export default apiRoute((app) =>
|
|||
context.req.url,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
blocks.map((u) => u.toApi()),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { generateChallenge } from "@/challenges";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -23,14 +22,17 @@ export default apiRoute((app) =>
|
|||
meta.allowedMethods,
|
||||
meta.route,
|
||||
auth(meta.auth, meta.permissions),
|
||||
async (_context) => {
|
||||
async (context) => {
|
||||
if (!config.validation.challenges.enabled) {
|
||||
return errorResponse("Challenges are disabled in config", 400);
|
||||
return context.json(
|
||||
{ error: "Challenges are disabled in config" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
const result = await generateChallenge();
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
id: result.id,
|
||||
...result.challenge,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -78,7 +78,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const emojis = await response.json();
|
||||
|
||||
|
|
@ -110,7 +112,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const emojis = await response.json();
|
||||
|
||||
|
|
@ -138,7 +142,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const emojis = await response.json();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { and, eq, isNull, or } from "drizzle-orm";
|
||||
import { Emojis, RolePermissions } from "~/drizzle/schema";
|
||||
import { Emoji } from "~/packages/database-interface/emoji";
|
||||
|
|
@ -37,7 +36,7 @@ export default apiRoute((app) =>
|
|||
),
|
||||
);
|
||||
|
||||
return jsonResponse(emojis.map((emoji) => emoji.toApi()));
|
||||
return context.json(emojis.map((emoji) => emoji.toApi()));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import {
|
|||
jsonOrForm,
|
||||
} from "@/api";
|
||||
import { mimeLookup } from "@/content_types";
|
||||
import { errorResponse, jsonResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -80,13 +80,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const emoji = await Emoji.fromId(id);
|
||||
|
||||
if (!emoji) {
|
||||
return errorResponse("Emoji not found", 404);
|
||||
return context.json({ error: "Emoji not found" }, 404);
|
||||
}
|
||||
|
||||
// Check if user is admin
|
||||
|
|
@ -94,7 +94,7 @@ export default apiRoute((app) =>
|
|||
!user.hasPermission(RolePermissions.ManageEmojis) &&
|
||||
emoji.data.ownerId !== user.data.id
|
||||
) {
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
{
|
||||
error: `You cannot modify this emoji, as it is either global, not owned by you, or you do not have the '${RolePermissions.ManageEmojis}' permission to manage global emojis`,
|
||||
},
|
||||
|
|
@ -117,8 +117,10 @@ export default apiRoute((app) =>
|
|||
const form = context.req.valid("json");
|
||||
|
||||
if (!form) {
|
||||
return errorResponse(
|
||||
"Invalid form data (must supply at least one of: shortcode, element, alt, category)",
|
||||
return context.json(
|
||||
{
|
||||
error: "Invalid form data (must supply at least one of: shortcode, element, alt, category)",
|
||||
},
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
|
@ -132,8 +134,10 @@ export default apiRoute((app) =>
|
|||
) &&
|
||||
form.global === undefined
|
||||
) {
|
||||
return errorResponse(
|
||||
"Invalid form data (must supply shortcode and/or element and/or alt and/or global)",
|
||||
return context.json(
|
||||
{
|
||||
error: "Invalid form data (must supply at least one of: shortcode, element, alt, category)",
|
||||
},
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
|
@ -142,8 +146,10 @@ export default apiRoute((app) =>
|
|||
!user.hasPermission(RolePermissions.ManageEmojis) &&
|
||||
form.global
|
||||
) {
|
||||
return errorResponse(
|
||||
`Only users with the '${RolePermissions.ManageEmojis}' permission can make an emoji global or not`,
|
||||
return context.json(
|
||||
{
|
||||
error: `Only users with the '${RolePermissions.ManageEmojis}' permission can make an emoji global or not`,
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
|
@ -158,7 +164,7 @@ export default apiRoute((app) =>
|
|||
: await mimeLookup(form.element);
|
||||
|
||||
if (!contentType.startsWith("image/")) {
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
{
|
||||
error: `Emojis must be images (png, jpg, gif, etc.). Detected: ${contentType}`,
|
||||
},
|
||||
|
|
@ -190,11 +196,11 @@ export default apiRoute((app) =>
|
|||
|
||||
await emoji.update(modified);
|
||||
|
||||
return jsonResponse(emoji.toApi());
|
||||
return context.json(emoji.toApi());
|
||||
}
|
||||
|
||||
case "GET": {
|
||||
return jsonResponse(emoji.toApi());
|
||||
return context.json(emoji.toApi());
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import {
|
|||
jsonOrForm,
|
||||
} from "@/api";
|
||||
import { mimeLookup } from "@/content_types";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, isNull, or } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -73,12 +72,14 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
if (!user.hasPermission(RolePermissions.ManageEmojis) && global) {
|
||||
return errorResponse(
|
||||
`Only users with the '${RolePermissions.ManageEmojis}' permission can upload global emojis`,
|
||||
return context.json(
|
||||
{
|
||||
error: `Only users with the '${RolePermissions.ManageEmojis}' permission can upload global emojis`,
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
|
@ -93,8 +94,10 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (existing) {
|
||||
return errorResponse(
|
||||
`An emoji with the shortcode ${shortcode} already exists, either owned by you or global.`,
|
||||
return context.json(
|
||||
{
|
||||
error: `An emoji with the shortcode ${shortcode} already exists, either owned by you or global.`,
|
||||
},
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
|
@ -108,8 +111,10 @@ export default apiRoute((app) =>
|
|||
: await mimeLookup(element);
|
||||
|
||||
if (!contentType.startsWith("image/")) {
|
||||
return errorResponse(
|
||||
`Emojis must be images (png, jpg, gif, etc.). Detected: ${contentType}`,
|
||||
return context.json(
|
||||
{
|
||||
error: `Emojis must be images (png, jpg, gif, etc.). Detected: ${contentType}`,
|
||||
},
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
|
@ -135,7 +140,7 @@ export default apiRoute((app) =>
|
|||
alt,
|
||||
});
|
||||
|
||||
return jsonResponse(emoji.toApi());
|
||||
return context.json(emoji.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -49,7 +48,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { objects: favourites, link } =
|
||||
|
|
@ -65,7 +64,7 @@ export default apiRoute((app) =>
|
|||
user?.id,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
await Promise.all(
|
||||
favourites.map(async (note) => note.toApi(user)),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { sendFollowAccept } from "~/classes/functions/user";
|
||||
|
|
@ -38,7 +37,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { account_id } = context.req.valid("param");
|
||||
|
|
@ -46,7 +45,7 @@ export default apiRoute((app) =>
|
|||
const account = await User.fromId(account_id);
|
||||
|
||||
if (!account) {
|
||||
return errorResponse("Account not found", 404);
|
||||
return context.json({ error: "Account not found" }, 404);
|
||||
}
|
||||
|
||||
const oppositeRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -70,7 +69,7 @@ export default apiRoute((app) =>
|
|||
await sendFollowAccept(account, user);
|
||||
}
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { sendFollowReject } from "~/classes/functions/user";
|
||||
|
|
@ -38,7 +37,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { account_id } = context.req.valid("param");
|
||||
|
|
@ -46,7 +45,7 @@ export default apiRoute((app) =>
|
|||
const account = await User.fromId(account_id);
|
||||
|
||||
if (!account) {
|
||||
return errorResponse("Account not found", 404);
|
||||
return context.json({ error: "Account not found" }, 404);
|
||||
}
|
||||
|
||||
const oppositeRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -70,7 +69,7 @@ export default apiRoute((app) =>
|
|||
await sendFollowReject(account, user);
|
||||
}
|
||||
|
||||
return jsonResponse(foundRelationship.toApi());
|
||||
return context.json(foundRelationship.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -49,7 +48,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { objects: followRequests, link } =
|
||||
|
|
@ -64,7 +63,7 @@ export default apiRoute((app) =>
|
|||
context.req.url,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
followRequests.map((u) => u.toApi()),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -15,7 +14,7 @@ export const meta = applyConfig({
|
|||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, () => {
|
||||
return jsonResponse(config.frontend.settings);
|
||||
app.on(meta.allowedMethods, meta.route, (context) => {
|
||||
return context.json(config.frontend.settings);
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { renderMarkdownInPath } from "@/markdown";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -20,13 +19,13 @@ export default apiRoute((app) =>
|
|||
meta.allowedMethods,
|
||||
meta.route,
|
||||
auth(meta.auth, meta.permissions),
|
||||
async () => {
|
||||
async (context) => {
|
||||
const { content, lastModified } = await renderMarkdownInPath(
|
||||
config.instance.extended_description_path ?? "",
|
||||
"This is a [Versia](https://versia.pub) server with the default extended description.",
|
||||
);
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
updated_at: lastModified.toISOString(),
|
||||
content,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { jsonResponse, proxyUrl } from "@/response";
|
||||
import { proxyUrl } from "@/response";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { Users } from "~/drizzle/schema";
|
||||
import manifest from "~/package.json";
|
||||
|
|
@ -25,7 +25,7 @@ export default apiRoute((app) =>
|
|||
meta.allowedMethods,
|
||||
meta.route,
|
||||
auth(meta.auth, meta.permissions),
|
||||
async () => {
|
||||
async (context) => {
|
||||
// Get software version from package.json
|
||||
const version = manifest.version;
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ export default apiRoute((app) =>
|
|||
const knownDomainsCount = await Instance.getCount();
|
||||
|
||||
// TODO: fill in more values
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
approval_required: false,
|
||||
configuration: {
|
||||
polls: {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { renderMarkdownInPath } from "@/markdown";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -20,13 +19,13 @@ export default apiRoute((app) =>
|
|||
meta.allowedMethods,
|
||||
meta.route,
|
||||
auth(meta.auth, meta.permissions),
|
||||
async () => {
|
||||
async (context) => {
|
||||
const { content, lastModified } = await renderMarkdownInPath(
|
||||
config.instance.privacy_policy_path ?? "",
|
||||
"This instance has not provided any privacy policy.",
|
||||
);
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
updated_at: lastModified.toISOString(),
|
||||
content,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -19,8 +18,8 @@ export default apiRoute((app) =>
|
|||
meta.allowedMethods,
|
||||
meta.route,
|
||||
auth(meta.auth, meta.permissions),
|
||||
async () => {
|
||||
return jsonResponse(
|
||||
async (context) => {
|
||||
return context.json(
|
||||
config.signups.rules.map((rule, index) => ({
|
||||
id: String(index),
|
||||
text: rule,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { renderMarkdownInPath } from "@/markdown";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -20,13 +19,13 @@ export default apiRoute((app) =>
|
|||
meta.allowedMethods,
|
||||
meta.route,
|
||||
auth(meta.auth, meta.permissions),
|
||||
async () => {
|
||||
async (context) => {
|
||||
const { content, lastModified } = await renderMarkdownInPath(
|
||||
config.instance.tos_path ?? "",
|
||||
"This instance has not provided any terms of service.",
|
||||
);
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
updated_at: lastModified.toISOString(),
|
||||
content,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import type { Marker as ApiMarker } from "@lysand-org/client/types";
|
||||
import { and, count, eq } from "drizzle-orm";
|
||||
|
|
@ -54,13 +53,13 @@ export default apiRoute((app) =>
|
|||
const timeline = Array.isArray(timelines) ? timelines : [];
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
switch (context.req.method) {
|
||||
case "GET": {
|
||||
if (!timeline) {
|
||||
return jsonResponse({});
|
||||
return context.json({});
|
||||
}
|
||||
|
||||
const markers: ApiMarker = {
|
||||
|
|
@ -132,7 +131,7 @@ export default apiRoute((app) =>
|
|||
}
|
||||
}
|
||||
|
||||
return jsonResponse(markers);
|
||||
return context.json(markers);
|
||||
}
|
||||
|
||||
case "POST": {
|
||||
|
|
@ -212,7 +211,7 @@ export default apiRoute((app) =>
|
|||
};
|
||||
}
|
||||
|
||||
return jsonResponse(markers);
|
||||
return context.json(markers);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
|
|
@ -54,19 +54,22 @@ export default apiRoute((app) =>
|
|||
const { id } = context.req.valid("param");
|
||||
|
||||
if (!id.match(idValidator)) {
|
||||
return errorResponse("Invalid ID, must be of type UUIDv7", 404);
|
||||
return context.json(
|
||||
{ error: "Invalid ID, must be of type UUIDv7" },
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
const attachment = await Attachment.fromId(id);
|
||||
|
||||
if (!attachment) {
|
||||
return errorResponse("Media not found", 404);
|
||||
return context.json({ error: "Media not found" }, 404);
|
||||
}
|
||||
|
||||
switch (context.req.method) {
|
||||
case "GET": {
|
||||
if (attachment.data.url) {
|
||||
return jsonResponse(attachment.toApi());
|
||||
return context.json(attachment.toApi());
|
||||
}
|
||||
return response(null, 206);
|
||||
}
|
||||
|
|
@ -95,14 +98,14 @@ export default apiRoute((app) =>
|
|||
thumbnailUrl,
|
||||
});
|
||||
|
||||
return jsonResponse(attachment.toApi());
|
||||
return context.json(attachment.toApi());
|
||||
}
|
||||
|
||||
return jsonResponse(attachment.toApi());
|
||||
return context.json(attachment.toApi());
|
||||
}
|
||||
}
|
||||
|
||||
return errorResponse("Method not allowed", 405);
|
||||
return context.json({ error: "Method not allowed" }, 405);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import sharp from "sharp";
|
||||
import { z } from "zod";
|
||||
|
|
@ -46,8 +45,10 @@ export default apiRoute((app) =>
|
|||
const { file, thumbnail, description } = context.req.valid("form");
|
||||
|
||||
if (file.size > config.validation.max_media_size) {
|
||||
return errorResponse(
|
||||
`File too large, max size is ${config.validation.max_media_size} bytes`,
|
||||
return context.json(
|
||||
{
|
||||
error: `File too large, max size is ${config.validation.max_media_size} bytes`,
|
||||
},
|
||||
413,
|
||||
);
|
||||
}
|
||||
|
|
@ -56,7 +57,7 @@ export default apiRoute((app) =>
|
|||
config.validation.enforce_mime_types &&
|
||||
!config.validation.allowed_mime_types.includes(file.type)
|
||||
) {
|
||||
return errorResponse("Invalid file type", 415);
|
||||
return context.json({ error: "Invalid file type" }, 415);
|
||||
}
|
||||
|
||||
const sha256 = new Bun.SHA256();
|
||||
|
|
@ -95,7 +96,7 @@ export default apiRoute((app) =>
|
|||
|
||||
// TODO: Add job to process videos and other media
|
||||
|
||||
return jsonResponse(newAttachment.toApi());
|
||||
return context.json(newAttachment.toApi());
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -49,7 +48,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { objects: mutes, link } = await Timeline.getUserTimeline(
|
||||
|
|
@ -63,7 +62,7 @@ export default apiRoute((app) =>
|
|||
context.req.url,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
mutes.map((u) => u.toApi()),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -39,7 +38,7 @@ export default apiRoute((app) =>
|
|||
|
||||
const { user } = context.req.valid("header");
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
await db
|
||||
|
|
@ -49,7 +48,7 @@ export default apiRoute((app) =>
|
|||
})
|
||||
.where(eq(Notifications.id, id));
|
||||
|
||||
return jsonResponse({});
|
||||
return context.json({});
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { findManyNotifications } from "~/classes/functions/notification";
|
||||
|
|
@ -38,7 +37,7 @@ export default apiRoute((app) =>
|
|||
|
||||
const { user } = context.req.valid("header");
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const notification = (
|
||||
|
|
@ -53,10 +52,10 @@ export default apiRoute((app) =>
|
|||
)[0];
|
||||
|
||||
if (!notification) {
|
||||
return errorResponse("Notification not found", 404);
|
||||
return context.json({ error: "Notification not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse(notification);
|
||||
return context.json(notification);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "~/drizzle/db";
|
||||
import { Notifications, RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -28,7 +27,7 @@ export default apiRoute((app) =>
|
|||
async (context) => {
|
||||
const { user } = context.req.valid("header");
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
await db
|
||||
|
|
@ -38,7 +37,7 @@ export default apiRoute((app) =>
|
|||
})
|
||||
.where(eq(Notifications.notifiedId, user.id));
|
||||
|
||||
return jsonResponse({});
|
||||
return context.json({});
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -38,7 +37,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { "ids[]": ids } = context.req.valid("query");
|
||||
|
|
@ -55,7 +54,7 @@ export default apiRoute((app) =>
|
|||
),
|
||||
);
|
||||
|
||||
return jsonResponse({});
|
||||
return context.json({});
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -111,7 +111,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiNotification[];
|
||||
|
||||
|
|
@ -163,7 +165,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiNotification[];
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { fetchTimeline } from "@/timelines";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { sql } from "drizzle-orm";
|
||||
|
|
@ -105,7 +104,7 @@ export default apiRoute((app) =>
|
|||
async (context) => {
|
||||
const { user } = context.req.valid("header");
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const {
|
||||
|
|
@ -119,8 +118,10 @@ export default apiRoute((app) =>
|
|||
} = context.req.valid("query");
|
||||
|
||||
if (types && exclude_types) {
|
||||
return errorResponse(
|
||||
"Can't use both types and exclude_types",
|
||||
return context.json(
|
||||
{
|
||||
error: "Can't use both types and exclude_types",
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
@ -191,7 +192,7 @@ export default apiRoute((app) =>
|
|||
user.id,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
await Promise.all(objects.map((n) => notificationToApi(n))),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -26,14 +25,14 @@ export default apiRoute((app) =>
|
|||
const { user: self } = context.req.valid("header");
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
await self.update({
|
||||
avatar: "",
|
||||
});
|
||||
|
||||
return jsonResponse(self.toApi(true));
|
||||
return context.json(self.toApi(true));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -26,14 +25,14 @@ export default apiRoute((app) =>
|
|||
const { user: self } = context.req.valid("header");
|
||||
|
||||
if (!self) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
await self.update({
|
||||
header: "",
|
||||
});
|
||||
|
||||
return jsonResponse(self.toApi(true));
|
||||
return context.json(self.toApi(true));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -40,7 +40,7 @@ export default apiRoute((app) =>
|
|||
const { id } = context.req.valid("param");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const userRoles = await Role.getUserRoles(
|
||||
|
|
@ -50,12 +50,12 @@ export default apiRoute((app) =>
|
|||
const role = await Role.fromId(id);
|
||||
|
||||
if (!role) {
|
||||
return errorResponse("Role not found", 404);
|
||||
return context.json({ error: "Role not found" }, 404);
|
||||
}
|
||||
|
||||
switch (context.req.method) {
|
||||
case "GET": {
|
||||
return jsonResponse(role.toApi());
|
||||
return context.json(role.toApi());
|
||||
}
|
||||
|
||||
case "POST": {
|
||||
|
|
@ -66,14 +66,10 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (role.data.priority > userHighestRole.data.priority) {
|
||||
return errorResponse(
|
||||
`Cannot assign role '${
|
||||
role.data.name
|
||||
}' with priority ${
|
||||
role.data.priority
|
||||
} to user with highest role priority ${
|
||||
userHighestRole.data.priority
|
||||
}`,
|
||||
return context.json(
|
||||
{
|
||||
error: `Cannot assign role '${role.data.name}' with priority ${role.data.priority} to user with highest role priority ${userHighestRole.data.priority}`,
|
||||
},
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
|
@ -90,14 +86,10 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (role.data.priority > userHighestRole.data.priority) {
|
||||
return errorResponse(
|
||||
`Cannot remove role '${
|
||||
role.data.name
|
||||
}' with priority ${
|
||||
role.data.priority
|
||||
} from user with highest role priority ${
|
||||
userHighestRole.data.priority
|
||||
}`,
|
||||
return context.json(
|
||||
{
|
||||
error: `Cannot remove role '${role.data.name}' with priority ${role.data.priority} from user with highest role priority ${userHighestRole.data.priority}`,
|
||||
},
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { Role } from "~/packages/database-interface/role";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -23,7 +22,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const userRoles = await Role.getUserRoles(
|
||||
|
|
@ -31,7 +30,7 @@ export default apiRoute((app) =>
|
|||
user.data.isAdmin,
|
||||
);
|
||||
|
||||
return jsonResponse(userRoles.map((r) => r.toApi()));
|
||||
return context.json(userRoles.map((r) => r.toApi()));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse, proxyUrl, response } from "@/response";
|
||||
import { proxyUrl, response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -44,7 +44,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const issuer = config.oidc.providers.find(
|
||||
|
|
@ -52,7 +52,7 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (!issuer) {
|
||||
return errorResponse("Issuer not found", 404);
|
||||
return context.json({ error: "Issuer not found" }, 404);
|
||||
}
|
||||
|
||||
switch (context.req.method) {
|
||||
|
|
@ -67,13 +67,15 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!account) {
|
||||
return errorResponse(
|
||||
"Account not found or is not linked to this issuer",
|
||||
return context.json(
|
||||
{
|
||||
error: "Account not found or is not linked to this issuer",
|
||||
},
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
id: issuer.id,
|
||||
name: issuer.name,
|
||||
icon: proxyUrl(issuer.icon) || undefined,
|
||||
|
|
@ -89,8 +91,10 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!account) {
|
||||
return errorResponse(
|
||||
"Account not found or is not linked to this issuer",
|
||||
return context.json(
|
||||
{
|
||||
error: "Account not found or is not linked to this issuer",
|
||||
},
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, jsonOrForm } from "@/api";
|
||||
import { oauthRedirectUri } from "@/constants";
|
||||
import { randomString } from "@/math";
|
||||
import { errorResponse, jsonResponse, proxyUrl } from "@/response";
|
||||
import { proxyUrl } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import {
|
||||
calculatePKCECodeChallenge,
|
||||
|
|
@ -58,7 +58,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
switch (context.req.method) {
|
||||
|
|
@ -68,7 +68,7 @@ export default apiRoute((app) =>
|
|||
where: (User, { eq }) => eq(User.userId, user.id),
|
||||
});
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
accounts
|
||||
.map((account) => {
|
||||
const issuer = config.oidc.providers.find(
|
||||
|
|
@ -95,8 +95,8 @@ export default apiRoute((app) =>
|
|||
}
|
||||
case "POST": {
|
||||
if (!form) {
|
||||
return errorResponse(
|
||||
"Missing issuer in form body",
|
||||
return context.json(
|
||||
{ error: "Missing issuer in form body" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
@ -104,8 +104,8 @@ export default apiRoute((app) =>
|
|||
const { issuer: issuerId } = form;
|
||||
|
||||
if (!issuerId) {
|
||||
return errorResponse(
|
||||
"Missing issuer in form body",
|
||||
return context.json(
|
||||
{ error: "Missing issuer in form body" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
@ -115,8 +115,8 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (!issuer) {
|
||||
return errorResponse(
|
||||
`Issuer ${issuerId} not found`,
|
||||
return context.json(
|
||||
{ error: `Issuer ${issuerId} not found` },
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
|
@ -157,7 +157,7 @@ export default apiRoute((app) =>
|
|||
const codeChallenge =
|
||||
await calculatePKCECodeChallenge(codeVerifier);
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
link: `${
|
||||
authServer.authorization_endpoint
|
||||
}?${new URLSearchParams({
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -40,14 +39,14 @@ export default apiRoute((app) =>
|
|||
const foundStatus = await Note.fromId(id, user?.id);
|
||||
|
||||
if (!foundStatus) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const ancestors = await foundStatus.getAncestors(user ?? null);
|
||||
|
||||
const descendants = await foundStatus.getDescendants(user ?? null);
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
ancestors: await Promise.all(
|
||||
ancestors.map((status) => status.toApi(user)),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { createLike } from "~/classes/functions/like";
|
||||
|
|
@ -40,13 +39,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const note = await Note.fromId(id, user?.id);
|
||||
|
||||
if (!note?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const existingLike = await db.query.Likes.findFirst({
|
||||
|
|
@ -64,10 +63,10 @@ export default apiRoute((app) =>
|
|||
const newNote = await Note.fromId(id, user.id);
|
||||
|
||||
if (!newNote) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse(await newNote.toApi(user));
|
||||
return context.json(await newNote.toApi(user));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -61,7 +61,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiAccount[];
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -55,13 +54,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const status = await Note.fromId(id, user?.id);
|
||||
|
||||
if (!status?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const { objects, link } = await Timeline.getUserTimeline(
|
||||
|
|
@ -75,7 +74,7 @@ export default apiRoute((app) =>
|
|||
context.req.url,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
objects.map((user) => user.toApi()),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import {
|
|||
idValidator,
|
||||
jsonOrForm,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import ISO6391 from "iso-639-1";
|
||||
import { z } from "zod";
|
||||
|
|
@ -121,16 +120,16 @@ export default apiRoute((app) =>
|
|||
const note = await Note.fromId(id, user?.id);
|
||||
|
||||
if (!note?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
switch (context.req.method) {
|
||||
case "GET": {
|
||||
return jsonResponse(await note.toApi(user));
|
||||
return context.json(await note.toApi(user));
|
||||
}
|
||||
case "DELETE": {
|
||||
if (note.author.id !== user?.id) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
// TODO: Delete and redraft
|
||||
|
|
@ -141,15 +140,15 @@ export default apiRoute((app) =>
|
|||
undoFederationRequest(user, note.getUri()),
|
||||
);
|
||||
|
||||
return jsonResponse(await note.toApi(user), 200);
|
||||
return context.json(await note.toApi(user), 200);
|
||||
}
|
||||
case "PUT": {
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
if (note.author.id !== user.id) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
if (media_ids.length > 0) {
|
||||
|
|
@ -157,7 +156,10 @@ export default apiRoute((app) =>
|
|||
await Attachment.fromIds(media_ids);
|
||||
|
||||
if (foundAttachments.length !== media_ids.length) {
|
||||
return errorResponse("Invalid media IDs", 422);
|
||||
return context.json(
|
||||
{ error: "Invalid media IDs" },
|
||||
422,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -175,7 +177,7 @@ export default apiRoute((app) =>
|
|||
mediaAttachments: media_ids,
|
||||
});
|
||||
|
||||
return jsonResponse(await newNote.toApi(user));
|
||||
return context.json(await newNote.toApi(user));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { db } from "~/drizzle/db";
|
||||
|
|
@ -44,17 +43,17 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const foundStatus = await Note.fromId(id, user?.id);
|
||||
|
||||
if (!foundStatus) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
if (foundStatus.author.id !== user.id) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
if (
|
||||
|
|
@ -66,12 +65,12 @@ export default apiRoute((app) =>
|
|||
),
|
||||
})
|
||||
) {
|
||||
return errorResponse("Already pinned", 422);
|
||||
return context.json({ error: "Already pinned" }, 422);
|
||||
}
|
||||
|
||||
await user.pin(foundStatus);
|
||||
|
||||
return jsonResponse(await foundStatus.toApi(user));
|
||||
return context.json(await foundStatus.toApi(user));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, jsonOrForm } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -45,13 +44,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const foundStatus = await Note.fromId(id, user.id);
|
||||
|
||||
if (!foundStatus?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const existingReblog = await Note.fromSql(
|
||||
|
|
@ -62,7 +61,7 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (existingReblog) {
|
||||
return errorResponse("Already reblogged", 422);
|
||||
return context.json({ error: "Already reblogged" }, 422);
|
||||
}
|
||||
|
||||
const newReblog = await Note.insert({
|
||||
|
|
@ -75,13 +74,13 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!newReblog) {
|
||||
return errorResponse("Failed to reblog", 500);
|
||||
return context.json({ error: "Failed to reblog" }, 500);
|
||||
}
|
||||
|
||||
const finalNewReblog = await Note.fromId(newReblog.id, user?.id);
|
||||
|
||||
if (!finalNewReblog) {
|
||||
return errorResponse("Failed to reblog", 500);
|
||||
return context.json({ error: "Failed to reblog" }, 500);
|
||||
}
|
||||
|
||||
if (foundStatus.author.isLocal() && user.isLocal()) {
|
||||
|
|
@ -93,7 +92,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
}
|
||||
|
||||
return jsonResponse(await finalNewReblog.toApi(user));
|
||||
return context.json(await finalNewReblog.toApi(user));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -61,7 +61,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiAccount[];
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -48,13 +47,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const status = await Note.fromId(id, user.id);
|
||||
|
||||
if (!status?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const { objects, link } = await Timeline.getUserTimeline(
|
||||
|
|
@ -68,7 +67,7 @@ export default apiRoute((app) =>
|
|||
context.req.url,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
objects.map((user) => user.toApi()),
|
||||
200,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import type { StatusSource as ApiStatusSource } from "@lysand-org/client/types";
|
||||
import { z } from "zod";
|
||||
|
|
@ -38,16 +37,16 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const status = await Note.fromId(id, user.id);
|
||||
|
||||
if (!status?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
id: status.id,
|
||||
// TODO: Give real source for spoilerText
|
||||
spoiler_text: status.data.spoilerText,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { deleteLike } from "~/classes/functions/like";
|
||||
|
|
@ -38,13 +37,13 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const note = await Note.fromId(id, user.id);
|
||||
|
||||
if (!note?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
await deleteLike(user, note);
|
||||
|
|
@ -52,10 +51,10 @@ export default apiRoute((app) =>
|
|||
const newNote = await Note.fromId(id, user.id);
|
||||
|
||||
if (!newNote) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse(await newNote.toApi(user));
|
||||
return context.json(await newNote.toApi(user));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -37,26 +36,26 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const status = await Note.fromId(id, user.id);
|
||||
|
||||
if (!status) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
if (status.author.id !== user.id) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
await user.unpin(status);
|
||||
|
||||
if (!status) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse(await status.toApi(user));
|
||||
return context.json(await status.toApi(user));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -39,14 +38,14 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const foundStatus = await Note.fromId(id, user.id);
|
||||
|
||||
// Check if user is authorized to view this status (if it's private)
|
||||
if (!foundStatus?.isViewableByUser(user)) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const existingReblog = await Note.fromSql(
|
||||
|
|
@ -59,7 +58,7 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (!existingReblog) {
|
||||
return errorResponse("Not already reblogged", 422);
|
||||
return context.json({ error: "Not already reblogged" }, 422);
|
||||
}
|
||||
|
||||
await existingReblog.delete();
|
||||
|
|
@ -71,10 +70,10 @@ export default apiRoute((app) =>
|
|||
const newNote = await Note.fromId(id, user.id);
|
||||
|
||||
if (!newNote) {
|
||||
return errorResponse("Record not found", 404);
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return jsonResponse(await newNote.toApi(user));
|
||||
return context.json(await newNote.toApi(user));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -173,7 +173,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const object = (await response.json()) as ApiStatus;
|
||||
|
||||
|
|
@ -198,7 +200,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const object = (await response.json()) as ApiStatus;
|
||||
|
||||
|
|
@ -237,7 +241,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response2.status).toBe(200);
|
||||
expect(response2.headers.get("content-type")).toBe("application/json");
|
||||
expect(response2.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const object2 = (await response2.json()) as ApiStatus;
|
||||
|
||||
|
|
@ -276,7 +282,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response2.status).toBe(200);
|
||||
expect(response2.headers.get("content-type")).toBe("application/json");
|
||||
expect(response2.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const object2 = (await response2.json()) as ApiStatus;
|
||||
|
||||
|
|
@ -299,7 +307,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const object = (await response.json()) as ApiStatus;
|
||||
|
||||
|
|
@ -326,7 +336,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -357,7 +367,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -388,7 +398,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -416,7 +426,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -442,7 +452,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, jsonOrForm } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import ISO6391 from "iso-639-1";
|
||||
import { z } from "zod";
|
||||
|
|
@ -112,7 +111,7 @@ export default apiRoute((app) =>
|
|||
const { user, application } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const {
|
||||
|
|
@ -132,17 +131,23 @@ export default apiRoute((app) =>
|
|||
const foundAttachments = await Attachment.fromIds(media_ids);
|
||||
|
||||
if (foundAttachments.length !== media_ids.length) {
|
||||
return errorResponse("Invalid media IDs", 422);
|
||||
return context.json({ error: "Invalid media IDs" }, 422);
|
||||
}
|
||||
}
|
||||
|
||||
// Check that in_reply_to_id and quote_id are real posts if provided
|
||||
if (in_reply_to_id && !(await Note.fromId(in_reply_to_id))) {
|
||||
return errorResponse("Invalid in_reply_to_id (not found)", 422);
|
||||
return context.json(
|
||||
{ error: "Invalid in_reply_to_id (not found)" },
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
||||
if (quote_id && !(await Note.fromId(quote_id))) {
|
||||
return errorResponse("Invalid quote_id (not found)", 422);
|
||||
return context.json(
|
||||
{ error: "Invalid quote_id (not found)" },
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
||||
const newNote = await Note.fromData({
|
||||
|
|
@ -165,7 +170,7 @@ export default apiRoute((app) =>
|
|||
await newNote.federateToUsers();
|
||||
}
|
||||
|
||||
return jsonResponse(await newNote.toApi(user));
|
||||
return context.json(await newNote.toApi(user));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -33,7 +33,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiStatus[];
|
||||
|
||||
|
|
@ -50,7 +52,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiStatus[];
|
||||
|
||||
|
|
@ -98,7 +102,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -150,7 +154,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -199,7 +203,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, gt, gte, lt, or, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -54,7 +53,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const { objects, link } = await Timeline.getNoteTimeline(
|
||||
|
|
@ -76,7 +75,7 @@ export default apiRoute((app) =>
|
|||
user.id,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
await Promise.all(
|
||||
objects.map(async (note) => note.toApi(user)),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -25,7 +25,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiStatus[];
|
||||
|
||||
|
|
@ -42,7 +44,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiStatus[];
|
||||
|
||||
|
|
@ -72,7 +76,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiStatus[];
|
||||
|
||||
|
|
@ -102,7 +108,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiStatus[];
|
||||
|
||||
|
|
@ -143,7 +151,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -195,7 +203,7 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
|
|
@ -245,7 +253,9 @@ describe(meta.route, () => {
|
|||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe("application/json");
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const objects = (await response.json()) as ApiStatus[];
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, gt, gte, lt, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -94,7 +93,7 @@ export default apiRoute((app) =>
|
|||
user?.id,
|
||||
);
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
await Promise.all(
|
||||
objects.map(async (note) => note.toApi(user)),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, jsonOrForm } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -82,7 +81,7 @@ export default apiRoute((app) =>
|
|||
const { id } = context.req.valid("param");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
const userFilter = await db.query.Filters.findFirst({
|
||||
|
|
@ -94,12 +93,12 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!userFilter) {
|
||||
return errorResponse("Filter not found", 404);
|
||||
return context.json({ error: "Filter not found" }, 404);
|
||||
}
|
||||
|
||||
switch (context.req.method) {
|
||||
case "GET": {
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
id: userFilter.id,
|
||||
title: userFilter.title,
|
||||
context: userFilter.context,
|
||||
|
|
@ -187,10 +186,13 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!updatedFilter) {
|
||||
return errorResponse("Failed to update filter", 500);
|
||||
return context.json(
|
||||
{ error: "Failed to update filter" },
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
id: updatedFilter.id,
|
||||
title: updatedFilter.title,
|
||||
context: updatedFilter.context,
|
||||
|
|
@ -216,7 +218,7 @@ export default apiRoute((app) =>
|
|||
),
|
||||
);
|
||||
|
||||
return jsonResponse({});
|
||||
return context.json({});
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError, jsonOrForm } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { db } from "~/drizzle/db";
|
||||
|
|
@ -69,7 +68,7 @@ export default apiRoute((app) =>
|
|||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
return context.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
switch (context.req.method) {
|
||||
case "GET": {
|
||||
|
|
@ -80,7 +79,7 @@ export default apiRoute((app) =>
|
|||
},
|
||||
});
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
userFilters.map((filter) => ({
|
||||
id: filter.id,
|
||||
title: filter.title,
|
||||
|
|
@ -103,8 +102,8 @@ export default apiRoute((app) =>
|
|||
case "POST": {
|
||||
const form = context.req.valid("json");
|
||||
if (!form) {
|
||||
return errorResponse(
|
||||
"Missing required Form fields",
|
||||
return context.json(
|
||||
{ error: "Missing required fields" },
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
|
@ -118,8 +117,10 @@ export default apiRoute((app) =>
|
|||
} = form;
|
||||
|
||||
if (!title || ctx?.length === 0) {
|
||||
return errorResponse(
|
||||
"Missing required fields (title and context)",
|
||||
return context.json(
|
||||
{
|
||||
error: "Missing required fields (title and context)",
|
||||
},
|
||||
422,
|
||||
);
|
||||
}
|
||||
|
|
@ -140,7 +141,10 @@ export default apiRoute((app) =>
|
|||
)[0];
|
||||
|
||||
if (!newFilter) {
|
||||
return errorResponse("Failed to create filter", 500);
|
||||
return context.json(
|
||||
{ error: "Failed to create filter" },
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
const insertedKeywords =
|
||||
|
|
@ -158,7 +162,7 @@ export default apiRoute((app) =>
|
|||
.returning()
|
||||
: [];
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
id: newFilter.id,
|
||||
title: newFilter.title,
|
||||
context: newFilter.context,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { jsonResponse, proxyUrl } from "@/response";
|
||||
import { proxyUrl } from "@/response";
|
||||
import type { Instance as ApiInstance } from "@lysand-org/client/types";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { Users } from "~/drizzle/schema";
|
||||
|
|
@ -20,7 +20,7 @@ export const meta = applyConfig({
|
|||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, async () => {
|
||||
app.on(meta.allowedMethods, meta.route, async (context) => {
|
||||
// Get software version from package.json
|
||||
const version = manifest.version;
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
// TODO: fill in more values
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
domain: new URL(config.http.base_url).hostname,
|
||||
title: config.instance.name,
|
||||
version: "4.3.0-alpha.3+glitch",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import sharp from "sharp";
|
||||
import { z } from "zod";
|
||||
|
|
@ -46,8 +45,10 @@ export default apiRoute((app) =>
|
|||
const { file, thumbnail, description } = context.req.valid("form");
|
||||
|
||||
if (file.size > config.validation.max_media_size) {
|
||||
return errorResponse(
|
||||
`File too large, max size is ${config.validation.max_media_size} bytes`,
|
||||
return context.json(
|
||||
{
|
||||
error: `File too large, max size is ${config.validation.max_media_size} bytes`,
|
||||
},
|
||||
413,
|
||||
);
|
||||
}
|
||||
|
|
@ -56,7 +57,7 @@ export default apiRoute((app) =>
|
|||
config.validation.enforce_mime_types &&
|
||||
!config.validation.allowed_mime_types.includes(file.type)
|
||||
) {
|
||||
return errorResponse("Invalid file type", 415);
|
||||
return context.json({ error: "Invalid file type" }, 415);
|
||||
}
|
||||
|
||||
const sha256 = new Bun.SHA256();
|
||||
|
|
@ -96,10 +97,10 @@ export default apiRoute((app) =>
|
|||
// TODO: Add job to process videos and other media
|
||||
|
||||
if (isImage) {
|
||||
return jsonResponse(newAttachment.toApi());
|
||||
return context.json(newAttachment.toApi());
|
||||
}
|
||||
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
{
|
||||
...newAttachment.toApi(),
|
||||
url: null,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import {
|
|||
parseUserAddress,
|
||||
userAddressValidator,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, inArray, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -63,19 +62,21 @@ export default apiRoute((app) =>
|
|||
context.req.valid("query");
|
||||
|
||||
if (!self && (resolve || offset)) {
|
||||
return errorResponse(
|
||||
"Cannot use resolve or offset without being authenticated",
|
||||
return context.json(
|
||||
{
|
||||
error: "Cannot use resolve or offset without being authenticated",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
if (!q) {
|
||||
return errorResponse("Query is required", 400);
|
||||
return context.json({ error: "Query is required" }, 400);
|
||||
}
|
||||
|
||||
if (!config.sonic.enabled) {
|
||||
return errorResponse(
|
||||
"Search is not enabled by your server administrator",
|
||||
return context.json(
|
||||
{ error: "Search is not enabled on this server" },
|
||||
501,
|
||||
);
|
||||
}
|
||||
|
|
@ -119,7 +120,7 @@ export default apiRoute((app) =>
|
|||
: null;
|
||||
|
||||
if (account) {
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
accounts: [account.toApi()],
|
||||
statuses: [],
|
||||
hashtags: [],
|
||||
|
|
@ -141,7 +142,7 @@ export default apiRoute((app) =>
|
|||
const newUser = await User.resolve(uri);
|
||||
|
||||
if (newUser) {
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
accounts: [newUser.toApi()],
|
||||
statuses: [],
|
||||
hashtags: [],
|
||||
|
|
@ -210,7 +211,7 @@ export default apiRoute((app) =>
|
|||
)
|
||||
: [];
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
accounts: accounts.map((account) => account.toApi()),
|
||||
statuses: await Promise.all(
|
||||
statuses.map((status) => status.toApi(self)),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { errorResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
|
||||
|
|
@ -50,7 +50,7 @@ export default apiRoute((app) =>
|
|||
const buffer = await file.arrayBuffer();
|
||||
|
||||
if (!(await file.exists())) {
|
||||
return errorResponse("File not found", 404);
|
||||
return context.json({ error: "File not found" }, 404);
|
||||
}
|
||||
|
||||
// Can't directly copy file into Response because this crashes Bun for now
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { errorResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
|
@ -34,8 +34,8 @@ export default apiRoute((app) =>
|
|||
|
||||
// Check if URL is valid
|
||||
if (!URL.canParse(id)) {
|
||||
return errorResponse(
|
||||
"Invalid URL (it should be encoded as base64url",
|
||||
return context.json(
|
||||
{ error: "Invalid URL (it should be encoded as base64url" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { randomString } from "@/math";
|
||||
import { errorResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { SignJWT } from "jose";
|
||||
|
|
@ -232,7 +232,7 @@ export default apiRoute((app) =>
|
|||
}
|
||||
|
||||
if (!flow.application) {
|
||||
return errorResponse("Application not found", 500);
|
||||
return context.json({ error: "Application not found" }, 500);
|
||||
}
|
||||
|
||||
const code = randomString(32, "hex");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, handleZodError, jsonOrForm } from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -51,15 +50,6 @@ export const schemas = {
|
|||
}),
|
||||
};
|
||||
|
||||
const returnError = (error: string, description: string) =>
|
||||
jsonResponse(
|
||||
{
|
||||
error,
|
||||
error_description: description,
|
||||
},
|
||||
401,
|
||||
);
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(
|
||||
meta.allowedMethods,
|
||||
|
|
@ -73,23 +63,32 @@ export default apiRoute((app) =>
|
|||
switch (grant_type) {
|
||||
case "authorization_code": {
|
||||
if (!code) {
|
||||
return returnError(
|
||||
"invalid_request",
|
||||
"Code is required",
|
||||
return context.json(
|
||||
{
|
||||
error: "invalid_request",
|
||||
error_description: "Code is required",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
if (!redirect_uri) {
|
||||
return returnError(
|
||||
"invalid_request",
|
||||
"Redirect URI is required",
|
||||
return context.json(
|
||||
{
|
||||
error: "invalid_request",
|
||||
error_description: "Redirect URI is required",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
if (!client_id) {
|
||||
return returnError(
|
||||
"invalid_client",
|
||||
"Client ID is required",
|
||||
return context.json(
|
||||
{
|
||||
error: "invalid_request",
|
||||
error_description: "Client ID is required",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -100,9 +99,12 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!client || client.secret !== client_secret) {
|
||||
return returnError(
|
||||
"invalid_client",
|
||||
"Invalid client credentials",
|
||||
return context.json(
|
||||
{
|
||||
error: "invalid_client",
|
||||
error_description: "Invalid client credentials",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -116,7 +118,13 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!token) {
|
||||
return returnError("invalid_grant", "Code not found");
|
||||
return context.json(
|
||||
{
|
||||
error: "invalid_grant",
|
||||
error_description: "Code not found",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
// Invalidate the code
|
||||
|
|
@ -125,7 +133,7 @@ export default apiRoute((app) =>
|
|||
.set({ code: null })
|
||||
.where(eq(Tokens.id, token.id));
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
access_token: token.accessToken,
|
||||
token_type: "Bearer",
|
||||
expires_in: token.expiresAt
|
||||
|
|
@ -145,9 +153,12 @@ export default apiRoute((app) =>
|
|||
}
|
||||
}
|
||||
|
||||
return returnError(
|
||||
"unsupported_grant_type",
|
||||
"Unsupported grant type",
|
||||
return context.json(
|
||||
{
|
||||
error: "unsupported_grant_type",
|
||||
error_description: "Unsupported grant type",
|
||||
},
|
||||
401,
|
||||
);
|
||||
},
|
||||
),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { errorResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import type { Entity } from "@lysand-org/federation/types";
|
||||
import { and, eq, inArray, sql } from "drizzle-orm";
|
||||
|
|
@ -60,7 +60,7 @@ export default apiRoute((app) =>
|
|||
|
||||
if (foundObject) {
|
||||
if (!foundObject.isViewableByUser(null)) {
|
||||
return errorResponse("Object not found", 404);
|
||||
return context.json({ error: "Object not found" }, 404);
|
||||
}
|
||||
} else {
|
||||
foundObject =
|
||||
|
|
@ -78,12 +78,12 @@ export default apiRoute((app) =>
|
|||
}
|
||||
|
||||
if (!(foundObject && apiObject)) {
|
||||
return errorResponse("Object not found", 404);
|
||||
return context.json({ error: "Object not found" }, 404);
|
||||
}
|
||||
|
||||
if (foundAuthor?.isRemote()) {
|
||||
return errorResponse(
|
||||
"Cannot view objects from remote instances",
|
||||
return context.json(
|
||||
{ error: "Cannot view objects from remote instances" },
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, debugRequest, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse, response } from "@/response";
|
||||
import { response } from "@/response";
|
||||
import { sentry } from "@/sentry";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
|
|
@ -75,12 +75,12 @@ export default apiRoute((app) =>
|
|||
const user = await User.fromId(uuid);
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
if (user.isRemote()) {
|
||||
return errorResponse(
|
||||
"Cannot view users from remote instances",
|
||||
return context.json(
|
||||
{ error: "Cannot view users from remote instances" },
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
|
@ -98,8 +98,10 @@ export default apiRoute((app) =>
|
|||
if (token) {
|
||||
// Request is bridge request
|
||||
if (token !== config.federation.bridge.token) {
|
||||
return errorResponse(
|
||||
"An invalid token was passed in the Authorization header. Please use the correct token, or remove the Authorization header.",
|
||||
return context.json(
|
||||
{
|
||||
error: "An invalid token was passed in the Authorization header. Please use the correct token, or remove the Authorization header.",
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
|
@ -116,8 +118,10 @@ export default apiRoute((app) =>
|
|||
}
|
||||
}
|
||||
} else {
|
||||
return errorResponse(
|
||||
"Request IP address is not available",
|
||||
return context.json(
|
||||
{
|
||||
error: "Request IP address is not available",
|
||||
},
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
|
@ -146,7 +150,10 @@ export default apiRoute((app) =>
|
|||
// Verify request signature
|
||||
if (checkSignature) {
|
||||
if (!sender) {
|
||||
return errorResponse("Could not resolve keyId", 400);
|
||||
return context.json(
|
||||
{ error: "Could not resolve keyId" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
if (config.debug.federation) {
|
||||
|
|
@ -186,7 +193,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!isValid) {
|
||||
return errorResponse("Invalid signature", 400);
|
||||
return context.json({ error: "Invalid signature" }, 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -199,7 +206,10 @@ export default apiRoute((app) =>
|
|||
const account = await User.resolve(note.author);
|
||||
|
||||
if (!account) {
|
||||
return errorResponse("Author not found", 404);
|
||||
return context.json(
|
||||
{ error: "Author not found" },
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
const newStatus = await Note.fromVersia(
|
||||
|
|
@ -212,7 +222,10 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
if (!newStatus) {
|
||||
return errorResponse("Failed to add status", 500);
|
||||
return context.json(
|
||||
{ error: "Failed to add status" },
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
return response("Note created", 201);
|
||||
|
|
@ -221,7 +234,10 @@ export default apiRoute((app) =>
|
|||
const account = await User.resolve(follow.author);
|
||||
|
||||
if (!account) {
|
||||
return errorResponse("Author not found", 400);
|
||||
return context.json(
|
||||
{ error: "Author not found" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
const foundRelationship =
|
||||
|
|
@ -260,7 +276,10 @@ export default apiRoute((app) =>
|
|||
const account = await User.resolve(followAccept.author);
|
||||
|
||||
if (!account) {
|
||||
return errorResponse("Author not found", 400);
|
||||
return context.json(
|
||||
{ error: "Author not found" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
const foundRelationship =
|
||||
|
|
@ -287,7 +306,10 @@ export default apiRoute((app) =>
|
|||
const account = await User.resolve(followReject.author);
|
||||
|
||||
if (!account) {
|
||||
return errorResponse("Author not found", 400);
|
||||
return context.json(
|
||||
{ error: "Author not found" },
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
const foundRelationship =
|
||||
|
|
@ -338,14 +360,18 @@ export default apiRoute((app) =>
|
|||
await user.delete();
|
||||
return response("Account deleted", 200);
|
||||
}
|
||||
return errorResponse(
|
||||
"Cannot delete other users than self",
|
||||
return context.json(
|
||||
{
|
||||
error: "Cannot delete other users than self",
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
return errorResponse(
|
||||
`Deletion of object ${toDelete} not implemented`,
|
||||
return context.json(
|
||||
{
|
||||
error: `Deletetion of object ${toDelete} not implemented`,
|
||||
},
|
||||
400,
|
||||
);
|
||||
},
|
||||
|
|
@ -356,7 +382,10 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (!updatedAccount) {
|
||||
return errorResponse("Failed to update user", 500);
|
||||
return context.json(
|
||||
{ error: "Failed to update user" },
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
return response("User refreshed", 200);
|
||||
|
|
@ -372,7 +401,10 @@ export default apiRoute((app) =>
|
|||
|
||||
// Refetch note
|
||||
if (!note) {
|
||||
return errorResponse("Note not found", 404);
|
||||
return context.json(
|
||||
{ error: "Note not found" },
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
await note.updateFromRemote();
|
||||
|
|
@ -385,14 +417,23 @@ export default apiRoute((app) =>
|
|||
return result;
|
||||
}
|
||||
|
||||
return errorResponse("Object has not been implemented", 400);
|
||||
return context.json(
|
||||
{ error: "Object has not been implemented" },
|
||||
400,
|
||||
);
|
||||
} catch (e) {
|
||||
if (isValidationError(e)) {
|
||||
return errorResponse((e as ValidationError).message, 400);
|
||||
return context.json(
|
||||
{
|
||||
error: "Failed to process request",
|
||||
error_description: (e as ValidationError).message,
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
logger.error`${e}`;
|
||||
sentry?.captureException(e);
|
||||
return jsonResponse(
|
||||
return context.json(
|
||||
{
|
||||
error: "Failed to process request",
|
||||
message: (e as Error).message,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { errorResponse, redirect, response } from "@/response";
|
||||
import { redirect, response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
|
@ -45,12 +45,12 @@ export default apiRoute((app) =>
|
|||
: await User.fromId(uuid);
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
if (user.isRemote()) {
|
||||
return errorResponse(
|
||||
"Cannot view users from remote instances",
|
||||
return context.json(
|
||||
{ error: "Cannot view users from remote instances" },
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { and, count, eq, inArray } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -44,12 +43,12 @@ export default apiRoute((app) =>
|
|||
const author = await User.fromId(uuid);
|
||||
|
||||
if (!author) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
if (author.isRemote()) {
|
||||
return errorResponse(
|
||||
"Cannot view users from remote instances",
|
||||
return context.json(
|
||||
{ error: "Cannot view users from remote instances" },
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
|
@ -80,7 +79,7 @@ export default apiRoute((app) =>
|
|||
)
|
||||
)[0].count;
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
first: new URL(
|
||||
`/users/${uuid}/outbox?page=1`,
|
||||
config.http.base_url,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { exportJWK } from "jose";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
|
|
@ -16,7 +15,7 @@ export const meta = applyConfig({
|
|||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, async () => {
|
||||
app.on(meta.allowedMethods, meta.route, async (context) => {
|
||||
const publicKey = await crypto.subtle.importKey(
|
||||
"spki",
|
||||
Buffer.from(config.oidc.jwt_key.split(";")[1], "base64"),
|
||||
|
|
@ -30,7 +29,7 @@ export default apiRoute((app) =>
|
|||
// Remove the private key
|
||||
jwk.d = undefined;
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
keys: [
|
||||
{
|
||||
...jwk,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import manifest from "~/package.json";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -15,8 +14,8 @@ export const meta = applyConfig({
|
|||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, () => {
|
||||
return jsonResponse({
|
||||
app.on(meta.allowedMethods, meta.route, (context) => {
|
||||
return context.json({
|
||||
version: "2.0",
|
||||
software: { name: "versia-server", version: manifest.version },
|
||||
protocols: ["versia"],
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { jsonResponse } from "@/response";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -15,9 +14,9 @@ export const meta = applyConfig({
|
|||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, () => {
|
||||
app.on(meta.allowedMethods, meta.route, (context) => {
|
||||
const baseUrl = new URL(config.http.base_url);
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
issuer: baseUrl.origin.toString(),
|
||||
authorization_endpoint: `${baseUrl.origin}/oauth/authorize`,
|
||||
token_endpoint: `${baseUrl.origin}/oauth/token`,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { urlToContentFormat } from "@/content_types";
|
||||
import { jsonResponse } from "@/response";
|
||||
import type { ServerMetadata } from "@lysand-org/federation/types";
|
||||
import pkg from "~/package.json";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
|
@ -18,8 +17,8 @@ export const meta = applyConfig({
|
|||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.on(meta.allowedMethods, meta.route, () => {
|
||||
return jsonResponse({
|
||||
app.on(meta.allowedMethods, meta.route, (context) => {
|
||||
return context.json({
|
||||
type: "ServerMetadata",
|
||||
name: config.instance.name,
|
||||
version: pkg.version,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
idValidator,
|
||||
webfingerMention,
|
||||
} from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
import type { ResponseError } from "@lysand-org/federation";
|
||||
|
|
@ -44,8 +43,10 @@ export default apiRoute((app) =>
|
|||
|
||||
// Check if resource is in the correct format (acct:uuid/username@domain)
|
||||
if (!resource.match(webfingerMention)) {
|
||||
return errorResponse(
|
||||
"Invalid resource (should be acct:(id or username)@domain)",
|
||||
return context.json(
|
||||
{
|
||||
error: "Invalid resource (should be acct:(id or username)@domain)",
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
|
@ -56,7 +57,7 @@ export default apiRoute((app) =>
|
|||
|
||||
// Check if user is a local user
|
||||
if (requestedUser.split("@")[1] !== host) {
|
||||
return errorResponse("User is a remote user", 404);
|
||||
return context.json({ error: "User is a remote user" }, 404);
|
||||
}
|
||||
|
||||
const isUuid = requestedUser.split("@")[0].match(idValidator);
|
||||
|
|
@ -72,7 +73,7 @@ export default apiRoute((app) =>
|
|||
);
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("User not found", 404);
|
||||
return context.json({ error: "User not found" }, 404);
|
||||
}
|
||||
|
||||
let activityPubUrl = "";
|
||||
|
|
@ -97,7 +98,7 @@ export default apiRoute((app) =>
|
|||
}
|
||||
}
|
||||
|
||||
return jsonResponse({
|
||||
return context.json({
|
||||
subject: `acct:${
|
||||
isUuid ? user.id : user.data.username
|
||||
}@${host}`,
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue