UNPKG

threadiverse

Version:

Unified typescript client for threadiverse instances (Lemmy, Piefed, Mbin etc)

1,815 lines (1,762 loc) 116 kB
import { satisfies } from 'compare-versions'; import * as LemmyV0 from 'lemmy-js-client-v0'; import { z } from 'zod/v4-mini'; import * as LemmyV1 from 'lemmy-js-client-v1'; import createClient from 'openapi-fetch'; class BaseClient { static mode; static softwareName; /** * NPM semver range, e.g. ">=1.0.0 <2.0.0" */ static softwareVersionRange; } class FediverseError extends Error { constructor(message, errorOptions) { super(message, errorOptions); this.name = "FediverseError"; } } class InvalidPayloadError extends FediverseError { constructor(message) { super(message); this.name = "InvalidPayloadError"; } } class UnexpectedResponseError extends FediverseError { constructor(message) { super(message); this.name = "UnexpectedResponseError"; } } class UnsupportedError extends FediverseError { constructor(message) { super(message); this.name = "UnsupportedError"; } } class UnsupportedSoftwareError extends UnsupportedError { constructor(message) { super(message); this.name = "UnsupportedSoftwareError"; } } function cleanThreadiverseParams(payload) { delete payload.mode; return payload; } const Person = z.object({ actor_id: z.string(), avatar: z.optional(z.string()), bot_account: z.boolean(), deleted: z.boolean(), display_name: z.optional(z.string()), id: z.number(), local: z.boolean(), name: z.string(), published: z.string() }); const PersonAggregates = z.object({ comment_count: z.number(), post_count: z.number() }); const PersonView = z.object({ counts: PersonAggregates, is_admin: z.boolean(), person: Person }); const Comment = z.object({ /** * The federated activity id / ap_id. */ ap_id: z.string(), content: z.string(), creator_id: z.number(), /** * Whether the comment has been deleted by its creator. */ deleted: z.boolean(), /** * Whether the comment has been distinguished(speaking officially) by a mod. */ distinguished: z.boolean(), id: z.number(), language_id: z.number(), /** * Whether the comment is local. */ local: z.boolean(), /** * The path / tree location of a comment, separated by dots, ending with the comment's id. Ex: * 0.24.27 */ path: z.string(), post_id: z.number(), published: z.string(), /** * Whether the comment has been removed. */ removed: z.boolean(), updated: z.optional(z.string()) }); const CommentReply = z.object({ comment_id: z.number(), id: z.number(), published: z.string(), read: z.boolean(), recipient_id: z.number() }); const CommunityVisibility = z.enum([ "Public", "Unlisted", "LocalOnlyPublic", "LocalOnlyPrivate", "Private" ]); const Community = z.object({ actor_id: z.string(), banner: z.optional(z.string()), deleted: z.boolean(), description: z.optional(z.string()), hidden: z.boolean(), icon: z.optional(z.string()), id: z.number(), local: z.boolean(), name: z.string(), nsfw: z.boolean(), posting_restricted_to_mods: z.boolean(), published: z.string(), removed: z.boolean(), title: z.string(), updated: z.optional(z.string()), visibility: CommunityVisibility }); const Post = z.object({ /** * An optional alt_text, usable for image posts. */ alt_text: z.optional(z.string()), /** * The federated activity id / ap_id. */ ap_id: z.string(), /** * An optional post body, in markdown. */ body: z.optional(z.string()), community_id: z.number(), creator_id: z.number(), /** * Whether the post is deleted. */ deleted: z.boolean(), /** * A description for the link. */ embed_description: z.optional(z.string()), /** * A title for the link. */ embed_title: z.optional(z.string()), /** * Whether the post is featured to its community. */ featured_community: z.boolean(), /** * Whether the post is featured to its site. */ featured_local: z.boolean(), id: z.number(), language_id: z.number(), /** * Whether the post is local. */ local: z.boolean(), /** * Whether the post is locked. */ locked: z.boolean(), name: z.string(), /** * Whether the post is NSFW. */ nsfw: z.boolean(), published: z.string(), /** * Whether the post is removed. */ removed: z.boolean(), /** * A thumbnail picture url. */ thumbnail_url: z.optional(z.string()), updated: z.optional(z.string()), /** * An optional link / url for the post. */ url: z.optional(z.string()), url_content_type: z.optional(z.string()) }); const SubscribedType = z.enum([ "Subscribed", "NotSubscribed", "Pending", "ApprovalRequired" ]); const Vote = z.number().check(z.gte(-1), z.lte(1)); const CommentAggregates = z.object({ /** * The total number of children in this comment branch. */ child_count: z.number(), comment_id: z.number(), downvotes: z.number(), published: z.string(), score: z.number(), upvotes: z.number() }); const CommentView = z.object({ banned_from_community: z.boolean(), comment: Comment, community: Community, counts: CommentAggregates, creator: Person, creator_banned_from_community: z.boolean(), creator_is_admin: z.boolean(), creator_is_moderator: z.boolean(), my_vote: z.optional(Vote), post: Post, saved: z.boolean(), subscribed: SubscribedType }); const CommentReplyView = z.object({ banned_from_community: z.boolean(), comment: Comment, comment_reply: CommentReply, community: Community, counts: CommentAggregates, creator: Person, creator_banned_from_community: z.boolean(), creator_blocked: z.boolean(), creator_is_admin: z.boolean(), creator_is_moderator: z.boolean(), my_vote: z.optional(Vote), post: Post, recipient: Person, saved: z.boolean(), subscribed: SubscribedType }); const CommentReport = z.object({ comment_id: z.number(), creator_id: z.number(), id: z.number(), original_comment_text: z.string(), published: z.string(), reason: z.string(), resolved: z.boolean(), resolver_id: z.optional(z.number()), updated: z.optional(z.string()) }); const CommentReportView = z.object({ comment: Comment, comment_creator: Person, comment_report: CommentReport, community: Community, counts: CommentAggregates, creator: Person, creator_banned_from_community: z.boolean(), creator_blocked: z.boolean(), creator_is_admin: z.boolean(), creator_is_moderator: z.boolean(), my_vote: z.optional(Vote), post: Post, resolver: z.optional(Person), saved: z.boolean(), subscribed: SubscribedType }); const BaseCommunityAggregates = z.object({ comments: z.number(), posts: z.number(), subscribers: z.number() }); const ExtendedCommunityAggregates = z.object({ comments: z.number(), posts: z.number(), subscribers: z.number(), subscribers_local: z.number(), /** * The number of users with any activity in the last day. */ users_active_day: z.number(), /** * The number of users with any activity in the last year. */ users_active_half_year: z.number(), /** * The number of users with any activity in the last month. */ users_active_month: z.number(), /** * The number of users with any activity in the last week. */ users_active_week: z.number() }); const CommunityAggregates = z.union([ BaseCommunityAggregates, ExtendedCommunityAggregates ]); const CommunityFollowerView = z.object({ community: Community, follower: Person }); const CommunityModeratorView = z.object({ community: Community, moderator: Person }); const CommunityView = z.object({ blocked: z.boolean(), community: Community, counts: CommunityAggregates, subscribed: SubscribedType }); const ReadableFederationState = z.object({ /** * how many failed attempts have been made to send the next activity */ fail_count: z.number(), instance_id: z.number(), /** * timestamp of the last retry attempt (when the last failing activity was resent) */ last_retry: z.optional(z.string()), /** * the last successfully sent activity id */ last_successful_id: z.optional(z.number()), last_successful_published_time: z.optional(z.string()), /** * timestamp of the next retry attempt (null if fail count is 0) */ next_retry: z.optional(z.string()) }); const InstanceWithFederationState = z.object({ domain: z.string(), /** * if federation to this instance is or was active, show state of outgoing federation to this * instance */ federation_state: z.optional(ReadableFederationState), id: z.number(), published: z.string(), software: z.optional(z.string()), updated: z.optional(z.string()), version: z.optional(z.string()) }); const FederatedInstances = z.object({ allowed: z.array(InstanceWithFederationState), blocked: z.array(InstanceWithFederationState), linked: z.array(InstanceWithFederationState) }); const CaptchaResponse = z.object({ /** * A Base64 encoded png */ png: z.string(), /** * The UUID for the captcha item. */ uuid: z.string(), /** * A Base64 encoded wav audio */ wav: z.string() }); const GetCaptchaResponse = z.object({ /** * Will be None if captchas are disabled. */ ok: z.optional(CaptchaResponse) }); const GetCommunityResponse = z.object({ community_view: CommunityView, moderators: z.array(CommunityModeratorView) }); const ModRemovePost = z.object({ id: z.number(), mod_person_id: z.number(), post_id: z.number(), reason: z.optional(z.string()), removed: z.boolean(), when_: z.string() }); const ModLockPost = z.object({ id: z.number(), locked: z.boolean(), mod_person_id: z.number(), post_id: z.number(), when_: z.string() }); const ModFeaturePost = z.object({ featured: z.boolean(), id: z.number(), is_featured_community: z.boolean(), mod_person_id: z.number(), post_id: z.number(), when_: z.string() }); const ModRemoveComment = z.object({ comment_id: z.number(), id: z.number(), mod_person_id: z.number(), reason: z.optional(z.string()), removed: z.boolean(), when_: z.string() }); const ModRemoveCommunity = z.object({ community_id: z.number(), id: z.number(), mod_person_id: z.number(), reason: z.optional(z.string()), removed: z.boolean(), when_: z.string() }); const ModBanFromCommunity = z.object({ banned: z.boolean(), community_id: z.number(), expires: z.optional(z.string()), id: z.number(), mod_person_id: z.number(), other_person_id: z.number(), reason: z.optional(z.string()), when_: z.string() }); const ModBan = z.object({ banned: z.boolean(), expires: z.optional(z.string()), id: z.number(), mod_person_id: z.number(), other_person_id: z.number(), reason: z.optional(z.string()), when_: z.string() }); const ModAddCommunity = z.object({ community_id: z.number(), id: z.number(), mod_person_id: z.number(), other_person_id: z.number(), removed: z.boolean(), when_: z.string() }); const ModTransferCommunity = z.object({ community_id: z.number(), id: z.number(), mod_person_id: z.number(), other_person_id: z.number(), when_: z.string() }); const ModAdd = z.object({ id: z.number(), mod_person_id: z.number(), other_person_id: z.number(), removed: z.boolean(), when_: z.string() }); const AdminPurgePerson = z.object({ admin_person_id: z.number(), id: z.number(), reason: z.optional(z.string()), when_: z.string() }); const AdminPurgeCommunity = z.object({ admin_person_id: z.number(), id: z.number(), reason: z.optional(z.string()), when_: z.string() }); const AdminPurgePost = z.object({ admin_person_id: z.number(), community_id: z.number(), id: z.number(), reason: z.optional(z.string()), when_: z.string() }); const AdminPurgeComment = z.object({ admin_person_id: z.number(), id: z.number(), post_id: z.number(), reason: z.optional(z.string()), when_: z.string() }); const ModHideCommunity = z.object({ community_id: z.number(), hidden: z.boolean(), id: z.number(), mod_person_id: z.number(), reason: z.optional(z.string()), when_: z.string() }); const ModRemovePostView = z.object({ community: Community, mod_remove_post: ModRemovePost, moderator: z.optional(Person), post: Post }); const ModLockPostView = z.object({ community: Community, mod_lock_post: ModLockPost, moderator: z.optional(Person), post: Post }); const ModFeaturePostView = z.object({ community: Community, mod_feature_post: ModFeaturePost, moderator: z.optional(Person), post: Post }); const ModRemoveCommentView = z.object({ comment: Comment, commenter: Person, community: Community, mod_remove_comment: ModRemoveComment, moderator: z.optional(Person), post: Post }); const ModRemoveCommunityView = z.object({ community: Community, mod_remove_community: ModRemoveCommunity, moderator: z.optional(Person) }); const ModBanFromCommunityView = z.object({ banned_person: Person, community: Community, mod_ban_from_community: ModBanFromCommunity, moderator: z.optional(Person) }); const ModBanView = z.object({ banned_person: Person, mod_ban: ModBan, moderator: z.optional(Person) }); const ModAddCommunityView = z.object({ community: Community, mod_add_community: ModAddCommunity, modded_person: Person, moderator: z.optional(Person) }); const ModTransferCommunityView = z.object({ community: Community, mod_transfer_community: ModTransferCommunity, modded_person: Person, moderator: z.optional(Person) }); const ModAddView = z.object({ mod_add: ModAdd, modded_person: Person, moderator: z.optional(Person) }); const AdminPurgePersonView = z.object({ admin: z.optional(Person), admin_purge_person: AdminPurgePerson }); const AdminPurgeCommunityView = z.object({ admin: z.optional(Person), admin_purge_community: AdminPurgeCommunity }); const AdminPurgePostView = z.object({ admin: z.optional(Person), admin_purge_post: AdminPurgePost, community: Community }); const AdminPurgeCommentView = z.object({ admin: z.optional(Person), admin_purge_comment: AdminPurgeComment, post: Post }); const ModHideCommunityView = z.object({ admin: z.optional(Person), community: Community, mod_hide_community: ModHideCommunity }); const ModlogItem = z.union([ ModRemovePostView, ModLockPostView, ModFeaturePostView, ModRemoveCommentView, ModRemoveCommunityView, ModBanFromCommunityView, ModBanView, ModAddCommunityView, ModTransferCommunityView, ModAddView, AdminPurgePersonView, AdminPurgeCommunityView, AdminPurgePostView, AdminPurgeCommentView, ModHideCommunityView ]); const GetPersonDetailsResponse = z.object({ // site?: Site; moderates: z.array(CommunityModeratorView), person_view: PersonView }); const LinkMetadata = z.object({ content_type: z.optional(z.string()), description: z.optional(z.string()), embed_video_url: z.optional(z.string()), image: z.optional(z.string()), title: z.optional(z.string()) }); const GetSiteMetadataResponse = z.object({ metadata: LinkMetadata }); const Instance = z.object({ domain: z.string(), id: z.number(), published: z.string(), software: z.optional(z.string()), updated: z.optional(z.string()), version: z.optional(z.string()) }); const RegistrationMode = z.enum([ "Closed", "RequireApplication", "Open" ]); const SiteAggregates = z.object({ comments: z.number(), communities: z.number(), posts: z.number(), users: z.number(), /** * The number of users with any activity in the last day. */ users_active_day: z.number(), /** * The number of users with any activity in the last half year. */ users_active_half_year: z.number(), /** * The number of users with any activity in the last month. */ users_active_month: z.number(), /** * The number of users with any activity in the last week. */ users_active_week: z.number() }); const MyUserInfo = z.object({ community_blocks: z.array(Community), follows: z.array(CommunityFollowerView), instance_blocks: z.array(Instance), local_user_view: z.object({ counts: PersonAggregates, local_user: z.object({ admin: z.boolean(), show_nsfw: z.boolean() }), person: Person }), moderates: z.array(CommunityModeratorView), person_blocks: z.array(Person) }); const FederationMode = z.enum(["All", "Local", "Disable"]); const LocalSite = z.object({ /** * An optional registration application questionnaire in markdown. */ application_question: z.optional(z.string()), captcha_enabled: z.boolean(), /** * What kind of comment downvotes your site allows. */ comment_downvotes: FederationMode, /** * What kind of comment upvotes your site allows. */ comment_upvotes: FederationMode, legal_information: z.optional(z.string()), /** * What kind of post downvotes your site allows. */ post_downvotes: FederationMode, /** * What kind of post upvotes your site allows. */ post_upvotes: FederationMode, registration_mode: RegistrationMode, require_email_verification: z.boolean() }); const Site = z.object({ actor_id: z.string(), banner: z.optional(z.string()), description: z.optional(z.string()), icon: z.optional(z.string()), name: z.string(), sidebar: z.optional(z.string()), version: z.optional(z.string()) }); const SiteView = z.object({ counts: z.optional(SiteAggregates), local_site: LocalSite, site: Site }); const GetSiteResponse = z.object({ admins: z.array(PersonView), my_user: z.optional(MyUserInfo), site_view: SiteView, version: z.string() }); const GetUnreadCountResponse = z.object({ mentions: z.number(), private_messages: z.number(), replies: z.number() }); const LoginResponse = z.object({ /** * This is None in response to `Register` if email verification is enabled, or the server * requires registration applications. */ jwt: z.optional(z.string()), /** * If registration applications are required, this will return true for a signup response. * * Omitted for piefed */ registration_created: z.optional(z.boolean()), /** * If email verifications are required, this will return true for a signup response. * * Omitted for piefed */ verify_email_sent: z.optional(z.boolean()) }); const PersonMention = z.object({ comment_id: z.number(), id: z.number(), published: z.string(), read: z.boolean(), recipient_id: z.number() }); const PersonMentionView = z.object({ banned_from_community: z.boolean(), comment: Comment, community: Community, counts: CommentAggregates, creator: Person, creator_banned_from_community: z.boolean(), creator_blocked: z.boolean(), creator_is_admin: z.boolean(), creator_is_moderator: z.boolean(), my_vote: z.optional(Vote), person_mention: PersonMention, post: Post, recipient: Person, saved: z.boolean(), subscribed: SubscribedType }); const PrivateMessage = z.object({ ap_id: z.string(), content: z.string(), creator_id: z.number(), deleted: z.boolean(), id: z.number(), local: z.boolean(), published: z.string(), read: z.boolean(), recipient_id: z.number(), updated: z.optional(z.string()) }); const PrivateMessageView = z.object({ creator: Person, private_message: PrivateMessage, recipient: Person }); const Notification = z.union([ CommentReplyView, PersonMentionView, PrivateMessageView ]); const PageCursor = z.union([z.string(), z.number()]); const PagableResponse = z.object({ next_page: z.optional(PageCursor), prev_page: z.optional(PageCursor) }); function buildPagableResponse(schema) { return z.extend(PagableResponse, { data: z.array(schema) }); } const PostAggregates = z.object({ comments: z.number(), downvotes: z.number(), /** * The time of the newest comment in the post. */ newest_comment_time: z.string(), published: z.string(), score: z.number(), upvotes: z.number() }); const PostView = z.object({ banned_from_community: z.boolean(), community: Community, counts: PostAggregates, creator: Person, creator_banned_from_community: z.boolean(), creator_blocked: z.boolean(), creator_is_admin: z.boolean(), creator_is_moderator: z.boolean(), hidden: z.boolean(), my_vote: z.optional(Vote), post: Post, read: z.boolean(), saved: z.boolean(), subscribed: SubscribedType, unread_comments: z.number() }); const PersonContentItem = z.union([PostView, CommentView]); const PostReport = z.object({ creator_id: z.number(), id: z.number(), /** * The original post body. */ original_post_body: z.optional(z.string()), /** * The original post title. */ original_post_name: z.string(), /** * The original post url. */ original_post_url: z.optional(z.string()), post_id: z.number(), published: z.string(), reason: z.string(), resolved: z.boolean(), resolver_id: z.optional(z.number()), updated: z.optional(z.string()) }); const PostReportView = z.object({ community: Community, counts: PostAggregates, creator: Person, creator_banned_from_community: z.boolean(), creator_blocked: z.boolean(), creator_is_admin: z.boolean(), creator_is_moderator: z.boolean(), hidden: z.boolean(), my_vote: z.optional(Vote), post: Post, post_creator: Person, post_report: PostReport, read: z.boolean(), resolver: z.optional(Person), saved: z.boolean(), subscribed: SubscribedType, unread_comments: z.number() }); const ReportItemView = z.union([CommentReportView, PostReportView]); const ResolveObjectResponse = z.object({ comment: z.optional(CommentView), community: z.optional(CommunityView), person: z.optional(PersonView), post: z.optional(PostView) }); const SearchItem = z.union([ CommentView, CommunityView, PersonView, PostView ]); const UploadImageResponse = z.object({ delete_token: z.optional(z.string()), url: z.string() }); const ListPostsResponse = buildPagableResponse(PostView); const ListCommentsResponse = buildPagableResponse(CommentView); const ListModlogResponse = buildPagableResponse(ModlogItem); const ListNotificationsResponse = buildPagableResponse( Notification ); const ListPersonMentionsResponse = buildPagableResponse( PersonMentionView ); const ListPrivateMessagesResponse = buildPagableResponse( PrivateMessageView ); const ListRepliesResponse = buildPagableResponse( CommentReplyView ); const ListCommentReportsResponse = buildPagableResponse( CommentReportView ); const ListCommunitiesResponse = buildPagableResponse( CommunityView ); const ListPersonContentResponse = buildPagableResponse( PersonContentItem ); const ListPostReportsResponse = buildPagableResponse( PostReportView ); const ListReportsResponse = buildPagableResponse(ReportItemView); const ListSearchResponse = buildPagableResponse(SearchItem); function buildSafeClient(_Client) { const Client = _Client; return class SafeClient extends Client { async banFromCommunity(...params) { return super.banFromCommunity(...params); } async blockCommunity(...params) { const { community_view } = await super.blockCommunity(...params); return { community_view: CommunityView.parse(community_view) }; } async blockInstance(...params) { return super.blockInstance(...params); } async blockPerson(...params) { const { person_view } = await super.blockPerson(...params); return { person_view: PersonView.parse(person_view) }; } async createComment(...params) { const { comment_view } = await super.createComment(...params); return { comment_view: CommentView.parse(comment_view) }; } async createCommentReport(...params) { return super.createCommentReport(...params); } async createPost(...params) { const { post_view } = await super.createPost(...params); return { post_view: PostView.parse(post_view) }; } async createPostReport(...params) { return super.createPostReport(...params); } async createPrivateMessage(...params) { const { private_message_view } = await super.createPrivateMessage( ...params ); return { private_message_view: PrivateMessageView.parse(private_message_view) }; } async createPrivateMessageReport(...params) { return super.createPrivateMessageReport(...params); } async deleteComment(...params) { const { comment_view } = await super.deleteComment(...params); return { comment_view: CommentView.parse(comment_view) }; } async deleteImage(...params) { return super.deleteImage(...params); } async deletePost(...params) { const { post_view } = await super.deletePost(...params); return { post_view: PostView.parse(post_view) }; } async distinguishComment(...params) { const { comment_view } = await super.distinguishComment(...params); return { comment_view: CommentView.parse(comment_view) }; } async editComment(...params) { const { comment_view } = await super.editComment(...params); return { comment_view: CommentView.parse(comment_view) }; } async editPost(...params) { const { post_view } = await super.editPost(...params); return { post_view: PostView.parse(post_view) }; } async featurePost(...params) { const { post_view } = await super.featurePost(...params); return { post_view: PostView.parse(post_view) }; } async followCommunity(...params) { const { community_view } = await super.followCommunity(...params); return { community_view: CommunityView.parse(community_view) }; } async getCaptcha(...params) { const response = await super.getCaptcha(...params); return GetCaptchaResponse.parse(response); } async getComments(...params) { const response = await super.getComments(...params); return ListCommentsResponse.parse(response); } async getCommunity(...params) { const response = await super.getCommunity(...params); return GetCommunityResponse.parse(response); } async getFederatedInstances(...params) { const { federated_instances } = await super.getFederatedInstances( ...params ); return { federated_instances: federated_instances ? FederatedInstances.parse(federated_instances) : void 0 }; } async getModlog(...params) { const response = await super.getModlog(...params); return ListModlogResponse.parse(response); } async getNotifications(...params) { const response = await super.getNotifications(...params); return ListNotificationsResponse.parse(response); } async getPersonDetails(...params) { const response = await super.getPersonDetails(...params); return GetPersonDetailsResponse.parse(response); } async getPersonMentions(...params) { const response = await super.getPersonMentions(...params); return ListPersonMentionsResponse.parse(response); } async getPost(...params) { const { post_view } = await super.getPost(...params); return { post_view: PostView.parse(post_view) }; } async getPosts(...params) { const response = await super.getPosts(...params); return ListPostsResponse.parse(response); } async getPrivateMessages(...params) { const response = await super.getPrivateMessages(...params); return ListPrivateMessagesResponse.parse(response); } async getRandomCommunity(...params) { const { community_view } = await super.getRandomCommunity(...params); return { community_view: CommunityView.parse(community_view) }; } async getReplies(...params) { const response = await super.getReplies(...params); return ListRepliesResponse.parse(response); } async getSite(...params) { const response = await super.getSite(...params); return GetSiteResponse.parse(response); } async getSiteMetadata(...params) { const response = await super.getSiteMetadata(...params); return GetSiteMetadataResponse.parse(response); } async getUnreadCount(...params) { const response = await super.getUnreadCount(...params); return GetUnreadCountResponse.parse(response); } async likeComment(...params) { const { comment_view } = await super.likeComment(...params); return { comment_view: CommentView.parse(comment_view) }; } async likePost(...params) { const { post_view } = await super.likePost(...params); return { post_view: PostView.parse(post_view) }; } async listCommentReports(...params) { const response = await super.listCommentReports(...params); return ListCommentReportsResponse.parse(response); } async listCommunities(...params) { const response = await super.listCommunities(...params); return ListCommunitiesResponse.parse(response); } async listPersonContent(...params) { const response = await super.listPersonContent(...params); return ListPersonContentResponse.parse(response); } async listPersonLiked(...params) { const response = await super.listPersonLiked(...params); return ListPersonContentResponse.parse(response); } async listPersonSaved(...params) { const response = await super.listPersonSaved(...params); return ListPersonContentResponse.parse(response); } async listPostReports(...params) { const response = await super.listPostReports(...params); return ListPostReportsResponse.parse(response); } async listReports(...params) { const response = await super.listReports(...params); return ListReportsResponse.parse(response); } async lockPost(...params) { const { post_view } = await super.lockPost(...params); return { post_view: PostView.parse(post_view) }; } async login(...params) { const response = await super.login(...params); return LoginResponse.parse(response); } async logout(...params) { return super.logout(...params); } async markAllAsRead(...params) { return super.markAllAsRead(...params); } async markCommentReplyAsRead(...params) { return super.markCommentReplyAsRead(...params); } async markPersonMentionAsRead(...params) { return super.markPersonMentionAsRead(...params); } async markPostAsRead(...params) { return super.markPostAsRead(...params); } async markPrivateMessageAsRead(...params) { return super.markPrivateMessageAsRead(...params); } async register(...params) { const response = await super.register(...params); return LoginResponse.parse(response); } async removeComment(...params) { const { comment_view } = await super.removeComment(...params); return { comment_view: CommentView.parse(comment_view) }; } async removePost(...params) { const { post_view } = await super.removePost(...params); return { post_view: PostView.parse(post_view) }; } async resolveCommentReport(...params) { return super.resolveCommentReport(...params); } async resolveObject(...params) { const response = await super.resolveObject(...params); return ResolveObjectResponse.parse(response); } async resolvePostReport(...params) { return super.resolvePostReport(...params); } async saveComment(...params) { const { comment_view } = await super.saveComment(...params); return { comment_view: CommentView.parse(comment_view) }; } async savePost(...params) { const { post_view } = await super.savePost(...params); return { post_view: PostView.parse(post_view) }; } async saveUserSettings(...params) { return super.saveUserSettings(...params); } async search(...params) { const response = await super.search(...params); return ListSearchResponse.parse(response); } async uploadImage(...params) { const response = await super.uploadImage(...params); return UploadImageResponse.parse(response); } }; } function fromPageParams$2(params) { const result = { ...params }; const page_cursor = result.page_cursor; delete result.page_cursor; if (typeof page_cursor === "string") throw new InvalidPayloadError( "lemmyv0 does not support string page_cursor" ); return { ...result, limit: params.limit, page: page_cursor ? Number(page_cursor) : void 0 }; } function toBlocks(blocks) { return { community_blocks: blocks.community_blocks.map((t) => "community" in t ? t.community : t).map(toCommunity$2), instance_blocks: blocks.instance_blocks.map( (t) => "instance" in t ? t.instance : t ), person_blocks: blocks.person_blocks.map( (t) => "target" in t ? t.target : t ) }; } function toCommentReportView$1(commentReport) { return { ...commentReport, community: toCommunity$2(commentReport.community) }; } function toCommentView$2(comment) { return { ...comment, banned_from_community: comment.banned_from_community ?? false, // v0.13.3 community: toCommunity$2(comment.community) }; } function toCommunity$2(community) { return { ...community, hidden: community.hidden ?? false, // v0.13.3 visibility: compatCommunityVisibility(community.visibility) }; } function toCommunityFollowerView(communityFollower) { return { ...communityFollower, community: toCommunity$2(communityFollower.community) }; } function toCommunityModeratorView$2(communityModerator) { return { ...communityModerator, community: toCommunity$2(communityModerator.community) }; } function toCommunityView$2(communityView) { return { ...communityView, community: toCommunity$2(communityView.community) }; } function toLocalSite$1(localSite) { const downvotes_disabled = localSite.enable_downvotes === false; const downvotesMode = downvotes_disabled ? "Disable" : "All"; return { ...localSite, comment_downvotes: downvotesMode, comment_upvotes: "All", post_downvotes: downvotesMode, post_upvotes: "All" }; } function toMentionView(personMention) { return { ...personMention, community: toCommunity$2(personMention.community) }; } function toModlogView$1(modlog) { if ("community" in modlog) { return { ...modlog, community: toCommunity$2(modlog.community) }; } return modlog; } function toPageResponse$1(params) { const page_cursor = params.page_cursor; if (typeof page_cursor === "string") throw new InvalidPayloadError( "lemmyv0 does not support string page_cursor" ); return { next_page: (page_cursor ?? 1) + 1 }; } function toPostReportView$1(postReport) { return { ...postReport, community: toCommunity$2(postReport.community) }; } function toPostView$2(post) { return { ...post, banned_from_community: post.banned_from_community ?? false, // v0.13.3 community: toCommunity$2(post.community), hidden: post.hidden ?? false // v0.13.3 }; } function toReplyView(personMention) { return { ...personMention, community: toCommunity$2(personMention.community) }; } function compatCommunityVisibility(visibility) { return visibility === "LocalOnly" ? "LocalOnlyPublic" : visibility ?? "Public"; } function getInboxItemPublished(item) { if ("comment_reply" in item) { return item.comment_reply.published; } if ("private_message" in item) { return item.private_message.published; } return item.person_mention.published; } function getLogDate(item) { switch (true) { case "mod_remove_comment" in item: return item.mod_remove_comment.when_; case "mod_remove_post" in item: return item.mod_remove_post.when_; case "mod_lock_post" in item: return item.mod_lock_post.when_; case "mod_feature_post" in item: return item.mod_feature_post.when_; case "mod_remove_community" in item: return item.mod_remove_community.when_; case "mod_ban_from_community" in item: return item.mod_ban_from_community.when_; case "mod_ban" in item: return item.mod_ban.when_; case "mod_add_community" in item: return item.mod_add_community.when_; case "mod_transfer_community" in item: return item.mod_transfer_community.when_; case "mod_add" in item: return item.mod_add.when_; case "admin_purge_person" in item: return item.admin_purge_person.when_; case "admin_purge_community" in item: return item.admin_purge_community.when_; case "admin_purge_post" in item: return item.admin_purge_post.when_; case "admin_purge_comment" in item: return item.admin_purge_comment.when_; case "mod_hide_community" in item: return item.mod_hide_community.when_; default: return item; } } function getPostCommentItemCreatedDate(item) { if ("comment" in item) return Date.parse(item.comment.published); return Date.parse(item.post.published); } const getPublishedDate = (item) => { if ("comment" in item) { return item.comment.published; } else { return item.post.published; } }; function sortPostCommentByPublished(a, b) { return getPublishedDate(b).localeCompare(getPublishedDate(a)); } class UnsafeLemmyV0Client { static mode = "lemmyv0"; static softwareName = "lemmy"; static softwareVersionRange = ">=0.19.0"; #client; constructor(hostname, options) { this.#client = new LemmyV0.LemmyHttp(hostname, options); } async banFromCommunity(...params) { await this.#client.banFromCommunity(...params); } async blockCommunity(...params) { const response = await this.#client.blockCommunity(...params); return { ...response, community_view: toCommunityView$2(response.community_view) }; } async blockInstance(...params) { await this.#client.blockInstance(...params); } async blockPerson(...params) { return this.#client.blockPerson(...params); } async createComment(...params) { const response = await this.#client.createComment(...params); return { comment_view: toCommentView$2(response.comment_view) }; } async createCommentReport(...params) { await this.#client.createCommentReport(...params); } async createPost(...params) { const response = await this.#client.createPost(...params); return { post_view: toPostView$2(response.post_view) }; } async createPostReport(...params) { await this.#client.createPostReport(...params); } async createPrivateMessage(...params) { return this.#client.createPrivateMessage(...params); } async createPrivateMessageReport(...params) { await this.#client.createPrivateMessageReport(...params); } async deleteComment(...params) { const response = await this.#client.deleteComment(...params); return { comment_view: toCommentView$2(response.comment_view) }; } async deleteImage(payload, options) { await this.#client.deleteImage( { filename: new URL(payload.url).pathname.split("/").pop(), token: payload.delete_token }, options ); } async deletePost(...params) { const response = await this.#client.deletePost(...params); return { post_view: toPostView$2(response.post_view) }; } async distinguishComment(...params) { const response = await this.#client.distinguishComment(...params); return { comment_view: toCommentView$2(response.comment_view) }; } async editComment(...params) { const response = await this.#client.editComment(...params); return { comment_view: toCommentView$2(response.comment_view) }; } async editPost(...params) { const response = await this.#client.editPost(...params); return { post_view: toPostView$2(response.post_view) }; } async featurePost(...params) { const response = await this.#client.featurePost(...params); return { post_view: toPostView$2(response.post_view) }; } async followCommunity(...params) { const response = await this.#client.followCommunity(...params); return { ...response, community_view: toCommunityView$2(response.community_view) }; } async getCaptcha(...params) { return this.#client.getCaptcha(...params); } async getComments(payload, options) { if (payload.mode && payload.mode !== "lemmyv0") throw new InvalidPayloadError( `Connected to lemmyv1, ${payload.mode} is not supported` ); const response = await this.#client.getComments( cleanThreadiverseParams(fromPageParams$2(payload)), options ); return { ...toPageResponse$1(payload), data: response.comments.map(toCommentView$2) }; } async getCommunity(...params) { const response = await this.#client.getCommunity(...params); return { ...response, community_view: { ...toCommunityView$2(response.community_view) }, moderators: response.moderators.map(toCommunityModeratorView$2) }; } async getFederatedInstances(...params) { return this.#client.getFederatedInstances(...params); } async getModlog(payload, options) { const response = await this.#client.getModlog( fromPageParams$2(payload), options ); return { ...toPageResponse$1(payload), data: Object.values(response).flat().map(toModlogView$1).sort((a, b) => Date.parse(getLogDate(b)) - Date.parse(getLogDate(a))) }; } async getNotifications(payload, options) { const params = fromPageParams$2(payload); const [replies, mentions, privateMessages] = await Promise.all([ this.getReplies(params, options), this.getPersonMentions(params, options), this.getPrivateMessages(params, options) ]); const data = [ ...replies.data, ...mentions.data, ...privateMessages.data ].sort( (a, b) => Date.parse(getInboxItemPublished(b)) - Date.parse(getInboxItemPublished(a)) ); return { ...toPageResponse$1(payload), data }; } async getPersonDetails(payload, options) { const response = await this.#client.getPersonDetails( { ...payload, limit: 1 // Lemmy melts down if limit is 0 }, options ); return { ...response, moderates: response.moderates.map(toCommunityModeratorView$2) }; } async getPersonMentions(payload, options) { const response = await this.#client.getPersonMentions( { ...payload, sort: "New" }, options ); return { ...toPageResponse$1(payload), data: response.mentions.map(toMentionView) }; } async getPost(...params) { const response = await this.#client.getPost(...params); return { post_view: toPostView$2(response.post_view) }; } async getPosts(payload, options) { if (payload.mode && payload.mode !== "lemmyv0") throw new InvalidPayloadError( `Connected to lemmyv1, ${payload.mode} is not supported` ); const page_cursor = payload.page_cursor; if (typeof page_cursor === "number") throw new InvalidPayloadError("page_cursor must be string"); const response = await this.#client.getPosts( { // Only endpoint in lemmy v0 that supports page_cursor // Do not call fromPageParams here! ...cleanThreadiverseParams(payload), page_cursor }, options ); return { data: response.posts.map(toPostView$2), next_page: response.next_page }; } async getPostSortType() { return [{ sort: "Top" }, { sort: "All" }]; } async getPrivateMessages(payload, options) { const response = await this.#client.getPrivateMessages( fromPageParams$2(payload), options ); return { ...toPageResponse$1(payload), data: response.private_messages }; } async getRandomCommunity(..._params) { throw new UnsupportedError( "Get random community is not supported by Lemmy v0" ); } async getReplies(payload, options) { const response = await this.#client.getReplies( fromPageParams$2({ ...payload, sort: "New" }), options ); return { ...toPageResponse$1(payload), data: response.replies.map(toReplyView) }; } async getSite(...params) { const site = await this.#client.getSite(...params); return { ...site, my_user: site.my_user ? { ...site.my_user, follows: site.my_user.follows.map(toCommunityFollowerView), moderates: site.my_user.moderates.map( toCommunityModeratorView$2 ), ...toBlocks(site.my_user) } : void 0, site_view: { ...site.site_view, local_site: toLocalSite$1(site.site_view.local_site) } }; } async getSiteMetadata(...params) { return this.#client.getSiteMetadata(...params); } async getUnreadCount(...params) { return this.#client.getUnreadCount(...params); } async likeComment(...params) { const response = await this.#client.likeComment(...params); return { comment_view: toCommentView$2(response.comment_view) }; } async likePost(...params) { const response = await this.#client.likePost(...params); return { post_view: toPostView$2(response.post_view) }; } async listCommentReports(payload, options) { const response = await this.#client.listCommentReports( fromPageParams$2(payload), options ); return { ...toPageResponse$1(payload), data: response.comment_reports.map(toCommentReportView$1) }; } async listCommunities(payload, options) { if (payload.mode && payload.mode !== "lemmyv0") throw new InvalidPayloadError( `Connected to lemmyv1, ${payload.mode} is not supported` ); const response = await this.#client.listCommunities( cleanThreadiverseParams(fromPageParams$2(payload)), options ); return { ...toPageResponse$1(payload), data: response.communities.map(toCommunityView$2) }; } async listPersonContent(payload, options) { const response = await this.#client.getPersonDetails( fromPageParams$2(payload), options ); const data = (() => { switch (payload.type) { case "All": case void 0: return [ ...response.posts.map(toPostView$2), ...response.comments.map(toCommentView$2) ].sort( (a, b) => getPostCommentItemCreatedDate(b) - getPostCommentItemCreatedDate(a) ); case "Comments": return response.comments.map(toCommentView$2); case "Posts": return response.posts.map(toPostView$2); } })(); return { ...toPageResponse$1(payload), data }; } async listPersonLiked({ type, ...payload }, options) { const v0Payload = { ...fromPageParams$2(payload), disliked_only: type === "Downvoted", liked_only: type === "Upvoted", show_read: true }; const [{ posts }, { comments }] = await Promise.all([ this.#client.getPosts(v0Payload, options), this.#client.getComments(v0Payload, options) ]); return { data: [ ...comments.map(toCommentView$2), ...posts.map(toPostView$2) ].sort(sortPostCommentByPublished), ...toPageResponse$1(payload) }; } async listPersonSaved(payload, options) { return this.listPersonContent( { ...payload, // @ts-expect-error Dogfood the api saved_only: true }, options ); } async listPostReports(payload, options) { const response = await this.#client.listPostReports( fromPageParams$2(payload), options ); return { ...toPageResponse$1(payload), data: response.post_reports.map(toPostReportView$1) }; } async listReports(payload, options) { const params = fromPageParams$2(payload); const [{ comment_reports }, { post_reports }] = await Promise.all([ this.#client.listCommentReports(params, options), this.#client.listPostReports(params, options) ]); return { ...toPageResponse$1(payload), data: [ ...comment_reports.map(toCommentReportView$1), ...post_reports.map(toPostReportView$1) ].sort( (a, b) => getPostCommentItemCreatedDate(b) - getPostCommentItemCreatedDate(a) ) }; } async lockPost(...params) { const response = await this.#client.lockPost(...params); return { post_view: toPostView$2(response.post_view) }; } async login(...params) { return this.#client.login(...params); } async logout(...params) { await this.#client.logout(...params); } async markAllAsRead(...params) { await this.#client.markAllAsRead(...params); } async markCommentReplyAsRead(...params) { await this.#client.markCommentReplyAsRead(...params); } async markPersonMentionAsRead(...params) { await this.#client.markPersonMentionAsRead(...params); } async markPostAsRead(...params) { await this.#client.markPostAsRead(...params); } async markPrivateMessageAsRead(...params) { await this.#client.markPrivateMessageAsRead(...params); } async register(...params) { return this.#client.register(...params); } async removeComment(...params) { const response = await this.#client.removeComment(...params); return { comment_view: toCommentView$2(response.comment_view) }; } async removePost(...params) { const response = await this.#client.removePost(...params); return { post_view: toPostView$2(response.post_view) }; } async resolveCommentReport(...params) { await this.#client.resolveCommentReport(...params); } async resolveObject(payload, options) { const response = await this.#client.resolveObject(payloa