2024-04-07 07:30:49 +02:00
|
|
|
import { config } from "config-manager";
|
2024-04-13 14:20:12 +02:00
|
|
|
import { type InferSelectModel, and, eq } from "drizzle-orm";
|
2024-04-10 01:54:10 +02:00
|
|
|
import type * as Lysand from "lysand-types";
|
2024-04-11 13:39:07 +02:00
|
|
|
import { db } from "~drizzle/db";
|
2024-04-13 14:20:12 +02:00
|
|
|
import { like, notification } from "~drizzle/schema";
|
|
|
|
|
import type { StatusWithRelations } from "./Status";
|
|
|
|
|
import type { UserWithRelations } from "./User";
|
2024-04-11 13:39:07 +02:00
|
|
|
|
|
|
|
|
export type Like = InferSelectModel<typeof like>;
|
2023-10-28 22:21:04 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a Like entity in the database.
|
|
|
|
|
*/
|
2024-04-16 23:18:47 +02:00
|
|
|
export const likeToLysand = (like: Like): Lysand.Like => {
|
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,
|
|
|
|
|
type: "Like",
|
|
|
|
|
created_at: new Date(like.createdAt).toISOString(),
|
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: to be rewritten
|
|
|
|
|
object: (like as any).liked?.uri,
|
2024-04-10 01:54:10 +02:00
|
|
|
uri: new URL(
|
|
|
|
|
`/objects/like/${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
|
|
|
|
|
* @param status Status being liked
|
|
|
|
|
*/
|
2023-11-23 19:43:56 +01:00
|
|
|
export const createLike = async (
|
2024-04-07 07:30:49 +02:00
|
|
|
user: UserWithRelations,
|
|
|
|
|
status: StatusWithRelations,
|
2023-11-23 19:43:56 +01:00
|
|
|
) => {
|
2024-04-11 13:39:07 +02:00
|
|
|
await db.insert(like).values({
|
|
|
|
|
likedId: status.id,
|
|
|
|
|
likerId: user.id,
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2023-11-23 19:43:56 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
if (status.author.instanceId === user.instanceId) {
|
|
|
|
|
// Notify the user that their post has been favourited
|
2024-04-11 13:39:07 +02:00
|
|
|
await db.insert(notification).values({
|
|
|
|
|
accountId: user.id,
|
|
|
|
|
type: "favourite",
|
|
|
|
|
notifiedId: status.authorId,
|
|
|
|
|
statusId: status.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
|
|
|
|
|
* @param status Status being unliked
|
|
|
|
|
*/
|
2023-11-23 19:55:33 +01:00
|
|
|
export const deleteLike = async (
|
2024-04-07 07:30:49 +02:00
|
|
|
user: UserWithRelations,
|
|
|
|
|
status: StatusWithRelations,
|
2023-11-23 19:55:33 +01:00
|
|
|
) => {
|
2024-04-11 13:39:07 +02:00
|
|
|
await db
|
|
|
|
|
.delete(like)
|
|
|
|
|
.where(and(eq(like.likedId, status.id), eq(like.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
|
|
|
|
|
.delete(notification)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(notification.accountId, user.id),
|
|
|
|
|
eq(notification.type, "favourite"),
|
|
|
|
|
eq(notification.notifiedId, status.authorId),
|
|
|
|
|
eq(notification.statusId, status.id),
|
|
|
|
|
),
|
|
|
|
|
);
|
2023-11-23 19:55:33 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
if (user.instanceId === null && status.author.instanceId !== null) {
|
|
|
|
|
// User is local, federate the delete
|
|
|
|
|
// TODO: Federate this
|
|
|
|
|
}
|
2023-11-23 19:55:33 +01:00
|
|
|
};
|