Skip to content

Commit

Permalink
update breakout room lists instantly after end
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Sep 16, 2024
1 parent 1ac12a6 commit c6614c1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/jordic/lti v0.0.0-20160211051708-2c756eacbab9
github.com/livekit/protocol v1.21.0
github.com/livekit/server-sdk-go/v2 v2.2.1
github.com/mynaparrot/plugnmeet-protocol v0.0.0-20240914113712-e6e9efae7d51
github.com/mynaparrot/plugnmeet-protocol v0.0.0-20240916153048-d72bf01c12fc
github.com/nats-io/jwt/v2 v2.6.0
github.com/nats-io/nats.go v1.37.0
github.com/nats-io/nkeys v0.4.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mynaparrot/plugnmeet-protocol v0.0.0-20240914113712-e6e9efae7d51 h1:M/3/IyT3ps5OoO+c8GlnVDsvuBQJ5jWMvK6blA5kOdY=
github.com/mynaparrot/plugnmeet-protocol v0.0.0-20240914113712-e6e9efae7d51/go.mod h1:oXYXVoAr5AQAO785GGRZdaF2Pn1t8PZsjoIs3cGcSJY=
github.com/mynaparrot/plugnmeet-protocol v0.0.0-20240916153048-d72bf01c12fc h1:V75DG+KfPXg2goGSs7LXYPPxdNrqxnEVs5oZWGJPcek=
github.com/mynaparrot/plugnmeet-protocol v0.0.0-20240916153048-d72bf01c12fc/go.mod h1:oXYXVoAr5AQAO785GGRZdaF2Pn1t8PZsjoIs3cGcSJY=
github.com/nats-io/jwt/v2 v2.6.0 h1:yXoBTdEotZw3NujMT+Nnu1UPNlFWdKQ3d0JJF/+pJag=
github.com/nats-io/jwt/v2 v2.6.0/go.mod h1:ZdWS1nZa6WMZfFwwgpEaqBV8EPGVgOTDHN/wTbz0Y5A=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
Expand Down
43 changes: 29 additions & 14 deletions pkg/models/bkroom_end.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
package models

import (
"errors"
"github.com/mynaparrot/plugnmeet-protocol/plugnmeet"
log "github.com/sirupsen/logrus"
)

func (m *BreakoutRoomModel) EndBreakoutRoom(r *plugnmeet.EndBreakoutRoomReq) error {
_, err := m.fetchBreakoutRoom(r.RoomId, r.BreakoutRoomId)
rm, err := m.natsService.GetBreakoutRoom(r.RoomId, r.BreakoutRoomId)
if err != nil {
return err
}
_, _ = m.rm.EndRoom(&plugnmeet.RoomEndReq{RoomId: r.BreakoutRoomId})

_ = m.natsService.DeleteBreakoutRoom(r.RoomId, r.BreakoutRoomId)
m.performPostHookTask(r.RoomId)
if rm == nil {
return errors.New("room not found")
}
m.proceedToEndBkRoom(r.BreakoutRoomId, r.RoomId)
return nil
}

