server/database/entities/Like.ts

87 lines
2 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import type { Like as LysandLike } from "~types/lysand/Object";
import type { Like } from "@prisma/client";
2023-11-23 19:43:56 +01:00
import { client } from "~database/datasource";
import type { UserWithRelations } from "./User";
import type { StatusWithRelations } from "./Status";
import { ConfigManager } from "config-manager";
const config = await new ConfigManager({}).getConfig();
2023-10-28 22:21:04 +02:00
/**
* Represents a Like entity in the database.
*/
export const toLysand = (like: Like): LysandLike => {
return {
id: like.id,
author: (like as any).liker?.uri,
type: "Like",
created_at: new Date(like.createdAt).toISOString(),
object: (like as any).liked?.uri,
uri: `${config.http.base_url}/actions/${like.id}`,
};
};
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 (
user: UserWithRelations,
status: StatusWithRelations
) => {
await client.like.create({
data: {
likedId: status.id,
likerId: user.id,
},
});
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-26 22:54:20 +01:00
/**
* Delete a like
* @param user User deleting their like
* @param status Status being unliked
*/
export const deleteLike = async (
user: UserWithRelations,
status: StatusWithRelations
) => {
await client.like.deleteMany({
where: {
likedId: status.id,
likerId: user.id,
},
});
// 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-26 22:54:20 +01:00
if (user.instanceId === null && status.author.instanceId !== null) {
// User is local, federate the delete
// TODO: Federate this
}
};