Skip to content

Commit

Permalink
Merge pull request #116 from KUIT-Space/refactoring/#100/voice-room-d…
Browse files Browse the repository at this point in the history
…ata-save

[Refactoring] voice room 데이터 관련 리팩토링
  • Loading branch information
drbug2000 authored Aug 18, 2024
2 parents 79a4780 + 582c3f7 commit 5704c22
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.*;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceId;
import space.space_spring.dao.UserSpaceDao;
import space.space_spring.dao.VoiceRoomRepository;
import space.space_spring.dto.VoiceRoom.*;
Expand Down Expand Up @@ -84,6 +85,7 @@ public BaseResponse<String> getToken(
@PathVariable("spaceId") @NotNull long spaceId,
@JwtLoginAuth Long userId,
@PathVariable("voiceRoomId") @NotNull Long roomId,
@UserSpaceId Long userSpaceId,
HttpServletResponse response
){

Expand All @@ -92,7 +94,7 @@ public BaseResponse<String> getToken(
//해당 voiceRoom이 해당 space에 속한것이 맞는지 확인
validateVoiceRoomInSpace(spaceId,roomId);

response.setHeader("Authorization", "Bearer " + voiceRoomService.getToken(spaceId, userId,roomId));
response.setHeader("Authorization", "Bearer " + voiceRoomService.getToken(spaceId, userId,userSpaceId,roomId));
return new BaseResponse<String>(
"보이스룸 토큰 생성에 성공했습니다."
);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/space/space_spring/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@ public User getUserByEmail(String email, UserSignupType signupType) {
public User findUserByUserId(Long userId) {
return em.find(User.class, userId);
}
public String findProfileImageByUserId(Long userId){
return "Test ProfileImage URL";//findUserByUserId(userId).getProfileImage();
}

}
17 changes: 17 additions & 0 deletions src/main/java/space/space_spring/dao/UserSpaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import space.space_spring.entity.enumStatus.UserSpaceAuth;

import java.util.*;
import java.util.stream.Stream;

import static space.space_spring.entity.enumStatus.UserSpaceAuth.MANAGER;

Expand Down Expand Up @@ -134,4 +135,20 @@ public String findUserSpaceAuthById(Long userSpaceId) {
.getSingleResult();

}

public Optional<String> findProfileImageById(Long userSpaceId){
String jpql = "SELECT us.userProfileImg FROM UserSpace us WHERE us.userSpaceId = :userSpaceId AND us.status = 'ACTIVE'";
List<String> result = em.createQuery(jpql, String.class)
.setParameter("userSpaceId",userSpaceId)
.getResultList();
return result.isEmpty() ? Optional.empty() : Optional.ofNullable(result.get(0));
}

public String findUserNameById(Long userSpaceId){
String jpql = "SELECT us.userName FROM UserSpace us WHERE us.userSpaceId = :userSpaceId AND us.status = 'ACTIVE'";
return em.createQuery(jpql, String.class)
.setParameter("userSpaceId", userSpaceId)
.getSingleResult();

}
}
1 change: 1 addition & 0 deletions src/main/java/space/space_spring/dao/VoiceRoomDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Long createVoiceRoom(String name,int order,Space space){
}
}


}


Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static class VoiceRoomInfo{
private int numParticipant;
private int order;
private List<GetParticipantList.ParticipantInfo> participantInfoList;
private String metadate;

public static VoiceRoomInfo convertRoomDto(RoomDto roomDto){
if(roomDto==null){return null;}
Expand All @@ -45,6 +46,7 @@ public static VoiceRoomInfo convertRoomDto(RoomDto roomDto){
.Active(roomDto.getNumParticipants()!=0)
.numParticipant(roomDto.getNumParticipants())
.order(roomDto.getOrder())
.metadate(roomDto.getMetadata())
.participantInfoList(
GetParticipantList.ParticipantInfo.convertParticipantDtoList(roomDto.getParticipantDTOList())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void setActiveRoom(List<LivekitModels.Room> liveKitRoomList){
if(liveKitRoomList==null||liveKitRoomList.isEmpty()){return;}
boolean find = false;
for(LivekitModels.Room resRoom : liveKitRoomList){
if(EqualRoomIdByNameTag(resRoom.getName(),this.id)){
if(String.valueOf(this.id).equals( resRoom.getName() )){
this.numParticipants = resRoom.getNumParticipants();
this.sid = resRoom.getSid();
this.metadata = resRoom.getMetadata();
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/space/space_spring/service/VoiceRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import space.space_spring.entity.Space;
import space.space_spring.entity.User;
import space.space_spring.entity.VoiceRoom;
import space.space_spring.exception.CustomException;
import space.space_spring.util.LiveKitUtils;
import space.space_spring.util.space.SpaceUtils;
import space.space_spring.util.user.UserUtils;
Expand All @@ -20,6 +21,8 @@
import java.util.List;
import java.util.Optional;

import static space.space_spring.response.status.BaseExceptionResponseStatus.USER_IS_NOT_IN_SPACE;

@Service
@RequiredArgsConstructor
public class VoiceRoomService {
Expand Down Expand Up @@ -75,7 +78,7 @@ public List<GetVoiceRoomList.VoiceRoomInfo> getVoiceRoomInfoList(long spaceId,Ge
List<ParticipantDto> participantDtoList = getParticipantDtoListById(roomDto.getId());
for(ParticipantDto participantDto: participantDtoList){
//Todo profileIamge 집어넣기
participantDto.setProfileImage(findProfileImageByUserId(participantDto.getId()));
participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId()));
}
//RoomDto에 값 집어넣기
//showParticipant = ture 일때, 참가자가 없으면 빈문자열[] 출력
Expand Down Expand Up @@ -121,8 +124,8 @@ public void deleteVoiceRoom(long voiceRoomId){
//Todo Base Entity에 일괄적으로 soft Delete를 적용하는 방법을 다같이 정하는 것이 좋아보임
}

private String findProfileImageByUserId(Long userId){
return userDao.findProfileImageByUserId(userId);
private String findProfileImageByUserId(Long userSpaceId){
return userSpaceDao.findProfileImageById(userSpaceId).orElse("");
}
public List<VoiceRoom> findBySpaceId(long spaceId){
return findBySpace(spaceUtils.findSpaceBySpaceId(spaceId));
Expand All @@ -132,13 +135,13 @@ private List<VoiceRoom> findBySpace(Space space){
}
private List<ParticipantDto> getParticipantDtoListById(long voiceRoomId){
Space space = voiceRoomRepository.findById(voiceRoomId).getSpace();
List<ParticipantDto> participantDtoList = liveKitUtils.getParticipantInfo(findNameTagById(voiceRoomId));
List<ParticipantDto> participantDtoList = liveKitUtils.getParticipantInfo(String.valueOf(voiceRoomId));
if(participantDtoList==null||participantDtoList.isEmpty()){
return Collections.emptyList();
}
for(ParticipantDto participantDto: participantDtoList){
//profileIamge 집어넣기
participantDto.setProfileImage(findProfileImageByUserId(participantDto.getId()));
participantDto.setProfileImage(findProfileImageByUserId(participantDto.getUserSpaceId()));
//userSpaceId 집어 넣기
User user = userDao.findUserByUserId(participantDto.getId());
participantDto.setUserSpaceId(userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceId());
Expand All @@ -159,11 +162,13 @@ private String findNameTagById(long id){
return name+" #"+String.valueOf(id);
}

public String getToken(long spaceId,long userId,long voiceRoomId){
String userName=userDao.findUserByUserId(userId).getUserName();
public String getToken(long spaceId,long userId,long userSpaceId,long voiceRoomId){
String userName=userSpaceDao.findUserNameById(userSpaceId);
String userIdentity=String.valueOf(userId);
String metadata="";
String roomName=findNameTagById(voiceRoomId);
//Metadata에 profileImage와 userName 추가
String metadata="userProfileImage : "+userSpaceDao.findProfileImageById(userSpaceId).orElse("");
//String roomName=findNameTagById(voiceRoomId);
String roomName = String.valueOf(voiceRoomId);
return liveKitUtils.getRoomToken(userName,userIdentity,roomName,metadata).toJwt();
}
}

0 comments on commit 5704c22

Please sign in to comment.