-
Notifications
You must be signed in to change notification settings - Fork 41
/
course.go
executable file
·337 lines (296 loc) · 9.48 KB
/
course.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package model
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// SourceMode 0 -> COMB, 1-> PRES, 2 -> CAM
type SourceMode int
type Course struct {
gorm.Model
UserID uint `gorm:"not null"` // Owner of the course
Name string `gorm:"not null"`
Slug string `gorm:"not null"` // eg. eidi
Year int `gorm:"not null"` // eg. 2021
TeachingTerm string `gorm:"not null"` // eg. Summer/Winter
TUMOnlineIdentifier string
VODEnabled bool `gorm:"default:true"`
DownloadsEnabled bool `gorm:"default:false"`
ChatEnabled bool `gorm:"default:false"`
AnonymousChatEnabled bool `gorm:"not null;default:true"`
ModeratedChatEnabled bool `gorm:"not null;default:false"`
VodChatEnabled bool
Visibility string `gorm:"default:loggedin"` // public, loggedin or enrolled
Streams []Stream
Users []User `gorm:"many2many:course_users;"`
Admins []User `gorm:"many2many:course_admins;"`
Token string
UserCreatedByToken bool `gorm:"default:false"`
CameraPresetPreferences string // json encoded. e.g. [{lectureHallID:1, presetID:4}, ...]
SourcePreferences string // json encoded. e.g. [{lectureHallID:1, sourceMode:0}, ...]
Pinned bool `gorm:"-"` // Used to determine if the course is pinned when loaded for a specific user.
LivePrivate bool `gorm:"not null; default:false"` // whether Livestreams are private
VodPrivate bool `gorm:"not null; default:false"` // Whether VODs are made private after livestreams
}
type CourseDTO struct {
ID uint
Name string
Slug string
Visibility string
TeachingTerm string
Year int
DownloadsEnabled bool
NextLecture StreamDTO
LastRecording StreamDTO
Streams []StreamDTO
IsAdmin bool // Set in API handler
}
func (c *Course) ToDTO() CourseDTO {
return CourseDTO{
ID: c.ID,
Name: c.Name,
Slug: c.Slug,
Visibility: c.Visibility,
TeachingTerm: c.TeachingTerm,
Year: c.Year,
DownloadsEnabled: c.DownloadsEnabled,
NextLecture: c.GetNextLecture().ToDTO(),
LastRecording: c.GetLastRecording().ToDTO(),
IsAdmin: false,
}
}
// GetUrl returns the URL of the course, e.g. /course/2022/S/MyCourse
func (c Course) GetUrl() string {
return fmt.Sprintf("/course/%d/%s/%s", c.Year, c.TeachingTerm, c.Slug)
}
// GetStreamUrl returns the URL of the stream, e.g. /w/MyStream/42
func (c Course) GetStreamUrl(stream Stream) string {
return fmt.Sprintf("/w/%s/%d", c.Slug, stream.ID)
}
type CameraPresetPreference struct {
LectureHallID uint `json:"lecture_hall_id"`
PresetID int `json:"preset_id"`
}
// GetCameraPresetPreference retrieves the camera preset preferences
func (c Course) GetCameraPresetPreference() []CameraPresetPreference {
var res []CameraPresetPreference
err := json.Unmarshal([]byte(c.CameraPresetPreferences), &res)
if err != nil {
return []CameraPresetPreference{}
}
return res
}
// SetCameraPresetPreference updates the camera preset preferences
func (c *Course) SetCameraPresetPreference(pref []CameraPresetPreference) {
pBytes, err := json.Marshal(pref)
if err != nil {
log.Println(err)
}
c.CameraPresetPreferences = string(pBytes)
}
type SourcePreference struct {
LectureHallID uint `json:"lecture_hall_id"`
SourceMode SourceMode `json:"source_mode"`
}
// GetSourcePreference retrieves the source preferences
func (c Course) GetSourcePreference() []SourcePreference {
var res []SourcePreference
err := json.Unmarshal([]byte(c.SourcePreferences), &res)
if err != nil {
return []SourcePreference{}
}
return res
}
// GetSourceModeForLectureHall retrieves the source preference for the given lecture hall, returns default SourcePreference if non-existing
func (c Course) GetSourceModeForLectureHall(id uint) SourceMode {
for _, preference := range c.GetSourcePreference() {
if preference.LectureHallID == id {
return preference.SourceMode
}
}
return 0
}
// CanUseSource returns whether the specified source type is allowed for the lecture hall id given
func (c Course) CanUseSource(lectureHallID uint, sourceType string) bool {
mode := c.GetSourceModeForLectureHall(lectureHallID)
switch sourceType {
case "PRES":
return mode != 2
case "CAM":
return mode != 1
case "COMB":
return mode != 1 && mode != 2
}
return true
}
// SetSourcePreference updates the source preferences
func (c *Course) SetSourcePreference(pref []SourcePreference) {
pBytes, err := json.Marshal(pref)
if err != nil {
logger.Error("Could not marshal source preference", "err", err)
return
}
c.SourcePreferences = string(pBytes)
}
// CompareTo used for sorting. Falling back to old java habits...
func (c Course) CompareTo(other Course) bool {
if !other.HasNextLecture() {
return true
}
return c.GetNextLectureDate().Before(other.GetNextLectureDate())
}
// IsLive returns whether the course has a lecture that is live
func (c Course) IsLive() bool {
for _, s := range c.Streams {
if s.LiveNow {
return true
}
}
return false
}
// IsNextLectureStartingSoon checks whether the course has a lecture that starts soon
func (c Course) IsNextLectureStartingSoon() bool {
for _, s := range c.Streams {
if s.IsComingUp() {
return true
}
}
return false
}
// NumStreams returns the number of streams for the course that are VoDs or live
func (c Course) NumStreams() int {
res := 0
for i := range c.Streams {
if c.Streams[i].Recording || c.Streams[i].LiveNow {
res++
}
}
return res
}
func (c Course) StreamTimes() []string {
streamTimes := make([]string, len(c.Streams))
for i, s := range c.Streams {
streamTimes[i] = s.Start.In(time.UTC).Format("2006-01-02T15:04:05") + ".000Z"
}
return streamTimes
}
// HasRecordings returns whether the course has any recordings.
func (c Course) HasRecordings() bool {
for i := range c.Streams {
if c.Streams[i].Recording {
return true
}
}
return false
}
// NumUsers returns the number of users enrolled in the course
func (c Course) NumUsers() int {
return len(c.Users)
}
// NextLectureHasReachedTimeSlot returns whether the courses next lecture arrived at its timeslot
func (c Course) NextLectureHasReachedTimeSlot() bool {
return c.GetNextLecture().TimeSlotReached()
}
// GetNextLecture returns the next lecture of the course
func (c Course) GetNextLecture() Stream {
var earliestLecture Stream
earliestLectureDate := time.Now().Add(time.Hour * 24 * 365 * 10) // 10 years from now.
for _, s := range c.Streams {
if s.Start.Before(earliestLectureDate) && s.End.After(time.Now()) {
earliestLectureDate = s.Start
earliestLecture = s
}
}
return earliestLecture
}
// GetLastRecording returns the most recent lecture of the course
// Assumes an ascending order of c.Streams
func (c Course) GetLastRecording() Stream {
var lastLecture Stream
now := time.Now()
for _, s := range c.Streams {
if s.Start.After(now) {
return lastLecture
}
if s.Recording {
lastLecture = s
}
}
return lastLecture
}
// GetLiveStreams returns the current live streams of the course or an empty slice if none are live
func (c Course) GetLiveStreams() []Stream {
var res []Stream
for _, s := range c.Streams {
if s.LiveNow {
res = append(res, s)
}
}
return res
}
// GetNextLectureDate returns the next lecture date of the course
func (c Course) GetNextLectureDate() time.Time {
// TODO: Refactor this with IsNextLectureSelfStream when the sorting error fixed
earliestLectureDate := time.Now().Add(time.Hour * 24 * 365 * 10) // 10 years from now.
for _, s := range c.Streams {
if s.Start.Before(earliestLectureDate) && s.End.After(time.Now()) {
earliestLectureDate = s.Start
}
}
return earliestLectureDate
}
// IsNextLectureSelfStream checks whether the next lecture is a self stream
func (c Course) IsNextLectureSelfStream() bool {
return c.GetNextLecture().IsSelfStream()
}
// GetNextLectureDateFormatted returns a JavaScript friendly formatted date string
func (c Course) GetNextLectureDateFormatted() string {
return c.GetNextLectureDate().Format("2006-01-02 15:04:05")
}
// HasNextLecture checks whether there is another upcoming lecture
func (c Course) HasNextLecture() bool {
n := time.Now()
for _, s := range c.Streams {
if s.Start.After(n) {
return true
}
}
return false
}
// HasStreams checks whether the lecture has any streams (recorded, live or upcoming) associated to it
func (c Course) HasStreams() bool {
return len(c.Streams) > 0
}
// GetRecordings returns all recording of this course as streams
func (c Course) GetRecordings() []Stream {
var recordings []Stream
for _, s := range c.Streams {
if s.Recording {
recordings = append(recordings, s)
}
}
return recordings
}
// IsHidden returns true if visibility is set to 'hidden' and false if not
func (c Course) IsHidden() bool {
return c.Visibility == "hidden"
}
// IsLoggedIn returns true if visibility is set to 'loggedin' and false if not
func (c Course) IsLoggedIn() bool {
return c.Visibility == "loggedin"
}
// IsEnrolled returns true if visibility is set to 'enrolled' and false if not
func (c Course) IsEnrolled() bool {
return c.Visibility == "enrolled"
}
// AdminJson is the JSON representation of a courses streams for the admin panel
func (c Course) AdminJson(lhs []LectureHall) []gin.H {
var res []gin.H
for _, s := range c.Streams {
res = append(res, s.getJson(lhs, c))
}
return res
}