server/database/entities/RawObject.ts

175 lines
4.4 KiB
TypeScript
Raw Normal View History

import {
BaseEntity,
Column,
Entity,
Index,
PrimaryGeneratedColumn,
} from "typeorm";
2023-09-27 20:45:07 +02:00
import { APImage, APObject, DateTime } from "activitypub-types";
2023-09-18 07:38:08 +02:00
import { getConfig } from "@config";
import { appendFile } from "fs/promises";
2023-09-20 02:16:50 +02:00
import { APIStatus } from "~types/entities/status";
import { RawActor } from "./RawActor";
import { APIAccount } from "~types/entities/account";
2023-09-27 00:19:10 +02:00
import { APIEmoji } from "~types/entities/emoji";
import { User } from "./User";
2023-09-12 22:48:10 +02:00
/**
2023-09-28 20:19:21 +02:00
* Represents a raw ActivityPub object in the database.
2023-09-12 22:48:10 +02:00
*/
@Entity({
name: "objects",
})
export class RawObject extends BaseEntity {
2023-09-28 20:19:21 +02:00
/**
* The unique identifier of the object.
*/
2023-09-12 22:48:10 +02:00
@PrimaryGeneratedColumn("uuid")
id!: string;
2023-09-28 20:19:21 +02:00
/**
* The data associated with the object.
*/
2023-09-14 05:39:11 +02:00
@Column("jsonb")
// Index ID, attributedTo, to and published for faster lookups
@Index({ unique: true, where: "(data->>'id') IS NOT NULL" })
@Index({ where: "(data->>'attributedTo') IS NOT NULL" })
@Index({ where: "(data->>'to') IS NOT NULL" })
@Index({ where: "(data->>'published') IS NOT NULL" })
2023-09-12 22:48:10 +02:00
data!: APObject;
2023-09-18 07:38:08 +02:00
2023-09-28 20:19:21 +02:00
/**
* Retrieves a RawObject instance by its ID.
* @param id The ID of the RawObject to retrieve.
* @returns A Promise that resolves to the RawObject instance, or undefined if not found.
*/
2023-09-18 07:38:08 +02:00
static async getById(id: string) {
return await RawObject.createQueryBuilder("object")
.where("object.data->>'id' = :id", {
id,
})
.getOne();
}
2023-09-28 20:19:21 +02:00
/**
* Parses the emojis associated with the object.
* @returns A Promise that resolves to an array of APIEmoji objects.
*/
2023-09-27 00:19:10 +02:00
// eslint-disable-next-line @typescript-eslint/require-await
async parseEmojis() {
const emojis = this.data.tag as {
id: string;
type: string;
name: string;
updated: string;
icon: {
type: "Image";
mediaType: string;
url: string;
};
}[];
return emojis.map(emoji => ({
shortcode: emoji.name,
static_url: (emoji.icon as APImage).url,
url: (emoji.icon as APImage).url,
visible_in_picker: true,
category: "custom",
})) as APIEmoji[];
}
2023-09-28 20:19:21 +02:00
/**
* Converts the RawObject instance to an APIStatus object.
* @returns A Promise that resolves to the APIStatus object.
*/
2023-09-20 02:16:50 +02:00
async toAPI(): Promise<APIStatus> {
2023-09-27 00:19:10 +02:00
const mentions = (
await Promise.all(
(this.data.to as string[]).map(
async person => await RawActor.getByActorId(person)
)
)
).filter(m => m) as RawActor[];
2023-09-20 02:16:50 +02:00
return {
account:
(await (
await User.getByActorId(this.data.attributedTo as string)
)?.toAPI()) ?? (null as unknown as APIAccount),
2023-09-20 02:16:50 +02:00
created_at: new Date(this.data.published as DateTime).toISOString(),
id: this.id,
in_reply_to_id: null,
application: null,
card: null,
content: this.data.content as string,
2023-09-27 00:19:10 +02:00
emojis: await this.parseEmojis(),
2023-09-20 02:16:50 +02:00
favourited: false,
favourites_count: 0,
media_attachments: [],
2023-09-27 00:19:10 +02:00
mentions: await Promise.all(
mentions.map(async m => await m.toAPIAccount())
),
2023-09-20 02:16:50 +02:00
in_reply_to_account_id: null,
language: null,
muted: false,
pinned: false,
poll: null,
reblog: null,
reblogged: false,
reblogs_count: 0,
replies_count: 0,
sensitive: false,
spoiler_text: "",
tags: [],
uri: this.data.id as string,
visibility: "public",
url: this.data.id as string,
bookmarked: false,
quote: null,
quote_id: undefined,
};
}
2023-09-28 20:19:21 +02:00
/**
* Determines whether the object is filtered based on the note filters in the configuration.
* @returns A Promise that resolves to a boolean indicating whether the object is filtered.
*/
2023-09-18 07:38:08 +02:00
async isObjectFiltered() {
const config = getConfig();
const filter_result = await Promise.all(
config.filters.note_filters.map(async filter => {
if (
this.data.type === "Note" &&
this.data.content?.match(filter)
) {
// Log filter
if (config.logging.log_filters)
await appendFile(
process.cwd() + "/logs/filters.log",
`${new Date().toISOString()} Filtered note content: "${this.data.content.replaceAll(
"\n",
" "
)}" (ID: ${
this.data.id
}) based on rule: ${filter}\n`
);
return true;
}
})
);
return filter_result.includes(true);
}
2023-09-28 20:19:21 +02:00
/**
* Determines whether a RawObject instance with the given ID exists in the database.
* @param id The ID of the RawObject to check for existence.
* @returns A Promise that resolves to a boolean indicating whether the RawObject exists.
*/
2023-09-18 07:38:08 +02:00
static async exists(id: string) {
return !!(await RawObject.getById(id));
}
2023-09-13 02:29:13 +02:00
}