-
Notifications
You must be signed in to change notification settings - Fork 63
/
room_duration.go
162 lines (139 loc) · 3.94 KB
/
room_duration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package models
import (
"context"
"errors"
"fmt"
"github.com/mynaparrot/plugnmeet-server/pkg/config"
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
"reflect"
"strings"
"time"
)
const (
roomWithDurationInfoKey = "pnm:roomWithDurationInfo"
)
type RoomDurationModel struct {
rc *redis.Client
ctx context.Context
}
type RoomDurationInfo struct {
Duration uint64 `redis:"duration"`
StartedAt uint64 `redis:"startedAt"`
}
func NewRoomDurationModel() *RoomDurationModel {
return &RoomDurationModel{
rc: config.AppCnf.RDS,
ctx: context.Background(),
}
}
func (m *RoomDurationModel) AddRoomWithDurationInfo(roomId string, r RoomDurationInfo) error {
_, err := m.rc.HSet(m.ctx, fmt.Sprintf("%s:%s", roomWithDurationInfoKey, roomId), r).Result()
if err != nil {
log.Error(err)
return err
}
return nil
}
func (m *RoomDurationModel) DeleteRoomWithDuration(roomId string) error {
_, err := m.rc.Del(m.ctx, fmt.Sprintf("%s:%s", roomWithDurationInfoKey, roomId)).Result()
if err != nil {
return err
}
return nil
}
func (m *RoomDurationModel) IncreaseRoomDuration(roomId string, duration uint64) (uint64, error) {
tm := &RoomDurationInfo{}
field, ok := reflect.TypeOf(tm).Elem().FieldByName("Duration")
if !ok {
return 0, nil
}
durationField := field.Tag.Get("redis")
info, err := m.GetRoomDurationInfo(roomId)
if err != nil {
return 0, err
}
// increase room duration
roomService := NewRoomService()
_, meta, err := roomService.LoadRoomWithMetadata(roomId)
if err != nil {
return 0, err
}
// check if this is a breakout room
if meta.IsBreakoutRoom && info != nil {
// need to check how long time left for this room
now := uint64(time.Now().Unix())
valid := info.StartedAt + (info.Duration * 60)
d := ((valid - now) / 60) + duration
// we'll need to make sure that breakout room duration isn't bigger than main room duration
err = m.CompareDurationWithParentRoom(meta.ParentRoomId, d)
if err != nil {
return 0, err
}
}
result, err := m.rc.HIncrBy(m.ctx, fmt.Sprintf("%s:%s", roomWithDurationInfoKey, roomId), durationField, int64(duration)).Result()
if err != nil {
return 0, err
}
d := uint64(result)
meta.RoomFeatures.RoomDuration = &d
_, err = roomService.UpdateRoomMetadataByStruct(roomId, meta)
if err != nil {
// if error then we'll fall back to set previous duration
m.setRoomDuration(roomId, durationField, d-duration)
return 0, err
}
return d, nil
}
func (m *RoomDurationModel) CompareDurationWithParentRoom(mainRoomId string, duration uint64) error {
info, err := m.GetRoomDurationInfo(mainRoomId)
if err != nil {
return err
}
if info == nil {
// this is indicating that the no info found
return nil
}
now := uint64(time.Now().Unix())
valid := info.StartedAt + (info.Duration * 60)
left := (valid - now) / 60
if left < duration {
return errors.New("breakout room's duration can't be more than parent room's duration")
}
return nil
}
func (m *RoomDurationModel) setRoomDuration(roomId, durationField string, val uint64) {
_, err := m.rc.HSet(m.ctx, fmt.Sprintf("%s:%s", roomWithDurationInfoKey, roomId), durationField, val).Result()
if err != nil {
log.Error(err)
}
}
func (m *RoomDurationModel) GetRoomDurationInfo(roomId string) (*RoomDurationInfo, error) {
val := new(RoomDurationInfo)
err := m.rc.HGetAll(m.ctx, fmt.Sprintf("%s:%s", roomWithDurationInfoKey, roomId)).Scan(val)
switch {
case err == redis.Nil:
return nil, nil
case err != nil:
return nil, err
}
return val, nil
}
func (m *RoomDurationModel) GetRoomsWithDurationMap() map[string]RoomDurationInfo {
roomsKey, err := m.rc.Keys(m.ctx, "pnm:roomWithDurationInfo:*").Result()
if err != nil {
return nil
}
out := make(map[string]RoomDurationInfo)
for _, key := range roomsKey {
var val RoomDurationInfo
err = m.rc.HGetAll(m.ctx, key).Scan(&val)
if err != nil {
log.Errorln(err)
continue
}
rId := strings.Replace(key, roomWithDurationInfoKey+":", "", 1)
out[rId] = val
}
return out
}