-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathparser.go
More file actions
196 lines (172 loc) · 6.24 KB
/
parser.go
File metadata and controls
196 lines (172 loc) · 6.24 KB
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
// Package helpers provides helper functions for parsing models to protobuf representations.
package helpers
import (
"time"
protobuf "github.com/TUM-Dev/gocast/apiv2/protobuf/server"
"github.com/TUM-Dev/gocast/model"
"google.golang.org/protobuf/types/known/timestamppb"
)
// ParseUserToProto converts a User model to its protobuf representation.
func ParseUserToProto(u *model.User) *protobuf.User {
user := &protobuf.User{
Id: uint32(u.ID),
Name: u.Name,
Email: u.Email.String,
MatriculationNumber: u.MatriculationNumber,
LrzId: u.LrzID,
Role: uint32(u.Role),
Settings: []*protobuf.UserSetting{},
}
if u.LastName != nil {
user.LastName = *u.LastName
}
for _, setting := range u.Settings {
user.Settings = append(user.Settings, ParseUserSettingToProto(setting))
}
return user
}
// ParseUserSettingToProto converts a UserSetting model to its protobuf representation.
func ParseUserSettingToProto(setting model.UserSetting) *protobuf.UserSetting {
return &protobuf.UserSetting{
Type: protobuf.UserSettingType(setting.Type - 1),
Value: setting.Value,
}
}
// ParseBookmarkToProto converts a Bookmark model to its protobuf representation.
func ParseBookmarkToProto(b model.Bookmark) *protobuf.Bookmark {
return &protobuf.Bookmark{
BookmarkId: uint32(b.ID),
Description: b.Description,
Hours: uint32(b.Hours),
Minutes: uint32(b.Minutes),
Seconds: uint32(b.Seconds),
UserId: uint32(b.UserID),
StreamId: uint32(b.StreamID),
}
}
// ParseCourseToProto converts a Course model to its protobuf representation.
func ParseCourseToProto(c model.Course, u *model.User) *protobuf.Course {
lastRecordingID := c.GetLastRecording(u).ID
nextLectureID := c.GetNextLecture(u).ID
return &protobuf.Course{
Id: uint32(c.ID),
Name: c.Name,
Slug: c.Slug,
Semester: &protobuf.Semester{
Year: uint32(c.Year),
TeachingTerm: c.TeachingTerm,
},
TumOnlineIdentifier: c.TUMOnlineIdentifier,
VodEnabled: c.VODEnabled,
DownloadsEnabled: c.DownloadsEnabled,
ChatEnabled: c.ChatEnabled,
AnonymousChatEnabled: c.AnonymousChatEnabled,
ModeratedChatEnabled: c.ModeratedChatEnabled,
VodChatEnabled: c.VodChatEnabled,
CameraPresetPreferences: c.CameraPresetPreferences,
SourcePreferences: c.SourcePreferences,
LastRecordingId: uint32(lastRecordingID),
NextLectureId: uint32(nextLectureID),
}
}
// ParseSemesterToProto converts a Semester model to its protobuf representation.
func ParseSemesterToProto(semester model.Semester) *protobuf.Semester {
return &protobuf.Semester{
Year: uint32(semester.Year),
TeachingTerm: semester.TeachingTerm,
}
}
// ParseStreamToProto converts a Stream model to its protobuf representation.
// It returns an error if the conversion of timestamps fails.
func ParseStreamToProto(stream model.Stream, downloads []model.DownloadableVod) *protobuf.Stream {
liveNow := stream.LiveNowTimestamp.After(time.Now())
s := &protobuf.Stream{
Id: uint32(stream.ID),
Name: stream.Name,
Description: stream.Description,
CourseId: uint32(stream.CourseID),
Start: timestamppb.New(stream.Start),
End: timestamppb.New(stream.End),
ChatEnabled: stream.ChatEnabled,
RoomName: stream.RoomName,
RoomCode: stream.RoomCode,
EventTypeName: stream.EventTypeName,
TumOnlineEventId: uint32(stream.TUMOnlineEventID),
SeriesIdentifier: stream.SeriesIdentifier,
PlaylistUrl: stream.PlaylistUrl,
PlaylistUrlPres: stream.PlaylistUrlPRES,
PlaylistUrlCam: stream.PlaylistUrlCAM,
LiveNow: liveNow,
LiveNowTimestamp: timestamppb.New(stream.LiveNowTimestamp),
Recording: stream.Recording,
Premiere: stream.Premiere,
Ended: stream.Ended,
VodViews: uint32(stream.VodViews),
StartOffset: uint32(stream.StartOffset),
EndOffset: uint32(stream.EndOffset),
IsPlanned: stream.IsPlanned(),
IsComingUp: stream.IsComingUp(),
HlsUrl: stream.HLSUrl(),
}
if stream.Duration.Valid {
s.Duration = uint32(stream.Duration.Int32)
}
for _, download := range downloads {
s.Downloads = append(s.Downloads, ParseDownloadToProto(download))
}
if len(stream.CustomLectureTitles) > 0 {
s.CustomLectureTitle = stream.CustomLectureTitles[0].Title
}
return s
}
func ParseLectureHallToProto(lh *model.LectureHall) *protobuf.LectureHall {
return &protobuf.LectureHall{
Id: uint32(lh.ID),
Name: lh.Name,
}
}
func ParseDownloadToProto(download model.DownloadableVod) *protobuf.Download {
return &protobuf.Download{
FriendlyName: download.FriendlyName,
DownloadUrl: download.DownloadURL,
}
}
func ParseNotificationToProto(notification model.Notification) *protobuf.UserGroupNotification {
var title string
if notification.Title != nil {
title = *notification.Title
}
return &protobuf.UserGroupNotification{
Title: title,
Body: notification.Body,
Target: protobuf.NotificationTarget(notification.Target),
CreatedAt: timestamppb.New(notification.CreatedAt),
}
}
func ParseServerNotificationToProto(notification model.ServerNotification) *protobuf.ServerNotification {
return &protobuf.ServerNotification{
Text: notification.Text,
Warn: notification.Warn,
Start: timestamppb.New(notification.Start),
Expires: timestamppb.New(notification.Expires),
}
}
// ParseStreamProgressToProto converts a StreamProgress model to its protobuf representation.
func ParseStreamProgressToProto(progress model.StreamProgress) *protobuf.StreamProgress {
return &protobuf.StreamProgress{
StreamId: uint32(progress.StreamID),
Progress: float32(progress.Progress),
Watched: progress.Watched,
}
}
// ParseVideoSectionToProto converts a VideoSection model to its protobuf representation.
func ParseVideoSectionToProto(section model.VideoSection) *protobuf.VideoSection {
return &protobuf.VideoSection{
Description: section.Description,
StartHours: uint32(section.StartHours),
StartMinutes: uint32(section.StartMinutes),
StartSeconds: uint32(section.StartSeconds),
StreamId: uint32(section.StreamID),
FileId: uint32(section.FileID),
}
}