Skip to content

feat:新增关注功能+只查询关注者作业#200

Merged
dragove merged 5 commits into
ZOOT-Plus:devfrom
half-yutou:dev
May 29, 2025
Merged

feat:新增关注功能+只查询关注者作业#200
dragove merged 5 commits into
ZOOT-Plus:devfrom
half-yutou:dev

Conversation

@half-yutou
Copy link
Copy Markdown
Contributor

val id: String? = null,
val userId: String,
val fansList: MutableSet<MaaUserInfo> = mutableSetOf(),
var updatedAt: LocalDateTime = LocalDateTime.now(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实用 Instant 更好,LocalDateTime 本身缺少时区信息

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果不考虑i8n的话LocalDateTime可能更方便吧,Instance还要展示层根据时区适配,以后遇到需要再改这里 :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实以前我们有海外用户反馈时间不对,因此我们向外序列化均为 ISO-8601,为了 LocalDateTime 不带时区的问题还特地写了一个 https://github.com/MaaAssistantArknights/MaaBackendCenter/blob/feaf83a371fa38616bce97e2e5a4fb3f5c5b17c8/src/main/kotlin/plus/maa/backend/config/JacksonConfig.kt#L29 配置(

Copy link
Copy Markdown
Contributor Author

@half-yutou half-yutou Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实以前我们有海外用户反馈时间不对,因此我们向外序列化均为 ISO-8601,为了 LocalDateTime 不带时区的问题还特地写了一个

https://github.com/MaaAssistantArknights/MaaBackendCenter/blob/feaf83a371fa38616bce97e2e5a4fb3f5c5b17c8/src/main/kotlin/plus/maa/backend/config/JacksonConfig.kt#L29

配置(

ok,既然序列化配置也有了那我也同步改成Instant

// 更新粉丝列表
val fans = userFansRepository.findByUserId(followUserId)
?: UserFans(userId = followUserId)
if (!fans.fansList.any { it.id == userId }) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于头部作者来说,数据量的传输可能有些大了

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那我改改更新用MongoDB的$push操作就不用再查一次列表了吧。然后关注/粉丝计数换成原子递增计数了而不是每次直接用列表的size了(虽然可能增加一点脏数据的恢复成本...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有 https://www.mongodb.com/docs/manual/reference/operator/aggregation/size/

好的我学习一下MongoDB,不太熟悉这个

Comment thread src/main/kotlin/plus/maa/backend/service/UserFollowService.kt
}

fun getFansList(userId: String, pageable: Pageable): Page<MaaUserInfo> {
val fans = userFansRepository.findByUserId(userId)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我的建议是直接聚合

@Pleasurecruise
Copy link
Copy Markdown
Contributor

接口测试是没问题的

@half-yutou
Copy link
Copy Markdown
Contributor Author

half-yutou commented Apr 23, 2025

发现一个bug,可能得改一下。

#195 (comment)

如上述架构图,当用户A关注用户B时,用户A的关注列表followList里存储了一个用户B此时的信息(包括了其此时的fans数和following数)
但是一段时间内,用户B的fans数和Following数量是会有所改变的。不可能每次用户B的fans数增加后,去修改用户A关注列表内已经存储好的用户B的信息
一段时间后,当获取A的关注列表时,这时返回的用户B的信息依旧是当时关注用户B的快照。

简单说就是,关注列表(followList)和粉丝列表存储的是关注时UserInfo的快照,与当前的信息不一致。
所以应该是,关注列表和粉丝列表仅存储用户ID,查询时再根据用户ID二次查询。

很抱歉我之前想当然地在followList列表中存储了用户信息的快照,可能要一点时间我重新写一下存储用户ID的版本

@half-yutou
Copy link
Copy Markdown
Contributor Author

此次提交
adv:

  1. 粉丝列表采用List存储,且存储粉丝用户的id而非粉丝用户的UserInfo
  2. 获取粉丝列表时,直接查询整个粉丝列表(存储ID信息,数量不会太大),再分页查User表返回粉丝用户信息
  3. 手写分页查询关注列表粉丝列表controller入参,与之前API分页起始位置1保持一致

@Handiwork 佬那个聚合相关的一堆东西我没学会... :( 要不下个迭代再优化优化
不过更改为只存储ID的情况下,直接把粉丝列表查出来其实数据传输压力也没那么大了吧(比如两千个64位ID字符串应该是可以接受的吧),查用户信息的时候再根据分页信息分批查的

分页的问题我直接简单在controller加了一个-1的校验和转换

不过测试的时候发现个问题:swagger配置了jwt认证去请求还是会被认成匿名用户,获取不到登录的用户id,很奇怪。
用postman加了jwt测试就正常

@Pleasurecruise
Copy link
Copy Markdown
Contributor

swagger配置了jwt认证去请求还是会被认成匿名用户,获取不到登录的用户id

swagger ui 没有配置安全需求
头部不会自动附带 JWT Token
我提了个pr #201

@Handiwork
Copy link
Copy Markdown
Contributor

不过测试的时候发现个问题:swagger配置了jwt认证去请求还是会被认成匿名用户,获取不到登录的用户id,很奇怪。 用postman加了jwt测试就正常

你是否在 swagger ui 上填写时候也加上了"Bearer ",它其实并不需要

@half-yutou
Copy link
Copy Markdown
Contributor Author

不过测试的时候发现个问题:swagger配置了jwt认证去请求还是会被认成匿名用户,获取不到登录的用户id,很奇怪。 用postman加了jwt测试就正常

你是否在 swagger ui 上填写时候也加上了"Bearer ",它其实并不需要

并没有,直接填写的jwt本身。并且能通过@RequireJwt的校验
#201

@Handiwork
Copy link
Copy Markdown
Contributor

@half-yutou @RequireJwt 只是指示 SpringDoc 该 endpoint 需要安全验证,并不会校验什么。另外,我拉取了你的 PR,并在内嵌的 Swagger UI 下对 UserFollow 下的四个接口进行了测试,没有发现获取不到用户 ID 的情况。我想这里需要你复现一下。

@Pleasurecruise
Copy link
Copy Markdown
Contributor

Pleasurecruise commented Apr 24, 2025

@Handiwork 主要是/set/query/copilot/query 当指定onlyFollowing为true时需要获取userId
头部没有传递jwt userid就会被解析为空...?

https://github.com/MaaAssistantArknights/MaaBackendCenter/blob/feaf83a371fa38616bce97e2e5a4fb3f5c5b17c8/src/main/kotlin/plus/maa/backend/config/security/JwtAuthenticationTokenFilter.kt#L37-L42

@half-yutou
Copy link
Copy Markdown
Contributor Author

@half-yutou @RequireJwt 只是指示 SpringDoc 该 endpoint 需要安全验证,并不会校验什么。另外,我拉取了你的 PR,并在内嵌的 Swagger UI 下对 UserFollow 下的四个接口进行了测试,没有发现获取不到用户 ID 的情况。我想这里需要你复现一下。

跟P佬说的一样,Follow的这几个接口本身是没什么问题的。而是CopilotController查询Copilot时,在swagger上配置了jwt也会获取不到Userid(userid == null),在postman上测试表现正常(可以正确获取userid)。

Copy link
Copy Markdown
Contributor

@dragove dragove left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抱歉,最近比较忙一直没review这个pr。前段时间因为性能问题所以先搁置这个PR了。现在回过头看了。
麻烦rebase到最新分支上,问题应该不大,rebase后我本地做下测试,改进相关(比如直接聚合查询用户信息)可以另外提交PR改善。合并的话可以让前端尽早开始开发。后续用户这块还需要其他改善功能。

Comment thread src/main/kotlin/plus/maa/backend/controller/request/copilotset/CopilotSetQuery.kt Outdated
Comment thread src/main/kotlin/plus/maa/backend/controller/UserFollowController.kt Outdated
Comment thread src/main/kotlin/plus/maa/backend/controller/UserFollowController.kt Outdated
@half-yutou
Copy link
Copy Markdown
Contributor Author

抱歉,最近比较忙一直没review这个pr。前段时间因为性能问题所以先搁置这个PR了。现在回过头看了。 麻烦rebase到最新分支上,问题应该不大,rebase后我本地做下测试,改进相关(比如直接聚合查询用户信息)可以另外提交PR改善。合并的话可以让前端尽早开始开发。后续用户这块还需要其他改善功能。

辛苦了!我争取这个周末前解决这些问题:)

xuyutong.csu and others added 4 commits May 29, 2025 13:38
1. 粉丝列表采用List存储,且存储粉丝用户的id而非粉丝用户的UserInfo
2. 获取粉丝列表时,直接查询整个粉丝列表(存储ID信息,数量不会太大),再分页查User表返回粉丝用户信息
3. 手写分页查询关注列表粉丝列表controller入参,与之前API分页起始位置1保持一致
Copy link
Copy Markdown
Contributor

@dragove dragove left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来可以合并了,有bug后面再修(逃

@dragove dragove merged commit 45a8725 into ZOOT-Plus:dev May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants