-
Notifications
You must be signed in to change notification settings - Fork 63
/
data_message.go
223 lines (189 loc) · 5.37 KB
/
data_message.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package models
import (
"database/sql"
"errors"
"github.com/google/uuid"
"github.com/livekit/protocol/livekit"
"github.com/mynaparrot/plugnmeet-protocol/plugnmeet"
"github.com/mynaparrot/plugnmeet-server/pkg/config"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"time"
)
type DataMessageReq struct {
Sid string `json:"sid"`
RoomId string `json:"room_id"`
MsgType string `json:"msg_type"`
Msg string `json:"msg"`
RequestedUserId string `json:"-"`
SendTo []string // user sids
IsAdmin bool
db *sql.DB
roomService *RoomService
msgBodyType plugnmeet.DataMsgBodyType
}
type ReqFrom struct {
Sid string `json:"sid"`
UserId string `json:"userId"`
Name string `json:"name"`
}
type DataMessageModel struct {
db *sql.DB
roomService *RoomService
analyticsModel *AnalyticsModel
}
func NewDataMessageModel() *DataMessageModel {
return &DataMessageModel{
db: config.AppCnf.DB,
roomService: NewRoomService(),
analyticsModel: NewAnalyticsModel(),
}
}
func (m *DataMessageModel) SendDataMessage(r *plugnmeet.DataMessageReq) error {
switch r.MsgBodyType {
case plugnmeet.DataMsgBodyType_RAISE_HAND:
return m.raiseHand(r)
case plugnmeet.DataMsgBodyType_LOWER_HAND:
return m.lowerHand(r)
case plugnmeet.DataMsgBodyType_OTHER_USER_LOWER_HAND:
return m.otherUserLowerHand(r)
case plugnmeet.DataMsgBodyType_INFO,
plugnmeet.DataMsgBodyType_ALERT,
plugnmeet.DataMsgBodyType_AZURE_COGNITIVE_SERVICE_SPEECH_TOKEN:
return m.sendNotification(r)
default:
return errors.New(r.MsgBodyType.String() + " yet not ready")
}
}
func (m *DataMessageModel) deliverMsg(roomId string, destinationUserIds []string, msg *plugnmeet.DataMessage) error {
data, err := proto.Marshal(msg)
if err != nil {
log.Errorln(err)
return err
}
// send as push message
_, err = m.roomService.SendData(roomId, data, livekit.DataPacket_RELIABLE, destinationUserIds)
if err != nil {
log.Errorln(err)
return err
}
return nil
}
func (m *DataMessageModel) raiseHand(r *plugnmeet.DataMessageReq) error {
participants, _ := m.roomService.LoadParticipants(r.RoomId)
var ids []string
for _, participant := range participants {
meta, err := m.roomService.UnmarshalParticipantMetadata(participant.Metadata)
if err != nil {
continue
}
if meta.IsAdmin && (r.RequestedUserId != participant.Identity) {
ids = append(ids, participant.Identity)
}
}
reqPar, metadata, _ := m.roomService.LoadParticipantWithMetadata(r.RoomId, r.RequestedUserId)
// now update user's metadata
metadata.RaisedHand = true
_, err := m.roomService.UpdateParticipantMetadataByStruct(r.RoomId, r.RequestedUserId, metadata)
if err != nil {
return err
}
if metadata.RaisedHand {
m.analyticsModel.HandleEvent(&plugnmeet.AnalyticsDataMsg{
EventType: plugnmeet.AnalyticsEventType_ANALYTICS_EVENT_TYPE_USER,
EventName: plugnmeet.AnalyticsEvents_ANALYTICS_EVENT_USER_RAISE_HAND,
RoomId: r.RoomId,
UserId: &r.RequestedUserId,
})
}
if len(ids) == 0 {
return nil
}
mId := uuid.NewString()
tm := time.Now().UTC().Format(time.RFC1123Z)
msg := &plugnmeet.DataMessage{
Type: plugnmeet.DataMsgType_SYSTEM,
MessageId: &mId,
Body: &plugnmeet.DataMsgBody{
Type: plugnmeet.DataMsgBodyType_RAISE_HAND,
Time: &tm,
From: &plugnmeet.DataMsgReqFrom{
Sid: r.UserId,
UserId: reqPar.Identity,
},
Msg: r.Msg,
},
}
// send as push message
err = m.deliverMsg(r.RoomId, ids, msg)
return err
}
func (m *DataMessageModel) lowerHand(r *plugnmeet.DataMessageReq) error {
_, metadata, err := m.roomService.LoadParticipantWithMetadata(r.RoomId, r.RequestedUserId)
if err != nil {
return err
}
// now update user's metadata
metadata.RaisedHand = false
_, err = m.roomService.UpdateParticipantMetadataByStruct(r.RoomId, r.RequestedUserId, metadata)
if err != nil {
return err
}
return nil
}
func (m *DataMessageModel) otherUserLowerHand(r *plugnmeet.DataMessageReq) error {
if !r.IsAdmin {
return errors.New("only allow for admin")
}
userId := r.Msg
_, metadata, err := m.roomService.LoadParticipantWithMetadata(r.RoomId, userId)
if err != nil {
return err
}
// now update user's metadata
metadata.RaisedHand = false
_, err = m.roomService.UpdateParticipantMetadataByStruct(r.RoomId, userId, metadata)
if err != nil {
return err
}
return nil
}
func (m *DataMessageModel) sendNotification(r *plugnmeet.DataMessageReq) error {
mId := uuid.NewString()
tm := time.Now().UTC().Format(time.RFC1123Z)
msg := &plugnmeet.DataMessage{
Type: plugnmeet.DataMsgType_SYSTEM,
MessageId: &mId,
Body: &plugnmeet.DataMsgBody{
Type: r.GetMsgBodyType(),
Time: &tm,
From: &plugnmeet.DataMsgReqFrom{
Sid: r.GetUserSid(),
},
Msg: r.GetMsg(),
},
}
err := m.deliverMsg(r.GetRoomId(), r.GetSendTo(), msg)
if err != nil {
return err
}
return nil
}
func (m *DataMessageModel) SendUpdatedMetadata(roomId, metadata string) error {
mId := uuid.NewString()
tm := time.Now().UTC().Format(time.RFC1123Z)
msg := &plugnmeet.DataMessage{
Type: plugnmeet.DataMsgType_SYSTEM,
MessageId: &mId,
Body: &plugnmeet.DataMsgBody{
Type: plugnmeet.DataMsgBodyType_UPDATE_ROOM_METADATA,
Time: &tm,
From: &plugnmeet.DataMsgReqFrom{
Sid: "system",
},
Msg: metadata,
},
}
err := m.deliverMsg(roomId, []string{}, msg)
return err
}