Skip to content

Commit 208c167

Browse files
committed
feat: add post listLikedUsers
1 parent 1327bde commit 208c167

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [x] 分享动态
3939
- [x] 点赞
4040
- [x] 取消点赞
41+
- [x] 获取点赞用户列表
4142
- 动态广场
4243
- [x] 获取动态推荐
4344
- 评论

src/api/posts.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { request, toResponse } from '../request'
2-
import type { CreatePostOption, PostType } from '../types/options'
2+
import type {
3+
CreatePostOption,
4+
PaginationOption,
5+
PostType,
6+
} from '../types/options'
37
import type { Posts } from '../types/api-responses'
48

59
/**
@@ -83,3 +87,22 @@ export const remove = <T = Posts.RemoveResponse>(type: PostType, id: string) =>
8387
json: { id },
8488
})
8589
)
90+
91+
/**
92+
* 获取点赞用户列表
93+
* @param id 动态ID
94+
*/
95+
export const listLikedUsers = <T = Posts.ListLikedUsersResponse>(
96+
type: PostType,
97+
id: string,
98+
option: PaginationOption<string> = {}
99+
) =>
100+
toResponse<T>(
101+
request.post(`1.0/${type}/listLikedUsers`, {
102+
json: {
103+
id,
104+
limit: option.limit ?? 100,
105+
loadMoreKey: option.loadMoreKey,
106+
},
107+
})
108+
)

src/client/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ export class JikeClient extends EventEmitter<EventMap> {
261261
}
262262
}
263263

264+
getPost(type: PostType, id: string) {
265+
return new JikePost(this, type, id)
266+
}
267+
264268
/**
265269
* 刷新 access token
266270
*/

src/client/post.ts

Lines changed: 26 additions & 1 deletion
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 } from '../types/entity'
11+
import type { Comment, PostDetail, User } from '../types/entity'
1212
import type { JikeClient } from './client'
1313

1414
/**
@@ -99,6 +99,31 @@ export class JikePost {
9999
return result.data.toast
100100
}
101101

102+
/**
103+
* 获取点赞用户列表
104+
* @returns 用户列表
105+
*/
106+
async listLikedUsers(option: PaginatedOption<User, never, string> = {}) {
107+
const fetcher: PaginatedFetcher<User, string> = async (lastKey) => {
108+
const result = await this.apiClient.posts.listLikedUsers(
109+
this.type,
110+
this.id,
111+
{ loadMoreKey: lastKey, limit: 500 }
112+
)
113+
if (!isSuccess(result))
114+
throwRequestFailureError(result, '获取点赞用户列表')
115+
116+
const newKey = result.data.loadMoreKey
117+
return [newKey, result.data.data]
118+
}
119+
120+
return fetchPaginated(
121+
fetcher,
122+
(item, data) => ({ total: data.length + 1 }),
123+
option
124+
)
125+
}
126+
102127
/**
103128
* 添加评论
104129
* @returns 评论信息

src/types/api-responses.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export namespace Posts {
2525
/** 提示文本 */
2626
toast: string
2727
}
28+
export interface ListLikedUsersResponse {
29+
data: User[]
30+
loadMoreKey?: string
31+
}
2832
}
2933

3034
export namespace Notifications {

src/types/entity/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface User {
6363
city?: string
6464
country?: string
6565
province?: string
66+
ref?: string
6667
refRemark?: RefRemark
6768
bio?: string
6869
decorations?: {

tests/api/posts.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,13 @@ describe('new post should work', () => {
9696
expect(isSuccess(result)).toBe(true)
9797
expect(result.data.toast).toBe('删除成功')
9898
})
99+
100+
it('listLikedUsers should work', async () => {
101+
const result = await api.posts.listLikedUsers(
102+
PostType.ORIGINAL,
103+
'626303a8079c68c18e2ac9bc'
104+
)
105+
expect(isSuccess(result)).toBe(true)
106+
expect(result.data.data.length).greaterThan(0)
107+
})
99108
})

0 commit comments

Comments
 (0)