2026-04-24 16:16:33 +08:00
|
|
|
package com.xuqm.im.controller;
|
|
|
|
|
|
|
|
|
|
import com.xuqm.common.model.ApiResponse;
|
|
|
|
|
import com.xuqm.im.entity.ImAccountEntity;
|
|
|
|
|
import com.xuqm.im.entity.ImGroupEntity;
|
|
|
|
|
import com.xuqm.im.repository.ImAccountRepository;
|
|
|
|
|
import com.xuqm.im.repository.ImGroupRepository;
|
|
|
|
|
import com.xuqm.im.repository.ImMessageRepository;
|
2026-04-24 20:53:48 +08:00
|
|
|
import com.xuqm.im.service.ImAccountService;
|
|
|
|
|
import com.xuqm.im.service.ImGroupService;
|
2026-04-28 09:45:20 +08:00
|
|
|
import com.xuqm.im.service.MessageService;
|
2026-04-24 16:16:33 +08:00
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
2026-04-28 09:45:20 +08:00
|
|
|
import java.time.LocalDateTime;
|
2026-04-24 16:16:33 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/im/admin")
|
|
|
|
|
public class ImAdminController {
|
|
|
|
|
|
|
|
|
|
private final ImAccountRepository accountRepository;
|
|
|
|
|
private final ImGroupRepository groupRepository;
|
|
|
|
|
private final ImMessageRepository messageRepository;
|
2026-04-24 20:53:48 +08:00
|
|
|
private final ImAccountService accountService;
|
|
|
|
|
private final ImGroupService groupService;
|
2026-04-28 09:45:20 +08:00
|
|
|
private final MessageService messageService;
|
2026-04-24 16:16:33 +08:00
|
|
|
|
|
|
|
|
public ImAdminController(ImAccountRepository accountRepository,
|
|
|
|
|
ImGroupRepository groupRepository,
|
2026-04-24 20:53:48 +08:00
|
|
|
ImMessageRepository messageRepository,
|
|
|
|
|
ImAccountService accountService,
|
2026-04-28 09:45:20 +08:00
|
|
|
ImGroupService groupService,
|
|
|
|
|
MessageService messageService) {
|
2026-04-24 16:16:33 +08:00
|
|
|
this.accountRepository = accountRepository;
|
|
|
|
|
this.groupRepository = groupRepository;
|
|
|
|
|
this.messageRepository = messageRepository;
|
2026-04-24 20:53:48 +08:00
|
|
|
this.accountService = accountService;
|
|
|
|
|
this.groupService = groupService;
|
2026-04-28 09:45:20 +08:00
|
|
|
this.messageService = messageService;
|
2026-04-24 16:16:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** List all registered IM users for the given appId. */
|
|
|
|
|
@GetMapping("/users")
|
|
|
|
|
public ResponseEntity<ApiResponse<Page<ImAccountEntity>>> listUsers(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestParam(defaultValue = "0") int page,
|
|
|
|
|
@RequestParam(defaultValue = "20") int size) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(
|
|
|
|
|
accountRepository.findByAppId(appId, PageRequest.of(page, size))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Ban or unban a user. */
|
|
|
|
|
@PutMapping("/users/{userId}/status")
|
|
|
|
|
public ResponseEntity<ApiResponse<ImAccountEntity>> updateUserStatus(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@PathVariable String userId,
|
|
|
|
|
@RequestBody Map<String, String> body) {
|
|
|
|
|
ImAccountEntity account = accountRepository.findByAppIdAndUserId(appId, userId)
|
|
|
|
|
.orElseThrow(() -> new RuntimeException("User not found"));
|
|
|
|
|
account.setStatus(ImAccountEntity.Status.valueOf(body.get("status").toUpperCase()));
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(accountRepository.save(account)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** List all groups for the given appId. */
|
|
|
|
|
@GetMapping("/groups")
|
|
|
|
|
public ResponseEntity<ApiResponse<List<ImGroupEntity>>> listGroups(@RequestParam String appId) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(groupRepository.findByAppId(appId)));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 20:53:48 +08:00
|
|
|
/** Admin registers a new IM user (or returns existing). */
|
|
|
|
|
@PostMapping("/users")
|
|
|
|
|
public ResponseEntity<ApiResponse<ImAccountEntity>> registerUser(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestBody RegisterUserRequest req) {
|
|
|
|
|
accountService.loginOrRegister(appId, req.userId(), req.nickname(), req.avatar());
|
|
|
|
|
ImAccountEntity account = accountRepository.findByAppIdAndUserId(appId, req.userId())
|
|
|
|
|
.orElseThrow();
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(account));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Admin creates a group. */
|
|
|
|
|
@PostMapping("/groups")
|
|
|
|
|
public ResponseEntity<ApiResponse<ImGroupEntity>> createGroup(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestBody CreateGroupRequest req) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(
|
2026-04-28 09:45:20 +08:00
|
|
|
groupService.create(appId, req.name(), req.creatorId(), req.memberIds(), "WORK")));
|
2026-04-24 20:53:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 16:41:10 +08:00
|
|
|
/** Fuzzy search users by userId or nickname. */
|
|
|
|
|
@GetMapping("/users/search")
|
|
|
|
|
public ResponseEntity<ApiResponse<List<ImAccountEntity>>> searchUsers(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestParam String keyword,
|
|
|
|
|
@RequestParam(defaultValue = "20") int size) {
|
|
|
|
|
List<ImAccountEntity> results = accountRepository.searchByKeyword(appId, keyword, PageRequest.of(0, size));
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(results));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 16:16:33 +08:00
|
|
|
/** Message statistics for the given appId. */
|
|
|
|
|
@GetMapping("/stats")
|
|
|
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> stats(@RequestParam String appId) {
|
|
|
|
|
long totalMessages = messageRepository.countByAppId(appId);
|
|
|
|
|
long totalUsers = accountRepository.countByAppId(appId);
|
|
|
|
|
long totalGroups = groupRepository.countByAppId(appId);
|
|
|
|
|
long todayMessages = messageRepository.countTodayByAppId(appId);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(Map.of(
|
|
|
|
|
"totalMessages", totalMessages,
|
|
|
|
|
"totalUsers", totalUsers,
|
|
|
|
|
"totalGroups", totalGroups,
|
|
|
|
|
"todayMessages", todayMessages
|
|
|
|
|
)));
|
|
|
|
|
}
|
2026-04-24 20:53:48 +08:00
|
|
|
|
2026-04-28 09:45:20 +08:00
|
|
|
/** Admin queries conversation history between any two users. */
|
|
|
|
|
@GetMapping("/messages")
|
|
|
|
|
public ResponseEntity<ApiResponse<Page<com.xuqm.im.entity.ImMessageEntity>>> history(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestParam String userA,
|
|
|
|
|
@RequestParam String userB,
|
|
|
|
|
@RequestParam(required = false) com.xuqm.im.entity.ImMessageEntity.MsgType msgType,
|
|
|
|
|
@RequestParam(required = false) String keyword,
|
|
|
|
|
@RequestParam(required = false) LocalDateTime startTime,
|
|
|
|
|
@RequestParam(required = false) LocalDateTime endTime,
|
|
|
|
|
@RequestParam(defaultValue = "0") int page,
|
|
|
|
|
@RequestParam(defaultValue = "20") int size) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(
|
|
|
|
|
messageRepository.findSingleConversationFiltered(
|
|
|
|
|
appId, userA, userB, msgType, keyword, startTime, endTime, PageRequest.of(page, size))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Admin revokes an arbitrary message. */
|
|
|
|
|
@PostMapping("/messages/{messageId}/revoke")
|
|
|
|
|
public ResponseEntity<ApiResponse<com.xuqm.im.entity.ImMessageEntity>> adminRevoke(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@PathVariable String messageId) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(messageService.adminRevoke(appId, messageId)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Admin force dismisses a group. */
|
|
|
|
|
@DeleteMapping("/groups/{groupId}")
|
|
|
|
|
public ResponseEntity<ApiResponse<Void>> adminDismissGroup(@PathVariable String groupId) {
|
|
|
|
|
groupService.adminDismiss(groupId);
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.ok());
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 20:53:48 +08:00
|
|
|
public record RegisterUserRequest(String userId, String nickname, String avatar) {}
|
|
|
|
|
public record CreateGroupRequest(String name, String creatorId, List<String> memberIds) {}
|
2026-04-24 16:16:33 +08:00
|
|
|
}
|