-
Notifications
You must be signed in to change notification settings - Fork 41
/
live_update.go
127 lines (110 loc) · 3.59 KB
/
live_update.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
package api
import (
"encoding/json"
"errors"
"github.com/RBG-TUM/commons"
"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
"github.com/joschahenningsen/TUM-Live/dao"
"github.com/joschahenningsen/TUM-Live/model"
"github.com/joschahenningsen/TUM-Live/tools"
"github.com/joschahenningsen/TUM-Live/tools/realtime"
"github.com/joschahenningsen/TUM-Live/tools/tum"
log "github.com/sirupsen/logrus"
"sync"
)
const (
LiveUpdateRoomName = "live-update"
UpdateTypeCourseWentLive = "course_went_live"
)
var liveUpdateListenerMutex sync.RWMutex
var liveUpdateListener = map[uint]*liveUpdateUserSessionsWrapper{}
type liveUpdateUserSessionsWrapper struct {
sessions []*realtime.Context
courses []uint
}
func RegisterLiveUpdateRealtimeChannel() {
RealtimeInstance.RegisterChannel(LiveUpdateRoomName, realtime.ChannelHandlers{
OnSubscribe: liveUpdateOnSubscribe,
OnUnsubscribe: liveUpdateOnUnsubscribe,
})
}
func liveUpdateOnUnsubscribe(psc *realtime.Context) {
ctx, _ := psc.Client.Get("ctx") // get gin context
foundContext, exists := ctx.(*gin.Context).Get("TUMLiveContext")
if !exists {
sentry.CaptureException(errors.New("context should exist but doesn't"))
return
}
tumLiveContext := foundContext.(tools.TUMLiveContext)
var userId uint = 0
if tumLiveContext.User != nil {
userId = tumLiveContext.User.ID
}
liveUpdateListenerMutex.Lock()
var newSessions []*realtime.Context
for _, session := range liveUpdateListener[userId].sessions {
if session != psc {
newSessions = append(newSessions, session)
}
}
if len(newSessions) == 0 {
delete(liveUpdateListener, userId)
} else {
liveUpdateListener[userId].sessions = newSessions
}
liveUpdateListenerMutex.Unlock()
}
func liveUpdateOnSubscribe(psc *realtime.Context) {
ctx, _ := psc.Client.Get("ctx") // get gin context
daoWrapper, _ := psc.Client.Get("dao")
foundContext, exists := ctx.(*gin.Context).Get("TUMLiveContext")
if !exists {
sentry.CaptureException(errors.New("context should exist but doesn't"))
return
}
tumLiveContext := foundContext.(tools.TUMLiveContext)
var userCourses []model.Course
var userId uint = 0
var err error
year, term := tum.GetCurrentSemester()
if tumLiveContext.User != nil {
userId = tumLiveContext.User.ID
if userCourses, err = daoWrapper.(dao.DaoWrapper).CoursesDao.GetPublicAndLoggedInCourses(year, term); err != nil {
log.WithError(err).Error("could not fetch public and logged in courses")
return
}
userCourses = commons.Unique(userCourses, func(c model.Course) uint { return c.ID })
} else {
if userCourses, err = daoWrapper.(dao.DaoWrapper).CoursesDao.GetPublicCourses(year, term); err != nil {
log.WithError(err).Error("could not fetch public courses")
return
}
}
var courses []uint
for _, course := range userCourses {
courses = append(courses, course.ID)
}
liveUpdateListenerMutex.Lock()
if liveUpdateListener[userId] != nil {
liveUpdateListener[userId] = &liveUpdateUserSessionsWrapper{append(liveUpdateListener[userId].sessions, psc), courses}
} else {
liveUpdateListener[userId] = &liveUpdateUserSessionsWrapper{[]*realtime.Context{psc}, courses}
}
liveUpdateListenerMutex.Unlock()
}
func NotifyLiveUpdateCourseWentLive(courseId uint) {
updateMessage, _ := json.Marshal(gin.H{"type": UpdateTypeCourseWentLive, "data": gin.H{"courseId": courseId}})
liveUpdateListenerMutex.Lock()
for _, userWrap := range liveUpdateListener {
for _, course := range userWrap.courses {
if course == courseId {
for _, session := range userWrap.sessions {
_ = session.Send(updateMessage)
}
break
}
}
}
liveUpdateListenerMutex.Unlock()
}