diff --git a/src/lib/client/piefed/piefed.ts b/src/lib/client/piefed/piefed.ts index c06b6017..4caf3516 100644 --- a/src/lib/client/piefed/piefed.ts +++ b/src/lib/client/piefed/piefed.ts @@ -1,9 +1,15 @@ import createClient from 'openapi-fetch' import type { BaseClient } from '../base' import { + fromGetComments, + fromGetPost, fromGetPosts, + toCommentView, + toCommunity, + toCommunityView, toLocalSite, toMyUser, + toPerson, toPersonView, toPostView, } from './rewrite' @@ -72,9 +78,24 @@ export class PiefedClient implements BaseClient { throw new Error('Unimplemented') } async getPost( - ...params: Parameters + params: Parameters[0], ): ReturnType { - throw new Error('Unimplemented') + const response = ( + await this.#client.GET('/post', { + // @ts-expect-error same here what is happening + 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 @@ -97,7 +118,8 @@ export class PiefedClient implements BaseClient { ): ReturnType { const response = ( await this.#client.GET('/post/list', { - params: fromGetPosts(params), + // @ts-expect-error same here what is happening + params: { query: fromGetPosts(params) }, }) ).data! @@ -297,11 +319,22 @@ export class PiefedClient implements BaseClient { ): ReturnType { throw new Error('Unimplemented') } + async getComments( - ...params: Parameters + params: Parameters[0], ): ReturnType { - throw new Error('Unimplemented') + const response = ( + await this.#client.GET('/comment/list', { + // @ts-expect-error same here what is happening + params: { query: fromGetComments(params) }, + }) + ).data! + + return { + comments: response.comments.map(toCommentView), + } } + async getComment( ...params: Parameters ): ReturnType { diff --git a/src/lib/client/piefed/rewrite.ts b/src/lib/client/piefed/rewrite.ts index 0bd2be5c..312d7f0b 100644 --- a/src/lib/client/piefed/rewrite.ts +++ b/src/lib/client/piefed/rewrite.ts @@ -1,5 +1,11 @@ import type { + Comment, + CommentSortType, + CommentView, Community, + CommunityView, + GetComments, + GetPost, GetPosts, LocalSite, MyUserInfo, @@ -35,6 +41,28 @@ export function toLocalSite(site: components['schemas']['Site']): LocalSite { return {} } +export function toCommunityView( + communityView: components['schemas']['CommunityView'], +): CommunityView { + return { + ...communityView, + community: toCommunity(communityView.community), + banned_from_community: false, + counts: { + comments: communityView.counts.post_reply_count, + posts: communityView.counts.post_count, + subscribers: communityView.counts.total_subscriptions_count, + published: communityView.community.published, + community_id: communityView.community.id, + subscribers_local: communityView.counts.subscriptions_count, + users_active_day: communityView.counts.active_daily, + users_active_half_year: communityView.counts.active_6monthly, + users_active_month: communityView.counts.active_monthly, + users_active_week: communityView.counts.active_weekly, + }, + } +} + export function toCommunity( community: components['schemas']['Community'], ): Community { @@ -107,21 +135,54 @@ export function toPost(post: components['schemas']['Post']): Post { } } +export function toCommentView( + commentView: components['schemas']['CommentView'], +): CommentView { + return { + ...commentView, + comment: toComment(commentView.comment), + creator: toPerson(commentView.creator), + post: toPost(commentView.post), + community: toCommunity(commentView.community), + } +} + +export function toComment(comment: components['schemas']['Comment']): Comment { + return { + ...comment, + content: comment.body, + creator_id: -1, + distinguished: comment.distinguished ?? false, + } +} + // Query calls export function fromGetPosts( getPosts: GetPosts, -): paths['/post/list']['get']['parameters'] { +): components['schemas']['GetPosts'] { return { - query: { - GetPosts: { - ...getPosts, - sort: toSortType(getPosts.sort), - page_cursor: Number(getPosts.page_cursor), - }, - }, + ...getPosts, + sort: toSortType(getPosts.sort), + page_cursor: Number(getPosts.page_cursor), } } +export function fromGetComments( + getComments: GetComments, +): components['schemas']['GetComments'] { + return { + ...getComments, + sort: toCommentSortType(getComments.sort), + } +} + +export function fromGetPost( + getPost: GetPost, +): components['schemas']['GetPost'] { + console.log(getPost) + return getPost +} + // Misc export function toSortType( sortType?: SortType, @@ -135,3 +196,10 @@ export function toSortType( sortType = 'New' return sortType } + +export function toCommentSortType( + commentSortType?: CommentSortType, +): components['schemas']['CommentSortType'] | undefined { + if (commentSortType == 'Controversial') commentSortType = undefined + return commentSortType +}