Skip to content

Commit 7589c71

Browse files
committed
feat(jike-client): improve post
1 parent 2862afb commit 7589c71

File tree

6 files changed

+61
-91
lines changed

6 files changed

+61
-91
lines changed

src/client/client.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import EventEmitter from 'eventemitter3'
33
import { resolveApiConfig } from '../request'
44
import { ApiClient } from '../api-client'
55
import { objectPick } from '../utils'
6+
import { PostType } from '../types/options'
67
import { isSuccess, throwRequestFailureError } from './utils/response'
78
import { resolveAreaCode } from './utils/user'
89
import { JikeUser } from './user'
910
import { fetchPaginated } from './utils/paginate'
1011
import { AuthorizationError } from './errors/AuthorizationError'
11-
import { JikePost } from './post'
12+
import { JikePost, JikePostWithDetail } from './post'
13+
import type { CreatePostOption } from '../types/options'
1214
import type { FollowingUpdatesMoreKey, JikeClientJSON } from './types'
13-
import type { CreatePostOption, PostType } from '../types/options'
14-
import type { FollowingUpdate, Notification } from '../types/entity'
15+
import type { FollowingUpdate, Notification, Post } from '../types/entity'
1516
import type { BeforeRetryState } from 'ky/distribution/types/hooks'
1617
import type { PaginatedFetcher, PaginatedOption } from './utils/paginate'
1718
import type { Api } from '../api'
@@ -228,14 +229,22 @@ export class JikeClient extends EventEmitter<EventMap> {
228229
const newKey = result.data.loadMoreKey
229230
return [newKey, result.data.data]
230231
}
231-
return fetchPaginated(
232+
const updates = await fetchPaginated(
232233
fetcher,
233234
(item, data) => ({
234235
createdAt: new Date(item.createdAt),
235236
total: data.length + 1,
236237
}),
237238
option
238239
)
240+
return updates.map((update) => {
241+
// TODO repost
242+
if (update.type === 'ORIGINAL_POST') {
243+
return this.getPost(PostType.ORIGINAL, update.id, update)
244+
} else {
245+
return update
246+
}
247+
})
239248
}
240249

241250
/**
@@ -253,14 +262,21 @@ export class JikeClient extends EventEmitter<EventMap> {
253262
if (!isSuccess(result)) throwRequestFailureError(result, '发送动态')
254263
return {
255264
/** 动态 */
256-
post: new JikePost(this, type, result.data.data.id, result.data.data),
265+
post: this.getPost(type, result.data.data.id, result.data.data),
257266
/** 提示文本 */
258267
toast: result.data.toast,
259268
}
260269
}
261270

