server/database/entities/Relationship.ts

145 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-09-22 05:18:05 +02:00
import {
BaseEntity,
Column,
CreateDateColumn,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from "typeorm";
import { User } from "./User";
import { APIRelationship } from "~types/entities/relationship";
/**
* Stores Mastodon API relationships
*/
@Entity({
name: "relationships",
})
export class Relationship extends BaseEntity {
2023-09-28 20:19:21 +02:00
/** The unique identifier for the relationship. */
2023-09-22 05:18:05 +02:00
@PrimaryGeneratedColumn("uuid")
id!: string;
2023-09-28 20:19:21 +02:00
/** The user who owns the relationship. */
2023-09-22 05:18:05 +02:00
@ManyToOne(() => User, user => user.relationships)
owner!: User;
2023-09-28 20:19:21 +02:00
/** The user who is the subject of the relationship. */
2023-09-22 05:18:05 +02:00
@ManyToOne(() => User)
subject!: User;
2023-09-28 20:19:21 +02:00
/** Whether the owner is following the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
following!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is showing reblogs from the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
showing_reblogs!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is receiving notifications from the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
notifying!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is followed by the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
followed_by!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is blocking the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
blocking!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is blocked by the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
blocked_by!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is muting the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
muting!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is muting notifications from the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
muting_notifications!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner has requested to follow the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
requested!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner is blocking the subject's domain. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
domain_blocking!: boolean;
2023-09-28 20:19:21 +02:00
/** Whether the owner has endorsed the subject. */
@Column("boolean")
2023-09-22 05:18:05 +02:00
endorsed!: boolean;
2023-09-28 20:19:21 +02:00
/** The languages the owner has specified for the subject. */
2023-09-22 05:18:05 +02:00
@Column("jsonb")
languages!: string[];
2023-09-28 20:19:21 +02:00
/** A note the owner has added for the subject. */
2023-09-22 05:18:05 +02:00
@Column("varchar")
note!: string;
2023-09-28 20:19:21 +02:00
/** The date the relationship was created. */
2023-09-22 05:18:05 +02:00
@CreateDateColumn()
created_at!: Date;
2023-09-28 20:19:21 +02:00
/** The date the relationship was last updated. */
2023-09-22 05:18:05 +02:00
@UpdateDateColumn()
updated_at!: Date;
2023-09-28 20:19:21 +02:00
/**
* Creates a new relationship between two users.
* @param owner The user who owns the relationship.
* @param other The user who is the subject of the relationship.
* @returns The newly created relationship.
*/
static async createNew(owner: User, other: User): Promise<Relationship> {
2023-09-22 05:18:05 +02:00
const newRela = new Relationship();
newRela.owner = owner;
newRela.subject = other;
newRela.languages = [];
newRela.following = false;
newRela.showing_reblogs = false;
newRela.notifying = false;
newRela.followed_by = false;
newRela.blocking = false;
newRela.blocked_by = false;
newRela.muting = false;
newRela.muting_notifications = false;
newRela.requested = false;
newRela.domain_blocking = false;
newRela.endorsed = false;
newRela.note = "";
await newRela.save();
return newRela;
}
2023-09-28 20:19:21 +02:00
/**
* Converts the relationship to an API-friendly format.
* @returns The API-friendly relationship.
*/
2023-09-22 05:18:05 +02:00
// eslint-disable-next-line @typescript-eslint/require-await
async toAPI(): Promise<APIRelationship> {
return {
blocked_by: this.blocked_by,
blocking: this.blocking,
domain_blocking: this.domain_blocking,
endorsed: this.endorsed,
followed_by: this.followed_by,
following: this.following,
id: this.subject.id,
muting: this.muting,
muting_notifications: this.muting_notifications,
notifying: this.notifying,
requested: this.requested,
showing_reblogs: this.showing_reblogs,
languages: this.languages,
note: this.note,
};
}
}