mobilizon.chapril.org-mobil.../js/src/types/actor/actor.model.ts

77 lines
1.6 KiB
TypeScript
Raw Normal View History

import type { IMedia } from "@/types/media.model";
import { ActorType } from "../enums";
2019-04-26 15:22:16 +02:00
export interface IActor {
id?: string;
2019-04-26 15:22:16 +02:00
url: string;
name: string;
domain: string | null;
mediaSize: number;
2019-04-26 15:22:16 +02:00
summary: string;
preferredUsername: string;
suspended: boolean;
avatar?: IMedia | null;
banner?: IMedia | null;
type: ActorType;
2019-04-26 15:22:16 +02:00
}
export class Actor implements IActor {
id?: string;
avatar: IMedia | null = null;
banner: IMedia | null = null;
2019-04-26 15:22:16 +02:00
domain: string | null = null;
mediaSize = 0;
name = "";
preferredUsername = "";
summary = "";
suspended = false;
url = "";
type: ActorType = ActorType.PERSON;
2019-04-26 15:22:16 +02:00
constructor(hash: IActor | Record<any, unknown> = {}) {
2019-04-26 15:22:16 +02:00
Object.assign(this, hash);
}
get displayNameAndUsername(): string {
return `${this.name} (${this.usernameWithDomain})`;
}
usernameWithDomain(): string {
const domain = this.domain ? `@${this.domain}` : "";
2019-04-26 15:22:16 +02:00
return `@${this.preferredUsername}${domain}`;
}
public displayName(): string {
return this.name != null && this.name !== ""
? this.name
: this.usernameWithDomain();
}
}
export function usernameWithDomain(actor: IActor, force = false): string {
if (actor.domain) {
return `${actor.preferredUsername}@${actor.domain}`;
}
if (force) {
return `${actor.preferredUsername}@${window.location.hostname}`;
2019-04-26 15:22:16 +02:00
}
return actor.preferredUsername;
2019-04-26 15:22:16 +02:00
}
export function displayNameAndUsername(actor: IActor): string {
if (actor.name) {
return `${actor.name} (@${usernameWithDomain(actor)})`;
}
return usernameWithDomain(actor);
}