262-
getPost(type: PostType, id: string) {
263-
return new JikePost(this, type, id)
271+
getPost(type: PostType, id: string): JikePost
272+
getPost(type: PostType, id: string, detail: Post): JikePostWithDetail
273+
getPost(
274+
type: PostType,
275+
id: string,
276+
detail?: Post
277+
): JikePost | JikePostWithDetail {
278+
if (!detail) return new JikePost(this, type, id)
279+
else return new JikePostWithDetail(this, type, id, detail)
264280
}
265281

266282
/**

src/client/post.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
ListCommentOption,
99
PostType,
1010
} from '../types/options'
11-
import type { Comment, PostDetail, User } from '../types/entity'
11+
import type { Comment, Post, User } from '../types/entity'
1212
import type { JikeClient } from './client'
1313

1414
/**
@@ -18,14 +18,9 @@ export class JikePost {
1818
#client: JikeClient
1919
type: PostType
2020
id: string
21-
detail?: PostDetail
21+
detail?: Post
2222

23-
constructor(
24-
client: JikeClient,
25-
type: PostType,
26-
id: string,
27-
detail?: PostDetail
28-
) {
23+
constructor(client: JikeClient, type: PostType, id: string, detail?: Post) {
2924
this.#client = client
3025
this.type = type
3126
this.id = id
@@ -53,6 +48,7 @@ export class JikePost {
5348
async like() {
5449
const result = await this.apiClient.posts.like(this.type, this.id)
5550
if (!isSuccess(result)) throwRequestFailureError(result, '动态点赞')
51+
if (this.detail) this.detail.liked = true
5652
}
5753

5854
/**
@@ -61,6 +57,7 @@ export class JikePost {
6157
async unlike() {
6258
const result = await this.apiClient.posts.unlike(this.type, this.id)
6359
if (!isSuccess(result)) throwRequestFailureError(result, '取消动态点赞')
60+
if (this.detail) this.detail.liked = false
6461
}
6562

6663
/**
@@ -172,17 +169,12 @@ export class JikePost {
172169
}
173170

174171
export class JikePostWithDetail extends JikePost {
175-
constructor(
176-
client: JikeClient,
177-
type: PostType,
178-
id: string,
179-
detail: PostDetail
180-
) {
172+
constructor(client: JikeClient, type: PostType, id: string, detail: Post) {
181173
super(client, type, id, detail)
182174
this.detail = detail
183175
}
184176

185-
getDetail(): PostDetail {
177+
getDetail(): Post {
186178
return this.detail!
187179
}
188180
}

src/client/user.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { PostType } from '../types/options'
12
import { isSuccess, throwRequestFailureError } from './utils/response'
23
import { fetchPaginated } from './utils/paginate'
3-
import { JikePostWithDetail } from './post'
4-
import { rawTypeToEnum } from './utils/post'
54
import type { UserUnfollowOption } from '../types/options'
6-
import type { PostDetail, PostTypeRaw, User } from '../types/entity'
5+
import type { PersonalPost, User } from '../types/entity'
76
import type { Users } from '../types/api-responses'
87
import type { PaginatedFetcher, PaginatedOption } from './utils/paginate'
98
import type { JikeClient } from './client'
@@ -76,9 +75,9 @@ export class JikeUser<M extends boolean = boolean> {
7675
* 查询用户动态
7776
*/
7877
async queryPersonalUpdate(
79-
option: PaginatedOption<PostDetail, 'createdAt', string> = {}
78+
option: PaginatedOption<PersonalPost, 'createdAt', string> = {}
8079
) {
81-
const fetcher: PaginatedFetcher<PostDetail, string> = async (lastKey) => {
80+
const fetcher: PaginatedFetcher<PersonalPost, string> = async (lastKey) => {
8281
const result = await this.#client.apiClient.personalUpdate.single(
8382
await this.getUsername(),
8483
{ limit: 500, loadMoreKey: lastKey ? { lastId: lastKey } : undefined }
@@ -89,7 +88,7 @@ export class JikeUser<M extends boolean = boolean> {
8988
return [newKey, result.data.data]
9089
}
9190

92-
const data = await fetchPaginated(
91+
const updates = await fetchPaginated(
9392
fetcher,
9493
(item, data) => ({
9594
createdAt: new Date(item.createdAt),
@@ -98,17 +97,10 @@ export class JikeUser<M extends boolean = boolean> {
9897
option
9998
)
10099

101-
return data
102-
.filter((item) => item.type !== 'PERSONAL_UPDATE')
103-
.map(
104-
(item) =>
105-
new JikePostWithDetail(
106-
this.#client,
107-
rawTypeToEnum(item.type as PostTypeRaw),
108-
item.id,
109-
item
110-
)
111-
)
100+
return updates.map((update) => {
101+
// TODO: repost
102+
return this.#client.getPost(PostType.ORIGINAL, update.id, update)
103+
})
112104
}
113105

114106
/**

src/types/api-responses.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type {
55
Comment,
66
FollowingUpdate,
77
Notification,
8-
PostDetail,
8+
OriginalPost,
9+
PersonalPost,
910
Profile as ProfileEntity,
1011
RecommendPost,
1112
Story,
@@ -17,10 +18,11 @@ export namespace Posts {
1718
export interface CreateResponse {
1819
/** 提示文本 */
1920
toast: string
20-
data: PostDetail
21+
data: OriginalPost
2122
}
2223
export interface GetResponse {
23-
data: PostDetail
24+
// TODO: post detail
25+
data: any
2426
}
2527
export interface RemoveResponse {
2628
/** 提示文本 */
@@ -136,7 +138,7 @@ export namespace Users {
136138

137139
export namespace PersonalUpdate {
138140
export interface SingleResponse {
139-
data: PostDetail[]
141+
data: PersonalPost[]
140142
loadMoreKey?: {
141143
lastId: string
142144
}

src/types/entity/other.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,10 @@
11
import type { Topic } from './topic'
2-
import type {
3-
LinkInfo,
4-
Picture,
5-
ReadTrackInfo,
6-
Rollouts,
7-
ScrollingSubtitle,
8-
UrlsInText,
9-
} from './post'
2+
import type { Picture, ReadTrackInfo, UrlsInText } from './post'
103
import type { User } from './user'
114

125
export type PostTypeRaw = 'ORIGINAL_POST' | 'REPOST'
136
export type TargetType = PostTypeRaw | 'STORY'
147

15-
export interface PostDetail {
16-
id: string
17-
type: PostTypeRaw | 'PERSONAL_UPDATE'
18-
content: string
19-
/** 评论并转发到动态时存在。仅评论转发的内容,不包含上层评论 */
20-
rawContent?: string
21-
urlsInText: UrlsInText[]
22-
status: string
23-
isCommentForbidden: boolean
24-
likeCount: number
25-
commentCount: number
26-
repostCount: number
27-
shareCount: number
28-
pictures: Picture[]
29-
collected: boolean
30-
collectTime?: string
31-
user: User
32-
createdAt: string
33-
isFeatured: boolean
34-
enablePictureComments: boolean
35-
repostable: boolean
36-
rollouts: Rollouts
37-
scrollingSubtitles: ScrollingSubtitle[]
38-
actionTime?: string
39-
pinned?: {
40-
personalUpdate: boolean
41-
}
42-
target?: Target
43-
targetType?: string
44-
rootType?: string
45-
liked?: boolean
46-
syncCommentId?: string
47-
replyToComment?: Comment
48-
topic?: Topic
49-
/** 链接 */
50-
linkInfo?: LinkInfo
51-
}
52-
538
export interface Target {
549
type: string
5510
id: string

src/types/entity/post.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface PersonalUpdate {
1818
*/
1919
createdAt: string
2020
updateIds: string[]
21-
action: LiteralUnion<'USER_FOLLOW'>
21+
action: LiteralUnion<'USER_FOLLOW' | 'LIVE_SHARE'>
2222
usernames: string[]
2323
targetUsernames: string[]
2424
users: User[]
@@ -78,15 +78,28 @@ export interface OriginalPost {
7878
likeIcon?: LiteralUnion<'book'>
7979
/** 阅读时的 track 信息 */
8080
readTrackInfo: ReadTrackInfo
81+
/** 是否点赞 */
82+
liked?: boolean
8183
}
8284

85+
/** 动态 */
86+
export type Post = OriginalPost /* | Repost */
87+
88+
/** 主页动态 */
89+
export type PersonalPost = {
90+
/**
91+
* ISO-8601 格式,如 `2015-03-04T00:00:00.000Z`
92+
*/
93+
actionTime: string
94+
} & Post
95+
8396
/** 关注动态 */
8497
export type FollowingUpdate = {
8598
/**
8699
* ISO-8601 格式,如 `2015-03-04T00:00:00.000Z`
87100
*/
88101
actionTime: string
89-
} & (PersonalUpdate | OriginalPost)
102+
} & (PersonalUpdate | Post)
90103

91104
/** 阅读时的 track 信息 */
92105
export interface ReadTrackInfo {

0 commit comments

Comments
 (0)