44import com .example .be .repository .RoomRepository ;
55import com .example .be .web .dto .OpenviduDTO ;
66import io .livekit .server .*;
7- import livekit .LivekitModels ;
87import lombok .RequiredArgsConstructor ;
98import lombok .extern .slf4j .Slf4j ;
109import org .springframework .beans .factory .annotation .Value ;
1110import org .springframework .http .ResponseEntity ;
11+ import org .springframework .transaction .annotation .Transactional ;
1212import org .springframework .web .bind .annotation .*;
1313
1414import 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