diff --git a/api/courses.go b/api/courses.go index b90721195..d9d1bafb4 100644 --- a/api/courses.go +++ b/api/courses.go @@ -140,6 +140,7 @@ func (r coursesRoutes) getLive(c *gin.Context) { livestreams := make([]CourseStream, 0) + user := tumLiveContext.User for _, stream := range streams { courseForLiveStream, _ := r.GetCourseById(context.Background(), stream.CourseID) @@ -179,7 +180,7 @@ func (r coursesRoutes) getLive(c *gin.Context) { } livestreams = append(livestreams, CourseStream{ - Course: courseForLiveStream.ToDTO(), + Course: courseForLiveStream.ToDTO(user), Stream: stream.ToDTO(), LectureHall: lectureHall.ToDTO(), Viewers: viewers, @@ -217,9 +218,10 @@ func (r coursesRoutes) getPublic(c *gin.Context) { courses = commons.Unique(public, func(c model.Course) uint { return c.ID }) } + user := tumLiveContext.User resp := make([]model.CourseDTO, len(courses)) for i, course := range courses { - resp[i] = course.ToDTO() + resp[i] = course.ToDTO(user) } c.JSON(http.StatusOK, resp) @@ -258,10 +260,12 @@ func (r coursesRoutes) getUsers(c *gin.Context) { sortCourses(courses) courses = commons.Unique(courses, func(c model.Course) uint { return c.ID }) + + user := tumLiveContext.User resp := make([]model.CourseDTO, 0, len(courses)) for _, course := range courses { if !course.IsHidden() { - resp = append(resp, course.ToDTO()) + resp = append(resp, course.ToDTO(user)) } } @@ -279,10 +283,11 @@ func (r coursesRoutes) getPinned(c *gin.Context) { } pinnedCourses = commons.Unique(pinnedCourses, func(c model.Course) uint { return c.ID }) + user := tumLiveContext.User resp := make([]model.CourseDTO, 0, len(pinnedCourses)) for _, course := range pinnedCourses { if !course.IsHidden() { - resp = append(resp, course.ToDTO()) + resp = append(resp, course.ToDTO(user)) } } @@ -391,7 +396,7 @@ func (r coursesRoutes) getCourseBySlug(c *gin.Context) { } } - courseDTO := course.ToDTO() + courseDTO := course.ToDTO(tumLiveContext.User) courseDTO.Streams = streamsDTO courseDTO.IsAdmin = isAdmin diff --git a/model/course.go b/model/course.go index 5514d390e..00e80b330 100755 --- a/model/course.go +++ b/model/course.go @@ -56,7 +56,7 @@ type CourseDTO struct { IsAdmin bool // Set in API handler } -func (c *Course) ToDTO() CourseDTO { +func (c *Course) ToDTO(u *User) CourseDTO { return CourseDTO{ ID: c.ID, Name: c.Name, @@ -65,8 +65,8 @@ func (c *Course) ToDTO() CourseDTO { TeachingTerm: c.TeachingTerm, Year: c.Year, DownloadsEnabled: c.DownloadsEnabled, - NextLecture: c.GetNextLecture().ToDTO(), - LastRecording: c.GetLastRecording().ToDTO(), + NextLecture: c.GetNextLecture(u).ToDTO(), + LastRecording: c.GetLastRecording(u).ToDTO(), IsAdmin: false, } } @@ -219,15 +219,18 @@ func (c Course) NumUsers() int { } // NextLectureHasReachedTimeSlot returns whether the courses next lecture arrived at its timeslot -func (c Course) NextLectureHasReachedTimeSlot() bool { - return c.GetNextLecture().TimeSlotReached() +func (c Course) NextLectureHasReachedTimeSlot(u *User) bool { + return c.GetNextLecture(u).TimeSlotReached() } // GetNextLecture returns the next lecture of the course -func (c Course) GetNextLecture() Stream { +func (c Course) GetNextLecture(u *User) Stream { var earliestLecture Stream earliestLectureDate := time.Now().Add(time.Hour * 24 * 365 * 10) // 10 years from now. for _, s := range c.Streams { + if s.Private && (u == nil || !u.IsAdminOfCourse(c)) { + continue + } if s.Start.Before(earliestLectureDate) && s.End.After(time.Now()) { earliestLectureDate = s.Start earliestLecture = s @@ -238,10 +241,13 @@ func (c Course) GetNextLecture() Stream { // GetLastRecording returns the most recent lecture of the course // Assumes an ascending order of c.Streams -func (c Course) GetLastRecording() Stream { +func (c Course) GetLastRecording(u *User) Stream { var lastLecture Stream now := time.Now() for _, s := range c.Streams { + if s.Private && (u == nil || !u.IsAdminOfCourse(c)) { + continue + } if s.Start.After(now) { return lastLecture } @@ -276,8 +282,8 @@ func (c Course) GetNextLectureDate() time.Time { } // IsNextLectureSelfStream checks whether the next lecture is a self stream -func (c Course) IsNextLectureSelfStream() bool { - return c.GetNextLecture().IsSelfStream() +func (c Course) IsNextLectureSelfStream(u *User) bool { + return c.GetNextLecture(u).IsSelfStream() } // GetNextLectureDateFormatted returns a JavaScript friendly formatted date string