refactor(federation): ♻️ Refactor extension entity names in functions

This commit is contained in:
Jesse Wierzbinski 2024-08-25 16:10:38 +02:00
parent d422e28943
commit 01d5ea360e
No known key found for this signature in database
3 changed files with 81 additions and 86 deletions

View file

@ -1,18 +1,18 @@
import type { import type {
Delete, Delete,
Dislike, DislikeExtension,
Follow, Follow,
FollowAccept, FollowAccept,
FollowReject, FollowReject,
Group, Group,
InstanceMetadata, InstanceMetadata,
Like, LikeExtension,
Note, Note,
Reaction, PollVoteExtension,
Share, ReactionExtension,
ShareExtension,
Unfollow, Unfollow,
User, User,
Vote,
} from "./schemas"; } from "./schemas";
import type { EntityValidator } from "./validator"; import type { EntityValidator } from "./validator";
@ -24,14 +24,16 @@ type ParserCallbacks<T> = {
followAccept: (followAccept: FollowAccept) => MaybePromise<T>; followAccept: (followAccept: FollowAccept) => MaybePromise<T>;
followReject: (followReject: FollowReject) => MaybePromise<T>; followReject: (followReject: FollowReject) => MaybePromise<T>;
user: (user: User) => MaybePromise<T>; user: (user: User) => MaybePromise<T>;
like: (like: Like) => MaybePromise<T>; "pub.versia:likes/Like": (like: LikeExtension) => MaybePromise<T>;
dislike: (dislike: Dislike) => MaybePromise<T>; "pub.versia:likes/Dislike": (dislike: DislikeExtension) => MaybePromise<T>;
delete: (undo: Delete) => MaybePromise<T>; delete: (undo: Delete) => MaybePromise<T>;
instanceMetadata: (instanceMetadata: InstanceMetadata) => MaybePromise<T>; instanceMetadata: (instanceMetadata: InstanceMetadata) => MaybePromise<T>;
group: (group: Group) => MaybePromise<T>; group: (group: Group) => MaybePromise<T>;
reaction: (reaction: Reaction) => MaybePromise<T>; "pub.versia:reactions/Reaction": (
share: (share: Share) => MaybePromise<T>; reaction: ReactionExtension,
vote: (vote: Vote) => MaybePromise<T>; ) => MaybePromise<T>;
"pub.versia:share/Share": (share: ShareExtension) => MaybePromise<T>;
"pub.versia:polls/Vote": (vote: PollVoteExtension) => MaybePromise<T>;
unfollow: (unfollow: Unfollow) => MaybePromise<T>; unfollow: (unfollow: Unfollow) => MaybePromise<T>;
unknown: (data: unknown) => MaybePromise<T>; unknown: (data: unknown) => MaybePromise<T>;
}; };
@ -140,19 +142,21 @@ export class RequestParserHandler {
break; break;
} }
case "Like": { case "Like": {
const like = await this.validator.Like(this.body); const like = await this.validator.LikeExtension(this.body);
if (callbacks.like) { if (callbacks["pub.versia:likes/Like"]) {
return await callbacks.like(like); return await callbacks["pub.versia:likes/Like"](like);
} }
break; break;
} }
case "Dislike": { case "Dislike": {
const dislike = await this.validator.Dislike(this.body); const dislike = await this.validator.DislikeExtension(
this.body,
);
if (callbacks.dislike) { if (callbacks["pub.versia:likes/Dislike"]) {
return await callbacks.dislike(dislike); return await callbacks["pub.versia:likes/Dislike"](dislike);
} }
break; break;
@ -188,28 +192,32 @@ export class RequestParserHandler {
break; break;
} }
case "Reaction": { case "Reaction": {
const reaction = await this.validator.Reaction(this.body); const reaction = await this.validator.ReactionExtension(
this.body,
);
if (callbacks.reaction) { if (callbacks["pub.versia:reactions/Reaction"]) {
return await callbacks.reaction(reaction); return await callbacks["pub.versia:reactions/Reaction"](
reaction,
);
} }
break; break;
} }
case "Share": { case "Share": {
const share = await this.validator.Share(this.body); const share = await this.validator.ShareExtension(this.body);
if (callbacks.share) { if (callbacks["pub.versia:share/Share"]) {
return await callbacks.share(share); return await callbacks["pub.versia:share/Share"](share);
} }
break; break;
} }
case "Vote": { case "Vote": {
const vote = await this.validator.Vote(this.body); const vote = await this.validator.PollVoteExtension(this.body);
if (callbacks.vote) { if (callbacks["pub.versia:polls/Vote"]) {
return await callbacks.vote(vote); return await callbacks["pub.versia:polls/Vote"](vote);
} }
break; break;

View file

@ -13,16 +13,10 @@ import type {
GroupSchema, GroupSchema,
InstanceMetadataSchema, InstanceMetadataSchema,
NoteSchema, NoteSchema,
PublicKeyDataSchema,
UnfollowSchema, UnfollowSchema,
UserSchema, UserSchema,
} from "./schemas/base"; } from "./schemas/base";
import type { import type { ContentFormatSchema } from "./schemas/content_format";
AudioOnlyContentFormatSchema,
ContentFormatSchema,
ImageOnlyContentFormatSchema,
TextOnlyContentFormatSchema,
} from "./schemas/content_format";
import type { ExtensionPropertySchema } from "./schemas/extensions"; import type { ExtensionPropertySchema } from "./schemas/extensions";
import type { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis"; import type { CustomEmojiExtensionSchema } from "./schemas/extensions/custom_emojis";
import type { LikeSchema } from "./schemas/extensions/likes"; import type { LikeSchema } from "./schemas/extensions/likes";
@ -37,25 +31,21 @@ type AnyZod = z.ZodType<any, any, any>;
type InferType<T extends AnyZod> = z.infer<T>; type InferType<T extends AnyZod> = z.infer<T>;
export type Note = InferType<typeof NoteSchema>; export type Note = InferType<typeof NoteSchema>;
export type ActorPublicKeyData = InferType<typeof PublicKeyDataSchema>; export type EntityExtensionProperty = InferType<typeof ExtensionPropertySchema>;
export type ExtensionProperty = InferType<typeof ExtensionPropertySchema>;
export type VanityExtension = InferType<typeof VanityExtensionSchema>; export type VanityExtension = InferType<typeof VanityExtensionSchema>;
export type User = InferType<typeof UserSchema>; export type User = InferType<typeof UserSchema>;
export type Follow = InferType<typeof FollowSchema>; export type Follow = InferType<typeof FollowSchema>;
export type FollowAccept = InferType<typeof FollowAcceptSchema>; export type FollowAccept = InferType<typeof FollowAcceptSchema>;
export type FollowReject = InferType<typeof FollowRejectSchema>; export type FollowReject = InferType<typeof FollowRejectSchema>;
export type ContentFormat = InferType<typeof ContentFormatSchema>; 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 CustomEmojiExtension = InferType<typeof CustomEmojiExtensionSchema>;
export type Entity = InferType<typeof EntitySchema>; export type Entity = InferType<typeof EntitySchema>;
export type Delete = InferType<typeof DeleteSchema>; export type Delete = InferType<typeof DeleteSchema>;
export type Group = InferType<typeof GroupSchema>; export type Group = InferType<typeof GroupSchema>;
export type InstanceMetadata = InferType<typeof InstanceMetadataSchema>; export type InstanceMetadata = InferType<typeof InstanceMetadataSchema>;
export type Unfollow = InferType<typeof UnfollowSchema>; export type Unfollow = InferType<typeof UnfollowSchema>;
export type Like = InferType<typeof LikeSchema>; export type LikeExtension = InferType<typeof LikeSchema>;
export type Dislike = InferType<typeof LikeSchema>; export type DislikeExtension = InferType<typeof LikeSchema>;
export type Vote = InferType<typeof VoteSchema>; export type PollVoteExtension = InferType<typeof VoteSchema>;
export type Reaction = InferType<typeof ReactionSchema>; export type ReactionExtension = InferType<typeof ReactionSchema>;
export type Share = InferType<typeof ShareSchema>; export type ShareExtension = InferType<typeof ShareSchema>;

View file

@ -1,5 +1,26 @@
import type { z } from "zod"; import type { z } from "zod";
import { fromError } from "zod-validation-error"; 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 { import {
DeleteSchema, DeleteSchema,
EntitySchema, EntitySchema,
@ -9,7 +30,6 @@ import {
GroupSchema, GroupSchema,
InstanceMetadataSchema, InstanceMetadataSchema,
NoteSchema, NoteSchema,
PublicKeyDataSchema,
UnfollowSchema, UnfollowSchema,
UserSchema, UserSchema,
} from "./schemas/base"; } from "./schemas/base";
@ -75,29 +95,16 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); 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. * Validates a VanityExtension entity.
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @returns A promise that resolves to the validated data.
*/ */
public VanityExtension( public VanityExtension(data: unknown): Promise<VanityExtension> {
data: unknown,
): Promise<InferType<typeof VanityExtensionSchema>> {
return this.validate(VanityExtensionSchema, data); return this.validate(VanityExtensionSchema, data);
} }
@ -106,7 +113,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(UserSchema, data);
} }
@ -115,7 +122,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(FollowSchema, data);
} }
@ -124,9 +131,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @returns A promise that resolves to the validated data.
*/ */
public FollowAccept( public FollowAccept(data: unknown): Promise<FollowAccept> {
data: unknown,
): Promise<InferType<typeof FollowAcceptSchema>> {
return this.validate(FollowAcceptSchema, data); return this.validate(FollowAcceptSchema, data);
} }
@ -135,9 +140,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @returns A promise that resolves to the validated data.
*/ */
public FollowReject( public FollowReject(data: unknown): Promise<FollowReject> {
data: unknown,
): Promise<InferType<typeof FollowRejectSchema>> {
return this.validate(FollowRejectSchema, data); return this.validate(FollowRejectSchema, data);
} }
@ -146,9 +149,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @returns A promise that resolves to the validated data.
*/ */
public ContentFormat( public ContentFormat(data: unknown): Promise<ContentFormat> {
data: unknown,
): Promise<InferType<typeof ContentFormatSchema>> {
return this.validate(ContentFormatSchema, data); return this.validate(ContentFormatSchema, data);
} }
@ -157,9 +158,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @returns A promise that resolves to the validated data.
*/ */
public CustomEmojiExtension( public CustomEmojiExtension(data: unknown): Promise<CustomEmojiExtension> {
data: unknown,
): Promise<InferType<typeof CustomEmojiExtensionSchema>> {
return this.validate(CustomEmojiExtensionSchema, data); return this.validate(CustomEmojiExtensionSchema, data);
} }
@ -168,7 +167,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(EntitySchema, data);
} }
@ -177,9 +176,9 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @returns A promise that resolves to the validated data.
*/ */
public ExtensionProperty( public EntityExtensionProperty(
data: unknown, data: unknown,
): Promise<InferType<typeof ExtensionPropertySchema>> { ): Promise<EntityExtensionProperty> {
return this.validate(ExtensionPropertySchema, data); return this.validate(ExtensionPropertySchema, data);
} }
@ -188,7 +187,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(DeleteSchema, data);
} }
@ -197,7 +196,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(GroupSchema, data);
} }
@ -206,9 +205,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @returns A promise that resolves to the validated data.
*/ */
public InstanceMetadata( public InstanceMetadata(data: unknown): Promise<InstanceMetadata> {
data: unknown,
): Promise<InferType<typeof InstanceMetadataSchema>> {
return this.validate(InstanceMetadataSchema, data); return this.validate(InstanceMetadataSchema, data);
} }
@ -217,7 +214,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(UnfollowSchema, data);
} }
@ -226,7 +223,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(LikeSchema, data);
} }
@ -235,7 +232,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(LikeSchema, data);
} }
@ -244,7 +241,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(VoteSchema, data);
} }
@ -253,7 +250,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(ReactionSchema, data);
} }
@ -262,7 +259,7 @@ export class EntityValidator {
* @param data - The data to validate * @param data - The data to validate
* @returns A promise that resolves to the validated data. * @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); return this.validate(ShareSchema, data);
} }
} }