2026-04-21 22:07:29 +08:00
|
|
|
package com.xuqm.im.service;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.xuqm.common.exception.BusinessException;
|
2026-04-24 15:08:54 +08:00
|
|
|
import com.xuqm.im.cluster.ImClusterPublisher;
|
2026-04-27 23:41:58 +08:00
|
|
|
import com.xuqm.im.entity.ImGroupEntity;
|
2026-04-21 22:07:29 +08:00
|
|
|
import com.xuqm.im.entity.ImMessageEntity;
|
|
|
|
|
import com.xuqm.im.entity.WebhookConfigEntity;
|
2026-04-27 23:41:58 +08:00
|
|
|
import com.xuqm.im.model.ConversationView;
|
2026-04-21 22:07:29 +08:00
|
|
|
import com.xuqm.im.model.SendMessageRequest;
|
|
|
|
|
import com.xuqm.im.repository.WebhookConfigRepository;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.http.HttpClient;
|
|
|
|
|
import java.net.http.HttpRequest;
|
|
|
|
|
import java.net.http.HttpResponse;
|
|
|
|
|
import java.time.LocalDateTime;
|
2026-04-27 23:41:58 +08:00
|
|
|
import java.time.ZoneOffset;
|
2026-04-25 17:27:06 +08:00
|
|
|
import com.xuqm.im.repository.ImMessageRepository;
|
2026-04-21 22:07:29 +08:00
|
|
|
import java.util.List;
|
2026-04-27 23:41:58 +08:00
|
|
|
import java.util.Objects;
|
2026-04-21 22:07:29 +08:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class MessageService {
|
|
|
|
|
|
|
|
|
|
private final ImMessageRepository messageRepository;
|
|
|
|
|
private final WebhookConfigRepository webhookRepository;
|
|
|
|
|
private final KeywordFilterService keywordFilterService;
|
2026-04-24 15:08:54 +08:00
|
|
|
private final ImClusterPublisher clusterPublisher;
|
2026-04-27 23:41:58 +08:00
|
|
|
private final ImGroupService groupService;
|
|
|
|
|
private final BlacklistService blacklistService;
|
|
|
|
|
private final ConversationStateService conversationStateService;
|
2026-04-21 22:07:29 +08:00
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
@Value("${im.webhook-timeout-ms:3000}")
|
|
|
|
|
private int webhookTimeoutMs;
|
|
|
|
|
|
|
|
|
|
public MessageService(ImMessageRepository messageRepository,
|
|
|
|
|
WebhookConfigRepository webhookRepository,
|
|
|
|
|
KeywordFilterService keywordFilterService,
|
2026-04-24 15:08:54 +08:00
|
|
|
ImClusterPublisher clusterPublisher,
|
2026-04-27 23:41:58 +08:00
|
|
|
ImGroupService groupService,
|
|
|
|
|
BlacklistService blacklistService,
|
|
|
|
|
ConversationStateService conversationStateService,
|
2026-04-21 22:07:29 +08:00
|
|
|
ObjectMapper objectMapper) {
|
|
|
|
|
this.messageRepository = messageRepository;
|
|
|
|
|
this.webhookRepository = webhookRepository;
|
|
|
|
|
this.keywordFilterService = keywordFilterService;
|
2026-04-24 15:08:54 +08:00
|
|
|
this.clusterPublisher = clusterPublisher;
|
2026-04-27 23:41:58 +08:00
|
|
|
this.groupService = groupService;
|
|
|
|
|
this.blacklistService = blacklistService;
|
|
|
|
|
this.conversationStateService = conversationStateService;
|
2026-04-21 22:07:29 +08:00
|
|
|
this.objectMapper = objectMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImMessageEntity send(String appId, String fromUserId, SendMessageRequest req) {
|
|
|
|
|
String content = req.content();
|
|
|
|
|
if (req.msgType() == ImMessageEntity.MsgType.TEXT) {
|
|
|
|
|
content = keywordFilterService.filter(appId, content);
|
|
|
|
|
if (content == null) {
|
|
|
|
|
throw new BusinessException("消息包含违禁内容");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-27 23:41:58 +08:00
|
|
|
ImGroupEntity group = null;
|
|
|
|
|
if (req.chatType() == ImMessageEntity.ChatType.GROUP) {
|
|
|
|
|
group = groupService.get(req.toId());
|
|
|
|
|
if (!groupService.memberIds(group).contains(fromUserId)) {
|
|
|
|
|
throw new BusinessException(403, "不在群内");
|
|
|
|
|
}
|
|
|
|
|
if (groupService.isMemberMuted(req.toId(), fromUserId)) {
|
|
|
|
|
throw new BusinessException(403, "当前用户已被禁言");
|
|
|
|
|
}
|
|
|
|
|
} else if (blacklistService.isEitherBlocked(appId, fromUserId, req.toId())) {
|
|
|
|
|
throw new BusinessException(403, "已被拉黑,无法发送消息");
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
|
|
|
|
|
ImMessageEntity message = new ImMessageEntity();
|
|
|
|
|
message.setId(UUID.randomUUID().toString());
|
|
|
|
|
message.setAppId(appId);
|
|
|
|
|
message.setFromUserId(fromUserId);
|
|
|
|
|
message.setToId(req.toId());
|
|
|
|
|
message.setChatType(req.chatType());
|
|
|
|
|
message.setMsgType(req.msgType());
|
|
|
|
|
message.setContent(content);
|
|
|
|
|
message.setStatus(ImMessageEntity.MsgStatus.SENT);
|
|
|
|
|
message.setMentionedUserIds(req.mentionedUserIds());
|
|
|
|
|
message.setCreatedAt(LocalDateTime.now());
|
|
|
|
|
messageRepository.save(message);
|
|
|
|
|
|
|
|
|
|
String destination = req.chatType() == ImMessageEntity.ChatType.SINGLE
|
|
|
|
|
? "/user/" + req.toId() + "/queue/messages"
|
|
|
|
|
: "/topic/group/" + req.toId();
|
2026-04-24 15:08:54 +08:00
|
|
|
clusterPublisher.publish(destination, message);
|
2026-04-24 10:42:11 +08:00
|
|
|
if (req.chatType() == ImMessageEntity.ChatType.SINGLE && !fromUserId.equals(req.toId())) {
|
2026-04-24 15:08:54 +08:00
|
|
|
clusterPublisher.publish("/user/" + fromUserId + "/queue/messages", message);
|
2026-04-27 23:41:58 +08:00
|
|
|
conversationStateService.clearHiddenForUsers(appId, req.toId(), req.chatType().name(), List.of(fromUserId, req.toId()));
|
|
|
|
|
} else if (req.chatType() == ImMessageEntity.ChatType.GROUP) {
|
|
|
|
|
conversationStateService.clearHiddenForUsers(appId, req.toId(), req.chatType().name(), groupService.memberIds(group));
|
2026-04-24 10:42:11 +08:00
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
|
|
|
|
|
dispatchWebhooks(appId, message);
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImMessageEntity revoke(String appId, String messageId, String requestUserId) {
|
|
|
|
|
ImMessageEntity message = messageRepository.findById(messageId)
|
|
|
|
|
.orElseThrow(() -> new BusinessException(404, "消息不存在"));
|
|
|
|
|
if (!message.getAppId().equals(appId)) {
|
|
|
|
|
throw new BusinessException(403, "无权操作");
|
|
|
|
|
}
|
|
|
|
|
if (!message.getFromUserId().equals(requestUserId)) {
|
|
|
|
|
throw new BusinessException(403, "只能撤回自己发送的消息");
|
|
|
|
|
}
|
|
|
|
|
message.setStatus(ImMessageEntity.MsgStatus.REVOKED);
|
|
|
|
|
message.setMsgType(ImMessageEntity.MsgType.REVOKED);
|
2026-04-24 10:42:11 +08:00
|
|
|
ImMessageEntity saved = messageRepository.save(message);
|
2026-04-24 15:08:54 +08:00
|
|
|
|
2026-04-24 10:42:11 +08:00
|
|
|
if (saved.getChatType() == ImMessageEntity.ChatType.SINGLE) {
|
2026-04-24 15:08:54 +08:00
|
|
|
clusterPublisher.publish("/user/" + saved.getToId() + "/queue/messages", saved);
|
2026-04-24 10:42:11 +08:00
|
|
|
if (!saved.getFromUserId().equals(saved.getToId())) {
|
2026-04-24 15:08:54 +08:00
|
|
|
clusterPublisher.publish("/user/" + saved.getFromUserId() + "/queue/messages", saved);
|
2026-04-24 10:42:11 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-04-24 15:08:54 +08:00
|
|
|
clusterPublisher.publish("/topic/group/" + saved.getToId(), saved);
|
2026-04-24 10:42:11 +08:00
|
|
|
}
|
|
|
|
|
return saved;
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-24 10:42:11 +08:00
|
|
|
public Page<ImMessageEntity> history(String appId, String userId, String toId, int page, int size) {
|
|
|
|
|
return messageRepository.findSingleConversation(
|
|
|
|
|
appId, userId, toId, PageRequest.of(page, size));
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 23:41:58 +08:00
|
|
|
public Page<ImMessageEntity> groupHistory(String appId, String groupId, String userId, int page, int size) {
|
|
|
|
|
ImGroupEntity group = groupService.get(groupId);
|
|
|
|
|
if (!groupService.memberIds(group).contains(userId)) {
|
|
|
|
|
throw new BusinessException(403, "不在群内");
|
|
|
|
|
}
|
2026-04-27 11:57:46 +08:00
|
|
|
return messageRepository.findGroupHistory(appId, groupId, PageRequest.of(page, size));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 17:27:06 +08:00
|
|
|
public List<ImMessageRepository.ConversationSummary> conversations(String appId, String userId, int size) {
|
|
|
|
|
return messageRepository.findConversations(appId, userId, size);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 23:41:58 +08:00
|
|
|
public List<ConversationView> conversationViews(String appId, String userId, int size) {
|
|
|
|
|
int fetchSize = Math.max(size * 3, size);
|
|
|
|
|
return messageRepository.findConversations(appId, userId, fetchSize).stream()
|
|
|
|
|
.map(summary -> toConversationView(appId, userId, summary))
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
.limit(size)
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ConversationView toConversationView(
|
|
|
|
|
String appId,
|
|
|
|
|
String userId,
|
|
|
|
|
ImMessageRepository.ConversationSummary summary
|
|
|
|
|
) {
|
|
|
|
|
String targetId = summary.getTargetId();
|
|
|
|
|
String chatType = summary.getChatType();
|
|
|
|
|
var state = conversationStateService.find(appId, userId, targetId, chatType);
|
|
|
|
|
if (state != null && state.isHidden()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Page<ImMessageEntity> page = chatType.equals("GROUP")
|
|
|
|
|
? messageRepository.findGroupHistory(appId, targetId, PageRequest.of(0, 1))
|
|
|
|
|
: messageRepository.findSingleConversation(appId, userId, targetId, PageRequest.of(0, 1));
|
|
|
|
|
ImMessageEntity lastMessage = page.getContent().stream().findFirst().orElse(null);
|
|
|
|
|
LocalDateTime lastReadAt = state == null ? null : state.getLastReadAt();
|
|
|
|
|
long unreadCount = chatType.equals("GROUP")
|
|
|
|
|
? messageRepository.countUnreadGroupConversation(appId, userId, targetId, lastReadAt)
|
|
|
|
|
: messageRepository.countUnreadSingleConversation(appId, userId, targetId, lastReadAt);
|
|
|
|
|
return new ConversationView(
|
|
|
|
|
targetId,
|
|
|
|
|
chatType,
|
|
|
|
|
lastMessage != null ? lastMessage.getContent() : null,
|
|
|
|
|
lastMessage != null ? lastMessage.getMsgType().name() : null,
|
|
|
|
|
toEpochMillis(lastMessage != null ? lastMessage.getCreatedAt() : summary.getLastTime()),
|
|
|
|
|
(int) unreadCount,
|
|
|
|
|
state != null && state.isMuted(),
|
|
|
|
|
state != null && state.isPinned()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long toEpochMillis(LocalDateTime time) {
|
|
|
|
|
return time == null ? 0L : time.toInstant(ZoneOffset.UTC).toEpochMilli();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
@Async
|
|
|
|
|
protected void dispatchWebhooks(String appId, ImMessageEntity message) {
|
|
|
|
|
List<WebhookConfigEntity> webhooks = webhookRepository.findByAppIdAndEnabledTrue(appId);
|
|
|
|
|
if (webhooks.isEmpty()) return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String body = objectMapper.writeValueAsString(message);
|
|
|
|
|
HttpClient client = HttpClient.newHttpClient();
|
|
|
|
|
for (WebhookConfigEntity webhook : webhooks) {
|
|
|
|
|
try {
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
.uri(URI.create(webhook.getUrl()))
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(body))
|
|
|
|
|
.build();
|
|
|
|
|
client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|