Skip to content

Commit 0d8f592

Browse files
committed
feat(jike-client): add isFollowing
1 parent 4eb9444 commit 0d8f592

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- [x] 获取被关注列表
3232
- [x] 关注
3333
- [x] 取消关注
34+
- [x] 是否关注用户
3435
- 动态帖子
3536
- [x] 发送动态
3637
- [x] 获取动态详情

src/client/user.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class JikeUser<M extends boolean = boolean> {
131131
}
132132

133133
/**
134-
* 查询用户被关注带关注时间,较慢)
134+
* 查询用户被关注带关注时间。逐个用户查询,耗时较长。
135135
*/
136136
queryFollowersWithTime(
137137
option: PaginatedOption<FollowerWithTime, 'followTime', string> = {}
@@ -163,6 +163,38 @@ export class JikeUser<M extends boolean = boolean> {
163163
)
164164
}
165165

166+
/**
167+
* 是否关注用户
168+
* @param user 用户名或用户实例
169+
* @param mode
170+
* `following`: 从 当前用户 的 关注 列表查找 目标用户 是否存在;
171+
* `follower` : 从 目标用户 的 被关注 列表查找 当前用户 是否存在。
172+
*
173+
* 为提升查询速度,建议哪个数量少使用哪个。
174+
*/
175+
async isFollowing(user: JikeUser | string, mode: 'following' | 'follower') {
176+
const target = typeof user === 'string' ? this.#client.getUser(user) : user
177+
const targetUsername = await (mode === 'following'
178+
? target.getUsername()
179+
: this.getUsername())
180+
181+
const includes = (data: User[]) =>
182+
data.some((item) => item.username === targetUsername)
183+
184+
let data: User[]
185+
if (mode === 'following') {
186+
data = await this.queryFollowings({
187+
limit: (opt, item, data) => !includes(data),
188+
})
189+
} else {
190+
data = await target.queryFollowers({
191+
limit: (opt, item, data) => !includes(data),
192+
})
193+
}
194+
195+
return includes(data)
196+
}
197+
166198
/**
167199
* 查询用户关注
168200
*/

src/client/utils/paginate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ export interface PaginatedOption<
55
L extends keyof LimitOptionAll = keyof LimitOptionAll,
66
K = unknown
77
> {
8+
/** @returns `true`: 继续下一页;`false`: 停止获取 */
89
limit?: LimitFn<L | 'total', T>
910
/**
10-
* 当获取下一页时触发,返回 false 将停止获取下一页
11+
* 当获取下一页时触发
12+
* @returns `false`: 将停止获取下一页
1113
*/
1214
onNextPage?: (
1315
currentPage: number,

src/types/entity/profile.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { StoryStatus } from './post'
12
import type { LiteralUnion } from '../../utils/typings'
23
import type { AvatarImage, BackgroundImage, StatsCount, User } from './user'
34

@@ -112,6 +113,22 @@ export interface Profile {
112113
isDefaultScreenName?: boolean
113114
/** 访客信息,仅自己可见 */
114115
profileVisitInfo?: ProfileVisitInfo
116+
/** 是否关注 */
117+
following: boolean
118+
/** 是否拉黑 */
119+
blocking: boolean
120+
/** 是否 不看TA的内容 */
121+
muting: boolean
122+
/** 是否 添加到桌面小组件 */
123+
watching: boolean
124+
/** 是否特别关心 */
125+
isWatching: boolean
126+
/** 日记状态 */
127+
storyStatus: LiteralUnion<StoryStatus>
128+
/** 是否夸夸过 */
129+
respected: boolean
130+
131+
[key: string]: any
115132
}
116133

117134
/**

tests/jike-client/user.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { JikeClient } from '../../src'
3+
import { config, refreshToken } from '../config'
4+
5+
describe('user', async () => {
6+
const client = new JikeClient({ ...config, refreshToken })
7+
const username = 'c00cf715-b71b-464b-9dcc-d73e01878137'
8+
9+
const profile = await client.getUser(username).queryProfile()
10+
const { following } = profile.user
11+
12+
describe('isFollowing should work', () => {
13+
it('follower mode should work', async () => {
14+
expect(await client.getSelf().isFollowing(username, 'follower')).toBe(
15+
following
16+
)
17+
})
18+
19+
it('following mode should work', async () => {
20+
expect(await client.getSelf().isFollowing(username, 'following')).toBe(
21+
following
22+
)
23+
})
24+
})
25+
})

0 commit comments

Comments
 (0)