|
| 1 | +package com.involutionhell.backend.usercenter.follows; |
| 2 | + |
| 3 | +import cn.dev33.satoken.annotation.SaCheckLogin; |
| 4 | +import cn.dev33.satoken.stp.StpUtil; |
| 5 | +import com.involutionhell.backend.common.api.ApiResponse; |
| 6 | +import com.involutionhell.backend.usercenter.dto.UserView; |
| 7 | +import com.involutionhell.backend.usercenter.model.UserAccount; |
| 8 | +import com.involutionhell.backend.usercenter.service.UserCenterService; |
| 9 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 10 | +import org.springframework.web.bind.annotation.GetMapping; |
| 11 | +import org.springframework.web.bind.annotation.PathVariable; |
| 12 | +import org.springframework.web.bind.annotation.PostMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestParam; |
| 15 | +import org.springframework.web.bind.annotation.RestController; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +/** |
| 23 | + * 用户关注 / 粉丝相关接口。 |
| 24 | + * |
| 25 | + * 公开接口(SaToken 白名单): |
| 26 | + * - GET /api/user-center/follows/stats/{identifier} 统计某人的粉丝/关注数 |
| 27 | + * - GET /api/user-center/follows/followers/{identifier} 谁关注了 Ta |
| 28 | + * - GET /api/user-center/follows/following/{identifier} Ta 关注了谁 |
| 29 | + * - GET /api/user-center/follows/is-following/{identifier} 当前登录用户是否关注 Ta(匿名返回 false) |
| 30 | + * |
| 31 | + * 登录才能操作的接口(SaCheckLogin): |
| 32 | + * - POST /api/user-center/follows/{identifier} |
| 33 | + * - DELETE /api/user-center/follows/{identifier} |
| 34 | + * |
| 35 | + * identifier 参数和 /u/{identifier} 保持一致:纯数字按 github_id 查,否则按 username 查。 |
| 36 | + */ |
| 37 | +@RestController |
| 38 | +@RequestMapping("/api/user-center/follows") |
| 39 | +public class FollowController { |
| 40 | + |
| 41 | + private final FollowService followService; |
| 42 | + private final UserCenterService userCenterService; |
| 43 | + |
| 44 | + public FollowController(FollowService followService, UserCenterService userCenterService) { |
| 45 | + this.followService = followService; |
| 46 | + this.userCenterService = userCenterService; |
| 47 | + } |
| 48 | + |
| 49 | + /** 关注 (identifier)。幂等。 */ |
| 50 | + @SaCheckLogin |
| 51 | + @PostMapping("/{identifier}") |
| 52 | + public ApiResponse<Map<String, Object>> follow(@PathVariable String identifier) { |
| 53 | + long followerId = StpUtil.getLoginIdAsLong(); |
| 54 | + UserAccount target = resolveOrThrow(identifier); |
| 55 | + followService.follow(followerId, target.id()); |
| 56 | + return okStats(target); |
| 57 | + } |
| 58 | + |
| 59 | + /** 取消关注 (identifier)。幂等。 */ |
| 60 | + @SaCheckLogin |
| 61 | + @DeleteMapping("/{identifier}") |
| 62 | + public ApiResponse<Map<String, Object>> unfollow(@PathVariable String identifier) { |
| 63 | + long followerId = StpUtil.getLoginIdAsLong(); |
| 64 | + UserAccount target = resolveOrThrow(identifier); |
| 65 | + followService.unfollow(followerId, target.id()); |
| 66 | + return okStats(target); |
| 67 | + } |
| 68 | + |
| 69 | + /** 统计 (identifier) 的粉丝 / 关注数。匿名可访问。 */ |
| 70 | + @GetMapping("/stats/{identifier}") |
| 71 | + public ApiResponse<Map<String, Object>> stats(@PathVariable String identifier) { |
| 72 | + UserAccount target = resolveOrThrow(identifier); |
| 73 | + return okStats(target); |
| 74 | + } |
| 75 | + |
| 76 | + /** 当前登录用户是否关注了 (identifier)。匿名返回 false。 */ |
| 77 | + @GetMapping("/is-following/{identifier}") |
| 78 | + public ApiResponse<Map<String, Object>> isFollowing(@PathVariable String identifier) { |
| 79 | + UserAccount target = resolveOrThrow(identifier); |
| 80 | + boolean yes = false; |
| 81 | + try { |
| 82 | + if (StpUtil.isLogin()) { |
| 83 | + long followerId = StpUtil.getLoginIdAsLong(); |
| 84 | + yes = followService.isFollowing(followerId, target.id()); |
| 85 | + } |
| 86 | + } catch (Exception ignored) { |
| 87 | + // 未登录或 token 非法都按 false 处理 |
| 88 | + } |
| 89 | + Map<String, Object> body = new HashMap<>(); |
| 90 | + body.put("isFollowing", yes); |
| 91 | + return ApiResponse.ok(body); |
| 92 | + } |
| 93 | + |
| 94 | + /** 粉丝列表:谁关注了 (identifier)。返回 UserView 数组。 */ |
| 95 | + @GetMapping("/followers/{identifier}") |
| 96 | + public ApiResponse<List<UserView>> followers( |
| 97 | + @PathVariable String identifier, |
| 98 | + @RequestParam(defaultValue = "50") int limit, |
| 99 | + @RequestParam(defaultValue = "0") int offset |
| 100 | + ) { |
| 101 | + UserAccount target = resolveOrThrow(identifier); |
| 102 | + List<Long> ids = followService.listFollowerIds(target.id(), limit, offset); |
| 103 | + return ApiResponse.ok(usersByIds(ids)); |
| 104 | + } |
| 105 | + |
| 106 | + /** 关注列表:(identifier) 关注了谁。返回 UserView 数组。 */ |
| 107 | + @GetMapping("/following/{identifier}") |
| 108 | + public ApiResponse<List<UserView>> following( |
| 109 | + @PathVariable String identifier, |
| 110 | + @RequestParam(defaultValue = "50") int limit, |
| 111 | + @RequestParam(defaultValue = "0") int offset |
| 112 | + ) { |
| 113 | + UserAccount target = resolveOrThrow(identifier); |
| 114 | + List<Long> ids = followService.listFollowingIds(target.id(), limit, offset); |
| 115 | + return ApiResponse.ok(usersByIds(ids)); |
| 116 | + } |
| 117 | + |
| 118 | + // ----- helpers ----- |
| 119 | + |
| 120 | + private UserAccount resolveOrThrow(String identifier) { |
| 121 | + Optional<UserAccount> account = userCenterService.findByIdentifier(identifier); |
| 122 | + if (account.isEmpty()) { |
| 123 | + throw new IllegalArgumentException("用户不存在: " + identifier); |
| 124 | + } |
| 125 | + return account.get(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * 给定用户的粉丝/关注数标准返回体。 |
| 130 | + * 登录用户再附带一条 `isFollowing` 方便前端一次拉到位。 |
| 131 | + */ |
| 132 | + private ApiResponse<Map<String, Object>> okStats(UserAccount target) { |
| 133 | + Map<String, Object> body = new HashMap<>(); |
| 134 | + body.put("userId", target.id()); |
| 135 | + body.put("followerCount", followService.countFollowers(target.id())); |
| 136 | + body.put("followingCount", followService.countFollowing(target.id())); |
| 137 | + try { |
| 138 | + if (StpUtil.isLogin()) { |
| 139 | + long me = StpUtil.getLoginIdAsLong(); |
| 140 | + body.put("isFollowing", |
| 141 | + me != target.id() && followService.isFollowing(me, target.id())); |
| 142 | + } else { |
| 143 | + body.put("isFollowing", false); |
| 144 | + } |
| 145 | + } catch (Exception ignored) { |
| 146 | + body.put("isFollowing", false); |
| 147 | + } |
| 148 | + return ApiResponse.ok(body); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * 批量把 user_accounts.id 展开成 UserView。保留输入顺序(关注时间倒序)。 |
| 153 | + */ |
| 154 | + private List<UserView> usersByIds(List<Long> ids) { |
| 155 | + return ids.stream() |
| 156 | + .map(userCenterService::getUser) // 不存在直接抛;这里我们默认 DB 一致 |
| 157 | + .toList(); |
| 158 | + } |
| 159 | +} |
0 commit comments