2024-04-07 07:30:49 +02:00
|
|
|
import type { Like, Prisma } from "@prisma/client";
|
|
|
|
|
import { config } from "config-manager";
|
|
|
|
|
import { client } from "~database/datasource";
|
2023-11-11 03:36:06 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
2023-11-23 05:10:37 +01:00
|
|
|
import type { Like as LysandLike } from "~types/lysand/Object";
|
2023-11-23 19:43:56 +01:00
|
|
|
import type { StatusWithRelations } from "./Status";
|
2024-04-07 07:30:49 +02:00
|
|
|
import type { UserWithRelations } from "./User";
|
2023-10-28 22:21:04 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a Like entity in the database.
|
|
|
|
|
*/
|
2023-11-11 03:36:06 +01:00
|
|
|
export const toLysand = (like: Like): LysandLike => {
|
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,
|
|
|
|
|
uri: `${config.http.base_url}/actions/${like.id}`,
|
|
|
|
|
};
|
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-07 07:30:49 +02:00
|
|
|
await client.like.create({
|
|
|
|
|
data: {
|
|
|
|
|
likedId: status.id,
|
|
|
|
|
likerId: user.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
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
|
|
|
|
|
await client.notification.create({
|
|
|
|
|
data: {
|
|
|
|
|
accountId: user.id,
|
|
|
|
|
type: "favourite",
|
|
|
|
|
notifiedId: status.authorId,
|
|
|
|
|
statusId: status.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} 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-07 07:30:49 +02:00
|
|
|
await client.like.deleteMany({
|
|
|
|
|
where: {
|
|
|
|
|
likedId: status.id,
|
|
|
|
|
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
|
|
|
|
|
await client.notification.deleteMany({
|
|
|
|
|
where: {
|
|
|
|
|
accountId: user.id,
|
|
|
|
|
type: "favourite",
|
|
|
|
|
notifiedId: status.authorId,
|
|
|
|
|
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
|
|
|
};
|