-
Notifications
You must be signed in to change notification settings - Fork 41
/
course.go
213 lines (195 loc) · 6.32 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
package web
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"github.com/TUM-Dev/gocast/model"
"github.com/TUM-Dev/gocast/tools"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type editCourseByTokenPageData struct {
Token string
Course model.Course
IndexData IndexData
}
func (r mainRoutes) editCourseByTokenPage(c *gin.Context) {
err := c.Request.ParseForm()
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
token := c.Request.Form.Get("token")
if token == "" {
_ = c.AbortWithError(http.StatusForbidden, tools.RequestError{
Status: http.StatusForbidden,
CustomMessage: "please provide a token",
Err: fmt.Errorf("token is empty"),
})
return
}
indexData := NewIndexDataWithContext(c)
course, err := r.CoursesDao.GetCourseByToken(token)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "Course not found"})
return
}
d := editCourseByTokenPageData{
Token: c.Request.Form.Get("token"),
Course: course,
IndexData: indexData,
}
err = templateExecutor.ExecuteTemplate(c.Writer, "edit-course-by-token.gohtml", d)
if err != nil {
logger.Error("Error executing template edit-course-by-token.gohtml", "err", err)
}
}
type OptOutPageData struct {
IndexData IndexData
Course *model.Course
}
func (r mainRoutes) optOutPage(c *gin.Context) {
err := c.Request.ParseForm()
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "Could not read your request.",
Err: err,
})
}
token := c.Request.Form.Get("token")
if token == "" {
_ = c.AbortWithError(http.StatusForbidden, tools.RequestError{
Status: http.StatusForbidden,
CustomMessage: "please provide a token",
Err: fmt.Errorf("token is empty"),
})
return
}
var d OptOutPageData
d.IndexData = NewIndexData()
course, err := r.CoursesDao.GetCourseByToken(token)
if err != nil {
d.Course = nil
} else {
d.Course = &course
}
err = templateExecutor.ExecuteTemplate(c.Writer, "opt-out.gohtml", d)
if err != nil {
logger.Error("can't render template", "err", err)
}
}
func (r mainRoutes) HighlightPage(c *gin.Context) {
course, err := r.CoursesDao.GetCourseByShortLink(c.Param("shortLink"))
if err != nil {
tools.RenderErrorPage(c, http.StatusNotFound, tools.PageNotFoundErrMsg)
return
}
indexData := NewIndexData()
var tumLiveContext tools.TUMLiveContext
tumLiveContextQueried, found := c.Get("TUMLiveContext")
if found {
tumLiveContext = tumLiveContextQueried.(tools.TUMLiveContext)
indexData.TUMLiveContext = tumLiveContext
} else {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
indexData.TUMLiveContext.Course = &course
s, err := r.CoursesDao.GetCurrentOrNextLectureForCourse(c, course.ID)
if err == nil {
indexData.TUMLiveContext.Stream = &s
} else if err == gorm.ErrRecordNotFound {
c.Redirect(http.StatusFound, fmt.Sprintf("/course/%d/%s/%s", course.Year, course.TeachingTerm, course.Slug))
return
} else {
sentry.CaptureException(err)
logger.Error("Error getting current or next lecture for course", "err", err)
}
description := ""
if indexData.TUMLiveContext.Stream != nil {
description = indexData.TUMLiveContext.Stream.GetDescriptionHTML()
}
_ = CoursePageData{IndexData: indexData, HighlightPage: true}
d2 := WatchPageData{
IndexData: indexData,
Description: template.HTML(description),
Version: "",
IsHighlightPage: true,
}
if err = templateExecutor.ExecuteTemplate(c.Writer, "watch.gohtml", d2); err != nil {
logger.Error("Error executing template watch.gohtml", "err", err)
return
}
}
func (r mainRoutes) CoursePage(c *gin.Context) {
indexData := NewIndexData()
var tumLiveContext tools.TUMLiveContext
tumLiveContextQueried, found := c.Get("TUMLiveContext")
if found {
tumLiveContext = tumLiveContextQueried.(tools.TUMLiveContext)
indexData.TUMLiveContext = tumLiveContext
} else {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
// When a user is not logged-in, we don't need the progress data for watch page since
// it is only saved for logged-in users.
if tumLiveContext.User == nil {
err := templateExecutor.ExecuteTemplate(c.Writer, "course-overview.gohtml",
CoursePageData{IndexData: indexData, Course: *tumLiveContext.Course})
if err != nil {
sentrygin.GetHubFromContext(c).CaptureException(err)
}
return
}
streamsWithWatchState, err := r.StreamsDao.GetStreamsWithWatchState((*tumLiveContext.Course).ID, (*tumLiveContext.User).ID)
if err != nil {
sentry.CaptureException(err)
logger.Error("loading streamsWithWatchState and progresses for a given course and user failed", "err", err)
}
tumLiveContext.Course.Streams = streamsWithWatchState // Update the course streams to contain the watch state.
for i := range tumLiveContext.Course.Streams {
err = tools.SetSignedPlaylists(&tumLiveContext.Course.Streams[i], tumLiveContext.User, false)
if err != nil {
logger.Warn("Can't sign playlists", "err", err)
}
}
// watchedStateData is used by the client to track the which VoDs are watched.
type watchedStateData struct {
ID uint `json:"streamID"`
Month string `json:"month"`
Watched bool `json:"watched"`
Recording bool `json:"recording"`
}
clientWatchState := make([]watchedStateData, 0)
for _, s := range streamsWithWatchState {
clientWatchState = append(clientWatchState, watchedStateData{
ID: s.Model.ID,
Month: s.Start.Month().String(),
Watched: s.Watched,
Recording: s.Recording,
})
}
// Create JSON encoded info about which streamsWithWatchState are watched. Used by the client to track the watched status.
encoded, err := json.Marshal(clientWatchState)
if err != nil {
sentry.CaptureException(err)
logger.Error("marshalling watched infos for client failed", "err", err)
}
err = templateExecutor.ExecuteTemplate(c.Writer, "course-overview.gohtml",
CoursePageData{IndexData: indexData, Course: *tumLiveContext.Course, WatchedData: string(encoded)})
if err != nil {
sentrygin.GetHubFromContext(c).CaptureException(err)
}
}
// CoursePageData is the data for the course page.
type CoursePageData struct {
IndexData IndexData
Course model.Course
HighlightPage bool
WatchedData string
}