Skip to content

Commit

Permalink
fix: mapping user email to nickname in MessageResponseDto
Browse files Browse the repository at this point in the history
  • Loading branch information
Troha7 committed May 11, 2024
1 parent 919edd8 commit af0680b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.chat.yourway.service.interfaces.ChatMessageService;
import com.chat.yourway.service.interfaces.ChatNotificationService;
import com.chat.yourway.service.interfaces.ContactEventService;
import com.chat.yourway.service.interfaces.ContactService;
import com.chat.yourway.service.interfaces.MessageService;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,13 +28,15 @@ public class ChatMessageServiceImpl implements ChatMessageService {
private final MessageService messageService;
private final ContactEventService contactEventService;
private final ChatNotificationService chatNotificationService;
private final ContactService contactService;

@Transactional
@Override
public MessageResponseDto sendToPublicTopic(Integer topicId, MessagePublicRequestDto message,
String email) {
log.trace("Started contact email: [{}] sendToTopic id: [{}]", email, topicId);
MessageResponseDto messageResponseDto = messageService.createPublic(topicId, message, email);
messageResponseDto.setSentFrom(contactService.findByEmail(email).getNickname());

sendToTopic(topicId, messageResponseDto);

Expand All @@ -49,6 +52,10 @@ public MessageResponseDto sendToPrivateTopic(Integer topicId, MessagePrivateRequ
log.trace("Started contact email: [{}] sendToPrivateTopic email: [{}]", email, sendTo);
MessageResponseDto messageResponseDto = messageService.createPrivate(message, email);

messageResponseDto.setSentFrom(contactService.findByEmail(email).getNickname());
messageResponseDto.setSendTo(
contactService.findByEmail(messageResponseDto.getSendTo()).getNickname());

sendToTopic(topicId, messageResponseDto);

log.trace("Contact [{}] sent message to [{}]", email, sendTo);
Expand All @@ -60,7 +67,10 @@ public List<MessageResponseDto> sendMessageHistoryByTopicId(Integer topicId,
PageRequestDto pageRequestDto, String email) {
log.trace("Started sendMessageHistoryByTopicId = [{}]", topicId);

List<MessageResponseDto> messages = messageService.findAllByTopicId(topicId, pageRequestDto);
List<MessageResponseDto> messages = messageService.findAllByTopicId(topicId, pageRequestDto)
.stream()
.peek(m -> m.setSentFrom(contactService.findByEmail(m.getSentFrom()).getNickname()))
.toList();
simpMessagingTemplate.convertAndSendToUser(email, toTopicDestination(topicId), messages);

log.trace("Message history was sent to topicId = [{}]", topicId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void sendToTopic_shouldReturnCorrectSendToPublicTopicAndReceivedMessageFromTopic
MessageResponseDto messageResponseDto = resultKeeper.get(3, SECONDS);
assertThat(messageResponseDto).isNotNull();
assertThat(messageResponseDto.getId()).isNotNull();
assertThat(messageResponseDto.getSentFrom()).isEqualTo("[email protected]");
assertThat(messageResponseDto.getSentFrom()).isEqualTo("Vasil");
assertThat(messageResponseDto.getSendTo()).isEqualTo("Topic id=" + topicId);
assertThat(messageResponseDto.getTimestamp()).isNotNull();
assertThat(messageResponseDto.getContent()).isEqualTo(messageRequestDto.getContent());
Expand Down Expand Up @@ -155,8 +155,8 @@ void sendToContact_shouldReturnCorrectReceivedPrivateMessageFromSelfToSelf() {
MessageResponseDto messageResponseDto = resultKeeper.get(3, SECONDS);
assertThat(messageResponseDto).isNotNull();
assertThat(messageResponseDto.getId()).isNotNull();
assertThat(messageResponseDto.getSentFrom()).isEqualTo("[email protected]");
assertThat(messageResponseDto.getSendTo()).isEqualTo("[email protected]");
assertThat(messageResponseDto.getSentFrom()).isEqualTo("Vasil");
assertThat(messageResponseDto.getSendTo()).isEqualTo("Vasil");
assertThat(messageResponseDto.getTimestamp()).isNotNull();
assertThat(messageResponseDto.getContent()).isEqualTo(messageRequestDto.getContent());
}
Expand Down Expand Up @@ -185,7 +185,7 @@ void getMessages_shouldReturnReceivedMessagesHistoryFromTopic() {
assertThat(messageResponseDtos).extracting("id").isNotNull();
assertThat(messageResponseDtos).extracting("timestamp").isNotNull();
assertThat(messageResponseDtos).extracting("sentFrom")
.contains("[email protected]", "[email protected]");
.contains("Anton", "Vasil");
assertThat(messageResponseDtos).extracting("sendTo")
.contains("[email protected]", "[email protected]");
assertThat(messageResponseDtos).extracting("content")
Expand Down

0 comments on commit af0680b

Please sign in to comment.