Skip to content
Merged
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 @@ -170,7 +170,7 @@ private Long getUserIdFromQuery(WebSocketSession session) {
}
}

private Long getUserIdFromSession(WebSocketSession session) {
public Long getUserIdFromSession(WebSocketSession session) {
Long userId = (Long) session.getAttributes().get("userId");
if (userId == null) {
throw new IllegalArgumentException("UserId not found in session");
Expand All @@ -182,6 +182,10 @@ private Long getUserIdFromSession(WebSocketSession session) {
private MessageDTO createEventMessage(String type, Long userId) {
return new MessageDTO(type, userId);
}

public List<WebSocketSession> getSessionsByRoomId(Long roomId) {
return webSocketBroadcaster.getSessions(roomId);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -41,5 +42,8 @@ public void broadcast(Long roomId, String message) {
}
}
}
public List<WebSocketSession> getSessions(Long roomId) {
return roomSessions.getOrDefault(roomId, Collections.emptyList());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.mogakserver.common.exception.model.NotFoundException;
import com.example.mogakserver.common.exception.model.UnAuthorizedException;
import com.example.mogakserver.external.redis.RedisService;
import com.example.mogakserver.external.socket.WebRtcWebSocketHandler;
import com.example.mogakserver.external.socket.WebSocketBroadCaster;
import com.example.mogakserver.external.socket.service.ScreenShareService;
import com.example.mogakserver.external.socket.service.TimerService;
Expand All @@ -30,8 +31,11 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -55,6 +59,8 @@ public class RoomUserService {
private final TimerService timerService;
private final RedisService redisService;
private final WebSocketBroadCaster webSocketBroadcaster;
private final WebSocketHandler webSocketHandler;
private final WebRtcWebSocketHandler webRtcWebSocketHandler;

public void updateIsScreenShareLargeAllowed(Long userId, Long roomId){
RoomUser roomUser = roomUserRepository.findByUserIdAndRoomId(userId, roomId).orElseThrow(()->new NotFoundException(ErrorCode.NOT_FOUND_ROOM_EXCEPTION));
Expand Down Expand Up @@ -216,21 +222,34 @@ private Map<Long, List<WorkHour>> getWorkHourMap(List<Long> hostRoomIds) {

@Transactional(readOnly = true)
public RoomUserListDTO getRoomUsers(Long userId, Long roomId) {
boolean isParticipant = roomUserRepository.findByUserIdAndRoomId(userId, roomId).isPresent();
List<WebSocketSession> roomSessions = webRtcWebSocketHandler.getSessionsByRoomId(roomId);
List<RoomUserDTO> users = new ArrayList<>();
boolean isEmptyRoom = false;

if(roomSessions==null || roomSessions.isEmpty()){
isEmptyRoom = true;
}
// 본인이 해당 방 참여자인지 검증
boolean isParticipant = roomSessions.stream()
.anyMatch(session -> userId.equals(webRtcWebSocketHandler.getUserIdFromSession(session)));

if (!isParticipant) {
throw new NotFoundException(ErrorCode.ROOM_PERMISSION_DENIED);
}

List<RoomUserDTO> users = roomUserRepository.findByRoomId(roomId).stream()
.map(roomUser -> {
User user = userRepository.findById(roomUser.getUserId())
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));
if(!isEmptyRoom){
users = roomSessions.stream()
.map(session -> {
Long connectedUserId = webRtcWebSocketHandler.getUserIdFromSession(session);
User user = userRepository.findById(connectedUserId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));
return RoomUserDTO.builder()
.userId(user.getId())
.nickName(user.getNickName())
.build();
.userId(user.getId())
.nickName(user.getNickName())
.build();
})
.collect(Collectors.toList());
.toList();
}

return RoomUserListDTO.builder()
.users(users)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.example.mogakserver.user.application.response.RankingDTO;
import com.example.mogakserver.user.application.response.RankingListDTO;
import com.example.mogakserver.user.application.service.UserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.example.mogakserver.user.domain.entity.User;
import com.example.mogakserver.user.infra.repository.JpaUserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -14,6 +16,7 @@

@Service
@RequiredArgsConstructor
@Slf4j
public class UserRankingScheduler {

private final JpaUserRepository userRepository;
Expand Down Expand Up @@ -66,6 +69,9 @@ private void initializeRanking() {

@Scheduled(cron = "0 0 5 * * ?") // 매일 새벽 5시에 랭킹 초기화
public void cleanupOldRanking() {

log.info("랭킹정리!!");
redisTemplate.delete(RANKING_KEY);
log.info("랭키정리 성공!!");
}
}