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 { config } from "config-manager";
import type { User } from "~packages/database-interface/user"; 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 ( export const objectToInboxRequest = async (
object: typeof EntityValidator.$Entity, object: typeof EntityValidator.$Entity,

View file

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

View file

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

View file

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