Skip to content

Commit

Permalink
Fixed linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SebiWrn committed Sep 21, 2024
1 parent 6d1c9b7 commit b3f6c5e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
43 changes: 20 additions & 23 deletions api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func (r searchRoutes) search(c *gin.Context) {
}
checkAndFillResponse(c, user, int64(limit), r.DaoWrapper, res, false)
c.JSON(http.StatusOK, responseToMap(res))
return
}

func responseToMap(res *meilisearch.MultiSearchResponse) MeiliSearchMap {
Expand Down Expand Up @@ -382,22 +381,21 @@ func meiliStreamFilter(c *gin.Context, user *model.User, semester model.Semester
if user == nil {
permissionFilter = "(visibility = \"public\" AND private = 0)"
} else {
enrolledCourses := user.CoursesForSemestersWithoutAdministeredCourses(semester, semester, nil, c)
enrolledCourses := user.CoursesForSemestersWithoutAdministeredCourses(semester, semester, nil)
enrolledCoursesFilter := courseSliceToString(enrolledCourses)
if len(user.AdministeredCourses) == 0 {
permissionFilter = fmt.Sprintf("((visibility = \"loggedin\" OR visibility = \"public\" OR (visibility = \"enrolled\" AND courseID IN %s)) AND private = 0)", enrolledCoursesFilter)
} else {
administeredCourses := user.AdministeredCoursesForSemesters(semester, semester, nil, c)
administeredCourses := user.AdministeredCoursesForSemesters(semester, semester, nil)
administeredCoursesFilter := courseSliceToString(administeredCourses)
permissionFilter = fmt.Sprintf("((visibility = \"loggedin\" OR visibility = \"public\" OR (visibility = \"enrolled\" AND courseID IN %s)) AND private = 0 OR courseID IN %s)", enrolledCoursesFilter, administeredCoursesFilter)
}
}

if permissionFilter == "" {
return semesterFilter
} else {
return fmt.Sprintf("(%s AND %s)", permissionFilter, semesterFilter)
}
return fmt.Sprintf("(%s AND %s)", permissionFilter, semesterFilter)
}

func meiliCourseFilter(c *gin.Context, user *model.User, firstSemester model.Semester, lastSemester model.Semester, semesters []model.Semester) string {
Expand All @@ -410,22 +408,21 @@ func meiliCourseFilter(c *gin.Context, user *model.User, firstSemester model.Sem
if user == nil {
permissionFilter = "(visibility = \"public\")"
} else {
enrolledCourses := user.CoursesForSemestersWithoutAdministeredCourses(firstSemester, lastSemester, semesters, c)
enrolledCourses := user.CoursesForSemestersWithoutAdministeredCourses(firstSemester, lastSemester, semesters)
enrolledCoursesFilter := courseSliceToString(enrolledCourses)
if len(user.AdministeredCourses) == 0 {
permissionFilter = fmt.Sprintf("(visibility = \"loggedin\" OR visibility = \"public\" OR (visibility = \"enrolled\" AND ID IN %s))", enrolledCoursesFilter)
} else {
administeredCourses := user.AdministeredCoursesForSemesters(firstSemester, lastSemester, semesters, c)
administeredCourses := user.AdministeredCoursesForSemesters(firstSemester, lastSemester, semesters)
administeredCoursesFilter := courseSliceToString(administeredCourses)
permissionFilter = fmt.Sprintf("(visibility = \"loggedin\" OR visibility = \"public\" OR (visibility = \"enrolled\" AND ID IN %s) OR ID IN %s)", enrolledCoursesFilter, administeredCoursesFilter)
}
}

if semesterFilter == "" || permissionFilter == "" {
return permissionFilter + semesterFilter
} else {
return fmt.Sprintf("(%s AND %s)", permissionFilter, semesterFilter)
}
return fmt.Sprintf("(%s AND %s)", permissionFilter, semesterFilter)
}

func meiliSemesterFilter(firstSemester model.Semester, lastSemester model.Semester, semesters []model.Semester) string {
Expand Down Expand Up @@ -453,17 +450,17 @@ func meiliSemesterFilter(firstSemester model.Semester, lastSemester model.Semest
}
if firstSemester.Year+1 < lastSemester.Year {
return fmt.Sprintf("(%s OR (year > %d AND year < %d) OR %s)", constraint1, firstSemester.Year, lastSemester.Year, constraint2)
} else {
return fmt.Sprintf("(%s OR %s)", constraint1, constraint2)
}
} else {
semesterStringsSlice := make([]string, len(semesters))
for i, semester := range semesters {
semesterStringsSlice[i] = fmt.Sprintf("(year = %d AND semester = \"%s\")", semester.Year, semester.TeachingTerm)
}
filter := "(" + strings.Join(semesterStringsSlice, " OR ") + ")"
return filter
return fmt.Sprintf("(%s OR %s)", constraint1, constraint2)

}

semesterStringsSlice := make([]string, len(semesters))
for i, semester := range semesters {
semesterStringsSlice[i] = fmt.Sprintf("(year = %d AND semester = \"%s\")", semester.Year, semester.TeachingTerm)
}
filter := "(" + strings.Join(semesterStringsSlice, " OR ") + ")"
return filter
}

// Utility functions
Expand All @@ -474,7 +471,7 @@ func parseSemesters(semestersParam string) ([]model.Semester, error) {
}
semesterStrings := strings.Split(semestersParam, ",")

regex, err := regexp.Compile(`^[0-9]{4}[WS]$`)
regex, err := regexp.Compile(`^[0-9]{4}[WS]$`) //nolint:all
if err != nil {
return nil, err
}
Expand All @@ -488,7 +485,7 @@ func parseSemesters(semestersParam string) ([]model.Semester, error) {
Year: year,
}
} else {
return nil, errors.New(fmt.Sprintf("semester %s is not valid", semester))
return nil, errors.New(fmt.Sprintf("semester %s is not valid", semester)) //nolint:all
}
}
return semesters, nil
Expand All @@ -498,7 +495,7 @@ func parseSemesters(semestersParam string) ([]model.Semester, error) {
func parseCourses(c *gin.Context, daoWrapper dao.DaoWrapper, urlParamCourse string) ([]model.Course, uint) {
coursesStrings := strings.Split(urlParamCourse, ",")

regex, err := regexp.Compile(`^.+[0-9]{4}[WS]$`)
regex, err := regexp.Compile(`^.+[0-9]{4}[WS]$`) //nolint:all
if err != nil {
return nil, 2
}
Expand All @@ -520,7 +517,7 @@ func parseCourses(c *gin.Context, daoWrapper dao.DaoWrapper, urlParamCourse stri
}

func courseSliceToString(courses []model.Course) string {
if courses == nil || len(courses) == 0 {
if len(courses) == 0 {
return "[]"
}
idsStringSlice := make([]string, len(courses))
Expand All @@ -532,7 +529,7 @@ func courseSliceToString(courses []model.Course) string {
}

func uintSliceToString(ids []uint) string {
if ids == nil || len(ids) == 0 {
if len(ids) == 0 {
return "[]"
}
idsStringSlice := make([]string, len(ids))
Expand Down
2 changes: 0 additions & 2 deletions api/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"github.com/meilisearch/meilisearch-go"
)

var emptySlice = []any{}

func SearchRouterWrapper(r *gin.Engine) {
configGinSearchRouter(r, dao.DaoWrapper{}, tools.NewMeiliSearchFunctions())
}
Expand Down
12 changes: 6 additions & 6 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type UserSetting struct {
}

// GetPreferredName returns the preferred name of the user if set, otherwise the firstName from TUMOnline
func (u User) GetPreferredName() string {
func (u *User) GetPreferredName() string {
for _, setting := range u.Settings {
if setting.Type == PreferredName {
return setting.Value
Expand Down Expand Up @@ -158,7 +158,7 @@ func (u *User) GetCustomSpeeds() (speeds CustomSpeeds) {
}

// GetPreferredGreeting returns the preferred greeting of the user if set, otherwise Moin
func (u User) GetPreferredGreeting() string {
func (u *User) GetPreferredGreeting() string {
for _, setting := range u.Settings {
if setting.Type == Greeting {
return setting.Value
Expand Down Expand Up @@ -190,7 +190,7 @@ func (u *User) GetSeekingTime() int {
}

// PreferredNameChangeAllowed returns false if the user has set a preferred name within the last 3 months, otherwise true
func (u User) PreferredNameChangeAllowed() bool {
func (u *User) PreferredNameChangeAllowed() bool {
for _, setting := range u.Settings {
if setting.Type == PreferredName && time.Since(setting.UpdatedAt) < time.Hour*24*30*3 {
return false
Expand All @@ -205,7 +205,7 @@ type AutoSkipSetting struct {
}

// GetAutoSkipEnabled returns whether the user has enabled auto skip
func (u User) GetAutoSkipEnabled() (AutoSkipSetting, error) {
func (u *User) GetAutoSkipEnabled() (AutoSkipSetting, error) {
for _, setting := range u.Settings {
if setting.Type == AutoSkip {
var a AutoSkipSetting
Expand Down Expand Up @@ -300,7 +300,7 @@ func (u *User) CoursesForSemester(year int, term string, context context.Context
return cRes
}

func (u *User) AdministeredCoursesForSemesters(firstSemester Semester, lastSemester Semester, semesters []Semester, context context.Context) []Course {
func (u *User) AdministeredCoursesForSemesters(firstSemester Semester, lastSemester Semester, semesters []Semester) []Course {
if u == nil {
return make([]Course, 0)
}
Expand All @@ -315,7 +315,7 @@ func (u *User) AdministeredCoursesForSemesters(firstSemester Semester, lastSemes
return administeredCourses
}

func (u *User) CoursesForSemestersWithoutAdministeredCourses(firstSemester Semester, lastSemester Semester, semesters []Semester, context context.Context) []Course {
func (u *User) CoursesForSemestersWithoutAdministeredCourses(firstSemester Semester, lastSemester Semester, semesters []Semester) []Course {
if u == nil {
return make([]Course, 0)
}
Expand Down

0 comments on commit b3f6c5e

Please sign in to comment.