2025-11-21 08:31:02 +01:00
|
|
|
import type { z } from "zod";
|
2025-04-08 16:01:10 +02:00
|
|
|
import { NoteSchema } from "../schemas/note.ts";
|
|
|
|
|
import type { JSONObject } from "../types.ts";
|
|
|
|
|
import { NonTextContentFormat, TextContentFormat } from "./contentformat.ts";
|
2026-02-25 02:34:27 +01:00
|
|
|
import { Entity, Reference } from "./entity.ts";
|
2025-04-08 16:01:10 +02:00
|
|
|
|
|
|
|
|
export class Note extends Entity {
|
2025-05-23 17:29:27 +02:00
|
|
|
public static override name = "Note";
|
2025-04-08 16:01:10 +02:00
|
|
|
|
2026-04-03 13:34:19 +02:00
|
|
|
public constructor(
|
|
|
|
|
public override data: z.infer<typeof NoteSchema>,
|
|
|
|
|
instanceDomain: string,
|
|
|
|
|
) {
|
|
|
|
|
super(data, instanceDomain);
|
2025-04-08 16:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-03 13:34:19 +02:00
|
|
|
public static override fromJSON(
|
|
|
|
|
json: JSONObject,
|
|
|
|
|
instanceDomain: string,
|
|
|
|
|
): Promise<Note> {
|
|
|
|
|
return NoteSchema.parseAsync(json).then(
|
|
|
|
|
(n) => new Note(n, instanceDomain),
|
|
|
|
|
);
|
2025-04-08 16:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-25 02:34:27 +01:00
|
|
|
public get author(): Reference {
|
2026-04-03 13:34:19 +02:00
|
|
|
return Reference.fromString(this.data.author, this.instanceDomain);
|
2026-02-25 02:34:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get group(): Reference | null {
|
|
|
|
|
if (
|
|
|
|
|
!this.data.group ||
|
|
|
|
|
["public", "followers"].includes(this.data.group)
|
|
|
|
|
) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 13:34:19 +02:00
|
|
|
return Reference.fromString(this.data.group, this.instanceDomain);
|
2026-02-25 02:34:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get mentions(): Reference[] {
|
2026-04-03 13:34:19 +02:00
|
|
|
return this.data.mentions.map((m) =>
|
|
|
|
|
Reference.fromString(m, this.instanceDomain),
|
|
|
|
|
);
|
2026-02-25 02:34:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get quotes(): Reference | null {
|
2026-04-03 13:34:19 +02:00
|
|
|
return this.data.quotes
|
|
|
|
|
? Reference.fromString(this.data.quotes, this.instanceDomain)
|
|
|
|
|
: null;
|
2026-02-25 02:34:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get repliesTo(): Reference | null {
|
|
|
|
|
return this.data.replies_to
|
2026-04-03 13:34:19 +02:00
|
|
|
? Reference.fromString(this.data.replies_to, this.instanceDomain)
|
2026-02-25 02:34:27 +01:00
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 16:01:10 +02:00
|
|
|
public get attachments(): NonTextContentFormat[] {
|
|
|
|
|
return (
|
|
|
|
|
this.data.attachments?.map((a) => new NonTextContentFormat(a)) ?? []
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get content(): TextContentFormat | undefined {
|
|
|
|
|
return this.data.content
|
|
|
|
|
? new TextContentFormat(this.data.content)
|
|
|
|
|
: undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|