-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
25 changed files
with
1,370 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/space/space_spring/controller/LiveKitController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package space.space_spring.controller; | ||
|
||
import io.livekit.server.AccessToken; | ||
import jakarta.annotation.Nullable; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import space.space_spring.dto.VoiceRoom.LiveKitDto.LiveKitSession; | ||
import space.space_spring.dto.VoiceRoom.ParticipantDto; | ||
import space.space_spring.dto.VoiceRoom.RoomDto; | ||
import space.space_spring.dto.VoiceRoom.LiveKitDto.GetTokenRequest; | ||
import space.space_spring.service.LiveKitService; | ||
|
||
import java.util.List; | ||
|
||
@Controller | ||
@RequestMapping("/live") | ||
@RequiredArgsConstructor | ||
public class LiveKitController { | ||
|
||
private final LiveKitService liveKitService; | ||
|
||
@GetMapping("/token") | ||
public ResponseEntity<String> showUserForm(@RequestBody @Nullable GetTokenRequest getTokenRequest){ | ||
AccessToken token = new AccessToken("APIS272JhrVicJF", "b8q8i1TDsPi5jTd6gq0kDMJF58Bc0wtgg4DJfEwfCf9B"); | ||
|
||
// Fill in token information. | ||
// token.setName("name"); | ||
// token.setIdentity("identity3"); | ||
// token.setMetadata("metadata3"); | ||
// token.addGrants(new RoomJoin(true), new RoomName("myroom")); | ||
//liveKitService.getRoomToken(TokenRequest); | ||
// Sign and create token string. | ||
System.out.println("New access token: " + token.toJwt()); | ||
return ResponseEntity.ok(liveKitService.getRoomToken(getTokenRequest).toJwt()); | ||
} | ||
|
||
@GetMapping("/sessions") | ||
public ResponseEntity<List<LiveKitSession>> getLiveKitSessions() { | ||
List<LiveKitSession> sessions = liveKitService.listLiveKitSessions(); | ||
if (sessions != null) { | ||
return ResponseEntity.ok(sessions); | ||
} else { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
} | ||
} | ||
|
||
@GetMapping("/roomList") | ||
public ResponseEntity<String> getLiveKitRoomList(){ | ||
List<RoomDto> roomList = liveKitService.getRoomDtoList(); | ||
if (roomList != null) { | ||
return ResponseEntity.ok(roomList.toString()); | ||
} else { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
} | ||
|
||
} | ||
|
||
@GetMapping("/participant") | ||
public ResponseEntity<String> participantList(@RequestParam("roomName") String roomName){ | ||
System.out.print("participant Input : "+roomName); | ||
List<ParticipantDto> participantList= liveKitService.getParticipantInfo(roomName); | ||
if (participantList != null) { | ||
//participantList.get(0).getTracksList().get(0).getMuted(); | ||
return ResponseEntity.ok(participantList.toString()); | ||
} else { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
} | ||
} | ||
|
||
} |
212 changes: 212 additions & 0 deletions
212
src/main/java/space/space_spring/controller/VoiceRoomController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package space.space_spring.controller; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth; | ||
import space.space_spring.dao.UserSpaceDao; | ||
import space.space_spring.dao.VoiceRoomRepository; | ||
import space.space_spring.dto.VoiceRoom.*; | ||
import space.space_spring.entity.Space; | ||
import space.space_spring.entity.User; | ||
import space.space_spring.entity.UserSpace; | ||
import space.space_spring.exception.VoiceRoomException; | ||
import space.space_spring.response.BaseResponse; | ||
import space.space_spring.service.LiveKitService; | ||
import space.space_spring.service.VoiceRoomService; | ||
import space.space_spring.util.space.SpaceUtils; | ||
import space.space_spring.util.user.UserUtils; | ||
import space.space_spring.util.userSpace.UserSpaceUtils; | ||
|
||
import javax.print.DocFlavor; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static space.space_spring.entity.enumStatus.UserSpaceAuth.MANAGER; | ||
import static space.space_spring.response.status.BaseExceptionResponseStatus.*; | ||
import static space.space_spring.util.bindingResult.BindingResultUtils.getErrorMessage; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/space/{spaceId}/voiceRoom") | ||
public class VoiceRoomController { | ||
private final LiveKitService liveKitService; | ||
private final VoiceRoomService voiceRoomService; | ||
private final UserSpaceUtils userSpaceUtils; | ||
private final VoiceRoomRepository voiceRoomRepository; | ||
private final UserSpaceDao userSpaceDao; | ||
private final UserUtils userUtils; | ||
private final SpaceUtils spaceUtils; | ||
|
||
//VoiceRoom ์์ฑ/์์ | ||
@PostMapping("") | ||
public BaseResponse<PostVoiceRoomDto.Response> createRoom( | ||
@PathVariable("spaceId") @NotNull long spaceId, | ||
@JwtLoginAuth Long userId, | ||
@Validated @RequestBody PostVoiceRoomDto.Request voiceRoomRequest, | ||
BindingResult bindingResult){ | ||
if(bindingResult.hasErrors()){ | ||
throw new VoiceRoomException(INVALID_VOICEROOM_REQUEST,getErrorMessage(bindingResult)); | ||
} | ||
|
||
//ํด๋น ์ ์ ๊ฐ voice์ด ์๋ space์ ํฌํจ๋์ด ์๋์ง(๊ถํ์ด ์๋์ง) ํ์ธ | ||
validateIsUserInSpace(spaceId,userId); | ||
//ํด๋น ์ ์ ๊ฐ ํ์ฌ space์ ๋ํด ๊ด๋ฆฌ์ ๊ถํ์ ๊ฐ๊ณ ์๋์ง ํ์ธ | ||
validateManagerPermission(spaceId,userId); | ||
//Todo response ๋ด์ฉ์ ๋ฌด์์ ์ฃผ๋ฉด ์ข์์ง ( POST response ์ ์ฒด ๊ธฐ๋ฅ ํต์ผ ํ๋ ๊ฒ์ผ ์ข์๋ณด์ ) | ||
PostVoiceRoomDto.Response res = new PostVoiceRoomDto.Response(voiceRoomService.createVoiceRoom(spaceId,voiceRoomRequest)); | ||
return new BaseResponse<>(res); | ||
} | ||
|
||
//ํ์ฌ space์ VoiceRoom ๋ชฉ๋ก | ||
@GetMapping("") | ||
public BaseResponse<GetVoiceRoomList.Response> getRoomList( | ||
@PathVariable("spaceId") @NotNull long spaceId, | ||
@JwtLoginAuth Long userId, | ||
//@RequestBody GetVoiceRoomList.Request voiceRoomList, | ||
@RequestParam(required = false,defaultValue = "0") Integer limit, | ||
@RequestParam(required = false,defaultValue = "false") Boolean showParticipant){ | ||
|
||
boolean showParticipantValue = (showParticipant != null) ? showParticipant : false; | ||
|
||
//ํด๋น ์ ์ ๊ฐ, voiceRoom์ด ์๋ space์ ํฌํจ๋์ด ์๋์ง(๊ถํ์ด ์๋์ง) ํ์ธ | ||
validateIsUserInSpace(spaceId,userId); | ||
|
||
GetVoiceRoomList.Request voiceRoomList=new GetVoiceRoomList.Request(limit, showParticipant); | ||
|
||
List<GetVoiceRoomList.VoiceRoomInfo> roomInfoList = voiceRoomService.getVoiceRoomInfoList(spaceId,voiceRoomList); | ||
return new BaseResponse<GetVoiceRoomList.Response>(new GetVoiceRoomList.Response(roomInfoList)); | ||
} | ||
|
||
//VoiceRoom์ ์ฅ token ๋ฐํ | ||
@GetMapping("/{voiceRoomId}/token") | ||
public BaseResponse<String> getToken( | ||
@PathVariable("spaceId") @NotNull long spaceId, | ||
@JwtLoginAuth Long userId, | ||
@PathVariable("voiceRoomId") @NotNull Long roomId, | ||
HttpServletResponse response | ||
){ | ||
//ํด๋น ์ ์ ๊ฐ, voiceRoom์ด ์๋ space์ ํฌํจ๋์ด ์๋์ง(๊ถํ์ด ์๋์ง) ํ์ธ | ||
validateIsUserInSpace(spaceId,userId); | ||
//ํด๋น voiceRoomId๊ฐ ์กด์ฌํ๋์ง ํ์ธ | ||
validateVoiceRoom(roomId); | ||
//ํด๋น voiceRoom์ด ํด๋น space์ ์ํ๊ฒ์ด ๋ง๋์ง ํ์ธ | ||
validateVoiceRoomInSpace(spaceId,roomId); | ||
|
||
response.setHeader("Authorization", "Bearer " + voiceRoomService.getToken(spaceId, userId,roomId)); | ||
return new BaseResponse<String>( | ||
"๋ณด์ด์ค๋ฃธ ํ ํฐ ์์ฑ์ ์ฑ๊ณตํ์ต๋๋ค." | ||
); | ||
} | ||
|
||
//VoiceRoom์ ์ฐธ๊ฐ์ ์ ๋ณด | ||
@GetMapping("/{voiceRoomId}/participant") | ||
public BaseResponse<GetParticipantList.Response> getParticipants( | ||
@PathVariable("spaceId") @NotNull long spaceId, | ||
@JwtLoginAuth Long userId, | ||
@PathVariable("voiceRoomId") @NotNull Long roomId | ||
){ | ||
//ํด๋น ์ ์ ๊ฐ voice์ด ์๋ space์ ํฌํจ๋์ด ์๋์ง(๊ถํ์ด ์๋์ง) ํ์ธ | ||
validateIsUserInSpace(spaceId,userId); | ||
//ํด๋น voiceRoomId๊ฐ ์กด์ฌํ๋์ง ํ์ธ | ||
validateVoiceRoom(roomId); | ||
//ํด๋น voiceRoom์ด ํด๋น space์ ์ํ๊ฒ์ด ๋ง๋์ง ํ์ธ | ||
validateVoiceRoomInSpace(spaceId,roomId); | ||
|
||
List<GetParticipantList.ParticipantInfo> participantInfoList = voiceRoomService.getParticipantInfoListById(roomId); | ||
return new BaseResponse<GetParticipantList.Response>(new GetParticipantList.Response(participantInfoList)); | ||
} | ||
|
||
@PatchMapping("") | ||
public BaseResponse<String> updateVoiceRoom( | ||
@PathVariable("spaceId") @NotNull long spaceId, | ||
@JwtLoginAuth Long userId, | ||
@Validated @RequestBody PatchVoiceRoom patchVoiceRoom, | ||
BindingResult bindingResult | ||
){ | ||
|
||
if(bindingResult.hasErrors()){ | ||
throw new VoiceRoomException(INVALID_VOICEROOM_REQUEST,getErrorMessage(bindingResult)); | ||
} | ||
//ํด๋น ์ ์ ๊ฐ voice์ด ์๋ space์ ํฌํจ๋์ด ์๋์ง(๊ถํ์ด ์๋์ง) ํ์ธ | ||
validateIsUserInSpace(spaceId,userId); | ||
//ํด๋น ์ ์ ๊ฐ ํ์ฌ space์ ๋ํด ๊ด๋ฆฌ์ ๊ถํ์ ๊ฐ๊ณ ์๋์ง ํ์ธ | ||
validateManagerPermission(spaceId,userId); | ||
//ํด๋น voiceRoom์ด ํด๋น space์ ์ํ๊ฒ์ด ๋ง๋์ง ํ์ธ | ||
for(PatchVoiceRoom.UpdateRoom updateRoom : patchVoiceRoom.getUpdateRoomList()) { | ||
validateVoiceRoomInSpace(spaceId, updateRoom.getRoomId()); | ||
} | ||
|
||
voiceRoomService.updateVoiceRoom(patchVoiceRoom.getUpdateRoomList()); | ||
|
||
return new BaseResponse<>("success"); | ||
} | ||
|
||
@DeleteMapping("/{voiceRoomId}") | ||
public BaseResponse<String> deleteVoiceRoom( | ||
@PathVariable("spaceId") @NotNull long spaceId, | ||
@JwtLoginAuth Long userId, | ||
@PathVariable("voiceRoomId") @NotNull Long voiceRoomId | ||
){ | ||
//ํด๋น ์ ์ ๊ฐ voice์ด ์๋ space์ ํฌํจ๋์ด ์๋์ง(๊ถํ์ด ์๋์ง) ํ์ธ | ||
validateIsUserInSpace(spaceId,userId); | ||
//ํด๋น ์ ์ ๊ฐ ํ์ฌ space์ ๋ํด ๊ด๋ฆฌ์ ๊ถํ์ ๊ฐ๊ณ ์๋์ง ํ์ธ | ||
validateManagerPermission(spaceId,userId); | ||
//ํด๋น voiceRoom์ด ํด๋น space์ ์ํ๊ฒ์ด ๋ง๋์ง ํ์ธ | ||
validateVoiceRoomInSpace(spaceId, voiceRoomId); | ||
|
||
voiceRoomService.deleteVoiceRoom(voiceRoomId); | ||
return new BaseResponse<>("success"); | ||
} | ||
|
||
//VoiceRoom ๋ณ๋์ฌํญ ์ ๋ฌ | ||
@PostMapping("/status") | ||
public BaseResponse<String> postRoomStatus(){ | ||
//Todo ํด๋น ์ ์ ๊ฐ voice์ด ์๋ space์ ํฌํจ๋์ด ์๋์ง(๊ถํ์ด ์๋์ง) ํ์ธ | ||
return new BaseResponse<String>(null); | ||
} | ||
|
||
private void validateIsUserInSpace( Long spaceId,Long userId) { | ||
// ์ ์ ๊ฐ ์คํ์ด์ค์ ์ํ ๊ฒฝ์ฐ exception์ด ํฐ์ง์ง ์์ ๊ฒ์ | ||
// ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, USER_IS_NOT_IN_SPACE ์์ธ๊ฐ ํฐ์ง ๊ฒ์ -> ์ถํ exception handling ๊ณผ์ ํ์ | ||
|
||
//ํ์ฌ๋ ์คํ์ด์ค ์ ๊ทผ ๊ถํ์ ์ผ๊ด์ ์ผ๋ก ์์ธ ์ฒ๋ฆฌ | ||
//๋ถ๋ฆฌ ๊ฐ๋ฅ์ฑ ๋ฐ ํจ์ฉ์ฑ ๊ฒํ ํ์ | ||
userSpaceUtils.isUserInSpace(userId, spaceId); | ||
} | ||
private boolean validateVoiceRoom(long voiceRoomId){ | ||
//Todo ํด๋น ๋ณด์ด์ค๋ฃธ์ด ์กด์ฌํ๋์ง ํ์ธ | ||
if(!voiceRoomRepository.existsByVoiceRoomId(voiceRoomId)){ | ||
throw new VoiceRoomException(VOICEROOM_NOT_EXIST); | ||
} | ||
return true; | ||
} | ||
private boolean validateVoiceRoomNameExist(String voiceRoomName){ | ||
if(!voiceRoomRepository.existsByName(voiceRoomName)){ | ||
throw new VoiceRoomException(VOICEROOM_NAME_ALREADY_EXIST); | ||
} | ||
return true; | ||
} | ||
private boolean validateVoiceRoomInSpace(long spaceId,long voiceRoomId){ | ||
if(! (voiceRoomRepository.findById(voiceRoomId).getSpace().getSpaceId().equals(spaceId))){ | ||
throw new VoiceRoomException(VOICEROOM_NOT_IN_SPACE); | ||
} | ||
return true; | ||
} | ||
private boolean validateManagerPermission(long spaceId,long userId){ | ||
//ํด๋น ์ ์ ๊ฐ ํ์ฌ space์ ๋ํด ๊ด๋ฆฌ์ ๊ถํ์ ๊ฐ๊ณ ์๋์ง ํ์ธ | ||
//TODO ๊ถํ ํ์ธ ๊ณผ์ ์ ์ผ๊ด์ ์ผ๋ก ์ฒ๋ฆฌ ํ ์ ์๋ ์ฝ๋๊ฐ ํ์ํด ๋ณด์ | ||
User user = userUtils.findUserByUserId(userId); | ||
Space space = spaceUtils.findSpaceBySpaceId(spaceId); | ||
//์ด๋ฏธ userSpace ์กด์ฌ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํด์ null ๊ฒ์ฌ๋ ์๋ตํจ | ||
|
||
if(!userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceAuth().toString().equals(MANAGER.getAuth())){ | ||
System.out.print("Author :" +userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceAuth().toString()); | ||
throw new VoiceRoomException(VOICEROOM_DO_NOT_HAVE_PERMISSION); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package space.space_spring.dao; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import space.space_spring.entity.Space; | ||
import space.space_spring.entity.VoiceRoom; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public class VoiceRoomDao { | ||
@PersistenceContext | ||
private EntityManager entityManager; | ||
@Transactional | ||
public Long createVoiceRoom(String name,int order,Space space){ | ||
try { | ||
VoiceRoom voiceRoom = VoiceRoom.createVoiceRoom(name, order, space); | ||
entityManager.persist(voiceRoom); | ||
entityManager.flush(); | ||
|
||
return voiceRoom.getVoiceRoomId(); | ||
}catch (Exception e){ | ||
System.out.print("create voiceRoom exception:"+e); | ||
//Todo ์์ธ ์ฒ๋ฆฌ ํ์ | ||
return null; | ||
} | ||
} | ||
@Transactional | ||
public Integer findMaxOrderBySpace(Space space) { | ||
String jpql = "SELECT MAX(r.order) FROM voice_room r WHERE r.space = :space"; | ||
|
||
return entityManager.createQuery(jpql, Integer.class) | ||
.setParameter("space", space) | ||
.getSingleResult(); | ||
} | ||
|
||
@Transactional | ||
public VoiceRoom findRoomWithMaxOrderBySpace(Space space) { | ||
String jpql = "SELECT r FROM VoiceRoom r WHERE r.space = :space AND r.order = (SELECT MAX(r2.order) FROM VoiceRoom r2 WHERE r2.space = :space)"; | ||
|
||
return entityManager.createQuery(jpql, VoiceRoom.class) | ||
.setParameter("space", space) | ||
.getSingleResult(); | ||
} | ||
|
||
|
||
} | ||
|
||
|
Oops, something went wrong.