import createClient from 'openapi-fetch' import { BaseClient, type ClientType, type NullableFnArg, type NullableFnReturn, } from '../base' import type { SubscribedType } from '../types' import { fromCreateComment, fromCreatePost, fromGetComments, fromGetPost, fromGetPosts, fromGetReplies, fromListCommunities, fromSearch, toComment, toCommentReplyView, toCommentSortType, toCommentView, toCommunity, toCommunityView, toFeedView, toLocalSite, toMyUser, toPerson, toPersonMentionView, toPersonView, toPost, toPostView, toPrivateMessageView, toSortType, toTopicView, } from './rewrite' import type { paths } from './schema' export class PiefedClient implements BaseClient { type: ClientType = { name: 'piefed', baseUrl: '/api/alpha' } #client: ReturnType> constructor( baseUrl: string, args: { fetchFunction: (input: any, init: any) => Promise headers: any }, ) { this.#client = createClient({ baseUrl: baseUrl, // @ts-expect-error i dont feel like fixing this right now fetch: args.fetchFunction, headers: args.headers, }) } async getSite(): ReturnType { const response = (await this.#client.GET('/api/alpha/site', {})).data! return { admins: response.admins.map(toPersonView), site_view: { counts: { comments: -1, communities: -1, posts: -1, site_id: -1, users: response.site.user_count ?? -1, users_active_day: -1, users_active_half_year: -1, users_active_month: -1, users_active_week: -1, }, local_site: toLocalSite(response.site), site: response.site, }, all_languages: (response.site.all_languages as { code: string id: number name: string }[]) ?? [], blocked_urls: [], custom_emojis: [], discussion_languages: [], taglines: [], version: response.version, my_user: response.my_user ? toMyUser(response.my_user) : undefined, } } async createPost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/post', { body: fromCreatePost(params), }) ).data! return { post_view: toPostView(response.post_view), } } async getPost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/post', { params: { query: fromGetPost(params) }, }) ).data! return { community_view: toCommunityView(response.community_view!), cross_posts: response.cross_posts!.map(toPostView), moderators: response.moderators!.map((i) => ({ community: toCommunity(i.community), moderator: toPerson(i.moderator), })), post_view: toPostView(response.post_view), } } async editPost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.PUT('/api/alpha/post', { body: params }) ).data! return { post_view: toPostView(response.post_view), } } async deletePost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/post/delete', { body: params }) ).data! return { post_view: toPostView(response.post_view), } } async removePost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/post/remove', { body: params }) ).data! return { post_view: toPostView(response.post_view), } } async getPosts( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/post/list', { params: { query: fromGetPosts(params) }, }) ).data! return { ...response, posts: response.posts.map(toPostView), next_page: (Number(params.page_cursor ?? 1) + 1).toString(), } } async likePost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/post/like', { body: { ...params }, }) ).data! return { post_view: toPostView(response.post_view), } } async generateTotpSecret(): ReturnType { throw new Error('unsupported') } async listLogins(): ReturnType { throw new Error('unsupported') } async listAllMedia(): ReturnType { throw new Error('unsupported') } async updateTotp(): ReturnType { throw new Error('unsupported') } async getModlog(): ReturnType { throw new Error('unsupported') } async search( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/search', { params: { query: fromSearch(params), }, }) ).data! return { comments: [], communities: response.communities.map(toCommunityView), posts: response.posts.map(toPostView), users: response.users.map(toPersonView), type_: response.type_, } } async resolveObject( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/resolve_object', { params: { query: params }, }) ).data! return { comment: response.comment ? toCommentView(response.comment) : undefined, community: response.community ? toCommunityView(response.community) : undefined, post: response.post ? toPostView(response.post) : undefined, person: response.person ? toPersonView(response.person) : undefined, } } async createCommunity( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/community', { body: params }) ).data! return { ...response, community_view: toCommunityView(response.community_view), } } async getCommunity( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/community', { params: { query: params, }, }) ).data! return { ...response, community_view: toCommunityView(response.community_view), moderators: response.moderators.map((i) => ({ community: toCommunity(i.community), moderator: toPerson(i.moderator), })), } } async editCommunity( params: Parameters[0], ): ReturnType { const response = ( await this.#client.PUT('/api/alpha/community', { body: params, }) ).data! return { ...response, community_view: toCommunityView(response.community_view), } } async listCommunities( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/community/list', { params: { query: fromListCommunities(params), }, }) ).data! return { communities: response.communities.map(toCommunityView), } } async followCommunity( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/community/follow', { body: params }) ).data! return { ...response, community_view: toCommunityView(response.community_view), } } async blockCommunity( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/community/block', { body: params }) ).data! return { ...response, community_view: toCommunityView(response.community_view), } } async deleteCommunity( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/community/delete', { body: params }) ).data! return { ...response, community_view: toCommunityView(response.community_view), } } async hideCommunity(): ReturnType { throw new Error('unsupported') } async removeCommunity(): ReturnType { throw new Error('unsupported') } async banFromCommunity( params: Parameters[0], ): ReturnType { const response = // piefed has separate methods for ban/unban ( params.ban ? await this.#client.POST('/api/alpha/community/moderate/ban', { body: { ...params, expiredAt: new Date( params.expires ?? '6767-06-07T00:00:00.000Z', ).toISOString(), user_id: params.person_id, reason: params.reason ?? 'No reason provided.', }, }) : await this.#client.PUT('/api/alpha/community/moderate/unban', { body: { community_id: params.community_id, user_id: params.person_id, }, }) ).data! return { banned: params.ban, person_view: { person: toPerson(response.banned_user!), counts: { comment_count: -1, person_id: -1, post_count: -1 }, is_admin: false, }, } } async addModToCommunity( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/community/mod', { body: params }) ).data! return { moderators: response.moderators.map((i) => ({ community: toCommunity(i.community), moderator: toPerson(i.moderator), })), } } async markPostAsRead( params: Parameters[0], ): ReturnType { await this.#client.POST('/api/alpha/post/mark_as_read', { body: { ...params }, }) return { success: true, } } async hidePost(): ReturnType { throw new Error('unsupported') } async lockPost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/post/lock', { body: params }) ).data! return { ...response, post_view: toPostView(response.post_view), } } async featurePost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/post/feature', { body: params }) ).data! return { ...response, post_view: toPostView(response.post_view), } } async listPostLikes( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/post/like/list', { params: { query: params }, }) ).data! return { post_likes: response.post_likes!.map((i) => ({ ...i, creator_banned_from_community: false, creator: toPerson(i.creator), })), } } async savePost( params: Parameters[0], ): ReturnType { const response = ( await this.#client.PUT('/api/alpha/post/save', { body: params }) ).data! return { post_view: toPostView(response.post_view), } } async createPostReport( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/post/report', { body: params }) ).data! return { post_report_view: { ...response.post_report_view, community: toCommunity(response.post_report_view.community), post: toPost(response.post_report_view.post), creator: toPerson(response.post_report_view.creator), // TODO figure out if this is a bug or not resolver: response.post_report_view.resolver ? toPerson(response.post_report_view.resolver!) : undefined, post_creator: toPerson(response.post_report_view.post_creator), read: false, hidden: false, creator_banned_from_community: false, unread_comments: -1, }, } } async resolvePostReport(): ReturnType { throw new Error('unsupported') } async listPostReports(): ReturnType { throw new Error('unsupported') } async getSiteMetadata(): ReturnType { throw new Error('unsupported') } async createComment( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/comment', { body: fromCreateComment(params), }) ).data! return { ...response, comment_view: toCommentView(response.comment_view), recipient_ids: [], } } async editComment( params: Parameters[0], ): ReturnType { const response = ( await this.#client.PUT('/api/alpha/comment', { body: { ...params, body: params.content }, }) ).data! return { comment_view: toCommentView(response.comment_view), recipient_ids: [], } } async deleteComment( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/comment/delete', { body: params }) ).data! return { comment_view: toCommentView(response.comment_view), recipient_ids: [], } } async removeComment( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/comment/remove', { body: params }) ).data! return { comment_view: toCommentView(response.comment_view), recipient_ids: [], } } async markCommentReplyAsRead( params: Parameters[0], ): ReturnType { await this.#client.POST('/api/alpha/comment/mark_as_read', { body: params, }) } async likeComment( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/comment/like', { body: { ...params, // TODO add private votes private: false, }, }) ).data! return { comment_view: toCommentView(response.comment_view), recipient_ids: [], } } async listCommentLikes( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/comment/like/list', { params: { query: params }, }) ).data! return { comment_likes: response.comment_likes!.map((i) => ({ ...i, creator: toPerson(i.creator), })), } } async saveComment( params: Parameters[0], ): ReturnType { const response = ( await this.#client.PUT('/api/alpha/comment/save', { body: params }) ).data! return { ...response, comment_view: toCommentView(response.comment_view), recipient_ids: [], } } async distinguishComment(): ReturnType { throw new Error('unsupported') } async getComments( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/comment/list', { params: { query: fromGetComments(params) }, }) ).data! return { comments: response.comments.map(toCommentView), } } async getComment( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/comment', { params: { query: params }, }) ).data! return { ...response, comment_view: toCommentView(response.comment_view), recipient_ids: [], } } async createCommentReport( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/comment/report', { body: { ...params, // TODO add remote reports report_remote: false, }, }) ).data! return { ...response, comment_report_view: { ...response.comment_report_view, comment: toComment(response.comment_report_view.comment), community: toCommunity(response.comment_report_view.community), comment_creator: toPerson(response.comment_report_view.comment_creator), creator: toPerson(response.comment_report_view.creator), post: toPost(response.comment_report_view.post), // TODO figure out what's wrong here resolver: response.comment_report_view.resolver ? toPerson(response.comment_report_view.resolver) : undefined, subscribed: response.comment_report_view.subscribed as SubscribedType, }, } } async resolveCommentReport(): ReturnType { throw new Error('unsupported') } async listCommentReports(): ReturnType { throw new Error('unsupported') } async getPrivateMessages( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/private_message/list', { params: { query: params }, }) ).data! return { private_messages: response.private_messages.map(toPrivateMessageView), } } async createPrivateMessage( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/private_message', { body: params }) ).data! return { ...response, private_message_view: toPrivateMessageView(response.private_message_view), } } async editPrivateMessage( params: Parameters[0], ): ReturnType { const response = ( await this.#client.PUT('/api/alpha/private_message', { body: params }) ).data! return { ...response, private_message_view: toPrivateMessageView(response.private_message_view), } } async deletePrivateMessage( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/private_message/delete', { body: params, }) ).data! return { ...response, private_message_view: toPrivateMessageView(response.private_message_view), } } async markPrivateMessageAsRead( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/private_message/mark_as_read', { body: params, }) ).data! return { ...response, private_message_view: toPrivateMessageView(response.private_message_view), } } async createPrivateMessageReport(): ReturnType< BaseClient['createPrivateMessageReport'] > { throw new Error('unsupported') } async resolvePrivateMessageReport(): ReturnType< BaseClient['resolvePrivateMessageReport'] > { throw new Error('unsupported') } async listPrivateMessageReports(): ReturnType< BaseClient['listPrivateMessageReports'] > { throw new Error('unsupported') } async register(): ReturnType { throw new Error('unsupported') } async login( params: Parameters[0], ): ReturnType { const response = await this.#client.POST('/api/alpha/user/login', { body: { username: params.username_or_email, password: params.password }, })! return { verify_email_sent: false, registration_created: false, jwt: response.data?.jwt, } } async logout(): ReturnType { throw new Error('unsupported') } async getPersonDetails( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/user', { params: { query: { ...params, include_content: true, sort: toSortType(params.sort), }, }, }) ).data! return { comments: response.comments.map(toCommentView), moderates: response.moderates.map((i) => ({ community: toCommunity(i.community), moderator: toPerson(i.moderator), })), person_view: toPersonView(response.person_view), posts: response.posts.map(toPostView), } } async getPersonMentions( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/user/mentions', { params: { query: { ...params, sort: toCommentSortType(params.sort) } }, }) ).data! return { mentions: response.replies.map(toPersonMentionView), } } async markPersonMentionAsRead( params: Parameters[0], ): ReturnType { // @ts-expect-error this method isnt documented await this.#client.POST('/api/alpha/mention/mark_as_read', { body: params, }) } async getReplies( params: Parameters[0], ): ReturnType { const response = ( await this.#client.GET('/api/alpha/user/replies', { params: { query: fromGetReplies(params) }, }) ).data! return { replies: response.replies.map(toCommentReplyView), } } async banPerson(): ReturnType { // this is for site view not community view throw new Error('unsupported') } async getBannedPersons(): ReturnType { // this is for site view not community view throw new Error('unsupported') } async blockPerson( params: Parameters[0], ): ReturnType { const response = ( await this.#client.POST('/api/alpha/user/block', { body: params }) ).data! return { ...response, person_view: toPersonView(response.person_view), } } async getCaptcha(): ReturnType { throw new Error('unsupported') } async deleteAccount(): ReturnType { throw new Error('unsupported') } async passwordReset(): ReturnType { throw new Error('unsupported') } async passwordChangeAfterReset(): ReturnType< BaseClient['passwordChangeAfterReset'] > { throw new Error('unsupported') } async markAllAsRead(): ReturnType { const response = ( await this.#client.POST('/api/alpha/user/mark_all_as_read') ).data! return { replies: response.replies.map(toCommentReplyView), } } async saveUserSettings( params: Parameters[0], ): ReturnType { await this.#client.PUT('/api/alpha/user/save_user_settings', { body: params, }) return { success: true, } } async changePassword(): ReturnType { throw new Error('unsupported') } async getReportCount(): ReturnType { throw new Error('unsupported') } async getUnreadCount(): ReturnType { return (await this.#client.GET('/api/alpha/user/unread_count')).data! } async verifyEmail(): ReturnType { throw new Error('unsupported') } async addAdmin(): ReturnType { throw new Error('unsupported') } async getUnreadRegistrationApplicationCount(): ReturnType< BaseClient['getUnreadRegistrationApplicationCount'] > { throw new Error('unsupported') } async listRegistrationApplications(): ReturnType< BaseClient['listRegistrationApplications'] > { throw new Error('unsupported') } async approveRegistrationApplication(): ReturnType< BaseClient['approveRegistrationApplication'] > { throw new Error('unsupported') } async getRegistrationApplication(): ReturnType< BaseClient['getRegistrationApplication'] > { throw new Error('unsupported') } async purgePerson(): ReturnType { throw new Error('unsupported') } async purgeCommunity(): ReturnType { throw new Error('unsupported') } async purgePost(): ReturnType { throw new Error('unsupported') } async purgeComment(): ReturnType { throw new Error('unsupported') } async getFederatedInstances(): ReturnType< BaseClient['getFederatedInstances'] > { return (await this.#client.GET('/api/alpha/federated_instances')).data! } async blockInstance( params: Parameters[0], ): ReturnType { return (await this.#client.POST('/api/alpha/site/block', { body: { ...params }, }))!.data! } async uploadImage( params: Parameters[0], ): ReturnType { const formData = new FormData() formData.append('file', params.image as File) const response = ( await this.#client.POST('/api/alpha/upload/image', { // @ts-expect-error this is the only working way to do this body: formData, }) ).data! return { ...response, msg: 'yeah it worked i think', } } async setFlair( params: NullableFnArg[0], ): NullableFnReturn { const response = ( await this.#client.POST('/api/alpha/user/set_flair', { body: params }) ).data! return toPersonView(response.person_view) } async editSite(): ReturnType { throw new Error('unsupported') } async deleteImage(): ReturnType { throw new Error('unsupported') } async getFeeds( params: NullableFnArg[0], ): NullableFnReturn { const response = ( await this.#client.GET('/api/alpha/feed/list', { params: { query: params }, }) ).data! return { ...response, feeds: response.feeds.map(toFeedView), } } async getTopics( params: NullableFnArg[0], ): NullableFnReturn { const response = ( await this.#client.GET('/api/alpha/topic/list', { params: { query: params }, }) ).data! return { ...response, topics: response.topics.map(toTopicView), } } async assignFlair( params: NullableFnArg[0], ): NullableFnReturn { const response = ( await this.#client.POST('/api/alpha/post/assign_flair', { body: params, }) ).data! return toPostView(response) } }