mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
refactor(federation): ♻️ Refactor inbox code to use new package builtins
This commit is contained in:
parent
fbc0c2c586
commit
fbe0e35587
|
|
@ -1 +1 @@
|
||||||
Subproject commit 16eae048c886de41f3bb9048fe3aeff113a442ef
|
Subproject commit 3ed54a7c21e69b79b410bac687f30f47d75e5397
|
||||||
|
|
@ -1,18 +1,13 @@
|
||||||
import { applyConfig, debugRequest, handleZodError } from "@api";
|
import { applyConfig, debugRequest, handleZodError } from "@api";
|
||||||
import { zValidator } from "@hono/zod-validator";
|
import { zValidator } from "@hono/zod-validator";
|
||||||
import { dualLogger } from "@loggers";
|
import { dualLogger } from "@loggers";
|
||||||
import {
|
|
||||||
EntityValidator,
|
|
||||||
type HttpVerb,
|
|
||||||
SignatureValidator,
|
|
||||||
} from "@lysand-org/federation";
|
|
||||||
import { errorResponse, jsonResponse, response } from "@response";
|
import { errorResponse, jsonResponse, response } from "@response";
|
||||||
import type { SocketAddress } from "bun";
|
import type { SocketAddress } from "bun";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
import { matches } from "ip-matching";
|
import { matches } from "ip-matching";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { isValidationError } from "zod-validation-error";
|
import { type ValidationError, isValidationError } from "zod-validation-error";
|
||||||
import { resolveNote } from "~database/entities/Status";
|
import { resolveNote } from "~database/entities/Status";
|
||||||
import {
|
import {
|
||||||
getRelationshipToOtherUser,
|
getRelationshipToOtherUser,
|
||||||
|
|
@ -23,6 +18,11 @@ import { Notifications, Relationships } from "~drizzle/schema";
|
||||||
import { config } from "~packages/config-manager";
|
import { config } from "~packages/config-manager";
|
||||||
import { User } from "~packages/database-interface/user";
|
import { User } from "~packages/database-interface/user";
|
||||||
import { LogLevel, LogManager } from "~packages/log-manager";
|
import { LogLevel, LogManager } from "~packages/log-manager";
|
||||||
|
import {
|
||||||
|
EntityValidator,
|
||||||
|
RequestParserHandler,
|
||||||
|
SignatureValidator,
|
||||||
|
} from "~packages/lysand-api/federation";
|
||||||
|
|
||||||
export const meta = applyConfig({
|
export const meta = applyConfig({
|
||||||
allowedMethods: ["POST"],
|
allowedMethods: ["POST"],
|
||||||
|
|
@ -192,13 +192,11 @@ export default (app: Hono) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
const validator = new EntityValidator();
|
const validator = new EntityValidator();
|
||||||
|
const handler = new RequestParserHandler(body, validator);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Add sent data to database
|
const result = await handler.parseBody({
|
||||||
switch (body.type) {
|
note: async (note) => {
|
||||||
case "Note": {
|
|
||||||
const note = await validator.Note(body);
|
|
||||||
|
|
||||||
const account = await User.resolve(note.author);
|
const account = await User.resolve(note.author);
|
||||||
|
|
||||||
if (!account) {
|
if (!account) {
|
||||||
|
|
@ -222,21 +220,8 @@ export default (app: Hono) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
return response("Note created", 201);
|
return response("Note created", 201);
|
||||||
}
|
},
|
||||||
case "Follow": {
|
follow: async (follow) => {
|
||||||
const follow = await validator.Follow(body);
|
|
||||||
|
|
||||||
if (
|
|
||||||
config.federation.discard.follows.find(
|
|
||||||
(blocked) =>
|
|
||||||
blocked.includes(origin) ||
|
|
||||||
origin.includes(blocked),
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
// Pretend to accept request
|
|
||||||
return response("Follow request sent", 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
const account = await User.resolve(follow.author);
|
const account = await User.resolve(follow.author);
|
||||||
|
|
||||||
if (!account) {
|
if (!account) {
|
||||||
|
|
@ -246,7 +231,6 @@ export default (app: Hono) =>
|
||||||
const foundRelationship =
|
const foundRelationship =
|
||||||
await getRelationshipToOtherUser(account, user);
|
await getRelationshipToOtherUser(account, user);
|
||||||
|
|
||||||
// Check if already following
|
|
||||||
if (foundRelationship.following) {
|
if (foundRelationship.following) {
|
||||||
return response("Already following", 200);
|
return response("Already following", 200);
|
||||||
}
|
}
|
||||||
|
|
@ -271,39 +255,21 @@ export default (app: Hono) =>
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user.getUser().isLocked) {
|
if (!user.getUser().isLocked) {
|
||||||
// Federate FollowAccept
|
|
||||||
await sendFollowAccept(account, user);
|
await sendFollowAccept(account, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response("Follow request sent", 200);
|
return response("Follow request sent", 200);
|
||||||
}
|
},
|
||||||
case "FollowAccept": {
|
followAccept: async (followAccept) => {
|
||||||
const followAccept = await validator.FollowAccept(body);
|
|
||||||
|
|
||||||
if (
|
|
||||||
config.federation.discard.follows.find(
|
|
||||||
(blocked) =>
|
|
||||||
blocked.includes(origin) ||
|
|
||||||
origin.includes(blocked),
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
// Pretend to accept request
|
|
||||||
return response("Follow request accepted", 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
const account = await User.resolve(followAccept.author);
|
const account = await User.resolve(followAccept.author);
|
||||||
|
|
||||||
if (!account) {
|
if (!account) {
|
||||||
return errorResponse("Author not found", 400);
|
return errorResponse("Author not found", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(account);
|
|
||||||
|
|
||||||
const foundRelationship =
|
const foundRelationship =
|
||||||
await getRelationshipToOtherUser(user, account);
|
await getRelationshipToOtherUser(user, account);
|
||||||
|
|
||||||
console.log(foundRelationship);
|
|
||||||
|
|
||||||
if (!foundRelationship.requested) {
|
if (!foundRelationship.requested) {
|
||||||
return response(
|
return response(
|
||||||
"There is no follow request to accept",
|
"There is no follow request to accept",
|
||||||
|
|
@ -320,21 +286,8 @@ export default (app: Hono) =>
|
||||||
.where(eq(Relationships.id, foundRelationship.id));
|
.where(eq(Relationships.id, foundRelationship.id));
|
||||||
|
|
||||||
return response("Follow request accepted", 200);
|
return response("Follow request accepted", 200);
|
||||||
}
|
},
|
||||||
case "FollowReject": {
|
followReject: async (followReject) => {
|
||||||
const followReject = await validator.FollowReject(body);
|
|
||||||
|
|
||||||
if (
|
|
||||||
config.federation.discard.follows.find(
|
|
||||||
(blocked) =>
|
|
||||||
blocked.includes(origin) ||
|
|
||||||
origin.includes(blocked),
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
// Pretend to accept request
|
|
||||||
return response("Follow request rejected", 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
const account = await User.resolve(followReject.author);
|
const account = await User.resolve(followReject.author);
|
||||||
|
|
||||||
if (!account) {
|
if (!account) {
|
||||||
|
|
@ -360,17 +313,17 @@ export default (app: Hono) =>
|
||||||
.where(eq(Relationships.id, foundRelationship.id));
|
.where(eq(Relationships.id, foundRelationship.id));
|
||||||
|
|
||||||
return response("Follow request rejected", 200);
|
return response("Follow request rejected", 200);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
default: {
|
|
||||||
return errorResponse(
|
return errorResponse("Object has not been implemented", 400);
|
||||||
"Object has not been implemented",
|
|
||||||
400,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isValidationError(e)) {
|
if (isValidationError(e)) {
|
||||||
return errorResponse(e.message, 400);
|
return errorResponse((e as ValidationError).message, 400);
|
||||||
}
|
}
|
||||||
dualLogger.logError(LogLevel.ERROR, "Inbox", e as Error);
|
dualLogger.logError(LogLevel.ERROR, "Inbox", e as Error);
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue