Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Sep 4, 2024
1 parent 6108001 commit 48431ff
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 95 deletions.
5 changes: 0 additions & 5 deletions pkg/models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
natsservice "github.com/mynaparrot/plugnmeet-server/pkg/services/nats"
"github.com/mynaparrot/plugnmeet-server/pkg/services/redis"
log "github.com/sirupsen/logrus"
"time"
)

type WebhookModel struct {
Expand Down Expand Up @@ -51,10 +50,6 @@ func NewWebhookModel(app *config.AppConfig, ds *dbservice.DatabaseService, rs *r
}

func (m *WebhookModel) HandleWebhookEvents(e *livekit.WebhookEvent) {
// wait 1 second before start processing
// otherwise services may not be ready &
// give unexpected results
time.Sleep(time.Second * 1)

switch e.GetEvent() {
case "room_started":
Expand Down
51 changes: 49 additions & 2 deletions pkg/models/webhook_room_start.go → pkg/models/webhook_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package models
import (
"fmt"
"github.com/livekit/protocol/livekit"
"github.com/mynaparrot/plugnmeet-protocol/plugnmeet"
"github.com/mynaparrot/plugnmeet-server/pkg/config"
natsservice "github.com/mynaparrot/plugnmeet-server/pkg/services/nats"
log "github.com/sirupsen/logrus"
"time"
Expand Down Expand Up @@ -38,7 +40,7 @@ func (m *WebhookModel) roomStarted(event *livekit.WebhookEvent) {
}
}

meta.StartedAt = uint64(time.Now().Unix())
meta.StartedAt = uint64(time.Now().UTC().Unix())
if meta.RoomFeatures.GetRoomDuration() > 0 {
// we'll add room info in map
rmDuration := NewRoomDurationModel(m.app, m.rs)
Expand Down Expand Up @@ -70,5 +72,50 @@ func (m *WebhookModel) roomStarted(event *livekit.WebhookEvent) {
event.Room.Sid = rInfo.RoomSid

// webhook notification
go m.sendToWebhookNotifier(event)
m.sendToWebhookNotifier(event)
}

func (m *WebhookModel) roomFinished(event *livekit.WebhookEvent) {
if event.Room == nil {
log.Warnln(fmt.Sprintf("invalid webhook info received: %+v", event))
return
}

rInfo, err := m.natsService.GetRoomInfo(event.Room.Name)
if err != nil || rInfo == nil {
return
}

event.Room.Metadata = rInfo.Metadata
event.Room.Sid = rInfo.RoomSid

// we are introducing a new event name here
// because for our case we still have remaining tasks
go m.sendCustomTypeWebhook(event, "session_ended")

if rInfo.Status != natsservice.RoomStatusEnded {
// so, this session was not ended by API call
// may be for some reason room was ended by livekit

// change status to ended
err = m.natsService.UpdateRoomStatus(rInfo.RoomId, natsservice.RoomStatusEnded)
if err != nil {
log.Errorln(err)
}
// end the room in proper way
m.rm.EndRoom(&plugnmeet.RoomEndReq{RoomId: rInfo.RoomId})
}

// now we'll perform a few service related tasks
time.Sleep(config.WaitBeforeTriggerOnAfterRoomEnded)

// at the end we'll handle event notification
// send it first
m.sendToWebhookNotifier(event)

// now clean up webhook for this room
err = m.webhookNotifier.DeleteWebhook(rInfo.RoomId)
if err != nil {
log.Errorln(err)
}
}
58 changes: 0 additions & 58 deletions pkg/models/webhook_room_end.go

This file was deleted.

23 changes: 23 additions & 0 deletions pkg/models/webhook_user_left.go → pkg/models/webhook_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ import (
log "github.com/sirupsen/logrus"
)

func (m *WebhookModel) participantJoined(event *livekit.WebhookEvent) {
if event.Room == nil {
log.Warnln(fmt.Sprintf("invalid webhook info received: %+v", event))
return
}

rInfo, err := m.natsService.GetRoomInfo(event.Room.Name)
if err != nil || rInfo == nil {
return
}

event.Room.Sid = rInfo.RoomSid
event.Room.Metadata = rInfo.Metadata

_, err = m.ds.IncrementOrDecrementNumParticipants(rInfo.RoomSid, "+")
if err != nil {
log.Errorln(err)
}

// webhook notification
m.sendToWebhookNotifier(event)
}

func (m *WebhookModel) participantLeft(event *livekit.WebhookEvent) {
if event.Room == nil {
log.Warnln(fmt.Sprintf("invalid webhook info received: %+v", event))
Expand Down
30 changes: 0 additions & 30 deletions pkg/models/webhook_user_join.go

This file was deleted.

0 comments on commit 48431ff

Please sign in to comment.