Skip to content

Commit f17d1c2

Browse files
committed
[FIX] #50 스터디룸 인원수 로직 수정
1 parent fec5ae9 commit f17d1c2

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/main/java/com/example/be/web/controller/OpenviduController.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import com.example.be.repository.RoomRepository;
55
import com.example.be.web.dto.OpenviduDTO;
66
import io.livekit.server.*;
7-
import livekit.LivekitModels;
87
import lombok.RequiredArgsConstructor;
98
import lombok.extern.slf4j.Slf4j;
109
import org.springframework.beans.factory.annotation.Value;
1110
import org.springframework.http.ResponseEntity;
11+
import org.springframework.transaction.annotation.Transactional;
1212
import org.springframework.web.bind.annotation.*;
1313

1414
import java.util.Map;
@@ -48,10 +48,12 @@ public ResponseEntity<Map<String, String>> createToken(@RequestBody OpenviduDTO.
4848
return ResponseEntity.ok(Map.of("token", token.toJwt()));
4949
}
5050

51-
@PostMapping(value = "/livekit/webhook", consumes = "application/webhook+json")
52-
public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody String body) {
53-
WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
54-
try {
51+
52+
@Transactional
53+
@PostMapping(value = "/livekit/webhook", consumes = "application/webhook+json")
54+
public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody String body) {
55+
WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
56+
try {
5557
WebhookEvent event = webhookReceiver.receive(body, authHeader);
5658
String roomName = event.getRoom().getName();
5759
int roomParticipantCount = event.getRoom().getNumParticipants();
@@ -72,7 +74,7 @@ public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") Str
7274
.title(roomName)
7375
.createDate(createAt)
7476
.participantCount(0)
75-
.maxParticipants(10) // 기본값 설정
77+
.maxParticipants(10)
7678
.build();
7779
roomRepository.save(newRoom);
7880
}
@@ -94,20 +96,20 @@ public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") Str
9496
}
9597
case "participant_left" -> {
9698
Room room = roomRepository.findByTitle(roomName);
97-
if (room != null && roomParticipantCount > 0) {
98-
room.setParticipantCount(roomParticipantCount);
99-
roomRepository.save(room);
100-
log.info("Participant left now. Current count: {}", roomParticipantCount);
99+
if (room != null) {
100+
if (roomParticipantCount == 0) {
101+
roomRepository.delete(room);
102+
log.info("Room deleted (LiveKit reported 0): {}", roomName);
103+
} else {
104+
room.setParticipantCount(roomParticipantCount);
105+
roomRepository.save(room);
106+
log.info("Participant left. Room: {}, Current count: {}", roomName, roomParticipantCount);
101107
}
102-
else if (room != null && roomParticipantCount == 0) {
103-
roomRepository.delete(room);
104-
log.info("Room deleted: {}", roomName);
105108
}
106109
}
107110
}
108111
} catch (Exception e) {
109112
log.info("Error validating webhook event: {}", e.getMessage());
110-
e.printStackTrace();
111113
return ResponseEntity.badRequest().body("Invalid webhook");
112114
}
113115

0 commit comments

Comments
 (0)