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

Fix #51 유니크 추가 및 post send 구현 #51

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -2,10 +2,7 @@

import com.gachtaxi.domain.members.entity.Members;
import com.gachtaxi.global.common.entity.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -18,6 +15,12 @@
@Entity
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(
name = "chatting_participant",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"members_id", "chatting_room_id"})
}
)
Comment on lines +18 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

멤버와 채팅방을 유니크로 묶었군요!!
데이터 무결성이 더 잘 지켜질 것 같습니당!!👍

public class ChattingParticipant extends BaseEntity {

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public void delete(long chattingRoomId) {
}

@Transactional
public void subscribeChatRoom(long roomId, SimpMessageHeaderAccessor accessor) {
public void postSubscribeChatroom(SimpMessageHeaderAccessor accessor) {
Long roomId = (Long) accessor.getSessionAttributes().get(CHAT_ROOM_ID);
Long senderId = (Long) accessor.getSessionAttributes().get(CHAT_USER_ID);

ChattingRoom chattingRoom = find(roomId);
Members members = memberService.findById(senderId);

accessor.getSessionAttributes().put(CHAT_ROOM_ID, roomId);
accessor.getSessionAttributes().put(CHAT_USER_NAME, members.getNickname());

if (chattingParticipantService.checkSubscription(chattingRoom, members)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {

return chatStrategyHandler.handle(message, accessor, channel);
}

@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);

chatStrategyHandler.handle(message, accessor, channel, sent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ public Message<?> handle(Message<?> message, StompHeaderAccessor accessor, Messa
.orElse(defaultCommandStrategy)
.preSend(message, accessor, channel);
}

public void handle(Message<?> message, StompHeaderAccessor accessor, MessageChannel channel, boolean sent) {
StompCommand command = accessor.getCommand();

stompCommandStrategies.stream()
.filter(strategy -> strategy.supports(command))
.findFirst()
.orElse(defaultCommandStrategy)
.postSend(message, accessor, channel, sent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public interface StompCommandStrategy {
boolean supports(StompCommand command);

Message<?> preSend(Message<?> message, StompHeaderAccessor accessor, MessageChannel channel);

default void postSend(Message<?> message, StompHeaderAccessor accessor, MessageChannel channel, boolean sent) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Message<?> preSend(Message<?> message, StompHeaderAccessor accessor, Mess

if (destination.startsWith(SUB_END_POINT)) {
Long roomId = Long.valueOf(destination.replace(SUB_END_POINT, ""));
chattingRoomService.subscribeChatRoom(roomId, accessor);
accessor.getSessionAttributes().put(CHAT_ROOM_ID, roomId);

return message;
}
Expand All @@ -45,5 +45,12 @@ public Message<?> preSend(Message<?> message, StompHeaderAccessor accessor, Mess

throw new ChatSubscribeException();
}

@Override
public void postSend(Message<?> message, StompHeaderAccessor accessor, MessageChannel channel, boolean sent) {
if (sent) {
chattingRoomService.postSubscribeChatroom(accessor);
}
}
}