forked from solid-connection/solid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatService.java
More file actions
178 lines (149 loc) · 8.46 KB
/
ChatService.java
File metadata and controls
178 lines (149 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.example.solidconnection.chat.service;
import static com.example.solidconnection.common.exception.ErrorCode.CHAT_PARTICIPANT_NOT_FOUND;
import static com.example.solidconnection.common.exception.ErrorCode.CHAT_PARTNER_NOT_FOUND;
import static com.example.solidconnection.common.exception.ErrorCode.INVALID_CHAT_ROOM_STATE;
import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND;
import com.example.solidconnection.chat.domain.ChatMessage;
import com.example.solidconnection.chat.domain.ChatParticipant;
import com.example.solidconnection.chat.domain.ChatRoom;
import com.example.solidconnection.chat.dto.ChatAttachmentResponse;
import com.example.solidconnection.chat.dto.ChatMessageResponse;
import com.example.solidconnection.chat.dto.ChatMessageSendRequest;
import com.example.solidconnection.chat.dto.ChatMessageSendResponse;
import com.example.solidconnection.chat.dto.ChatParticipantResponse;
import com.example.solidconnection.chat.dto.ChatRoomListResponse;
import com.example.solidconnection.chat.dto.ChatRoomResponse;
import com.example.solidconnection.chat.repository.ChatMessageRepository;
import com.example.solidconnection.chat.repository.ChatParticipantRepository;
import com.example.solidconnection.chat.repository.ChatReadStatusRepository;
import com.example.solidconnection.chat.repository.ChatRoomRepository;
import com.example.solidconnection.common.dto.SliceResponse;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ChatService {
private final ChatRoomRepository chatRoomRepository;
private final ChatMessageRepository chatMessageRepository;
private final ChatParticipantRepository chatParticipantRepository;
private final ChatReadStatusRepository chatReadStatusRepository;
private final SiteUserRepository siteUserRepository;
private final SimpMessageSendingOperations simpMessageSendingOperations;
public ChatService(ChatRoomRepository chatRoomRepository,
ChatMessageRepository chatMessageRepository,
ChatParticipantRepository chatParticipantRepository,
ChatReadStatusRepository chatReadStatusRepository,
SiteUserRepository siteUserRepository,
@Lazy SimpMessageSendingOperations simpMessageSendingOperations) {
this.chatRoomRepository = chatRoomRepository;
this.chatMessageRepository = chatMessageRepository;
this.chatParticipantRepository = chatParticipantRepository;
this.chatReadStatusRepository = chatReadStatusRepository;
this.siteUserRepository = siteUserRepository;
this.simpMessageSendingOperations = simpMessageSendingOperations;
}
@Transactional(readOnly = true)
public ChatRoomListResponse getChatRooms(long siteUserId) {
// todo : n + 1 문제 해결 필요!
List<ChatRoom> chatRooms = chatRoomRepository.findOneOnOneChatRoomsByUserId(siteUserId);
List<ChatRoomResponse> chatRoomInfos = chatRooms.stream()
.map(chatRoom -> toChatRoomResponse(chatRoom, siteUserId))
.toList();
return ChatRoomListResponse.of(chatRoomInfos);
}
private ChatRoomResponse toChatRoomResponse(ChatRoom chatRoom, long siteUserId) {
Optional<ChatMessage> latestMessage = chatMessageRepository.findFirstByChatRoomIdOrderByCreatedAtDesc(chatRoom.getId());
String lastChatMessage = latestMessage.map(ChatMessage::getContent).orElse("");
ZonedDateTime lastReceivedTime = latestMessage.map(ChatMessage::getCreatedAt).orElse(null);
ChatParticipant partnerParticipant = findPartner(chatRoom, siteUserId);
SiteUser siteUser = siteUserRepository.findById(partnerParticipant.getSiteUserId())
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
ChatParticipantResponse partner = ChatParticipantResponse.of(siteUser.getId(), siteUser.getNickname(), siteUser.getProfileImageUrl());
long unReadCount = chatRoomRepository.countUnreadMessages(chatRoom.getId(), siteUserId);
return ChatRoomResponse.of(chatRoom.getId(), lastChatMessage, lastReceivedTime, partner, unReadCount);
}
private ChatParticipant findPartner(ChatRoom chatRoom, long siteUserId) {
if (chatRoom.isGroup()) {
throw new CustomException(INVALID_CHAT_ROOM_STATE);
}
return chatRoom.getChatParticipants().stream()
.filter(participant -> participant.getSiteUserId() != siteUserId)
.findFirst()
.orElseThrow(() -> new CustomException(CHAT_PARTNER_NOT_FOUND));
}
@Transactional(readOnly = true)
public SliceResponse<ChatMessageResponse> getChatMessages(long siteUserId, long roomId, Pageable pageable) {
validateChatRoomParticipant(siteUserId, roomId);
Slice<ChatMessage> chatMessages = chatMessageRepository.findByRoomIdWithPaging(roomId, pageable);
List<ChatMessageResponse> content = chatMessages.getContent().stream()
.map(this::toChatMessageResponse)
.toList();
return SliceResponse.of(content, chatMessages);
}
public void validateChatRoomParticipant(long siteUserId, long roomId) {
boolean isParticipant = chatParticipantRepository.existsByChatRoomIdAndSiteUserId(roomId, siteUserId);
if (!isParticipant) {
throw new CustomException(CHAT_PARTICIPANT_NOT_FOUND);
}
}
private ChatMessageResponse toChatMessageResponse(ChatMessage message) {
List<ChatAttachmentResponse> attachments = message.getChatAttachments().stream()
.map(attachment -> ChatAttachmentResponse.of(
attachment.getId(),
attachment.getIsImage(),
attachment.getUrl(),
attachment.getThumbnailUrl(),
attachment.getCreatedAt()
))
.toList();
return ChatMessageResponse.of(
message.getId(),
message.getContent(),
message.getSenderId(),
message.getCreatedAt(),
attachments
);
}
@Transactional
public void markChatMessagesAsRead(long siteUserId, long roomId) {
ChatParticipant participant = chatParticipantRepository
.findByChatRoomIdAndSiteUserId(roomId, siteUserId)
.orElseThrow(() -> new CustomException(CHAT_PARTICIPANT_NOT_FOUND));
chatReadStatusRepository.upsertReadStatus(roomId, participant.getId());
}
@Transactional
public void sendChatMessage(ChatMessageSendRequest chatMessageSendRequest, long siteUserId, long roomId) {
long senderId = chatParticipantRepository.findByChatRoomIdAndSiteUserId(roomId, siteUserId)
.orElseThrow(() -> new CustomException(CHAT_PARTICIPANT_NOT_FOUND))
.getId();
ChatMessage chatMessage = new ChatMessage(
chatMessageSendRequest.content(),
senderId,
chatRoomRepository.findById(roomId)
.orElseThrow(() -> new CustomException(INVALID_CHAT_ROOM_STATE))
);
chatMessageRepository.save(chatMessage);
ChatMessageSendResponse chatMessageResponse = ChatMessageSendResponse.from(chatMessage);
simpMessageSendingOperations.convertAndSend("/topic/chat/" + roomId, chatMessageResponse);
}
@Transactional
public void createMentoringChatRoom(Long mentoringId, Long mentorId, Long menteeId) {
if (chatRoomRepository.existsByMentoringId(mentoringId)) {
return;
}
ChatRoom chatRoom = new ChatRoom(mentoringId, false);
chatRoom = chatRoomRepository.save(chatRoom);
ChatParticipant mentorParticipant = new ChatParticipant(mentorId, chatRoom);
ChatParticipant menteeParticipant = new ChatParticipant(menteeId, chatRoom);
chatParticipantRepository.saveAll(List.of(mentorParticipant, menteeParticipant));
}
}