2023-11-11 03:36:06 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
2023-11-23 05:10:37 +01:00
|
|
|
import type { LysandObject } from "@prisma/client";
|
2023-11-11 03:36:06 +01:00
|
|
|
import { client } from "~database/datasource";
|
2023-11-23 05:10:37 +01:00
|
|
|
import type { LysandObjectType } from "~types/lysand/Object";
|
2023-11-04 04:34:31 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a Lysand object in the database.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
export const createFromObject = async (object: LysandObjectType) => {
|
|
|
|
|
const foundObject = await client.lysandObject.findFirst({
|
|
|
|
|
where: { remote_id: object.id },
|
|
|
|
|
include: {
|
|
|
|
|
author: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-11-04 04:34:31 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
if (foundObject) {
|
|
|
|
|
return foundObject;
|
2023-11-04 04:34:31 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
const author = await client.lysandObject.findFirst({
|
|
|
|
|
where: { uri: (object as any).author },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await client.lysandObject.create({
|
|
|
|
|
data: {
|
|
|
|
|
authorId: author?.id,
|
|
|
|
|
created_at: new Date(object.created_at),
|
|
|
|
|
extensions: object.extensions || {},
|
|
|
|
|
remote_id: object.id,
|
|
|
|
|
type: object.type,
|
|
|
|
|
uri: object.uri,
|
|
|
|
|
// Rest of data (remove id, author, created_at, extensions, type, uri)
|
|
|
|
|
extra_data: Object.fromEntries(
|
|
|
|
|
Object.entries(object).filter(
|
|
|
|
|
([key]) =>
|
|
|
|
|
![
|
|
|
|
|
"id",
|
|
|
|
|
"author",
|
|
|
|
|
"created_at",
|
|
|
|
|
"extensions",
|
|
|
|
|
"type",
|
|
|
|
|
"uri",
|
|
|
|
|
].includes(key)
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const toLysand = (lyObject: LysandObject): LysandObjectType => {
|
|
|
|
|
return {
|
|
|
|
|
id: lyObject.remote_id || lyObject.id,
|
|
|
|
|
created_at: new Date(lyObject.created_at).toISOString(),
|
|
|
|
|
type: lyObject.type,
|
|
|
|
|
uri: lyObject.uri,
|
|
|
|
|
// @ts-expect-error This works, I promise
|
|
|
|
|
...lyObject.extra_data,
|
|
|
|
|
extensions: lyObject.extensions,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isPublication = (lyObject: LysandObject): boolean => {
|
|
|
|
|
return lyObject.type === "Note" || lyObject.type === "Patch";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isAction = (lyObject: LysandObject): boolean => {
|
|
|
|
|
return [
|
|
|
|
|
"Like",
|
|
|
|
|
"Follow",
|
|
|
|
|
"Dislike",
|
|
|
|
|
"FollowAccept",
|
|
|
|
|
"FollowReject",
|
|
|
|
|
"Undo",
|
|
|
|
|
"Announce",
|
|
|
|
|
].includes(lyObject.type);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isActor = (lyObject: LysandObject): boolean => {
|
|
|
|
|
return lyObject.type === "User";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isExtension = (lyObject: LysandObject): boolean => {
|
|
|
|
|
return lyObject.type === "Extension";
|
|
|
|
|
};
|