2026-04-25 17:27:06 +08:00
|
|
|
package com.xuqm.im.controller;
|
|
|
|
|
|
|
|
|
|
import com.xuqm.common.model.ApiResponse;
|
|
|
|
|
import com.xuqm.im.entity.ImFriendEntity;
|
|
|
|
|
import com.xuqm.im.repository.ImFriendRepository;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
2026-04-28 21:05:06 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
2026-04-25 17:27:06 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
2026-04-28 21:05:06 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.LinkedHashSet;
|
2026-04-25 17:27:06 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/im/friends")
|
|
|
|
|
public class FriendController {
|
|
|
|
|
|
|
|
|
|
private final ImFriendRepository friendRepository;
|
|
|
|
|
|
|
|
|
|
public FriendController(ImFriendRepository friendRepository) {
|
|
|
|
|
this.friendRepository = friendRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping
|
|
|
|
|
public ResponseEntity<ApiResponse<List<String>>> listFriends(
|
|
|
|
|
@AuthenticationPrincipal String userId,
|
|
|
|
|
@RequestParam String appId) {
|
|
|
|
|
List<String> friendIds = friendRepository.findByAppIdAndUserId(appId, userId)
|
|
|
|
|
.stream()
|
|
|
|
|
.map(ImFriendEntity::getFriendId)
|
|
|
|
|
.toList();
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(friendIds));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping
|
|
|
|
|
public ResponseEntity<ApiResponse<ImFriendEntity>> addFriend(
|
|
|
|
|
@AuthenticationPrincipal String userId,
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestParam String friendId) {
|
2026-04-28 21:05:06 +08:00
|
|
|
return ResponseEntity.ok(ApiResponse.success(addFriendLink(appId, userId, friendId)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/batch")
|
|
|
|
|
public ResponseEntity<ApiResponse<List<ImFriendEntity>>> addFriends(
|
|
|
|
|
@AuthenticationPrincipal String userId,
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestBody FriendBatchRequest req) {
|
|
|
|
|
List<ImFriendEntity> links = new ArrayList<>();
|
|
|
|
|
for (String friendId : unique(req.friendIds())) {
|
|
|
|
|
if (friendId == null || friendId.isBlank() || userId.equals(friendId)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
links.add(addFriendLink(appId, userId, friendId));
|
|
|
|
|
}
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(links));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/{friendId}")
|
|
|
|
|
public ResponseEntity<ApiResponse<Void>> removeFriend(
|
|
|
|
|
@AuthenticationPrincipal String userId,
|
|
|
|
|
@PathVariable String friendId,
|
|
|
|
|
@RequestParam String appId) {
|
|
|
|
|
friendRepository.deleteByAppIdAndUserIdAndFriendId(appId, userId, friendId);
|
|
|
|
|
friendRepository.deleteByAppIdAndUserIdAndFriendId(appId, friendId, userId);
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/batch/remove")
|
|
|
|
|
public ResponseEntity<ApiResponse<Void>> removeFriends(
|
|
|
|
|
@AuthenticationPrincipal String userId,
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestBody FriendBatchRequest req) {
|
|
|
|
|
for (String friendId : unique(req.friendIds())) {
|
|
|
|
|
if (friendId == null || friendId.isBlank() || userId.equals(friendId)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
friendRepository.deleteByAppIdAndUserIdAndFriendId(appId, userId, friendId);
|
|
|
|
|
friendRepository.deleteByAppIdAndUserIdAndFriendId(appId, friendId, userId);
|
|
|
|
|
}
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ImFriendEntity addFriendLink(String appId, String userId, String friendId) {
|
2026-04-25 17:27:06 +08:00
|
|
|
ImFriendEntity forward = friendRepository
|
|
|
|
|
.findByAppIdAndUserIdAndFriendId(appId, userId, friendId)
|
|
|
|
|
.orElseGet(() -> {
|
|
|
|
|
ImFriendEntity e = new ImFriendEntity();
|
|
|
|
|
e.setAppId(appId);
|
|
|
|
|
e.setUserId(userId);
|
|
|
|
|
e.setFriendId(friendId);
|
|
|
|
|
return friendRepository.save(e);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
friendRepository.findByAppIdAndUserIdAndFriendId(appId, friendId, userId)
|
|
|
|
|
.orElseGet(() -> {
|
|
|
|
|
ImFriendEntity e = new ImFriendEntity();
|
|
|
|
|
e.setAppId(appId);
|
|
|
|
|
e.setUserId(friendId);
|
|
|
|
|
e.setFriendId(userId);
|
|
|
|
|
return friendRepository.save(e);
|
|
|
|
|
});
|
2026-04-28 21:05:06 +08:00
|
|
|
return forward;
|
2026-04-25 17:27:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 21:05:06 +08:00
|
|
|
private List<String> unique(List<String> friendIds) {
|
|
|
|
|
return friendIds == null ? List.of() : new ArrayList<>(new LinkedHashSet<>(friendIds));
|
2026-04-25 17:27:06 +08:00
|
|
|
}
|
2026-04-28 21:05:06 +08:00
|
|
|
|
|
|
|
|
public record FriendBatchRequest(List<String> friendIds) {}
|
2026-04-25 17:27:06 +08:00
|
|
|
}
|