stage3: add basic piefed features
This commit is contained in:
2 files changed
+114
-13
No files matched your search
@@ -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<BaseClient['getPost']>
|
||||
params: Parameters<BaseClient['getPost']>[0],
|
||||
): 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(
|
||||
...params: Parameters<BaseClient['editPost']>
|
||||
@@ -97,7 +118,8 @@ export class PiefedClient implements BaseClient {
|
||||
): ReturnType<BaseClient['getPosts']> {
|
||||
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<BaseClient['distinguishComment']> {
|
||||
throw new Error('Unimplemented')
|
||||
}
|
||||
|
||||
async getComments(
|
||||
...params: Parameters<BaseClient['getComments']>
|
||||
params: Parameters<BaseClient['getComments']>[0],
|
||||
): 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(
|
||||
...params: Parameters<BaseClient['getComment']>
|
||||
): ReturnType<BaseClient['getComment']> {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user