2023-09-12 22:48:10 +02:00
|
|
|
import { getConfig } from "@config";
|
|
|
|
|
import {
|
|
|
|
|
BaseEntity,
|
|
|
|
|
Column,
|
|
|
|
|
CreateDateColumn,
|
|
|
|
|
Entity,
|
2023-09-22 03:09:14 +02:00
|
|
|
JoinTable,
|
2023-09-12 22:48:10 +02:00
|
|
|
ManyToMany,
|
|
|
|
|
ManyToOne,
|
2023-09-27 00:19:10 +02:00
|
|
|
OneToOne,
|
2023-09-12 22:48:10 +02:00
|
|
|
PrimaryGeneratedColumn,
|
2023-09-27 00:19:10 +02:00
|
|
|
RemoveOptions,
|
2023-09-12 22:48:10 +02:00
|
|
|
UpdateDateColumn,
|
|
|
|
|
} from "typeorm";
|
|
|
|
|
import { APIStatus } from "~types/entities/status";
|
|
|
|
|
import { User } from "./User";
|
|
|
|
|
import { Application } from "./Application";
|
|
|
|
|
import { Emoji } from "./Emoji";
|
2023-09-13 07:30:45 +02:00
|
|
|
import { RawActivity } from "./RawActivity";
|
2023-09-27 00:19:10 +02:00
|
|
|
import { RawObject } from "./RawObject";
|
|
|
|
|
import { RawActor } from "./RawActor";
|
2023-09-12 22:48:10 +02:00
|
|
|
|
|
|
|
|
const config = getConfig();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stores ActivityPub notes
|
|
|
|
|
*/
|
|
|
|
|
@Entity({
|
|
|
|
|
name: "statuses",
|
|
|
|
|
})
|
|
|
|
|
export class Status extends BaseEntity {
|
|
|
|
|
@PrimaryGeneratedColumn("uuid")
|
|
|
|
|
id!: string;
|
|
|
|
|
|
2023-09-13 02:29:13 +02:00
|
|
|
@ManyToOne(() => User, user => user.id)
|
2023-09-12 22:48:10 +02:00
|
|
|
account!: User;
|
|
|
|
|
|
|
|
|
|
@CreateDateColumn()
|
|
|
|
|
created_at!: Date;
|
|
|
|
|
|
|
|
|
|
@UpdateDateColumn()
|
|
|
|
|
updated_at!: Date;
|
|
|
|
|
|
2023-09-13 02:29:13 +02:00
|
|
|
@ManyToOne(() => Status, status => status.id, {
|
2023-09-12 22:48:10 +02:00
|
|
|
nullable: true,
|
|
|
|
|
})
|
|
|
|
|
reblog?: Status;
|
|
|
|
|
|
2023-09-27 00:19:10 +02:00
|
|
|
@OneToOne(() => Status)
|
|
|
|
|
object!: RawObject;
|
|
|
|
|
|
2023-09-12 22:48:10 +02:00
|
|
|
@Column("boolean")
|
|
|
|
|
isReblog!: boolean;
|
|
|
|
|
|
|
|
|
|
@Column("varchar", {
|
|
|
|
|
default: "",
|
|
|
|
|
})
|
|
|
|
|
content!: string;
|
|
|
|
|
|
|
|
|
|
@Column("varchar")
|
|
|
|
|
visibility!: APIStatus["visibility"];
|
|
|
|
|
|
2023-09-27 00:19:10 +02:00
|
|
|
@ManyToOne(() => RawObject, {
|
|
|
|
|
nullable: true,
|
|
|
|
|
})
|
|
|
|
|
in_reply_to_post!: RawObject;
|
|
|
|
|
|
|
|
|
|
@ManyToOne(() => RawActor, {
|
|
|
|
|
nullable: true,
|
|
|
|
|
})
|
|
|
|
|
in_reply_to_account!: RawActor;
|
|
|
|
|
|
2023-09-12 22:48:10 +02:00
|
|
|
@Column("boolean")
|
|
|
|
|
sensitive!: boolean;
|
|
|
|
|
|
|
|
|
|
@Column("varchar", {
|
|
|
|
|
default: "",
|
|
|
|
|
})
|
|
|
|
|
spoiler_text!: string;
|
|
|
|
|
|
2023-09-13 02:29:13 +02:00
|
|
|
@ManyToOne(() => Application, app => app.id, {
|
|
|
|
|
nullable: true,
|
2023-09-12 22:48:10 +02:00
|
|
|
})
|
|
|
|
|
application!: Application | null;
|
|
|
|
|
|
2023-09-13 02:29:13 +02:00
|
|
|
@ManyToMany(() => Emoji, emoji => emoji.id)
|
2023-09-22 03:09:14 +02:00
|
|
|
@JoinTable()
|
2023-09-12 22:48:10 +02:00
|
|
|
emojis!: Emoji[];
|
|
|
|
|
|
2023-09-22 03:09:14 +02:00
|
|
|
@ManyToMany(() => RawActivity, activity => activity.id)
|
|
|
|
|
@JoinTable()
|
2023-09-14 04:25:45 +02:00
|
|
|
likes!: RawActivity[];
|
2023-09-13 07:30:45 +02:00
|
|
|
|
2023-09-22 03:09:14 +02:00
|
|
|
@ManyToMany(() => RawActivity, activity => activity.id)
|
|
|
|
|
@JoinTable()
|
2023-09-14 04:25:45 +02:00
|
|
|
announces!: RawActivity[];
|
2023-09-13 02:29:13 +02:00
|
|
|
|
2023-09-27 00:19:10 +02:00
|
|
|
async remove(options?: RemoveOptions | undefined) {
|
|
|
|
|
// Delete object
|
|
|
|
|
await this.object.remove(options);
|
|
|
|
|
|
|
|
|
|
await super.remove(options);
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 03:09:14 +02:00
|
|
|
static async createNew(data: {
|
|
|
|
|
account: User;
|
|
|
|
|
application: Application | null;
|
|
|
|
|
content: string;
|
|
|
|
|
visibility: APIStatus["visibility"];
|
|
|
|
|
sensitive: boolean;
|
|
|
|
|
spoiler_text: string;
|
|
|
|
|
emojis: Emoji[];
|
2023-09-27 00:19:10 +02:00
|
|
|
reply?: {
|
|
|
|
|
object: RawObject;
|
|
|
|
|
actor: RawActor;
|
|
|
|
|
};
|
2023-09-22 03:09:14 +02:00
|
|
|
}) {
|
|
|
|
|
const newStatus = new Status();
|
|
|
|
|
|
|
|
|
|
newStatus.account = data.account;
|
|
|
|
|
newStatus.application = data.application ?? null;
|
|
|
|
|
newStatus.content = data.content;
|
|
|
|
|
newStatus.visibility = data.visibility;
|
|
|
|
|
newStatus.sensitive = data.sensitive;
|
|
|
|
|
newStatus.spoiler_text = data.spoiler_text;
|
|
|
|
|
newStatus.emojis = data.emojis;
|
|
|
|
|
newStatus.likes = [];
|
|
|
|
|
newStatus.announces = [];
|
|
|
|
|
newStatus.isReblog = false;
|
|
|
|
|
newStatus.announces = [];
|
|
|
|
|
|
2023-09-27 00:19:10 +02:00
|
|
|
newStatus.object = new RawObject();
|
|
|
|
|
|
|
|
|
|
if (data.reply) {
|
|
|
|
|
newStatus.in_reply_to_post = data.reply.object;
|
|
|
|
|
newStatus.in_reply_to_account = data.reply.actor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newStatus.object.data = {
|
|
|
|
|
id: `${config.http.base_url}/@${data.account.username}/statuses/${newStatus.id}`,
|
|
|
|
|
type: "Note",
|
|
|
|
|
summary: data.spoiler_text,
|
|
|
|
|
content: data.content, // TODO: Format as HTML
|
|
|
|
|
inReplyTo: data.reply?.object
|
|
|
|
|
? data.reply.object.data.id
|
|
|
|
|
: undefined,
|
|
|
|
|
attributedTo: `${config.http.base_url}/@${data.account.username}`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Get people mentioned in the content
|
|
|
|
|
const mentionedPeople = [
|
|
|
|
|
...data.content.matchAll(/@([a-zA-Z0-9_]+)/g),
|
|
|
|
|
].map(match => {
|
|
|
|
|
return `${config.http.base_url}/@${match[1]}`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Map this to Actors
|
|
|
|
|
const mentionedActors = (
|
|
|
|
|
await Promise.all(
|
|
|
|
|
mentionedPeople.map(async person => {
|
|
|
|
|
// Check if post is in format @username or @username@instance.com
|
|
|
|
|
// If is @username, the user is a local user
|
|
|
|
|
const instanceUrl =
|
|
|
|
|
person.split("@").length === 3
|
|
|
|
|
? person.split("@")[2]
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (instanceUrl) {
|
|
|
|
|
const actor = await RawActor.createQueryBuilder("actor")
|
|
|
|
|
.where("actor.data->>'id' = :id", {
|
|
|
|
|
// Where ID contains the instance URL
|
|
|
|
|
id: `%${instanceUrl}%`,
|
|
|
|
|
})
|
|
|
|
|
// Where actor preferredUsername is the username
|
|
|
|
|
.andWhere(
|
|
|
|
|
"actor.data->>'preferredUsername' = :username",
|
|
|
|
|
{
|
|
|
|
|
username: person.split("@")[1],
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.getOne();
|
|
|
|
|
|
|
|
|
|
return actor?.data.id;
|
|
|
|
|
} else {
|
|
|
|
|
const actor = await User.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
username: person.split("@")[1],
|
|
|
|
|
},
|
|
|
|
|
relations: {
|
|
|
|
|
actor: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return actor?.actor.data.id;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
).map(actor => actor as string);
|
|
|
|
|
|
|
|
|
|
newStatus.object.data.to = mentionedActors;
|
|
|
|
|
|
|
|
|
|
if (data.visibility === "private") {
|
|
|
|
|
newStatus.object.data.cc = [
|
|
|
|
|
`${config.http.base_url}/@${data.account.username}/followers`,
|
|
|
|
|
];
|
|
|
|
|
} else if (data.visibility === "direct") {
|
|
|
|
|
// Add nothing else
|
|
|
|
|
} else if (data.visibility === "public") {
|
|
|
|
|
newStatus.object.data.to = [
|
|
|
|
|
...newStatus.object.data.to,
|
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public",
|
|
|
|
|
];
|
|
|
|
|
newStatus.object.data.cc = [
|
|
|
|
|
`${config.http.base_url}/@${data.account.username}/followers`,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
|
else if (data.visibility === "unlisted") {
|
|
|
|
|
newStatus.object.data.to = [
|
|
|
|
|
...newStatus.object.data.to,
|
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public",
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 23:41:05 +02:00
|
|
|
// TODO: Add default language
|
|
|
|
|
|
2023-09-22 03:09:14 +02:00
|
|
|
await newStatus.save();
|
|
|
|
|
return newStatus;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 22:48:10 +02:00
|
|
|
async toAPI(): Promise<APIStatus> {
|
|
|
|
|
return {
|
|
|
|
|
account: await this.account.toAPI(),
|
2023-09-13 02:29:13 +02:00
|
|
|
application: (await this.application?.toAPI()) ?? null,
|
2023-09-12 22:48:10 +02:00
|
|
|
bookmarked: false,
|
|
|
|
|
created_at: this.created_at.toISOString(),
|
2023-09-13 02:29:13 +02:00
|
|
|
emojis: await Promise.all(
|
|
|
|
|
this.emojis.map(async emoji => await emoji.toAPI())
|
|
|
|
|
),
|
2023-09-12 22:48:10 +02:00
|
|
|
favourited: false,
|
2023-09-22 03:09:14 +02:00
|
|
|
favourites_count: this.likes.length,
|
2023-09-12 22:48:10 +02:00
|
|
|
id: this.id,
|
|
|
|
|
in_reply_to_account_id: null,
|
|
|
|
|
in_reply_to_id: null,
|
|
|
|
|
language: null,
|
|
|
|
|
media_attachments: [],
|
|
|
|
|
mentions: [],
|
|
|
|
|
muted: false,
|
|
|
|
|
pinned: false,
|
|
|
|
|
poll: null,
|
2023-09-13 02:29:13 +02:00
|
|
|
reblog: this.isReblog ? (await this.reblog?.toAPI()) ?? null : null,
|
2023-09-12 22:48:10 +02:00
|
|
|
reblogged: false,
|
2023-09-22 03:09:14 +02:00
|
|
|
reblogs_count: this.announces.length,
|
2023-09-12 22:48:10 +02:00
|
|
|
replies_count: 0,
|
|
|
|
|
sensitive: false,
|
|
|
|
|
spoiler_text: "",
|
|
|
|
|
tags: [],
|
|
|
|
|
card: null,
|
2023-09-22 03:09:14 +02:00
|
|
|
content: this.content,
|
2023-09-12 22:48:10 +02:00
|
|
|
uri: `${config.http.base_url}/@${this.account.username}/${this.id}`,
|
|
|
|
|
url: `${config.http.base_url}/@${this.account.username}/${this.id}`,
|
|
|
|
|
visibility: "public",
|
|
|
|
|
quote: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|