diff --git a/router/user_handler.go b/router/user_handler.go index b660786e..d5fbbd33 100644 --- a/router/user_handler.go +++ b/router/user_handler.go @@ -67,7 +67,7 @@ func UserLoginFormHandler(w http.ResponseWriter, r *http.Request) { func UserProfileHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] - T := languages.GetTfuncFromRequest(r) + Ts, _ := languages.GetTfuncAndLanguageFromRequest(r) messages := msg.GetMessages(r) userProfile, _, errorUser := userService.RetrieveUserForAdmin(id) @@ -89,10 +89,10 @@ func UserProfileHandler(w http.ResponseWriter, r *http.Request) { } } else { if follow != nil { - messages.AddInfof("infos", string(T("user_followed_msg")), userProfile.Username) + messages.AddInfof("infos", Ts("user_followed_msg"), userProfile.Username) } if unfollow != nil { - messages.AddInfof("infos", string(T("user_unfollowed_msg")), userProfile.Username) + messages.AddInfof("infos", Ts("user_unfollowed_msg"), userProfile.Username) } userProfile.ParseSettings() htv := UserProfileVariables{NewCommonVariables(r), &userProfile, messages.GetAllInfos()} @@ -148,12 +148,13 @@ func UserProfileFormHandler(w http.ResponseWriter, r *http.Request) { userSettingsForm := form.UserSettingsForm{} - T := languages.GetTfuncFromRequest(r) + Ts, _ := languages.GetTfuncAndLanguageFromRequest(r) + if len(r.PostFormValue("email")) > 0 { - form.EmailValidation(r.PostFormValue("email"), &messages) + form.EmailValidation(r.PostFormValue("email"), messages) } if len(r.PostFormValue("username")) > 0 { - form.ValidateUsername(r.PostFormValue("username"), &messages) + form.ValidateUsername(r.PostFormValue("username"), messages) } if !messages.HasErrors() { @@ -167,18 +168,18 @@ func UserProfileFormHandler(w http.ResponseWriter, r *http.Request) { messages.AddError("errors", "Elevating status to moderator is prohibited") } } - modelHelper.ValidateForm(&userForm, &messages) + modelHelper.ValidateForm(&userForm, messages) if !messages.HasErrors() { if userForm.Email != userProfile.Email { userService.SendVerificationToUser(*currentUser, userForm.Email) - messages.AddInfof("infos", string(T("email_changed")), userForm.Email) + messages.AddInfof("infos", Ts("email_changed"), userForm.Email) userForm.Email = userProfile.Email // reset, it will be set when user clicks verification } userProfile, _, errorUser = userService.UpdateUser(w, &userForm, &userSettingsForm, currentUser, id) if errorUser != nil { messages.ImportFromError("errors", errorUser) } else { - messages.AddInfo("infos", string(T("profile_updated"))) + messages.AddInfo("infos", Ts("profile_updated")) } } } @@ -207,12 +208,12 @@ func UserRegisterPostHandler(w http.ResponseWriter, r *http.Request) { } if !messages.HasErrors() { if len(r.PostFormValue("email")) > 0 { - form.EmailValidation(r.PostFormValue("email"), &messages) + form.EmailValidation(r.PostFormValue("email"), messages) } - form.ValidateUsername(r.PostFormValue("username"), &messages) + form.ValidateUsername(r.PostFormValue("username"), messages) if !messages.HasErrors() { modelHelper.BindValueForm(&b, r) - modelHelper.ValidateForm(&b, &messages) + modelHelper.ValidateForm(&b, messages) if !messages.HasErrors() { _, errorUser := userService.CreateUser(w, r) if errorUser != nil { @@ -259,7 +260,7 @@ func UserLoginPostHandler(w http.ResponseWriter, r *http.Request) { modelHelper.BindValueForm(&b, r) messages := msg.GetMessages(r) - modelHelper.ValidateForm(&b, &messages) + modelHelper.ValidateForm(&b, messages) if !messages.HasErrors() { _, errorUser := userService.CreateUserAuthentication(w, r) if errorUser != nil { diff --git a/service/user/cookie_helper.go b/service/user/cookie_helper.go index 59bc9aa3..d4c2113e 100644 --- a/service/user/cookie_helper.go +++ b/service/user/cookie_helper.go @@ -71,7 +71,7 @@ func SetCookieHandler(w http.ResponseWriter, r *http.Request, email string, pass var user model.User messages := msg.GetMessages(r) // search by email or username - isValidEmail := formStruct.EmailValidation(email, &messages) + isValidEmail := formStruct.EmailValidation(email, messages) if isValidEmail { if db.ORM.Where("email = ?", email).First(&user).RecordNotFound() { return http.StatusNotFound, errors.New("User not found") diff --git a/util/messages/messages.go b/util/messages/messages.go index af312943..fd9ecd81 100644 --- a/util/messages/messages.go +++ b/util/messages/messages.go @@ -13,76 +13,78 @@ type Messages struct { r *http.Request } -func GetMessages(r *http.Request) Messages { +func GetMessages(r *http.Request) *Messages { if rv := context.Get(r, MessagesKey); rv != nil { - return rv.(Messages) + return rv.(*Messages) } else { - context.Set(r, MessagesKey, Messages{}) - return Messages{make(map[string][]string),make(map[string][]string), r} + context.Set(r, MessagesKey, &Messages{}) + return &Messages{make(map[string][]string),make(map[string][]string), r} } } -func (mes Messages) AddError(name string, msg string) { +func (mes *Messages) AddError(name string, msg string) { if (mes.Errors == nil) { mes.Errors = make(map[string][]string) } mes.Errors[name] = append(mes.Errors[name], msg) mes.setMessagesInContext() } -func (mes Messages) AddErrorf( name string, msg string, args ...interface{}) { +func (mes *Messages) AddErrorf( name string, msg string, args ...interface{}) { mes.AddError(name, fmt.Sprintf(msg, args...)) } -func (mes Messages) ImportFromError(name string, err error) { +func (mes *Messages) ImportFromError(name string, err error) { mes.AddError(name, err.Error()) } -func (mes Messages) AddInfo(name string, msg string) { +func (mes *Messages) AddInfo(name string, msg string) { if (mes.Infos == nil) { mes.Infos = make(map[string][]string) } mes.Infos[name] = append(mes.Infos[name], msg) mes.setMessagesInContext() } -func (mes Messages) AddInfof(name string, msg string, args ...interface{}) { +func (mes *Messages) AddInfof(name string, msg string, args ...interface{}) { + fmt.Println(msg) + fmt.Printf(msg, args) mes.AddInfo(name, fmt.Sprintf(msg, args...)) } -func (mes Messages) ClearErrors() { +func (mes *Messages) ClearErrors() { mes.Infos = nil mes.setMessagesInContext() } -func (mes Messages) ClearInfos() { +func (mes *Messages) ClearInfos() { mes.Errors = nil mes.setMessagesInContext() } -func (mes Messages) GetAllErrors() map[string][]string { +func (mes *Messages) GetAllErrors() map[string][]string { mes = GetMessages(mes.r) // We need to look if any new errors from other functions has updated context return mes.Errors } -func (mes Messages) GetErrors(name string) []string { +func (mes *Messages) GetErrors(name string) []string { mes = GetMessages(mes.r) // We need to look if any new errors from other functions has updated context return mes.Errors[name] } -func (mes Messages) GetAllInfos() map[string][]string { +func (mes *Messages) GetAllInfos() map[string][]string { mes = GetMessages(mes.r) // We need to look if any new errors from other functions has updated context return mes.Infos } -func (mes Messages) GetInfos(name string) []string { +func (mes *Messages) GetInfos(name string) []string { mes = GetMessages(mes.r) // We need to look if any new errors from other functions has updated context return mes.Infos[name] } -func (mes Messages) HasErrors() bool { +func (mes *Messages) HasErrors() bool { mes = GetMessages(mes.r) // We need to look if any new errors from other functions has updated context return len(mes.Errors) > 0 } -func (mes Messages) HasInfos() bool { +func (mes *Messages) HasInfos() bool { mes = GetMessages(mes.r) // We need to look if any new errors from other functions has updated context return len(mes.Infos) > 0 } -func (mes Messages) setMessagesInContext() { +func (mes *Messages) setMessagesInContext() { context.Set(mes.r, MessagesKey, mes) } \ No newline at end of file