fix(federation): 🐛 Fix multiple incorrect outputs in federation routes

This commit is contained in:
Jesse Wierzbinski 2024-05-17 07:39:59 -10:00
parent b4b8f51a5a
commit a603b602e6
No known key found for this signature in database
5 changed files with 45 additions and 36 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -5,7 +5,8 @@ import {
import { config } from "config-manager";
import type { User } from "~packages/database-interface/user";
export const localObjectURI = (id: string) => `/objects/${id}`;
export const localObjectURI = (id: string) =>
new URL(`/objects/${id}`, config.http.base_url).toString();
export const objectToInboxRequest = async (
object: typeof EntityValidator.$Entity,

View file

@ -268,11 +268,7 @@ export class User {
inbox: data.inbox,
outbox: data.outbox,
},
fields:
data.fields?.map((f) => ({
key: f.name,
value: f.value,
})) ?? [],
fields: data.fields ?? [],
updatedAt: new Date(data.created_at).toISOString(),
instanceId: instance.id,
avatar: data.avatar
@ -537,10 +533,7 @@ export class User {
avatar: urlToContentFormat(this.getAvatarUrl(config)) ?? undefined,
header: urlToContentFormat(this.getHeaderUrl(config)) ?? undefined,
display_name: user.displayName,
fields: user.fields.map((f) => ({
name: f.key,
value: f.value,
})),
fields: user.fields,
public_key: {
actor: new URL(
`/users/${user.id}`,

View file

@ -24,7 +24,7 @@ export const meta = applyConfig({
export const schemas = {
param: z.object({
uuid: z.string().uuid(),
id: z.string().uuid(),
}),
};
@ -34,14 +34,14 @@ export default (app: Hono) =>
meta.route,
zValidator("param", schemas.param, handleZodError),
async (context) => {
const { uuid } = context.req.valid("param");
const { id } = context.req.valid("param");
let foundObject: Note | Like | null = null;
let apiObject: typeof EntityValidator.$Entity | null = null;
foundObject = await Note.fromSql(
and(
eq(Notes.id, uuid),
eq(Notes.id, id),
inArray(Notes.visibility, ["public", "unlisted"]),
),
);
@ -52,7 +52,7 @@ export default (app: Hono) =>
(await db.query.Likes.findFirst({
where: (like, { eq, and }) =>
and(
eq(like.id, uuid),
eq(like.id, id),
sql`EXISTS (SELECT 1 FROM statuses WHERE statuses.id = ${like.likedId} AND statuses.visibility IN ('public', 'unlisted'))`,
),
})) ?? null;

View file

@ -1,6 +1,6 @@
import { applyConfig, handleZodError } from "@api";
import { zValidator } from "@hono/zod-validator";
import { jsonResponse } from "@response";
import { errorResponse, jsonResponse } from "@response";
import { and, count, eq, inArray } from "drizzle-orm";
import type { Hono } from "hono";
import { z } from "zod";
@ -8,6 +8,7 @@ import { db } from "~drizzle/db";
import { Notes } from "~drizzle/schema";
import { config } from "~packages/config-manager";
import { Note } from "~packages/database-interface/note";
import { User } from "~packages/database-interface/user";
export const meta = applyConfig({
allowedMethods: ["GET"],
@ -30,6 +31,8 @@ export const schemas = {
}),
};
const NOTES_PER_PAGE = 20;
export default (app: Hono) =>
app.on(
meta.allowedMethods,
@ -39,8 +42,13 @@ export default (app: Hono) =>
async (context) => {
const { uuid } = context.req.valid("param");
const author = await User.fromId(uuid);
if (!author) {
return errorResponse("User not found", 404);
}
const pageNumber = Number(context.req.valid("query").page) || 1;
const host = new URL(config.http.base_url).hostname;
const notes = await Note.manyFromSql(
and(
@ -48,11 +56,12 @@ export default (app: Hono) =>
inArray(Notes.visibility, ["public", "unlisted"]),
),
undefined,
20,
20 * (pageNumber - 1),
NOTES_PER_PAGE,
NOTES_PER_PAGE * (pageNumber - 1),
);
const totalNotes = await db
const totalNotes = (
await db
.select({
count: count(),
})
@ -62,19 +71,25 @@ export default (app: Hono) =>
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
),
);
)
)[0].count;
return jsonResponse({
first: `${host}/users/${uuid}/outbox?page=1`,
last: `${host}/users/${uuid}/outbox?page=1`,
total_items: totalNotes,
// Server actor
author: new URL(
"/users/actor",
first: new URL(
`/users/${uuid}/outbox?page=1`,
config.http.base_url,
).toString(),
last: new URL(
`/users/${uuid}/outbox?page=${Math.ceil(
totalNotes / NOTES_PER_PAGE,
)}`,
config.http.base_url,
).toString(),
total_items: totalNotes,
// Server actor
author: author.getUri(),
next:
notes.length === 20
notes.length === NOTES_PER_PAGE
? new URL(
`/users/${uuid}/outbox?page=${pageNumber + 1}`,
config.http.base_url,