From a4c23dda1f02c582efb8553cc606c56afc81fe33 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 21 May 2017 20:20:40 +0200 Subject: [PATCH] wiiiip --- model/user.go | 16 +++++++++------- router/modpanel.go | 9 ++++----- router/upload_handler.go | 3 ++- router/user_handler.go | 5 ++--- router/view_torrent_handler.go | 1 - service/user/cookie_helper.go | 12 +++++++----- service/user/user.go | 5 +++-- util/modelHelper/model_helper.go | 1 - 8 files changed, 27 insertions(+), 25 deletions(-) diff --git a/model/user.go b/model/user.go index 61b44bc5..1147c7f6 100644 --- a/model/user.go +++ b/model/user.go @@ -1,6 +1,7 @@ package model import ( + "encoding/json" "time" "github.com/NyaaPantsu/nyaa/config" @@ -102,7 +103,7 @@ type UserUploadsOld struct { } type UserSettings struct { - settings map[string]interface{} `json:"settings"` + settings map[string]bool`json:"settings"` } func (c UserUploadsOld) TableName() string { @@ -124,19 +125,19 @@ func (u *User) ToJSON() UserJSON { /* User Settings */ -func(s UserSettings) Get(key string) interface{} { - if (s.settings[key] != nil) { - return s.settings[key] +func(s UserSettings) Get(key string) bool { + if val, ok:= s.settings[key]; ok { + return val } else { return config.DefaultUserSettings[key] } } -func (s UserSettings) GetSettings() { +func (s UserSettings) GetSettings() map[string]bool { return s.settings } -func (s UserSettings) Set(key string, val interface{}) { +func (s UserSettings) Set(key string, val bool) { s.settings[key] = val } @@ -145,7 +146,8 @@ func (s UserSettings) ToDefault() { } func (u User) SaveSettings() { - u.UserSettings , _ = json.Marshal(u.Settings.GetSettings()) + byteArray, _ := json.Marshal(u.Settings.GetSettings()) + u.UserSettings = string(byteArray[:]) } func (u User) ParseSettings() { diff --git a/router/modpanel.go b/router/modpanel.go index d2bcd08a..8a4b1668 100644 --- a/router/modpanel.go +++ b/router/modpanel.go @@ -16,7 +16,6 @@ import ( "github.com/NyaaPantsu/nyaa/service/torrent" "github.com/NyaaPantsu/nyaa/service/user" "github.com/NyaaPantsu/nyaa/service/user/permission" - form "github.com/NyaaPantsu/nyaa/service/user/form" "github.com/NyaaPantsu/nyaa/util/log" msg "github.com/NyaaPantsu/nyaa/util/messages" "github.com/NyaaPantsu/nyaa/util/search" @@ -235,6 +234,7 @@ func CommentsListPanel(w http.ResponseWriter, r *http.Request) { func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("id") torrent, _ := torrentService.GetTorrentById(id) + messages:= msg.GetMessages(r) torrentJson := torrent.ToJSON() uploadForm := NewUploadForm() @@ -243,7 +243,7 @@ func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) { uploadForm.Status = torrentJson.Status uploadForm.WebsiteLink = string(torrentJson.WebsiteLink) uploadForm.Description = string(torrentJson.Description) - htv := PanelTorrentEdVbs{NewPanelCommonVariables(r), uploadForm, form.NewErrors(), form.NewInfos()} + htv := PanelTorrentEdVbs{NewPanelCommonVariables(r), uploadForm, messages.GetAllErrors(), messages.GetAllInfos()} err := panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv) log.CheckError(err) } @@ -252,7 +252,6 @@ func TorrentPostEditModPanel(w http.ResponseWriter, r *http.Request) { var uploadForm UploadForm id := r.URL.Query().Get("id") messages := msg.GetMessages(r) - infos := form.NewInfos() torrent, _ := torrentService.GetTorrentById(id) if torrent.ID > 0 { errUp := uploadForm.ExtractEditInfo(r) @@ -310,7 +309,8 @@ func TorrentReportDeleteModPanel(w http.ResponseWriter, r *http.Request) { } func TorrentReassignModPanel(w http.ResponseWriter, r *http.Request) { - htv := PanelTorrentReassignVbs{NewPanelCommonVariables(r), ReassignForm{}, form.NewErrors(), form.NewInfos()} + messages := msg.GetMessages(r) + htv := PanelTorrentReassignVbs{NewPanelCommonVariables(r), ReassignForm{}, messages.GetAllErrors(), messages.GetAllInfos()} err := panelTorrentReassign.ExecuteTemplate(w, "admin_index.html", htv) log.CheckError(err) } @@ -318,7 +318,6 @@ func TorrentReassignModPanel(w http.ResponseWriter, r *http.Request) { func TorrentPostReassignModPanel(w http.ResponseWriter, r *http.Request) { var rForm ReassignForm messages := msg.GetMessages(r) - infos := form.NewInfos() err2 := rForm.ExtractInfo(r) if err2 != nil { diff --git a/router/upload_handler.go b/router/upload_handler.go index 88e01919..5e500811 100644 --- a/router/upload_handler.go +++ b/router/upload_handler.go @@ -6,6 +6,7 @@ import ( "strconv" "time" + "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/db" "github.com/NyaaPantsu/nyaa/model" "github.com/NyaaPantsu/nyaa/service/captcha" @@ -84,7 +85,7 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) { if len(user.Likings) > 0 { // If we are followed by at least someone for _, follower := range user.Likings { follower.ParseSettings() // We need to call it before checking settings - if follower.Settings.Get("notifications.new_torrent"] { + if follower.Settings.Get("notifications.new_torrent") { T, _, _ := languages.TfuncAndLanguageWithFallback(user.Language, user.Language) // We need to send the notification to every user in their language notifierService.NotifyUser(&follower, torrent.Identifier(), fmt.Sprintf(T("new_torrent_uploaded"), torrent.Name, user.Username), url.String()) diff --git a/router/user_handler.go b/router/user_handler.go index 0131c078..31a18951 100644 --- a/router/user_handler.go +++ b/router/user_handler.go @@ -1,7 +1,6 @@ package router import ( - "fmt" "net/http" "strconv" @@ -198,7 +197,7 @@ func UserProfileFormHandler(w http.ResponseWriter, r *http.Request) { // Post Registration controller, we do some check on the form here, the rest on user service func UserRegisterPostHandler(w http.ResponseWriter, r *http.Request) { b := form.RegistrationForm{} - messages = msg.GetMessages(r) + messages := msg.GetMessages(r) if !captcha.Authenticate(captcha.Extract(r)) { messages.AddError("errors", "Wrong captcha!") @@ -255,7 +254,7 @@ func UserVerifyEmailHandler(w http.ResponseWriter, r *http.Request) { func UserLoginPostHandler(w http.ResponseWriter, r *http.Request) { b := form.LoginForm{} modelHelper.BindValueForm(&b, r) - messages := msg.GetAllErrors() + messages := msg.GetMessages(r) modelHelper.ValidateForm(&b, &messages) if !messages.HasErrors() { diff --git a/router/view_torrent_handler.go b/router/view_torrent_handler.go index e7b80d91..3221ee02 100644 --- a/router/view_torrent_handler.go +++ b/router/view_torrent_handler.go @@ -12,7 +12,6 @@ import ( "github.com/NyaaPantsu/nyaa/service/notifier" "github.com/NyaaPantsu/nyaa/service/torrent" "github.com/NyaaPantsu/nyaa/service/user/permission" - "github.com/NyaaPantsu/nyaa/util" "github.com/NyaaPantsu/nyaa/util/log" msg "github.com/NyaaPantsu/nyaa/util/messages" "github.com/gorilla/mux" diff --git a/service/user/cookie_helper.go b/service/user/cookie_helper.go index b4d7f640..59bc9aa3 100644 --- a/service/user/cookie_helper.go +++ b/service/user/cookie_helper.go @@ -5,6 +5,7 @@ import ( "github.com/NyaaPantsu/nyaa/db" "github.com/NyaaPantsu/nyaa/model" formStruct "github.com/NyaaPantsu/nyaa/service/user/form" + msg "github.com/NyaaPantsu/nyaa/util/messages" "github.com/NyaaPantsu/nyaa/util/modelHelper" "github.com/NyaaPantsu/nyaa/util/timeHelper" "github.com/gorilla/context" @@ -62,14 +63,15 @@ func ClearCookie(w http.ResponseWriter) (int, error) { } // SetCookieHandler sets the authentication cookie -func SetCookieHandler(w http.ResponseWriter, email string, pass string) (int, error) { +func SetCookieHandler(w http.ResponseWriter, r *http.Request, email string, pass string) (int, error) { if email == "" || pass == "" { return http.StatusNotFound, errors.New("No username/password entered") } var user model.User + messages := msg.GetMessages(r) // search by email or username - isValidEmail, _ := formStruct.EmailValidation(email, formStruct.NewErrors()) + isValidEmail := formStruct.EmailValidation(email, &messages) if isValidEmail { if db.ORM.Where("email = ?", email).First(&user).RecordNotFound() { return http.StatusNotFound, errors.New("User not found") @@ -104,17 +106,17 @@ func SetCookieHandler(w http.ResponseWriter, email string, pass string) (int, er } // RegisterHanderFromForm sets cookie from a RegistrationForm. -func RegisterHanderFromForm(w http.ResponseWriter, registrationForm formStruct.RegistrationForm) (int, error) { +func RegisterHanderFromForm(w http.ResponseWriter, r *http.Request, registrationForm formStruct.RegistrationForm) (int, error) { username := registrationForm.Username // email isn't set at this point pass := registrationForm.Password - return SetCookieHandler(w, username, pass) + return SetCookieHandler(w, r, username, pass) } // RegisterHandler sets a cookie when user registered. func RegisterHandler(w http.ResponseWriter, r *http.Request) (int, error) { var registrationForm formStruct.RegistrationForm modelHelper.BindValueForm(®istrationForm, r) - return RegisterHanderFromForm(w, registrationForm) + return RegisterHanderFromForm(w, r, registrationForm) } // CurrentUser determines the current user from the request or context diff --git a/service/user/user.go b/service/user/user.go index adf07ad6..abc638d3 100644 --- a/service/user/user.go +++ b/service/user/user.go @@ -81,7 +81,8 @@ func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.Use user.Email = "" // unset email because it will be verified later user.CreatedAt = time.Now() // User settings to default - user.ToDefault() + user.Settings.ToDefault() + user.SaveSettings() // currently unused but needs to be set: user.ApiToken = "" user.ApiTokenExpiry = time.Unix(0, 0) @@ -321,7 +322,7 @@ func CreateUserAuthentication(w http.ResponseWriter, r *http.Request) (int, erro modelHelper.BindValueForm(&form, r) username := form.Username pass := form.Password - status, err := SetCookieHandler(w, username, pass) + status, err := SetCookieHandler(w, r, username, pass) return status, err } diff --git a/util/modelHelper/model_helper.go b/util/modelHelper/model_helper.go index a22fb649..fadbd57e 100644 --- a/util/modelHelper/model_helper.go +++ b/util/modelHelper/model_helper.go @@ -1,7 +1,6 @@ package modelHelper import ( - "fmt" "github.com/NyaaPantsu/nyaa/util/log" msg "github.com/NyaaPantsu/nyaa/util/messages" "net/http"