Skip to content

Commit

Permalink
refactor(federation): ♻️ Refactor extension entity names in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed Aug 25, 2024
1 parent d422e28 commit 01d5ea3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 86 deletions.
58 changes: 33 additions & 25 deletions federation/http.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type {
Delete,
Dislike,
DislikeExtension,
Follow,
FollowAccept,
FollowReject,
Group,
InstanceMetadata,
Like,
LikeExtension,
Note,
Reaction,
Share,
PollVoteExtension,
ReactionExtension,
ShareExtension,
Unfollow,
User,
Vote,
} from "./schemas";
import type { EntityValidator } from "./validator";

Expand All @@ -24,14 +24,16 @@ type ParserCallbacks<T> = {
followAccept: (followAccept: FollowAccept) => MaybePromise<T>;
followReject: (followReject: FollowReject) => MaybePromise<T>;
user: (user: User) => MaybePromise<T>;
like: (like: Like) => MaybePromise<T>;
dislike: (dislike: Dislike) => MaybePromise<T>;
"pub.versia:likes/Like": (like: LikeExtension) => MaybePromise<T>;
"pub.versia:likes/Dislike": (dislike: DislikeExtension) => 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>;
"pub.versia:reactions/Reaction": (
reaction: ReactionExtension,
) => MaybePromise<T>;
"pub.versia:share/Share": (share: ShareExtension) => MaybePromise<T>;
"pub.versia:polls/Vote": (vote: PollVoteExtension) => MaybePromise<T>;
unfollow: (unfollow: Unfollow) => MaybePromise<T>;
unknown: (data: unknown) => MaybePromise<T>;
};
Expand Down Expand Up @@ -140,19 +142,21 @@ export class RequestParserHandler {
break;
}
case "Like": {
const like = await this.validator.Like(this.body);
const like = await this.validator.LikeExtension(this.body);

if (callbacks.like) {
return await callbacks.like(like);
if (callbacks["pub.versia:likes/Like"]) {
return await callbacks["pub.versia:likes/Like"](like);
}

break;
}
case "Dislike": {
const dislike = await this.validator.Dislike(this.body);
const dislike = await this.validator.DislikeExtension(
this.body,
);

if (callbacks.dislike) {
return await callbacks.dislike(dislike);
if (callbacks["pub.versia:likes/Dislike"]) {
return await callbacks["pub.versia:likes/Dislike"](dislike);
}

break;
Expand Down Expand Up @@ -188,28 +192,32 @@ export class RequestParserHandler {
break;
}
case "Reaction": {
const reaction = await this.validator.Reaction(this.body);
const reaction = await this.validator.ReactionExtension(
this.body,
);

if (callbacks.reaction) {
return await callbacks.reaction(reaction);
if (callbacks["pub.versia:reactions/Reaction"]) {
return await callbacks["pub.versia:reactions/Reaction"](
reaction,
);
}

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

if (callbacks.share) {
return await callbacks.share(share);
if (callbacks["pub.versia:share/Share"]) {
return await callbacks["pub.versia:share/Share"](share);
}

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

if (callbacks.vote) {
return await callbacks.vote(vote);
if (callbacks["pub.versia:polls/Vote"]) {
return await callbacks["pub.versia:polls/Vote"](vote);
}

break;
Expand Down
24 changes: 7 additions & 17 deletions federation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ import type {
GroupSchema,
InstanceMetadataSchema,
NoteSchema,
PublicKeyDataSchema,
UnfollowSchema,
UserSchema,
} from "./schemas/base";
import type {
AudioOnlyContentFormatSchema,
ContentFormatSchema,
ImageOnlyContentFormatSchema,
TextOnlyContentFormatSchema,
} from "./schemas/content_format";
import type { ContentFormatSchema } 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";
Expand All @@ -37,25 +31,21 @@ type AnyZod = z.ZodType<any, any, any>;
type InferType<T extends AnyZod> = z.infer<T>;

export type Note = InferType<typeof NoteSchema>;
export type ActorPublicKeyData = InferType<typeof PublicKeyDataSchema>;
export type ExtensionProperty = InferType<typeof ExtensionPropertySchema>;
export type EntityExtensionProperty = InferType<typeof ExtensionPropertySchema>;
export type VanityExtension = InferType<typeof VanityExtensionSchema>;
export type User = InferType<typeof UserSchema>;
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>;
export type LikeExtension = InferType<typeof LikeSchema>;
export type DislikeExtension = InferType<typeof LikeSchema>;
export type PollVoteExtension = InferType<typeof VoteSchema>;
export type ReactionExtension = InferType<typeof ReactionSchema>;
export type ShareExtension = InferType<typeof ShareSchema>;
85 changes: 41 additions & 44 deletions federation/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import type { z } from "zod";
import { fromError } from "zod-validation-error";
import type {
ContentFormat,
CustomEmojiExtension,
Delete,
DislikeExtension,
Entity,
EntityExtensionProperty,
Follow,
FollowAccept,
FollowReject,
Group,
InstanceMetadata,
LikeExtension,
Note,
PollVoteExtension,
ReactionExtension,
ShareExtension,
Unfollow,
User,
VanityExtension,
} from "./schemas";
import {
DeleteSchema,
EntitySchema,
Expand All @@ -9,7 +30,6 @@ import {
GroupSchema,
InstanceMetadataSchema,
NoteSchema,
PublicKeyDataSchema,
UnfollowSchema,
UserSchema,
} from "./schemas/base";
Expand Down Expand Up @@ -75,29 +95,16 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Note(data: unknown): Promise<InferType<typeof NoteSchema>> {
public Note(data: unknown): Promise<Note> {
return this.validate(NoteSchema, data);
}

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

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

Expand All @@ -106,7 +113,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public User(data: unknown): Promise<InferType<typeof UserSchema>> {
public User(data: unknown): Promise<User> {
return this.validate(UserSchema, data);
}

Expand All @@ -115,7 +122,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Follow(data: unknown): Promise<InferType<typeof FollowSchema>> {
public Follow(data: unknown): Promise<Follow> {
return this.validate(FollowSchema, data);
}

Expand All @@ -124,9 +131,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public FollowAccept(
data: unknown,
): Promise<InferType<typeof FollowAcceptSchema>> {
public FollowAccept(data: unknown): Promise<FollowAccept> {
return this.validate(FollowAcceptSchema, data);
}

Expand All @@ -135,9 +140,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public FollowReject(
data: unknown,
): Promise<InferType<typeof FollowRejectSchema>> {
public FollowReject(data: unknown): Promise<FollowReject> {
return this.validate(FollowRejectSchema, data);
}

Expand All @@ -146,9 +149,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public ContentFormat(
data: unknown,
): Promise<InferType<typeof ContentFormatSchema>> {
public ContentFormat(data: unknown): Promise<ContentFormat> {
return this.validate(ContentFormatSchema, data);
}

Expand All @@ -157,9 +158,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public CustomEmojiExtension(
data: unknown,
): Promise<InferType<typeof CustomEmojiExtensionSchema>> {
public CustomEmojiExtension(data: unknown): Promise<CustomEmojiExtension> {
return this.validate(CustomEmojiExtensionSchema, data);
}

Expand All @@ -168,7 +167,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Entity(data: unknown): Promise<InferType<typeof EntitySchema>> {
public Entity(data: unknown): Promise<Entity> {
return this.validate(EntitySchema, data);
}

Expand All @@ -177,9 +176,9 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public ExtensionProperty(
public EntityExtensionProperty(
data: unknown,
): Promise<InferType<typeof ExtensionPropertySchema>> {
): Promise<EntityExtensionProperty> {
return this.validate(ExtensionPropertySchema, data);
}

Expand All @@ -188,7 +187,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Delete(data: unknown): Promise<InferType<typeof DeleteSchema>> {
public Delete(data: unknown): Promise<Delete> {
return this.validate(DeleteSchema, data);
}

Expand All @@ -197,7 +196,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Group(data: unknown): Promise<InferType<typeof GroupSchema>> {
public Group(data: unknown): Promise<Group> {
return this.validate(GroupSchema, data);
}

Expand All @@ -206,9 +205,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public InstanceMetadata(
data: unknown,
): Promise<InferType<typeof InstanceMetadataSchema>> {
public InstanceMetadata(data: unknown): Promise<InstanceMetadata> {
return this.validate(InstanceMetadataSchema, data);
}

Expand All @@ -217,7 +214,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Unfollow(data: unknown): Promise<InferType<typeof UnfollowSchema>> {
public Unfollow(data: unknown): Promise<Unfollow> {
return this.validate(UnfollowSchema, data);
}

Expand All @@ -226,7 +223,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Like(data: unknown): Promise<InferType<typeof LikeSchema>> {
public LikeExtension(data: unknown): Promise<LikeExtension> {
return this.validate(LikeSchema, data);
}

Expand All @@ -235,7 +232,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Dislike(data: unknown): Promise<InferType<typeof LikeSchema>> {
public DislikeExtension(data: unknown): Promise<DislikeExtension> {
return this.validate(LikeSchema, data);
}

Expand All @@ -244,7 +241,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Vote(data: unknown): Promise<InferType<typeof VoteSchema>> {
public PollVoteExtension(data: unknown): Promise<PollVoteExtension> {
return this.validate(VoteSchema, data);
}

Expand All @@ -253,7 +250,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Reaction(data: unknown): Promise<InferType<typeof ReactionSchema>> {
public ReactionExtension(data: unknown): Promise<ReactionExtension> {
return this.validate(ReactionSchema, data);
}

Expand All @@ -262,7 +259,7 @@ export class EntityValidator {
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public Share(data: unknown): Promise<InferType<typeof ShareSchema>> {
public ShareExtension(data: unknown): Promise<ShareExtension> {
return this.validate(ShareSchema, data);
}
}

0 comments on commit 01d5ea3

Please sign in to comment.