Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat 232 mapping user nickname #81

Merged
merged 5 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,15 @@ 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());
if (isPrivate(m)) {
m.setSendTo(contactService.findByEmail(m.getSendTo()).getNickname());
}
})
.toList();
simpMessagingTemplate.convertAndSendToUser(email, toTopicDestination(topicId), messages);

log.trace("Message history was sent to topicId = [{}]", topicId);
Expand All @@ -85,4 +100,8 @@ private String toTopicDestination(Integer topicId) {
return properties.getTopicPrefix() + "/" + topicId;
}

private boolean isPrivate(MessageResponseDto message) {
return message.getSendTo().contains("@");
}

}
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