Skip to content

Commit

Permalink
On the home page, don't show private VoDs as "LastRecording" / "most …
Browse files Browse the repository at this point in the history
…recent VoDs" to non course admins
  • Loading branch information
YiranDuan721 committed Mar 24, 2024
1 parent ed5f5d1 commit a23bd27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
15 changes: 10 additions & 5 deletions api/courses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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

Expand Down
24 changes: 15 additions & 9 deletions model/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a23bd27

Please sign in to comment.