Skip to content

Commit

Permalink
Merge pull request #88 from kookmin-sw/refactor/chatapi
Browse files Browse the repository at this point in the history
[Refactor] Chat api 불필요 코드 삭제 및 경고 fix
  • Loading branch information
imjanghyeok authored May 21, 2024
2 parents 62749dc + 42c791a commit 1d137c8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 60 deletions.
25 changes: 14 additions & 11 deletions src/main/java/capstone/facefriend/chat/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
Expand All @@ -27,7 +27,6 @@ public class RedisConfig {
@Value("${spring.data.redis.port}")
private int redisPort;


@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration =
Expand All @@ -43,7 +42,6 @@ public RedisMessageListenerContainer redisMessageListenerContainer(
) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// RedisMessageListenerContainer 에 Bean 으로 등록한 listenerAdapter, channelTopic 추가
container.addMessageListener(listenerAdapter, channelTopic);
return container;
}
Expand All @@ -53,12 +51,8 @@ public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisC
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

// Jackson2JsonRedisSerializer를 사용하여 RedisTemplate 설정
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()); // Java 8 시간 모듈 등록
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 날짜를 타임스탬프로 변환하지 않도록 설정
serializer.setObjectMapper(objectMapper);
// GenericJackson2JsonRedisSerializer를 사용하여 RedisTemplate 설정
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(objectMapper());

// 문자열 직렬화 설정
redisTemplate.setKeySerializer(new StringRedisSerializer());
Expand All @@ -81,6 +75,15 @@ public MessageListenerAdapter listenerAdapter(RedisSubscriber subscriber) {
}

@Bean
public ChannelTopic channelTopic() {return new ChannelTopic("chatroom");}
public ChannelTopic channelTopic() {
return new ChannelTopic("chatroom");
}

}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return objectMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ ResponseEntity<Map<String, Object>> getChatRoomList(
return ResponseEntity.ok(chatRoomService.getChatRoomList(memberId));
}

@PostMapping("/room/{roomId}/enter")
public ResponseEntity<ChatRoomEnterResponse> enterChatRoom(
@PathVariable("roomId") Long roomId,
@AuthMember Long memberId,
@RequestParam(required = false, defaultValue = "0", value = "page") int pageNo
){
return ResponseEntity.ok(chatRoomService.enterRoom(roomId, memberId));
}

@PostMapping("/room/{roomId}/exit")
public ResponseEntity<ChatRoomExitResponse> exitChatRoom(
@PathVariable("roomId") Long roomId,
@AuthMember Long memberId
){
return ResponseEntity.ok(chatRoomService.exitRoom(roomId, memberId));
}

@PostMapping("/room/{roomId}/left")
public ResponseEntity<String> leftChatRoom(
@PathVariable("roomId") Long roomId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,6 @@ public Map<String, Object> getChatRoomList(Long memberId) {
return chatRooms;
}

public ChatRoomEnterResponse enterRoom(Long roomId, Long memberId) {
String chatRoomInfoId = roomId + "/member/" + memberId;
ChatRoomInfo chatRoomInfo = new ChatRoomInfo();
chatRoomInfo.setChatRoomInfoId(chatRoomInfoId);
chatRoomInfo.setEnterTime(LocalDateTime.now());
chatRoomInfoRedisRepository.save(chatRoomInfo);
return ChatRoomEnterResponse.of(roomId, memberId, chatRoomInfo);
}

public ChatRoomExitResponse exitRoom(Long roomId, Long memberId) {
String chatRoomInfoId = roomId + "/member/" + memberId;
ChatRoomInfo chatRoomInfo = findChatRoomInfo(chatRoomInfoId);
chatRoomInfoRedisRepository.delete(chatRoomInfo);
LocalDateTime exitChatRoomTime = LocalDateTime.now();
return ChatRoomExitResponse.of(roomId, memberId, exitChatRoomTime);
}

@Transactional
public String leftRoom(Long roomId, Long memberId) {
ChatRoom chatRoom = findRoomById(roomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,13 @@ private Boolean isExistSubscriber(Long memberId) {


private void saveUnReadMessage(String destination, MessageResponse messageResponse) {
Boolean isUnRead = redisTemplate.hasKey(destination);
log.info(isUnRead.toString());
if (isUnRead) {
messageResponse.setMethod("connectChat");
redisTemplate.opsForList().rightPush(destination, messageResponse);
} else {
messageResponse.setMethod("connectChat");
redisTemplate.opsForList().rightPush(destination, messageResponse);
}
messageResponse.setMethod("connectChat");
redisTemplate.opsForList().rightPush(destination, messageResponse);

}

private void saveUnReadHeart(String destination, SendHeartResponse sendHeartResponse) {
Boolean isUnRead = redisTemplate.hasKey(destination);
if (isUnRead) {
sendHeartResponse.setMethod("connectHeart");
redisTemplate.opsForList().rightPush(destination, sendHeartResponse);
} else {
sendHeartResponse.setMethod("connectHeart");
redisTemplate.opsForList().rightPush(destination, sendHeartResponse);
}
}
}

0 comments on commit 1d137c8

Please sign in to comment.