2024-08-26 19:06:49 +02:00
|
|
|
import type { LikeExtension } from "@versia/federation/types";
|
2024-04-13 14:20:12 +02:00
|
|
|
import { type InferSelectModel, and, eq } from "drizzle-orm";
|
2024-10-24 16:28:38 +02:00
|
|
|
import type { Note } from "~/classes/database/note";
|
|
|
|
|
import type { User } from "~/classes/database/user";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { db } from "~/drizzle/db";
|
|
|
|
|
import { Likes, Notifications } from "~/drizzle/schema";
|
2024-10-04 15:22:48 +02:00
|
|
|
import { config } from "~/packages/config-manager/index.ts";
|
2024-04-11 13:39:07 +02:00
|
|
|
|
2024-06-20 01:21:02 +02:00
|
|
|
export type LikeType = InferSelectModel<typeof Likes>;
|
2023-10-28 22:21:04 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a Like entity in the database.
|
|
|
|
|
*/
|
2024-08-26 19:06:49 +02:00
|
|
|
export const likeToVersia = (like: LikeType): LikeExtension => {
|
2024-04-07 07:30:49 +02:00
|
|
|
return {
|
|
|
|
|
id: like.id,
|
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: to be rewritten
|
|
|
|
|
author: (like as any).liker?.uri,
|
2024-08-26 19:06:49 +02:00
|
|
|
type: "pub.versia:likes/Like",
|
2024-04-07 07:30:49 +02:00
|
|
|
created_at: new Date(like.createdAt).toISOString(),
|
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: to be rewritten
|
2024-08-26 19:06:49 +02:00
|
|
|
liked: (like as any).liked?.uri,
|
2024-04-16 23:20:18 +02:00
|
|
|
uri: new URL(`/objects/${like.id}`, config.http.base_url).toString(),
|
2024-04-07 07:30:49 +02:00
|
|
|
};
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|
2023-11-23 19:43:56 +01:00
|
|
|
|
2023-11-26 22:54:20 +01:00
|
|
|
/**
|
|
|
|
|
* Create a like
|
|
|
|
|
* @param user User liking the status
|
2024-04-25 05:40:27 +02:00
|
|
|
* @param note Status being liked
|
2023-11-26 22:54:20 +01:00
|
|
|
*/
|
2024-04-25 05:40:27 +02:00
|
|
|
export const createLike = async (user: User, note: Note) => {
|
2024-04-17 08:36:01 +02:00
|
|
|
await db.insert(Likes).values({
|
2024-04-25 05:40:27 +02:00
|
|
|
likedId: note.id,
|
2024-04-11 13:39:07 +02:00
|
|
|
likerId: user.id,
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2023-11-23 19:43:56 +01:00
|
|
|
|
2024-06-13 02:45:07 +02:00
|
|
|
if (note.author.data.instanceId === user.data.instanceId) {
|
2024-04-07 07:30:49 +02:00
|
|
|
// Notify the user that their post has been favourited
|
2024-04-17 08:36:01 +02:00
|
|
|
await db.insert(Notifications).values({
|
2024-04-11 13:39:07 +02:00
|
|
|
accountId: user.id,
|
|
|
|
|
type: "favourite",
|
2024-06-13 02:45:07 +02:00
|
|
|
notifiedId: note.author.id,
|
2024-04-25 05:40:27 +02:00
|
|
|
noteId: note.id,
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: Add database jobs for federating this
|
|
|
|
|
}
|
2023-11-23 19:43:56 +01:00
|
|
|
};
|
2023-11-23 19:55:33 +01:00
|
|
|
|
2023-11-26 22:54:20 +01:00
|
|
|
/**
|
|
|
|
|
* Delete a like
|
|
|
|
|
* @param user User deleting their like
|
2024-04-25 05:40:27 +02:00
|
|
|
* @param note Status being unliked
|
2023-11-26 22:54:20 +01:00
|
|
|
*/
|
2024-04-25 05:40:27 +02:00
|
|
|
export const deleteLike = async (user: User, note: Note) => {
|
2024-04-11 13:39:07 +02:00
|
|
|
await db
|
2024-04-17 08:36:01 +02:00
|
|
|
.delete(Likes)
|
2024-04-25 05:40:27 +02:00
|
|
|
.where(and(eq(Likes.likedId, note.id), eq(Likes.likerId, user.id)));
|
2023-11-23 19:55:33 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
// Notify the user that their post has been favourited
|
2024-04-11 13:39:07 +02:00
|
|
|
await db
|
2024-04-17 08:36:01 +02:00
|
|
|
.delete(Notifications)
|
2024-04-11 13:39:07 +02:00
|
|
|
.where(
|
|
|
|
|
and(
|
2024-04-17 08:36:01 +02:00
|
|
|
eq(Notifications.accountId, user.id),
|
|
|
|
|
eq(Notifications.type, "favourite"),
|
2024-06-13 02:45:07 +02:00
|
|
|
eq(Notifications.notifiedId, note.author.id),
|
2024-04-25 05:40:27 +02:00
|
|
|
eq(Notifications.noteId, note.id),
|
2024-04-11 13:39:07 +02:00
|
|
|
),
|
|
|
|
|
);
|
2023-11-23 19:55:33 +01:00
|
|
|
|
2024-06-13 02:45:07 +02:00
|
|
|
if (user.isLocal() && note.author.isRemote()) {
|
2024-04-07 07:30:49 +02:00
|
|
|
// User is local, federate the delete
|
|
|
|
|
// TODO: Federate this
|
|
|
|
|
}
|
2023-11-23 19:55:33 +01:00
|
|
|
};
|