mirror of
https://github.com/versia-pub/server.git
synced 2026-04-27 20:59:15 +02:00
feat(federation): ✨ Port to Versia 0.6
This commit is contained in:
parent
de69f27877
commit
fca30b4dad
62 changed files with 1614 additions and 2008 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import type { z } from "zod";
|
||||
import { DeleteSchema } from "../schemas/delete.ts";
|
||||
import type { JSONObject } from "../types.ts";
|
||||
import { Entity } from "./entity.ts";
|
||||
import { Entity, Reference } from "./entity.ts";
|
||||
|
||||
export class Delete extends Entity {
|
||||
public static override name = "Delete";
|
||||
|
|
@ -10,6 +10,14 @@ export class Delete extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get deleted(): Reference {
|
||||
return Reference.fromString(this.data.deleted);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Delete> {
|
||||
return DeleteSchema.parseAsync(json).then((u) => new Delete(u));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,3 +15,31 @@ export class Entity {
|
|||
return this.data;
|
||||
}
|
||||
}
|
||||
|
||||
export class Reference {
|
||||
public constructor(
|
||||
public id: string,
|
||||
public domain?: string,
|
||||
) {}
|
||||
|
||||
public static fromString(str: string): Reference {
|
||||
// Expect format: domain:id or id (if domain is the local instance)
|
||||
// Handle IPv6 addresses in brackets
|
||||
const chunks = str.split(":");
|
||||
if (chunks.length === 2) {
|
||||
return new Reference(chunks[1], chunks[0]);
|
||||
}
|
||||
|
||||
if (chunks.length > 2) {
|
||||
const domain = chunks.slice(0, -1).join(":");
|
||||
const id = chunks.at(-1) as string;
|
||||
return new Reference(id, domain);
|
||||
}
|
||||
|
||||
return new Reference(str);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return this.domain ? `${this.domain}:${this.id}` : this.id;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { z } from "zod";
|
||||
import { DislikeSchema, LikeSchema } from "../../schemas/extensions/likes.ts";
|
||||
import type { JSONObject } from "../../types.ts";
|
||||
import { Entity } from "../entity.ts";
|
||||
import { Entity, Reference } from "../entity.ts";
|
||||
|
||||
export class Like extends Entity {
|
||||
public static override name = "pub.versia:likes/Like";
|
||||
|
|
@ -10,6 +10,14 @@ export class Like extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get liked(): Reference {
|
||||
return Reference.fromString(this.data.liked);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Like> {
|
||||
return LikeSchema.parseAsync(json).then((u) => new Like(u));
|
||||
}
|
||||
|
|
@ -22,6 +30,14 @@ export class Dislike extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get disliked(): Reference {
|
||||
return Reference.fromString(this.data.disliked);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Dislike> {
|
||||
return DislikeSchema.parseAsync(json).then((u) => new Dislike(u));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { z } from "zod";
|
||||
import { VoteSchema } from "../../schemas/extensions/polls.ts";
|
||||
import type { JSONObject } from "../../types.ts";
|
||||
import { Entity } from "../entity.ts";
|
||||
import { Entity, Reference } from "../entity.ts";
|
||||
|
||||
export class Vote extends Entity {
|
||||
public static override name = "pub.versia:polls/Vote";
|
||||
|
|
@ -10,6 +10,14 @@ export class Vote extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get poll(): Reference {
|
||||
return Reference.fromString(this.data.poll);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Vote> {
|
||||
return VoteSchema.parseAsync(json).then((u) => new Vote(u));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { z } from "zod";
|
||||
import { ReactionSchema } from "../../schemas/extensions/reactions.ts";
|
||||
import type { JSONObject } from "../../types.ts";
|
||||
import { Entity } from "../entity.ts";
|
||||
import { Entity, Reference } from "../entity.ts";
|
||||
|
||||
export class Reaction extends Entity {
|
||||
public static override name = "pub.versia:reactions/Reaction";
|
||||
|
|
@ -10,6 +10,14 @@ export class Reaction extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get object(): Reference {
|
||||
return Reference.fromString(this.data.object);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Reaction> {
|
||||
return ReactionSchema.parseAsync(json).then((u) => new Reaction(u));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { z } from "zod";
|
||||
import { ReportSchema } from "../../schemas/extensions/reports.ts";
|
||||
import type { JSONObject } from "../../types.ts";
|
||||
import { Entity } from "../entity.ts";
|
||||
import { Entity, Reference } from "../entity.ts";
|
||||
|
||||
export class Report extends Entity {
|
||||
public static override name = "pub.versia:reports/Report";
|
||||
|
|
@ -10,6 +10,14 @@ export class Report extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference | null {
|
||||
return this.data.author ? Reference.fromString(this.data.author) : null;
|
||||
}
|
||||
|
||||
public get reported(): Reference[] {
|
||||
return this.data.reported.map((r) => Reference.fromString(r));
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Report> {
|
||||
return ReportSchema.parseAsync(json).then((u) => new Report(u));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { z } from "zod";
|
||||
import { ShareSchema } from "../../schemas/extensions/share.ts";
|
||||
import type { JSONObject } from "../../types.ts";
|
||||
import { Entity } from "../entity.ts";
|
||||
import { Entity, Reference } from "../entity.ts";
|
||||
|
||||
export class Share extends Entity {
|
||||
public static override name = "pub.versia:share/Share";
|
||||
|
|
@ -10,6 +10,14 @@ export class Share extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get shared(): Reference {
|
||||
return Reference.fromString(this.data.shared);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Share> {
|
||||
return ShareSchema.parseAsync(json).then((u) => new Share(u));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import {
|
|||
UnfollowSchema,
|
||||
} from "../schemas/follow.ts";
|
||||
import type { JSONObject } from "../types.ts";
|
||||
import { Entity } from "./entity.ts";
|
||||
import { Entity, Reference } from "./entity.ts";
|
||||
|
||||
export class Follow extends Entity {
|
||||
public static override name = "Follow";
|
||||
|
|
@ -15,6 +15,14 @@ export class Follow extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get followee(): Reference {
|
||||
return Reference.fromString(this.data.followee);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Follow> {
|
||||
return FollowSchema.parseAsync(json).then((u) => new Follow(u));
|
||||
}
|
||||
|
|
@ -29,6 +37,14 @@ export class FollowAccept extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get follower(): Reference {
|
||||
return Reference.fromString(this.data.follower);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<FollowAccept> {
|
||||
return FollowAcceptSchema.parseAsync(json).then(
|
||||
(u) => new FollowAccept(u),
|
||||
|
|
@ -45,6 +61,14 @@ export class FollowReject extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get follower(): Reference {
|
||||
return Reference.fromString(this.data.follower);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<FollowReject> {
|
||||
return FollowRejectSchema.parseAsync(json).then(
|
||||
(u) => new FollowReject(u),
|
||||
|
|
@ -59,6 +83,14 @@ export class Unfollow extends Entity {
|
|||
super(data);
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get followee(): Reference {
|
||||
return Reference.fromString(this.data.followee);
|
||||
}
|
||||
|
||||
public static override fromJSON(json: JSONObject): Promise<Unfollow> {
|
||||
return UnfollowSchema.parseAsync(json).then((u) => new Unfollow(u));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ export {
|
|||
VideoContentFormat,
|
||||
} from "./contentformat.ts";
|
||||
export { Delete } from "./delete.ts";
|
||||
export { Entity } from "./entity.ts";
|
||||
export { Entity, Reference } from "./entity.ts";
|
||||
export { Dislike, Like } from "./extensions/likes.ts";
|
||||
export { Vote } from "./extensions/polls.ts";
|
||||
export { Reaction } from "./extensions/reactions.ts";
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import type { z } from "zod";
|
|||
import { NoteSchema } from "../schemas/note.ts";
|
||||
import type { JSONObject } from "../types.ts";
|
||||
import { NonTextContentFormat, TextContentFormat } from "./contentformat.ts";
|
||||
import { Entity } from "./entity.ts";
|
||||
import { Entity, Reference } from "./entity.ts";
|
||||
|
||||
export class Note extends Entity {
|
||||
public static override name = "Note";
|
||||
|
|
@ -15,6 +15,35 @@ export class Note extends Entity {
|
|||
return NoteSchema.parseAsync(json).then((n) => new Note(n));
|
||||
}
|
||||
|
||||
public get author(): Reference {
|
||||
return Reference.fromString(this.data.author);
|
||||
}
|
||||
|
||||
public get group(): Reference | null {
|
||||
if (
|
||||
!this.data.group ||
|
||||
["public", "followers"].includes(this.data.group)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Reference.fromString(this.data.group);
|
||||
}
|
||||
|
||||
public get mentions(): Reference[] {
|
||||
return this.data.mentions.map((m) => Reference.fromString(m));
|
||||
}
|
||||
|
||||
public get quotes(): Reference | null {
|
||||
return this.data.quotes ? Reference.fromString(this.data.quotes) : null;
|
||||
}
|
||||
|
||||
public get repliesTo(): Reference | null {
|
||||
return this.data.replies_to
|
||||
? Reference.fromString(this.data.replies_to)
|
||||
: null;
|
||||
}
|
||||
|
||||
public get attachments(): NonTextContentFormat[] {
|
||||
return (
|
||||
this.data.attachments?.map((a) => new NonTextContentFormat(a)) ?? []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue