diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java index b97246c..b2fc5fd 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java @@ -235,8 +235,14 @@ public class LectureServiceImpl implements LectureService { Lecture l; l = lecture.get(); - l.setOnline(true); - + if(l.isOnline()) + { + l.setOnline(false); + } + else + { + l.setOnline(true); + } lectureRepository.save(l); diff --git a/backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java b/backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java index a94758e..c4b06e5 100644 --- a/backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java +++ b/backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java @@ -36,180 +36,127 @@ import livekit.LivekitWebhook.WebhookEvent; @RequiredArgsConstructor public class Controller { - private final JWTUtil jwtUtil; - private final UserService userService; - private final LectureService lectureService; - private final VideoSertvice videoSertvice; - private final RegistrationService registrationService; - private final UserRepository userRepository; - private final LectureRepository lectureRepository; - @Value("${livekit.api.key}") - private String LIVEKIT_API_KEY; + private final JWTUtil jwtUtil; + private final UserService userService; + private final LectureService lectureService; + private final VideoSertvice videoSertvice; + private final RegistrationService registrationService; + private final UserRepository userRepository; + private final LectureRepository lectureRepository; - @Value("${livekit.api.secret}") - private String LIVEKIT_API_SECRET; - private static final ObjectMapper objectMapper = new ObjectMapper(); + @Value("${livekit.api.key}") + private String LIVEKIT_API_KEY; - /** - * @param params JSON object with roomName and participantName - * @return JSON object with the JWT token - */ -// @PostMapping(value = "/token") -// public ResponseEntity> createToken(@RequestBody Map params) { -// String roomName = params.get("roomName"); -// String participantName = params.get("participantName"); -// -// if (roomName == null || participantName == null) { -// return ResponseEntity.badRequest().body(Map.of("errorMessage", "roomName and participantName are required")); -// } -// -// AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); -// token.setName(participantName); -// token.setIdentity(participantName); -// token.addGrants(new RoomJoin(true), new RoomName(roomName)); -// -// -// -// return ResponseEntity.ok(Map.of("token", token.toJwt())); -// } - - @PostMapping(value = "/makeroom/{lecture_id}") - public ResponseEntity startLecture(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception { + @Value("${livekit.api.secret}") + private String LIVEKIT_API_SECRET; + private static final ObjectMapper objectMapper = new ObjectMapper(); - String userToken = request.getHeader("Authorization"); - Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); + public static String serializeIdentityData(IdentityData identityData) throws JsonProcessingException, JsonProcessingException { + return objectMapper.writeValueAsString(identityData); + } + + @PostMapping(value = "/joinroom/{lecture_id}") + public ResponseEntity> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception { - videoSertvice.startOnline(userId, id); - - return new ResponseEntity<>("방만들기 성공",HttpStatus.OK); - - } - public static String serializeIdentityData(IdentityData identityData) throws JsonProcessingException, JsonProcessingException { - return objectMapper.writeValueAsString(identityData); - } - - @PostMapping(value = "/joinroom/{lecture_id}") - public ResponseEntity> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception - { - - // 방 조인할떄 고려해야할점 - // 1. 신청됀 강의인지 - // 2. 그 강의기 On인지 - String userToken = request.getHeader("Authorization"); + String userToken = request.getHeader("Authorization"); - Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); - User findUser= userRepository.findById(userId).orElse(null); - Lecture lecture= lectureRepository.findById(id).orElse(null); + Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); + User findUser = userRepository.findById(userId).orElse(null); + Lecture lecture = lectureRepository.findById(id).orElse(null); - Random random = new Random(); + String roomName = lecture.getTitle(); + String participantName = userService.getUserName(userId); + System.out.println(participantName); - System.out.println(); - int randomNumber = 10000 + random.nextInt(80000); - - String randStr = String.valueOf(randomNumber); - - String roomName = lecture.getTitle(); - String participantName = userService.getUserName(userId); - System.out.println(participantName); - - AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); + AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); - if(findUser.getRole()==UserRole.ADMIN ){//&& lecture.isOnline() ) { + if (findUser.getRole() == UserRole.STUDENT) { + + if (videoSertvice.isRoomAccessible(id, userId)) { + IdentityData identityData = new IdentityData(participantName, "학생"); + String jsonIdentity = serializeIdentityData(identityData); + token.setIdentity(jsonIdentity); + token.setName(participantName); - IdentityData identityData = new IdentityData(participantName, "강사"); - String jsonIdentity = serializeIdentityData(identityData); + token.addGrants(new RoomJoin(true), new RoomName(roomName)); - - token.setIdentity(jsonIdentity); - token.setName(participantName); - - token.addGrants(new RoomJoin(true), new RoomName(roomName), new RoomCreate(true)); - - videoSertvice.startOnline(userId, id); - - System.out.println(); - - return ResponseEntity.ok(Map.of("token", token.toJwt())); - - } - else if(findUser.getRole()==UserRole.STUDENT )// && lecture.isOnline() ) - { + return ResponseEntity.ok(Map.of("token", token.toJwt())); + } else { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(Map.of("error", "방에 들어갈 수 없습니다.")); + } - IdentityData identityData = new IdentityData(participantName, "학생"); - String jsonIdentity = serializeIdentityData(identityData); + } + return ResponseEntity.ok(Map.of("token", null)); - token.setIdentity(jsonIdentity); - token.setName(participantName); - - token.addGrants(new RoomJoin(true), new RoomName(roomName)); + } - videoSertvice.startOnline(userId, id); + @PostMapping(value = "/makeroom/{lecture_id}") + public ResponseEntity> makeRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception { + String userToken = request.getHeader("Authorization"); - return ResponseEntity.ok(Map.of("token", token.toJwt())); - } + Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); + User findUser = userRepository.findById(userId).orElse(null); + Lecture lecture = lectureRepository.findById(id).orElse(null); - return ResponseEntity.ok(Map.of("token", null)); + String roomName = lecture.getTitle(); + String participantName = userService.getUserName(userId); + System.out.println(participantName); -// String userToken = request.getHeader("Authorization"); -// -// Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); -// LectureDetailResponse lecture= lectureService.findLectureById(userId,id); -// -// -// //RegistrationStatus registrationStatus = registrationService.isOnline(userId,id); -// -// if(registrationStatus==RegistrationStatus.ACCEPTED) -// { -// String roomName = lecture.getTitle(); -// String participantName = userService.getUserName(userId); -// -// -// AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); -// token.setName(participantName); -// token.setIdentity(participantName); -// token.addGrants(new RoomJoin(true), new RoomName(roomName)); -// -// //videoSertvice.startOnline(userId,id); -// -// -// -// return ResponseEntity.ok(Map.of("token", token.toJwt())); -// } -// else{ -// return ResponseEntity.status(HttpStatus.FORBIDDEN).body(Map.of("errorMessage", "Not accepted")); -// -// } + AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); + if (findUser.getRole() == UserRole.ADMIN) {//&& lecture.isOnline() ) { - } - @PostMapping(value = "/livekit/webhook", consumes = "application/webhook+json") - public ResponseEntity receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody String body) { - WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); - try { - WebhookEvent event = webhookReceiver.receive(body, authHeader); - System.out.println("LiveKit Webhook: " + event.toString()); - } catch (Exception e) { - System.err.println("Error validating webhook event: " + e.getMessage()); - } - return ResponseEntity.ok("ok"); - } + videoSertvice.startOnline(userId, id); + + IdentityData identityData = new IdentityData(participantName, "강사"); + String jsonIdentity = serializeIdentityData(identityData); + + + token.setIdentity(jsonIdentity); + token.setName(participantName); + + token.addGrants(new RoomJoin(true), new RoomName(roomName), new RoomCreate(true)); + + + return ResponseEntity.ok(Map.of("token", token.toJwt())); + + } + + + return ResponseEntity.ok(Map.of("token", null)); + + } + + @PostMapping(value = "/livekit/webhook", consumes = "application/webhook+json") + public ResponseEntity receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody String body) { + WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); + try { + WebhookEvent event = webhookReceiver.receive(body, authHeader); + System.out.println("LiveKit Webhook: " + event.toString()); + } catch (Exception e) { + System.err.println("Error validating webhook event: " + e.getMessage()); + } + + + return ResponseEntity.ok("ok"); + } - } diff --git a/backend/src/main/java/com/edufocus/edufocus/video/service/VideoSertvice.java b/backend/src/main/java/com/edufocus/edufocus/video/service/VideoSertvice.java index 981c5c4..53428f9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/video/service/VideoSertvice.java +++ b/backend/src/main/java/com/edufocus/edufocus/video/service/VideoSertvice.java @@ -5,4 +5,7 @@ import java.sql.SQLException; public interface VideoSertvice { void startOnline(Long userId,Long lectureId) throws SQLException; + + boolean isRoomAccessible(Long lectureId,Long userId); + } diff --git a/backend/src/main/java/com/edufocus/edufocus/video/service/VideoServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/video/service/VideoServiceImpl.java index af82559..c6328e2 100644 --- a/backend/src/main/java/com/edufocus/edufocus/video/service/VideoServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/video/service/VideoServiceImpl.java @@ -1,7 +1,10 @@ package com.edufocus.edufocus.video.service; import com.edufocus.edufocus.lecture.entity.Lecture; +import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; import com.edufocus.edufocus.lecture.service.LectureService; +import com.edufocus.edufocus.registration.entity.RegistrationStatus; +import com.edufocus.edufocus.registration.service.RegistrationService; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -13,9 +16,31 @@ import java.sql.SQLException; public class VideoServiceImpl implements VideoSertvice{ private final LectureService lectureService; + private final RegistrationService registrationService; @Override public void startOnline(Long userId,Long lectureId) throws SQLException { lectureService.changeState(lectureId); } + + @Override + public boolean isRoomAccessible(Long lectureId, Long userId) { + + + + LectureDetailResponse lecture= lectureService.findLectureById(userId,userId); + + + RegistrationStatus registrationStatus = registrationService.getStatus(userId,lectureId); + + + if(registrationStatus==RegistrationStatus.ACCEPTED && lecture.isOnline()) + { + return true; + } + else + { + return false; + } + } } diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 6b6aae7..b774bfc 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -21,7 +21,7 @@ jwt.salt=${SALT} jwt.access-token.expiretime=3000000 # Refresh Token ?? ?? (??? ??) -jwt.refresh-token.expiretime=50400000 +jwt.refresh-token.expiretime=504000000 #jwt.refresh-token.expiretime=4000 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=${DATA_SOURCE_URL}