Skip to content

Commit

Permalink
feat: add listMergedMentions (#175)
Browse files Browse the repository at this point in the history
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
  • Loading branch information
Leizhenpeng and sxzz committed Feb 10, 2024
1 parent 3f8d9cd commit e2b8d7e
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ docs
.pnpm-debug.log*
.vercel
.eslintcache
.idea
42 changes: 42 additions & 0 deletions src/api/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { request, toResponse } from '../request'
import type { PaginationOption } from '../types/options'
import type { Notifications } from '../types/api-responses'
import type { Notification } from '../types/entity'

/**
* 获取通知列表
Expand All @@ -19,3 +20,44 @@ export const list = <T = Notifications.ListResponse>(
},
}),
)

/**
* 获取合并通知的列表
* @param option 起始通知 ID
*/
export const listMergedMentions = <
T = Notifications.ListMergedMentionsResponse,
>(
id: string,
) =>
toResponse<T>(
request.post(`1.0/notifications/listMergedMentions`, {
json: {
startNotificationId: id,
},
}),
)

/**
* 获取通知列表,自动展开合并通知
* @param option 分页选项
*/
export const listWithMerged = async (
option: Pick<
PaginationOption<{ lastNotificationId: string }>,
'loadMoreKey'
> = {},
) => {
const result = await list(option)
const notifications: Notification[] = []
for (const item of result.data.data) {
if (item.linkType === 'MERGED_MENTION') {
const merged = (await listMergedMentions(item.id)).data.data
notifications.push(...merged)
} else {
notifications.push(item)
}
}
result.data.data = notifications
return result
}
8 changes: 7 additions & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,21 @@ export class JikeClient extends EventEmitter<EventMap> {
'createdAt' | 'updatedAt',
string
> = {},
withMerged = false,
) {
const fetcher: PaginatedFetcher<Notification, string> = async (lastKey) => {
const result = await this.#client.notifications.list({
const listFn =
this.#client.notifications[withMerged ? 'listWithMerged' : 'list']

const result = await listFn({
loadMoreKey: lastKey ? { lastNotificationId: lastKey } : undefined,
})
if (!isSuccess(result)) throwRequestFailureError(result, '查询通知')

const newKey = result.data.loadMoreKey?.lastNotificationId
return [newKey, result.data.data]
}

return fetchPaginated(
fetcher,
(item, data) => ({
Expand Down
4 changes: 4 additions & 0 deletions src/types/api-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export namespace Notifications {
lastNotificationId: string
}
}

export interface ListMergedMentionsResponse {
data: Notification[]
}
}

export namespace UserRelation {
Expand Down
6 changes: 4 additions & 2 deletions src/types/entity/notification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LiteralUnion } from '../../utils'
import type { Picture, PostStatus, PostType } from './post'
import type { LiteralUnion } from '../../utils'
import type { User } from './user'

export interface Notification {
Expand All @@ -20,6 +20,8 @@ export interface Notification {
| 'PERSONAL_UPDATE_REPOSTED'
| 'LIKE_AVATAR'
| 'AVATAR_GREET'
| 'MERGED_COMMENT'
| 'MENTION_FROM_UNFOLLOWED_USER'
>
/**
* ISO-8601 格式,如 `2015-03-04T00:00:00.000Z`
Expand All @@ -35,7 +37,7 @@ export interface Notification {
actionType: LiteralUnion<'actionType' | 'USER_LIST'>
actionItem: ActionItem
linkUrl: string
linkType: LiteralUnion<PostType>
linkType: LiteralUnion<PostType | 'MERGED_MENTION'>
referenceItem: ReferenceItem
}

Expand Down
1 change: 1 addition & 0 deletions src/types/entity/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface OriginalPost {
*/
export interface Repost {
type: 'REPOST'
id: string
[key: string]: any
}

Expand Down
13 changes: 13 additions & 0 deletions tests/api/notifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@ describe('notifications should work', () => {
expect(isSuccess(result)).toBe(true)
expect(result.data.data.length).greaterThanOrEqual(1)
})

it('list merged_notifications should work', async () => {
const testMergedCommentId = '65c464b350cd1a4d56c9bb53'
const result =
await api.notifications.listMergedMentions(testMergedCommentId)
expect(isSuccess(result)).toBe(true)
expect(result.data.data.length).greaterThanOrEqual(1)
})

it.only('list with merged should work', async () => {
const result = await api.notifications.listWithMerged()
expect(result.data.data.length).greaterThanOrEqual(1)
})
})
14 changes: 12 additions & 2 deletions tests/jike-client/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import { describe, expect, it } from 'vitest'
import { JikeClient, limit } from '../../src'
import { config, refreshToken } from '../config'

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

it('queryNotifications should work', async () => {
const notifications = await client.queryNotifications({
limit: limit.limitMaxCount(100),
})
expect(notifications.length).toBe(100)
expect(notifications.length).lessThanOrEqual(100)
})

it('queryNotifications with merged should work', async () => {
const notifications = await client.queryNotifications(
{
limit: limit.limitMaxCount(100),
},
true,
)
expect(notifications.length).lessThanOrEqual(100)
})
})

0 comments on commit e2b8d7e

Please sign in to comment.