Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package plus.maa.backend.controller

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.constraints.Max
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import plus.maa.backend.config.doc.RequireJwt
import plus.maa.backend.config.security.AuthenticationHelper
import plus.maa.backend.controller.response.MaaResult
import plus.maa.backend.controller.response.MaaResult.Companion.fail
import plus.maa.backend.controller.response.MaaResult.Companion.success
import plus.maa.backend.controller.response.user.MaaUserInfo
import plus.maa.backend.service.UserFollowService

@RestController
@RequestMapping("/follow")
@Tag(name = "UserFollow", description = "用户关注管理接口")
class UserFollowController(
private val userFollowService: UserFollowService,
private val helper: AuthenticationHelper,
) {

@Operation(summary = "关注用户")
@ApiResponse(description = "关注结果")
@RequireJwt
@PostMapping("/follow/{followUserId}")
fun follow(@PathVariable followUserId: String): MaaResult<Unit> = success(
userFollowService.follow(helper.requireUserId(), followUserId),
)

@Operation(summary = "取消关注")
@ApiResponse(description = "取消关注结果")
@RequireJwt
@PostMapping("/unfollow/{followUserId}")
fun unfollow(@PathVariable followUserId: String): MaaResult<Unit> = success(
userFollowService.unfollow(helper.requireUserId(), followUserId),
)

@Operation(summary = "获取关注列表")
@ApiResponse(description = "关注列表")
@RequireJwt
@GetMapping("/followingList")
fun getFollowingList(
@RequestParam page: Int = 1,
@RequestParam @Max(value = 50, message = "单页大小不得超过50") size: Int = 10): MaaResult<Page<MaaUserInfo>> {
// 之前的API约定分页从1开始,与Spring默认约定不同,在此转换
if (page < 1) {
return fail(422, "页数请从1开始")
}
val realPageable = PageRequest.of(
page - 1,
size,
)
return success(
userFollowService.getFollowingList(helper.requireUserId(), realPageable),
)
}

@Operation(summary = "获取粉丝列表")
@ApiResponse(description = "粉丝列表")
@RequireJwt
@GetMapping("/fansList")
fun getFansList(
@RequestParam page: Int = 1,
@RequestParam @Max(value = 50, message = "单页大小不得超过50") size: Int = 10): MaaResult<Page<MaaUserInfo>> {
// 之前的API约定分页从1开始,与Spring默认约定不同,在此转换
if (page < 1) {
return fail(422, "页数请从1开始")
}
val realPageable = PageRequest.of(
page - 1,
size,
)
return success(
userFollowService.getFansList(helper.requireUserId(), realPageable),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import plus.maa.backend.service.model.CopilotSetStatus

/**
* @author LoMu
* Date 2022-12-26 2:48
* Date 2022-12-26 2:48
*/
data class CopilotQueriesRequest(
val page: Int = 0,
Expand All @@ -22,4 +22,5 @@ data class CopilotQueriesRequest(
val language: String? = null,
@BindParam("copilot_ids") var copilotIds: List<Long>? = null,
val status: CopilotSetStatus? = null,
val onlyFollowing: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ data class CopilotSetQuery(
val keyword: String? = null,
@Schema(title = "创建者id")
val creatorId: String? = null,
@Schema(title = "仅查询关注者的作业集")
var onlyFollowing: Boolean = false,
@Schema(title = "需要包含的作业id列表")
val copilotIds: List<Long>? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ data class MaaUserInfo(
val id: String,
val userName: String,
val activated: Boolean = false,
val followingCount: Int = 0,
val fansCount: Int = 0,
) {
constructor(user: MaaUser) : this(user.userId!!, user.userName, user.status == 1)
constructor(user: MaaUser) : this(
id = user.userId!!,
userName = user.userName,
activated = user.status == 1,
followingCount = user.followingCount,
fansCount = user.fansCount,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package plus.maa.backend.repository

import org.springframework.data.mongodb.repository.MongoRepository
import plus.maa.backend.repository.entity.UserFans

interface UserFansRepository : MongoRepository<UserFans, String> {
fun findByUserId(userId: String): UserFans?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package plus.maa.backend.repository

import org.springframework.data.mongodb.repository.MongoRepository
import plus.maa.backend.repository.entity.UserFollowing

interface UserFollowingRepository : MongoRepository<UserFollowing, String> {
fun findByUserId(userId: String): UserFollowing?
}
2 changes: 2 additions & 0 deletions src/main/kotlin/plus/maa/backend/repository/entity/MaaUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ data class MaaUser(
var password: String,
var status: Int = 0,
var pwdUpdateTime: Instant = Instant.MIN,
var followingCount: Int = 0,
var fansCount: Int = 0,
) : Serializable {

companion object {
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/plus/maa/backend/repository/entity/UserFans.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package plus.maa.backend.repository.entity

import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import java.time.Instant

@Document("user_fans")
data class UserFans(
@Id
val id: String? = null,
val userId: String,
val fansList: MutableList<String> = mutableListOf(),
var updatedAt: Instant = Instant.now(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package plus.maa.backend.repository.entity

import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import java.time.Instant

@Document("user_following")
data class UserFollowing(
@Id
val id: String? = null,
val userId: String,
val followList: MutableList<String> = mutableListOf(),
var updatedAt: Instant = Instant.now(),
)
15 changes: 14 additions & 1 deletion src/main/kotlin/plus/maa/backend/service/CopilotService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import plus.maa.backend.controller.response.copilot.CopilotPageInfo
import plus.maa.backend.repository.CommentsAreaRepository
import plus.maa.backend.repository.CopilotRepository
import plus.maa.backend.repository.RedisCache
import plus.maa.backend.repository.UserFollowingRepository
import plus.maa.backend.repository.entity.Copilot
import plus.maa.backend.repository.entity.Copilot.OperationGroup
import plus.maa.backend.repository.entity.MaaUser
Expand Down Expand Up @@ -70,6 +71,7 @@ class CopilotService(
private val copilotConverter: CopilotConverter,
private val sensitiveWordService: SensitiveWordService,
private val segmentService: SegmentService,
private val userFollowingRepository: UserFollowingRepository,
) {
private val log = KotlinLogging.logger { }

Expand Down Expand Up @@ -198,7 +200,8 @@ class CopilotService(
request.levelKeyword.isNullOrBlank() &&
request.uploaderId.isNullOrBlank() &&
request.operator.isNullOrBlank() &&
request.copilotIds.isNullOrEmpty()
request.copilotIds.isNullOrEmpty() &&
!request.onlyFollowing
) {
request.orderBy?.blankAsNull()
?.let { key -> HOME_PAGE_CACHE_CONFIG[key] }
Expand Down Expand Up @@ -234,6 +237,16 @@ class CopilotService(

andQueries.add(Criteria.where("delete").`is`(false))

if (request.onlyFollowing && userId != null) {
val userFollowing = userFollowingRepository.findByUserId(userId)
val followingIds = userFollowing?.followList ?: emptyList()

if (followingIds.isEmpty()) {
return CopilotPageInfo(false, 0, 0, emptyList())
}
// 添加查询范围为关注者
andQueries.add(Criteria.where("uploaderId").`in`(followingIds))
}
// 仅查询自己的作业时才展示所有数据,否则只查询公开作业
if (request.uploaderId == "me" && userId != null) {
if (request.status != null) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/plus/maa/backend/service/CopilotSetService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import plus.maa.backend.controller.request.copilotset.CopilotSetUpdateReq
import plus.maa.backend.controller.response.copilotset.CopilotSetPageRes
import plus.maa.backend.controller.response.copilotset.CopilotSetRes
import plus.maa.backend.repository.CopilotSetRepository
import plus.maa.backend.repository.UserFollowingRepository
import plus.maa.backend.repository.entity.CopilotSet
import plus.maa.backend.service.model.CopilotSetStatus
import java.time.LocalDateTime
Expand All @@ -31,6 +32,7 @@ import java.util.regex.Pattern
class CopilotSetService(
private val idComponent: IdComponent,
private val converter: CopilotSetConverter,
private val userFollowingRepository: UserFollowingRepository,
private val repository: CopilotSetRepository,
private val userService: UserService,
private val mongoTemplate: MongoTemplate,
Expand Down Expand Up @@ -123,6 +125,17 @@ class CopilotSetService(
}
andList.add(permissionCriterion)
andList.add(Criteria.where("delete").`is`(false))

if (req.onlyFollowing == true && userId != null) {
val userFollowing = userFollowingRepository.findByUserId(userId)
val followingIds = userFollowing?.followList ?: emptyList()
if (followingIds.isEmpty()) {
return CopilotSetPageRes(false, 0, 0, mutableListOf())
}

andList.add(Criteria.where("creatorId").`in`(followingIds))
}

if (!req.copilotIds.isNullOrEmpty()) {
andList.add(Criteria.where("copilotIds").all(req.copilotIds))
}
Expand Down
Loading