mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): 🚚 Move Likes to our custom ORM
This commit is contained in:
parent
e52e230ce3
commit
5a26bdf2f8
7 changed files with 262 additions and 119 deletions
|
|
@ -2,8 +2,6 @@ import { apiRoute, applyConfig, auth } from "@/api";
|
|||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { z } from "zod";
|
||||
import { Note } from "~/classes/database/note";
|
||||
import { createLike } from "~/classes/functions/like";
|
||||
import { db } from "~/drizzle/db";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -79,21 +77,10 @@ export default apiRoute((app) =>
|
|||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const existingLike = await db.query.Likes.findFirst({
|
||||
where: (like, { and, eq }) =>
|
||||
and(eq(like.likedId, note.data.id), eq(like.likerId, user.id)),
|
||||
});
|
||||
await user.like(note);
|
||||
|
||||
if (!existingLike) {
|
||||
await createLike(user, note);
|
||||
}
|
||||
await note.reload(user.id);
|
||||
|
||||
const newNote = await Note.fromId(id, user.id);
|
||||
|
||||
if (!newNote) {
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return context.json(await newNote.toApi(user), 200);
|
||||
return context.json(await note.toApi(user), 200);
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { apiRoute, applyConfig, auth } from "@/api";
|
|||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { z } from "zod";
|
||||
import { Note } from "~/classes/database/note";
|
||||
import { deleteLike } from "~/classes/functions/like";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -77,14 +76,10 @@ export default apiRoute((app) =>
|
|||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
await deleteLike(user, note);
|
||||
await user.unlike(note);
|
||||
|
||||
const newNote = await Note.fromId(id, user.id);
|
||||
await note.reload(user.id);
|
||||
|
||||
if (!newNote) {
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return context.json(await newNote.toApi(user), 200);
|
||||
return context.json(await note.toApi(user), 200);
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -6,11 +6,10 @@ import {
|
|||
} from "@versia/federation/schemas";
|
||||
import { and, eq, inArray, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { Like } from "~/classes/database/like";
|
||||
import { Note } from "~/classes/database/note";
|
||||
import { User } from "~/classes/database/user";
|
||||
import { type LikeType, likeToVersia } from "~/classes/functions/like";
|
||||
import { db } from "~/drizzle/db";
|
||||
import { Notes } from "~/drizzle/schema";
|
||||
import { Likes, Notes } from "~/drizzle/schema";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { ErrorSchema, type KnownEntity } from "~/types/api";
|
||||
|
||||
|
|
@ -70,7 +69,7 @@ export default apiRoute((app) =>
|
|||
app.openapi(route, async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
|
||||
let foundObject: Note | LikeType | null = null;
|
||||
let foundObject: Note | Like | null = null;
|
||||
let foundAuthor: User | null = null;
|
||||
let apiObject: KnownEntity | null = null;
|
||||
|
||||
|
|
@ -88,17 +87,15 @@ export default apiRoute((app) =>
|
|||
return context.json({ error: "Object not found" }, 404);
|
||||
}
|
||||
} else {
|
||||
foundObject =
|
||||
(await db.query.Likes.findFirst({
|
||||
where: (like, { eq, and }) =>
|
||||
and(
|
||||
eq(like.id, id),
|
||||
sql`EXISTS (SELECT 1 FROM "Notes" WHERE "Notes"."id" = ${like.likedId} AND "Notes"."visibility" IN ('public', 'unlisted'))`,
|
||||
),
|
||||
})) ?? null;
|
||||
apiObject = foundObject ? likeToVersia(foundObject) : null;
|
||||
foundObject = await Like.fromSql(
|
||||
and(
|
||||
eq(Likes.id, id),
|
||||
sql`EXISTS (SELECT 1 FROM "Notes" WHERE "Notes"."id" = ${Likes.likedId} AND "Notes"."visibility" IN ('public', 'unlisted'))`,
|
||||
),
|
||||
);
|
||||
apiObject = foundObject ? foundObject.toVersia() : null;
|
||||
foundAuthor = foundObject
|
||||
? await User.fromId(foundObject.likerId)
|
||||
? await User.fromId(foundObject.data.likerId)
|
||||
: null;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue