Skip to content

Commit e2b8d7e

Browse files
Leizhenpengsxzz
andauthored
feat: add listMergedMentions (#175)
Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
1 parent 3f8d9cd commit e2b8d7e

File tree

8 files changed

+84
-5
lines changed

8 files changed

+84
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ docs
55
.pnpm-debug.log*
66
.vercel
77
.eslintcache
8+
.idea

src/api/notifications.ts

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { request, toResponse } from '../request'
22
import type { PaginationOption } from '../types/options'
33
import type { Notifications } from '../types/api-responses'
4+
import type { Notification } from '../types/entity'
45

56
/**
67
* 获取通知列表
@@ -19,3 +20,44 @@ export const list = <T = Notifications.ListResponse>(
1920
},
2021
}),
2122
)
23+
24+
/**
25+
* 获取合并通知的列表
26+
* @param option 起始通知 ID
27+
*/
28+
export const listMergedMentions = <
29+
T = Notifications.ListMergedMentionsResponse,
30+
>(
31+
id: string,
32+
) =>
33+
toResponse<T>(
34+
request.post(`1.0/notifications/listMergedMentions`, {
35+
json: {
36+
startNotificationId: id,
37+
},
38+
}),
39+
)
40+
41+
/**
42+
* 获取通知列表,自动展开合并通知
43+
* @param option 分页选项
44+
*/
45+
export const listWithMerged = async (
46+
option: Pick<
47+
PaginationOption<{ lastNotificationId: string }>,
48+
'loadMoreKey'
49+
> = {},
50+
) => {
51+
const result = await list(option)
52+
const notifications: Notification[] = []
53+
for (const item of result.data.data) {
54+
if (item.linkType === 'MERGED_MENTION') {
55+
const merged = (await listMergedMentions(item.id)).data.data
56+
notifications.push(...merged)
57+
} else {
58+
notifications.push(item)
59+
}
60+
}
61+
result.data.data = notifications
62+
return result
63+
}

src/client/client.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,21 @@ export class JikeClient extends EventEmitter<EventMap> {
198198
'createdAt' | 'updatedAt',
199199
string
200200
> = {},
201+
withMerged = false,
201202
) {
202203
const fetcher: PaginatedFetcher<Notification, string> = async (lastKey) => {
203-
const result = await this.#client.notifications.list({
204+
const listFn =
205+
this.#client.notifications[withMerged ? 'listWithMerged' : 'list']
206+
207+
const result = await listFn({
204208
loadMoreKey: lastKey ? { lastNotificationId: lastKey } : undefined,
205209
})
206210
if (!isSuccess(result)) throwRequestFailureError(result, '查询通知')
211+
207212
const newKey = result.data.loadMoreKey?.lastNotificationId
208213
return [newKey, result.data.data]
209214
}
215+
210216
return fetchPaginated(
211217
fetcher,
212218
(item, data) => ({

src/types/api-responses.ts

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export namespace Notifications {
5656
lastNotificationId: string
5757
}
5858
}
59+
60+
export interface ListMergedMentionsResponse {
61+
data: Notification[]
62+
}
5963
}
6064

6165
export namespace UserRelation {

src/types/entity/notification.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { LiteralUnion } from '../../utils'
21
import type { Picture, PostStatus, PostType } from './post'
2+
import type { LiteralUnion } from '../../utils'
33
import type { User } from './user'
44

55
export interface Notification {
@@ -20,6 +20,8 @@ export interface Notification {
2020
| 'PERSONAL_UPDATE_REPOSTED'
2121
| 'LIKE_AVATAR'
2222
| 'AVATAR_GREET'
23+
| 'MERGED_COMMENT'
24+
| 'MENTION_FROM_UNFOLLOWED_USER'
2325
>
2426
/**
2527
* ISO-8601 格式,如 `2015-03-04T00:00:00.000Z`
@@ -35,7 +37,7 @@ export interface Notification {
3537
actionType: LiteralUnion<'actionType' | 'USER_LIST'>
3638
actionItem: ActionItem
3739
linkUrl: string
38-
linkType: LiteralUnion<PostType>
40+
linkType: LiteralUnion<PostType | 'MERGED_MENTION'>
3941
referenceItem: ReferenceItem
4042
}
4143

src/types/entity/post.ts

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface OriginalPost {
8787
*/
8888
export interface Repost {
8989
type: 'REPOST'
90+
id: string
9091
[key: string]: any
9192
}
9293

tests/api/notifications.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@ describe('notifications should work', () => {
1010
expect(isSuccess(result)).toBe(true)
1111
expect(result.data.data.length).greaterThanOrEqual(1)
1212
})
13+
14+
it('list merged_notifications should work', async () => {
15+
const testMergedCommentId = '65c464b350cd1a4d56c9bb53'
16+
const result =
17+
await api.notifications.listMergedMentions(testMergedCommentId)
18+
expect(isSuccess(result)).toBe(true)
19+
expect(result.data.data.length).greaterThanOrEqual(1)
20+
})
21+
22+
it.only('list with merged should work', async () => {
23+
const result = await api.notifications.listWithMerged()
24+
expect(result.data.data.length).greaterThanOrEqual(1)
25+
})
1326
})

tests/jike-client/notification.test.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ import { describe, expect, it } from 'vitest'
22
import { JikeClient, limit } from '../../src'
33
import { config, refreshToken } from '../config'
44

5-
describe('notifications should work', () => {
5+
describe('notification should work', () => {
66
const client = new JikeClient({ ...config, refreshToken })
77

88
it('queryNotifications should work', async () => {
99
const notifications = await client.queryNotifications({
1010
limit: limit.limitMaxCount(100),
1111
})
12-
expect(notifications.length).toBe(100)
12+
expect(notifications.length).lessThanOrEqual(100)
13+
})
14+
15+
it('queryNotifications with merged should work', async () => {
16+
const notifications = await client.queryNotifications(
17+
{
18+
limit: limit.limitMaxCount(100),
19+
},
20+
true,
21+
)
22+
expect(notifications.length).lessThanOrEqual(100)
1323
})
1424
})

0 commit comments

Comments
 (0)