stage3: add basic piefed features

This commit is contained in:
James Xylight committed 2025-09-03 20:52:56 -07:00
1 parent f03bcca1df
commit d00e8b0d08
2 files changed
+111 -10

No files matched your search

+38 -5
View File
@@ -1,9 +1,15 @@
import createClient from 'openapi-fetch' import createClient from 'openapi-fetch'
import type { BaseClient } from '../base' import type { BaseClient } from '../base'
import { import {
fromGetComments,
fromGetPost,
fromGetPosts, fromGetPosts,
toCommentView,
toCommunity,
toCommunityView,
toLocalSite, toLocalSite,
toMyUser, toMyUser,
toPerson,
toPersonView, toPersonView,
toPostView, toPostView,
} from './rewrite' } from './rewrite'
@@ -72,9 +78,24 @@ export class PiefedClient implements BaseClient {
throw new Error('Unimplemented') throw new Error('Unimplemented')
} }
async getPost( async getPost(
...params: Parameters<BaseClient['getPost']> params: Parameters<BaseClient['getPost']>[0],
): ReturnType<BaseClient['getPost']> { ): ReturnType<BaseClient['getPost']> {
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( async editPost(
...params: Parameters<BaseClient['editPost']> ...params: Parameters<BaseClient['editPost']>
@@ -97,7 +118,8 @@ export class PiefedClient implements BaseClient {
): ReturnType<BaseClient['getPosts']> { ): ReturnType<BaseClient['getPosts']> {
const response = ( const response = (
await this.#client.GET('/post/list', { await this.#client.GET('/post/list', {
params: fromGetPosts(params), // @ts-expect-error same here what is happening
params: { query: fromGetPosts(params) },
}) })
).data! ).data!
@@ -297,11 +319,22 @@ export class PiefedClient implements BaseClient {
): ReturnType<BaseClient['distinguishComment']> { ): ReturnType<BaseClient['distinguishComment']> {
throw new Error('Unimplemented') throw new Error('Unimplemented')
} }
async getComments( async getComments(
...params: Parameters<BaseClient['getComments']> params: Parameters<BaseClient['getComments']>[0],
): ReturnType<BaseClient['getComments']> { ): ReturnType<BaseClient['getComments']> {
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( async getComment(
...params: Parameters<BaseClient['getComment']> ...params: Parameters<BaseClient['getComment']>
): ReturnType<BaseClient['getComment']> { ): ReturnType<BaseClient['getComment']> {
+73 -5
View File
@@ -1,5 +1,11 @@
import type { import type {
Comment,
CommentSortType,
CommentView,
Community, Community,
CommunityView,
GetComments,
GetPost,
GetPosts, GetPosts,
LocalSite, LocalSite,
MyUserInfo, MyUserInfo,
@@ -35,6 +41,28 @@ export function toLocalSite(site: components['schemas']['Site']): LocalSite {
return {} 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( export function toCommunity(
community: components['schemas']['Community'], community: components['schemas']['Community'],
): 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 // Query calls
export function fromGetPosts( export function fromGetPosts(
getPosts: GetPosts, getPosts: GetPosts,
): paths['/post/list']['get']['parameters'] { ): components['schemas']['GetPosts'] {
return { return {
query: {
GetPosts: {
...getPosts, ...getPosts,
sort: toSortType(getPosts.sort), sort: toSortType(getPosts.sort),
page_cursor: Number(getPosts.page_cursor), 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 // Misc
export function toSortType( export function toSortType(
sortType?: SortType, sortType?: SortType,
@@ -135,3 +196,10 @@ export function toSortType(
sortType = 'New' sortType = 'New'
return sortType return sortType
} }
export function toCommentSortType(
commentSortType?: CommentSortType,
): components['schemas']['CommentSortType'] | undefined {
if (commentSortType == 'Controversial') commentSortType = undefined
return commentSortType
}