func (m *BreakoutRoomModel) EndAllBreakoutRoomsByParentRoomId(parentRoomId string) error {
rooms, err := m.fetchBreakoutRooms(parentRoomId)
ids, err := m.natsService.GetBreakoutRoomIdsByParentRoomId(parentRoomId)
if err != nil {
return err
}

if rooms == nil || len(rooms) == 0 {
if ids == nil || len(ids) == 0 {
return m.updateParentRoomMetadata(parentRoomId)
}

for _, r := range rooms {
_ = m.EndBreakoutRoom(&plugnmeet.EndBreakoutRoomReq{
BreakoutRoomId: r.Id,
RoomId: parentRoomId,
})
for _, i := range ids {
m.proceedToEndBkRoom(i, parentRoomId)
}
return nil
}

func (m *BreakoutRoomModel) performPostHookTask(roomId string) {
func (m *BreakoutRoomModel) proceedToEndBkRoom(bkRoomId, parentRoomId string) {
ok, msg := m.rm.EndRoom(&plugnmeet.RoomEndReq{RoomId: bkRoomId})
if !ok {
log.Errorln(msg)
}

err := m.natsService.DeleteBreakoutRoom(parentRoomId, bkRoomId)
if err != nil {
log.Errorln(err)
}

m.onAfterBkRoomEnded(parentRoomId)
// notify to the room for updating list
_ = m.natsService.BroadcastSystemEventToRoom(plugnmeet.NatsMsgServerToClientEvents_BREAKOUT_ROOM_ENDED, parentRoomId, bkRoomId, nil)
}

func (m *BreakoutRoomModel) onAfterBkRoomEnded(roomId string) {
if c, err := m.natsService.CountBreakoutRooms(roomId); err == nil && c == 0 {
// no room left so, delete breakoutRoomKey key for this room
m.natsService.DeleteAllBreakoutRoomsByParentRoomId(roomId)
Expand Down Expand Up @@ -78,7 +93,7 @@ func (m *BreakoutRoomModel) PostTaskAfterRoomEndWebhook(roomId, metadata string)

if meta.IsBreakoutRoom {
_ = m.natsService.DeleteBreakoutRoom(meta.ParentRoomId, roomId)
m.performPostHookTask(meta.ParentRoomId)
m.onAfterBkRoomEnded(meta.ParentRoomId)
} else {
err = m.EndAllBreakoutRoomsByParentRoomId(roomId)
if err != nil {
Expand Down
33 changes: 28 additions & 5 deletions pkg/services/nats/bk_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func (s *NatsService) DeleteBreakoutRoom(parentRoomId, bkRoomId string) error {
return err
}

_ = kv.Purge(s.ctx, bkRoomId)
return nil
return kv.Purge(s.ctx, bkRoomId)
}

func (s *NatsService) GetBreakoutRoom(parentRoomId, bkRoomId string) ([]byte, error) {
Expand Down Expand Up @@ -71,12 +70,14 @@ func (s *NatsService) CountBreakoutRooms(parentRoomId string) (int64, error) {
}

keys, err := kv.ListKeys(s.ctx)
defer keys.Stop()
if err != nil {
return 0, err
}

return int64(len(keys.Keys())), nil
var count int64
for range keys.Keys() {
count++
}
return count, nil
}

func (s *NatsService) GetAllBreakoutRoomsByParentRoomId(parentRoomId string) (map[string][]byte, error) {
Expand Down Expand Up @@ -106,3 +107,25 @@ func (s *NatsService) GetAllBreakoutRoomsByParentRoomId(parentRoomId string) (ma
func (s *NatsService) DeleteAllBreakoutRoomsByParentRoomId(parentRoomId string) {
_ = s.js.DeleteKeyValue(s.ctx, fmt.Sprintf(breakoutRoomBucket, parentRoomId))
}

func (s *NatsService) GetBreakoutRoomIdsByParentRoomId(parentRoomId string) ([]string, error) {
kv, err := s.js.KeyValue(s.ctx, fmt.Sprintf(breakoutRoomBucket, parentRoomId))
switch {
case errors.Is(err, jetstream.ErrBucketNotFound):
return nil, nil
case err != nil:
return nil, err
}

keys, err := kv.ListKeys(s.ctx)
if err != nil {
return nil, err
}

var ids []string
for k := range keys.Keys() {
ids = append(ids, k)
}

return ids, nil
}
8 changes: 6 additions & 2 deletions pkg/services/nats/etherpad.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ func (s *NatsService) GetEtherpadActiveRoomsNum(nodeId string) (int64, error) {
}

keys, err := kv.ListKeys(s.ctx)
defer keys.Stop()
if err != nil {
return 0, err
}

return int64(len(keys.Keys())), nil
var count int64
for range keys.Keys() {
count++
}

return count, nil
}

func (s *NatsService) RemoveRoomFromEtherpad(nodeId, roomId string) error {
Expand Down

0 comments on commit c6614c1

Please sign in to comment.