feat: 강의 만들기/입장 로직 추가

This commit is contained in:
박정민 2024-08-05 10:19:53 +09:00
parent ec839ab8f0
commit 2636833458
5 changed files with 124 additions and 143 deletions

View File

@ -235,8 +235,14 @@ public class LectureServiceImpl implements LectureService {
Lecture l; Lecture l;
l = lecture.get(); l = lecture.get();
l.setOnline(true); if(l.isOnline())
{
l.setOnline(false);
}
else
{
l.setOnline(true);
}
lectureRepository.save(l); lectureRepository.save(l);

View File

@ -36,180 +36,127 @@ import livekit.LivekitWebhook.WebhookEvent;
@RequiredArgsConstructor @RequiredArgsConstructor
public class Controller { public class Controller {
private final JWTUtil jwtUtil; private final JWTUtil jwtUtil;
private final UserService userService; private final UserService userService;
private final LectureService lectureService; private final LectureService lectureService;
private final VideoSertvice videoSertvice; private final VideoSertvice videoSertvice;
private final RegistrationService registrationService; private final RegistrationService registrationService;
private final UserRepository userRepository; private final UserRepository userRepository;
private final LectureRepository lectureRepository; private final LectureRepository lectureRepository;
@Value("${livekit.api.key}")
private String LIVEKIT_API_KEY;
@Value("${livekit.api.secret}") @Value("${livekit.api.key}")
private String LIVEKIT_API_SECRET; private String LIVEKIT_API_KEY;
private static final ObjectMapper objectMapper = new ObjectMapper();
/** @Value("${livekit.api.secret}")
* @param params JSON object with roomName and participantName private String LIVEKIT_API_SECRET;
* @return JSON object with the JWT token private static final ObjectMapper objectMapper = new ObjectMapper();
*/
// @PostMapping(value = "/token")
// public ResponseEntity<Map<String, String>> createToken(@RequestBody Map<String, String> 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<String> startLecture(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
String userToken = request.getHeader("Authorization"); public static String serializeIdentityData(IdentityData identityData) throws JsonProcessingException, JsonProcessingException {
Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); return objectMapper.writeValueAsString(identityData);
}
@PostMapping(value = "/joinroom/{lecture_id}")
public ResponseEntity<Map<String, String>> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
videoSertvice.startOnline(userId, id); String userToken = request.getHeader("Authorization");
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<Map<String, String>> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception
{
// 조인할떄 고려해야할점
// 1. 신청됀 강의인지
// 2. 강의기 On인지
String userToken = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
User findUser= userRepository.findById(userId).orElse(null); User findUser = userRepository.findById(userId).orElse(null);
Lecture lecture= lectureRepository.findById(id).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(); AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
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);
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, "강사"); token.addGrants(new RoomJoin(true), new RoomName(roomName));
String jsonIdentity = serializeIdentityData(identityData);
return ResponseEntity.ok(Map.of("token", token.toJwt()));
token.setIdentity(jsonIdentity); } else {
token.setName(participantName); return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Map.of("error", "방에 들어갈 수 없습니다."));
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() )
{
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<Map<String, String>> 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"); AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
//
// 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"));
//
// }
if (findUser.getRole() == UserRole.ADMIN) {//&& lecture.isOnline() ) {
}
@PostMapping(value = "/livekit/webhook", consumes = "application/webhook+json") videoSertvice.startOnline(userId, id);
public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody String body) {
WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); IdentityData identityData = new IdentityData(participantName, "강사");
try { String jsonIdentity = serializeIdentityData(identityData);
WebhookEvent event = webhookReceiver.receive(body, authHeader);
System.out.println("LiveKit Webhook: " + event.toString());
} catch (Exception e) { token.setIdentity(jsonIdentity);
System.err.println("Error validating webhook event: " + e.getMessage()); token.setName(participantName);
}
return ResponseEntity.ok("ok"); 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<String> 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");
}
} }

View File

@ -5,4 +5,7 @@ import java.sql.SQLException;
public interface VideoSertvice { public interface VideoSertvice {
void startOnline(Long userId,Long lectureId) throws SQLException; void startOnline(Long userId,Long lectureId) throws SQLException;
boolean isRoomAccessible(Long lectureId,Long userId);
} }

View File

@ -1,7 +1,10 @@
package com.edufocus.edufocus.video.service; package com.edufocus.edufocus.video.service;
import com.edufocus.edufocus.lecture.entity.Lecture; 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.lecture.service.LectureService;
import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import com.edufocus.edufocus.registration.service.RegistrationService;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -13,9 +16,31 @@ import java.sql.SQLException;
public class VideoServiceImpl implements VideoSertvice{ public class VideoServiceImpl implements VideoSertvice{
private final LectureService lectureService; private final LectureService lectureService;
private final RegistrationService registrationService;
@Override @Override
public void startOnline(Long userId,Long lectureId) throws SQLException { public void startOnline(Long userId,Long lectureId) throws SQLException {
lectureService.changeState(lectureId); 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;
}
}
} }

View File

@ -21,7 +21,7 @@ jwt.salt=${SALT}
jwt.access-token.expiretime=3000000 jwt.access-token.expiretime=3000000
# Refresh Token ?? ?? (??? ??) # Refresh Token ?? ?? (??? ??)
jwt.refresh-token.expiretime=50400000 jwt.refresh-token.expiretime=504000000
#jwt.refresh-token.expiretime=4000 #jwt.refresh-token.expiretime=4000
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=${DATA_SOURCE_URL} spring.datasource.url=${DATA_SOURCE_URL}