Skip to content

Commit

Permalink
refactor(federation): 👽 Update rest of federation code to Working Dra…
Browse files Browse the repository at this point in the history
…ft 4
  • Loading branch information
CPlusPatch committed Aug 25, 2024
1 parent 4a470f5 commit d422e28
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 40 deletions.
8 changes: 3 additions & 5 deletions federation/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("RequestParserHandler", () => {
expect(noteCallback).not.toHaveBeenCalled();
});

test("Throw on incorrect body type property", async () => {
test("Correctly handle unknown body type property", async () => {
const handler = new RequestParserHandler(
{
type: "DoesntExist",
Expand All @@ -89,9 +89,7 @@ describe("RequestParserHandler", () => {
);

const noteCallback = jest.fn();
await expect(
handler.parseBody({ note: noteCallback }),
).rejects.toThrow();
expect(noteCallback).not.toHaveBeenCalled();
await handler.parseBody({ unknown: noteCallback });
expect(noteCallback).toHaveBeenCalled();
});
});
104 changes: 70 additions & 34 deletions federation/http.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type {
Delete,
Dislike,
Extension,
Follow,
FollowAccept,
FollowReject,
Group,
InstanceMetadata,
Like,
Note,
Patch,
ServerMetadata,
Undo,
Reaction,
Share,
Unfollow,
User,
Vote,
} from "./schemas";
import type { EntityValidator } from "./validator";

Expand All @@ -23,10 +26,14 @@ type ParserCallbacks<T> = {
user: (user: User) => MaybePromise<T>;
like: (like: Like) => MaybePromise<T>;
dislike: (dislike: Dislike) => MaybePromise<T>;
undo: (undo: Undo) => MaybePromise<T>;
serverMetadata: (serverMetadata: ServerMetadata) => MaybePromise<T>;
extension: (extension: Extension) => MaybePromise<T>;
patch: (patch: Patch) => MaybePromise<T>;
delete: (undo: Delete) => MaybePromise<T>;
instanceMetadata: (instanceMetadata: InstanceMetadata) => MaybePromise<T>;
group: (group: Group) => MaybePromise<T>;
reaction: (reaction: Reaction) => MaybePromise<T>;
share: (share: Share) => MaybePromise<T>;
vote: (vote: Vote) => MaybePromise<T>;
unfollow: (unfollow: Unfollow) => MaybePromise<T>;
unknown: (data: unknown) => MaybePromise<T>;
};

/**
Expand Down Expand Up @@ -92,15 +99,6 @@ export class RequestParserHandler {

break;
}
case "Patch": {
const patch = await this.validator.Patch(this.body);

if (callbacks.patch) {
return await callbacks.patch(patch);
}

break;
}
case "Follow": {
const follow = await this.validator.Follow(this.body);

Expand Down Expand Up @@ -159,41 +157,79 @@ export class RequestParserHandler {

break;
}
case "Undo": {
const undo = await this.validator.Undo(this.body);
case "Delete": {
// "delete" isn't an allowed variable name
const delete_ = await this.validator.Delete(this.body);

if (callbacks.undo) {
return await callbacks.undo(undo);
if (callbacks.delete) {
return await callbacks.delete(delete_);
}

break;
}
case "ServerMetadata": {
const serverMetadata = await this.validator.ServerMetadata(
case "InstanceMetadata": {
const instanceMetadata = await this.validator.InstanceMetadata(
this.body,
);

if (callbacks.serverMetadata) {
return await callbacks.serverMetadata(serverMetadata);
if (callbacks.instanceMetadata) {
return await callbacks.instanceMetadata(instanceMetadata);
}

break;
}
case "Extension": {
const extension = await this.validator.Extension(this.body);
case "Group": {
const group = await this.validator.Group(this.body);

if (callbacks.extension) {
return await callbacks.extension(extension);
if (callbacks.group) {
return await callbacks.group(group);
}

break;
}
default:
throw new Error(
`Invalid type field in body: ${this.body.type}`,
);
case "Reaction": {
const reaction = await this.validator.Reaction(this.body);

if (callbacks.reaction) {
return await callbacks.reaction(reaction);
}

break;
}
case "Share": {
const share = await this.validator.Share(this.body);

if (callbacks.share) {
return await callbacks.share(share);
}

break;
}
case "Vote": {
const vote = await this.validator.Vote(this.body);

if (callbacks.vote) {
return await callbacks.vote(vote);
}

break;
}
case "Unfollow": {
const unfollow = await this.validator.Unfollow(this.body);

if (callbacks.unfollow) {
return await callbacks.unfollow(unfollow);
}

break;
}
default: {
if (callbacks.unknown) {
return await callbacks.unknown(this.body);
}
}
}

throw new Error(`Invalid type field in body: ${this.body.type}`);
return undefined as ReturnType;
}
}
19 changes: 18 additions & 1 deletion federation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ import type {
UnfollowSchema,
UserSchema,
} from "./schemas/base";
import type { ContentFormatSchema } from "./schemas/content_format";
import type {
AudioOnlyContentFormatSchema,
ContentFormatSchema,
ImageOnlyContentFormatSchema,
TextOnlyContentFormatSchema,
} from "./schemas/content_format";
import type { ExtensionPropertySchema } from "./schemas/extensions";
import type { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis";
import type { LikeSchema } from "./schemas/extensions/likes";
import type { VoteSchema } from "./schemas/extensions/polls";
import type { ReactionSchema } from "./schemas/extensions/reactions";
import type { ShareSchema } from "./schemas/extensions/share";
import type { VanityExtensionSchema } from "./schemas/extensions/vanity";

// biome-ignore lint/suspicious/noExplicitAny: Used only as a base type
Expand All @@ -36,9 +45,17 @@ export type Follow = InferType<typeof FollowSchema>;
export type FollowAccept = InferType<typeof FollowAcceptSchema>;
export type FollowReject = InferType<typeof FollowRejectSchema>;
export type ContentFormat = InferType<typeof ContentFormatSchema>;
export type ImageContentFormat = InferType<typeof ImageOnlyContentFormatSchema>;
export type TextContentFormat = InferType<typeof TextOnlyContentFormatSchema>;
export type AudioContentFormat = InferType<typeof AudioOnlyContentFormatSchema>;
export type CustomEmojiExtension = InferType<typeof CustomEmojiExtensionSchema>;
export type Entity = InferType<typeof EntitySchema>;
export type Delete = InferType<typeof DeleteSchema>;
export type Group = InferType<typeof GroupSchema>;
export type InstanceMetadata = InferType<typeof InstanceMetadataSchema>;
export type Unfollow = InferType<typeof UnfollowSchema>;
export type Like = InferType<typeof LikeSchema>;
export type Dislike = InferType<typeof LikeSchema>;
export type Vote = InferType<typeof VoteSchema>;
export type Reaction = InferType<typeof ReactionSchema>;
export type Share = InferType<typeof ShareSchema>;
91 changes: 91 additions & 0 deletions federation/validator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import type { z } from "zod";
import { fromError } from "zod-validation-error";
import {
DeleteSchema,
EntitySchema,
FollowAcceptSchema,
FollowRejectSchema,
FollowSchema,
GroupSchema,
InstanceMetadataSchema,
NoteSchema,
PublicKeyDataSchema,
UnfollowSchema,
UserSchema,
} from "./schemas/base";
import { ContentFormatSchema } from "./schemas/content_format";
import { ExtensionPropertySchema } from "./schemas/extensions";
import { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis";
import { LikeSchema } from "./schemas/extensions/likes";
import { VoteSchema } from "./schemas/extensions/polls";
import { ReactionSchema } from "./schemas/extensions/reactions";
import { ShareSchema } from "./schemas/extensions/share";
import { VanityExtensionSchema } from "./schemas/extensions/vanity";

// biome-ignore lint/suspicious/noExplicitAny: Used only as a base type
Expand Down Expand Up @@ -174,4 +182,87 @@ export class EntityValidator {
): Promise<InferType<typeof ExtensionPropertySchema>> {
return this.validate(ExtensionPropertySchema, data);
}

/**
* Validates a Delete entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Delete(data: unknown): Promise<InferType<typeof DeleteSchema>> {
return this.validate(DeleteSchema, data);
}

/**
* Validates a Group entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Group(data: unknown): Promise<InferType<typeof GroupSchema>> {
return this.validate(GroupSchema, data);
}

/**
* Validates an InstanceMetadata entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public InstanceMetadata(
data: unknown,
): Promise<InferType<typeof InstanceMetadataSchema>> {
return this.validate(InstanceMetadataSchema, data);
}

/**
* Validates an Unfollow entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Unfollow(data: unknown): Promise<InferType<typeof UnfollowSchema>> {
return this.validate(UnfollowSchema, data);
}

/**
* Validates a Like entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Like(data: unknown): Promise<InferType<typeof LikeSchema>> {
return this.validate(LikeSchema, data);
}

/**
* Validates a Dislike entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Dislike(data: unknown): Promise<InferType<typeof LikeSchema>> {
return this.validate(LikeSchema, data);
}

/**
* Validates a Vote entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Vote(data: unknown): Promise<InferType<typeof VoteSchema>> {
return this.validate(VoteSchema, data);
}

/**
* Validates a Reaction entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Reaction(data: unknown): Promise<InferType<typeof ReactionSchema>> {
return this.validate(ReactionSchema, data);
}

/**
* Validates a Share entity.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Share(data: unknown): Promise<InferType<typeof ShareSchema>> {
return this.validate(ShareSchema, data);
}
}

0 comments on commit d422e28

Please sign in to comment.