1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-26 18:21:01 +00:00

Refactor HTTP Client and LocalizedError packages

This commit is contained in:
Frédéric Guillot 2023-10-21 19:50:29 -07:00
parent 120aabfbce
commit 14e25ab9fe
104 changed files with 1277 additions and 10672 deletions

View file

@ -10,13 +10,13 @@ import (
)
// ValidateUserCreationWithPassword validates user creation with a password.
func ValidateUserCreationWithPassword(store *storage.Storage, request *model.UserCreationRequest) *ValidationError {
func ValidateUserCreationWithPassword(store *storage.Storage, request *model.UserCreationRequest) *locale.LocalizedError {
if request.Username == "" {
return NewValidationError("error.user_mandatory_fields")
return locale.NewLocalizedError("error.user_mandatory_fields")
}
if store.UserExists(request.Username) {
return NewValidationError("error.user_already_exists")
return locale.NewLocalizedError("error.user_already_exists")
}
if err := validatePassword(request.Password); err != nil {
@ -27,12 +27,12 @@ func ValidateUserCreationWithPassword(store *storage.Storage, request *model.Use
}
// ValidateUserModification validates user modifications.
func ValidateUserModification(store *storage.Storage, userID int64, changes *model.UserModificationRequest) *ValidationError {
func ValidateUserModification(store *storage.Storage, userID int64, changes *model.UserModificationRequest) *locale.LocalizedError {
if changes.Username != nil {
if *changes.Username == "" {
return NewValidationError("error.user_mandatory_fields")
return locale.NewLocalizedError("error.user_mandatory_fields")
} else if store.AnotherUserExists(userID, *changes.Username) {
return NewValidationError("error.user_already_exists")
return locale.NewLocalizedError("error.user_already_exists")
}
}
@ -105,80 +105,80 @@ func ValidateUserModification(store *storage.Storage, userID int64, changes *mod
return nil
}
func validateReadingSpeed(readingSpeed int) *ValidationError {
func validateReadingSpeed(readingSpeed int) *locale.LocalizedError {
if readingSpeed <= 0 {
return NewValidationError("error.settings_reading_speed_is_positive")
return locale.NewLocalizedError("error.settings_reading_speed_is_positive")
}
return nil
}
func validatePassword(password string) *ValidationError {
func validatePassword(password string) *locale.LocalizedError {
if len(password) < 6 {
return NewValidationError("error.password_min_length")
return locale.NewLocalizedError("error.password_min_length")
}
return nil
}
func validateTheme(theme string) *ValidationError {
func validateTheme(theme string) *locale.LocalizedError {
themes := model.Themes()
if _, found := themes[theme]; !found {
return NewValidationError("error.invalid_theme")
return locale.NewLocalizedError("error.invalid_theme")
}
return nil
}
func validateLanguage(language string) *ValidationError {
func validateLanguage(language string) *locale.LocalizedError {
languages := locale.AvailableLanguages()
if _, found := languages[language]; !found {
return NewValidationError("error.invalid_language")
return locale.NewLocalizedError("error.invalid_language")
}
return nil
}
func validateTimezone(store *storage.Storage, timezone string) *ValidationError {
func validateTimezone(store *storage.Storage, timezone string) *locale.LocalizedError {
timezones, err := store.Timezones()
if err != nil {
return NewValidationError(err.Error())
return locale.NewLocalizedError(err.Error())
}
if _, found := timezones[timezone]; !found {
return NewValidationError("error.invalid_timezone")
return locale.NewLocalizedError("error.invalid_timezone")
}
return nil
}
func validateEntryDirection(direction string) *ValidationError {
func validateEntryDirection(direction string) *locale.LocalizedError {
if direction != "asc" && direction != "desc" {
return NewValidationError("error.invalid_entry_direction")
return locale.NewLocalizedError("error.invalid_entry_direction")
}
return nil
}
func validateEntriesPerPage(entriesPerPage int) *ValidationError {
func validateEntriesPerPage(entriesPerPage int) *locale.LocalizedError {
if entriesPerPage < 1 {
return NewValidationError("error.entries_per_page_invalid")
return locale.NewLocalizedError("error.entries_per_page_invalid")
}
return nil
}
func validateDisplayMode(displayMode string) *ValidationError {
func validateDisplayMode(displayMode string) *locale.LocalizedError {
if displayMode != "fullscreen" && displayMode != "standalone" && displayMode != "minimal-ui" && displayMode != "browser" {
return NewValidationError("error.invalid_display_mode")
return locale.NewLocalizedError("error.invalid_display_mode")
}
return nil
}
func validateGestureNav(gestureNav string) *ValidationError {
func validateGestureNav(gestureNav string) *locale.LocalizedError {
if gestureNav != "none" && gestureNav != "tap" && gestureNav != "swipe" {
return NewValidationError("error.invalid_gesture_nav")
return locale.NewLocalizedError("error.invalid_gesture_nav")
}
return nil
}
func validateDefaultHomePage(defaultHomePage string) *ValidationError {
func validateDefaultHomePage(defaultHomePage string) *locale.LocalizedError {
defaultHomePages := model.HomePages()
if _, found := defaultHomePages[defaultHomePage]; !found {
return NewValidationError("error.invalid_default_home_page")
return locale.NewLocalizedError("error.invalid_default_home_page")
}
return nil
}