From 4cbbb95e4f349d0e8fad84c7a0bb62dc6fe5ddd4 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sat, 20 May 2017 20:53:05 +0200 Subject: [PATCH 1/5] Notification for Users (WIP) --- config/config.go | 1 + db/gorm.go | 2 +- model/notification.go | 23 +++++++++++++++++++++++ model/torrent.go | 4 ++++ model/user.go | 20 ++++++++++++++++---- router/upload_handler.go | 14 ++++++++++++++ service/notifier/notifier.go | 18 ++++++++++++++++++ service/user/cookie_helper.go | 2 +- service/user/user.go | 15 ++++++++++++++- 9 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 model/notification.go create mode 100644 service/notifier/notifier.go diff --git a/config/config.go b/config/config.go index 98798f01..ae7e4472 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ const ( CommentsTableName = "comments" UploadsOldTableName = "user_uploads_old" FilesTableName = "files" + NotificationTableName = "notifications" // for sukebei: //LastOldTorrentID = 2303945 diff --git a/db/gorm.go b/db/gorm.go index ebe2fd43..185c26f1 100644 --- a/db/gorm.go +++ b/db/gorm.go @@ -54,7 +54,7 @@ func GormInit(conf *config.Config, logger Logger) (*gorm.DB, error) { db.SetLogger(logger) } - db.AutoMigrate(&model.User{}, &model.UserFollows{}, &model.UserUploadsOld{}) + db.AutoMigrate(&model.User{}, &model.UserFollows{}, &model.UserUploadsOld{}, &model.Notification{}) if db.Error != nil { return db, db.Error } diff --git a/model/notification.go b/model/notification.go new file mode 100644 index 00000000..a2c1e5e9 --- /dev/null +++ b/model/notification.go @@ -0,0 +1,23 @@ +package model + +import ( + "github.com/NyaaPantsu/nyaa/config" +) + +type Notification struct { + ID uint + Content string + Read bool + Identifier string + UserID uint +// User *User `gorm:"AssociationForeignKey:UserID;ForeignKey:user_id"` // Don't think that we need it here +} + +func NewNotification(identifier string, c string) Notification { + return Notification{Identifier: identifier, Content: c} +} + +func (n *Notification) TableName() string { + return config.NotificationTableName +} + diff --git a/model/torrent.go b/model/torrent.go index 4358a0ab..361991e3 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -86,6 +86,10 @@ func (t Torrent) TableName() string { return config.TorrentsTableName } +func (t Torrent) Identifier() string { + return "torrent_"+strconv.Itoa(int(t.ID)) +} + func (t Torrent) IsNormal() bool { return t.Status == TorrentStatusNormal } diff --git a/model/user.go b/model/user.go index 6d98ca21..8782873d 100644 --- a/model/user.go +++ b/model/user.go @@ -26,13 +26,14 @@ type User struct { Language string `gorm:"column:language"` // TODO: move this to PublicUser - LikingCount int `json:"likingCount" gorm:"-"` - LikedCount int `json:"likedCount" gorm:"-"` Likings []User // Don't work `gorm:"foreignkey:user_id;associationforeignkey:follower_id;many2many:user_follows"` Liked []User // Don't work `gorm:"foreignkey:follower_id;associationforeignkey:user_id;many2many:user_follows"` MD5 string `json:"md5" gorm:"column:md5"` // Hash of email address, used for Gravatar Torrents []Torrent `gorm:"ForeignKey:UploaderID"` + + UnreadNotifications int // We don't want to loop every notifications when accessing user unread notif + Notifications []Notification `gorm:"ForeignKey:UserID"` } type UserJSON struct { @@ -72,6 +73,17 @@ func (u User) IsModerator() bool { return u.Status == UserStatusModerator } +func (u User) GetUnreadNotifications() int { + if u.UnreadNotifications == 0 { + for _, notif := range u.Notifications { + if !notif.Read { + u.UnreadNotifications++ + } + } + } + return u.UnreadNotifications +} + type PublicUser struct { User *User } @@ -98,8 +110,8 @@ func (u *User) ToJSON() UserJSON { Username: u.Username, Status: u.Status, CreatedAt: u.CreatedAt.Format(time.RFC3339), - LikingCount: u.LikingCount, - LikedCount: u.LikedCount, + LikingCount: len(u.Likings), + LikedCount: len(u.Liked), } return json } diff --git a/router/upload_handler.go b/router/upload_handler.go index 5677ab9c..f857474f 100644 --- a/router/upload_handler.go +++ b/router/upload_handler.go @@ -8,7 +8,9 @@ import ( "github.com/NyaaPantsu/nyaa/db" "github.com/NyaaPantsu/nyaa/model" "github.com/NyaaPantsu/nyaa/service/captcha" + "github.com/NyaaPantsu/nyaa/service/notifier" "github.com/NyaaPantsu/nyaa/service/upload" + "github.com/NyaaPantsu/nyaa/service/user" "github.com/NyaaPantsu/nyaa/service/user/permission" "github.com/NyaaPantsu/nyaa/util/languages" msg "github.com/NyaaPantsu/nyaa/util/messages" @@ -75,6 +77,18 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) { UploaderID: user.ID} db.ORM.Create(&torrent) + if (user.ID > 0) { // If we are a member + userService.GetLiked(user) // We populate the liked field for users + if len(user.Liked) > 0 { // If we are followed by at least someone + for _, follower := range user.Liked { + T, _, _ := languages.TfuncAndLanguageWithFallback(user.Language, user.Language) // We need to send the notification to every user in their language + + notifierService.NotifyUser(&follower, torrent.Identifier(), T("new_torrent_uploaded", torrent.Name, user.Username)) + + } + } + } + // add filelist to files db, if we have one if len(uploadForm.FileList) > 0 { for _, uploadedFile := range uploadForm.FileList { diff --git a/service/notifier/notifier.go b/service/notifier/notifier.go new file mode 100644 index 00000000..0ea1fa01 --- /dev/null +++ b/service/notifier/notifier.go @@ -0,0 +1,18 @@ +package notifierService + +import ( + "github.com/NyaaPantsu/nyaa/db" + "github.com/NyaaPantsu/nyaa/model" +) + + +func NotifyUser(user *model.User, name string, msg string) { + if (user.ID > 0) { + user.Notifications = append(user.Notifications, model.NewNotification(name, msg)) + // TODO: Email notification + } +} + +func ToggleReadNotification(identifier string, id uint) { // + db.ORM.Model(&model.Notification{}).Where("identifier = ? AND user_id = ?", identifier, id).Updates(model.Notification{Read: true}) +} \ No newline at end of file diff --git a/service/user/cookie_helper.go b/service/user/cookie_helper.go index 3c092b36..b4d7f640 100644 --- a/service/user/cookie_helper.go +++ b/service/user/cookie_helper.go @@ -141,7 +141,7 @@ func CurrentUser(r *http.Request) (model.User, error) { if userFromContext.ID > 0 && user_id == userFromContext.ID { user = userFromContext } else { - if db.ORM.Where("user_id = ?", user_id).First(&user).RecordNotFound() { + if db.ORM.Preload("Notifications").Where("user_id = ?", user_id).First(&user).RecordNotFound() { // We only load unread notifications return user, errors.New("User not found") } else { setUserToContext(r, user) diff --git a/service/user/user.go b/service/user/user.go index dd8e445c..54b7b058 100644 --- a/service/user/user.go +++ b/service/user/user.go @@ -280,7 +280,7 @@ func RetrieveOldUploadsByUsername(username string) ([]uint, error) { // RetrieveUserForAdmin retrieves a user for an administrator. func RetrieveUserForAdmin(id string) (model.User, int, error) { var user model.User - if db.ORM.Preload("Torrents").Last(&user, id).RecordNotFound() { + if db.ORM.Preload("Notifications").Preload("Torrents").Last(&user, id).RecordNotFound() { return user, http.StatusNotFound, errors.New("user not found") } var liked, likings []model.User @@ -300,6 +300,19 @@ func RetrieveUsersForAdmin(limit int, offset int) ([]model.User, int) { return users, nbUsers } +func GetLiked(user *model.User) *model.User { + var liked []model.User + db.ORM.Joins("JOIN user_follows on user_follows.following=?", user.ID).Where("users.user_id = user_follows.user_id").Group("users.user_id").Find(&liked) + user.Liked = liked + return user +} +func GetLikings(user *model.User) *model.User { + var likings []model.User + db.ORM.Joins("JOIN user_follows on user_follows.user_id=?", user.ID).Where("users.user_id = user_follows.following").Group("users.user_id").Find(&likings) + user.Likings = likings + return user +} + // CreateUserAuthentication creates user authentication. func CreateUserAuthentication(w http.ResponseWriter, r *http.Request) (int, error) { var form formStruct.LoginForm From 0390fc5215fe3737871e2fa9103f9bed2d6d8deb Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 21 May 2017 00:02:57 +0200 Subject: [PATCH 2/5] Mostly done --- model/notification.go | 5 ++- model/user.go | 2 +- router/router.go | 2 + router/template.go | 6 +++ router/template_variables.go | 8 ++++ router/upload_handler.go | 12 ++--- router/user_handler.go | 18 +++++++- service/notifier/notifier.go | 10 ++++- templates/_badgemenu.html | 5 ++- templates/_user_notifications.html | 18 ++++++++ templates/user/profile.html | 5 +++ templates/user/profile_edit.html | 5 +++ templates/user/profile_notifications.html | 54 +++++++++++++++++++++++ translations/en-us.all.json | 8 ++++ 14 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 templates/_user_notifications.html create mode 100644 templates/user/profile_notifications.html diff --git a/model/notification.go b/model/notification.go index a2c1e5e9..857df4ac 100644 --- a/model/notification.go +++ b/model/notification.go @@ -9,12 +9,13 @@ type Notification struct { Content string Read bool Identifier string + Url string UserID uint // User *User `gorm:"AssociationForeignKey:UserID;ForeignKey:user_id"` // Don't think that we need it here } -func NewNotification(identifier string, c string) Notification { - return Notification{Identifier: identifier, Content: c} +func NewNotification(identifier string, c string, url string) Notification { + return Notification{Identifier: identifier, Content: c, Url: url} } func (n *Notification) TableName() string { diff --git a/model/user.go b/model/user.go index 8782873d..d6dc565c 100644 --- a/model/user.go +++ b/model/user.go @@ -32,7 +32,7 @@ type User struct { MD5 string `json:"md5" gorm:"column:md5"` // Hash of email address, used for Gravatar Torrents []Torrent `gorm:"ForeignKey:UploaderID"` - UnreadNotifications int // We don't want to loop every notifications when accessing user unread notif + UnreadNotifications int `gorm:"-"` // We don't want to loop every notifications when accessing user unread notif Notifications []Notification `gorm:"ForeignKey:UserID"` } diff --git a/router/router.go b/router/router.go index b38e4a27..1879e384 100755 --- a/router/router.go +++ b/router/router.go @@ -28,6 +28,7 @@ func init() { gzipUserProfileHandler := http.HandlerFunc(UserProfileHandler) gzipUserDetailsHandler := http.HandlerFunc(UserDetailsHandler) gzipUserProfileFormHandler := http.HandlerFunc(UserProfileFormHandler) + gzipUserNotificationsHandler := http.HandlerFunc(UserNotificationsHandler) gzipDumpsHandler := handlers.CompressHandler(dumpsHandler) gzipGpgKeyHandler := handlers.CompressHandler(gpgKeyHandler) gzipDatabaseDumpHandler := handlers.CompressHandler(http.HandlerFunc(DatabaseDumpHandler)) @@ -63,6 +64,7 @@ func init() { Router.HandleFunc("/user/{id}/{username}/follow", UserFollowHandler).Name("user_follow").Methods("GET") Router.Handle("/user/{id}/{username}/edit", wrapHandler(gzipUserDetailsHandler)).Name("user_profile_details").Methods("GET") Router.Handle("/user/{id}/{username}/edit", wrapHandler(gzipUserProfileFormHandler)).Name("user_profile_edit").Methods("POST") + Router.Handle("/user/notifications", wrapHandler(gzipUserNotificationsHandler)).Name("user_notifications") Router.HandleFunc("/user/{id}/{username}/feed", RSSHandler).Name("feed_user") Router.HandleFunc("/user/{id}/{username}/feed/{page}", RSSHandler).Name("feed_user_page") diff --git a/router/template.go b/router/template.go index f4e922c7..f64c10b1 100644 --- a/router/template.go +++ b/router/template.go @@ -20,6 +20,7 @@ var homeTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, + viewProfileNotifTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate, @@ -99,6 +100,11 @@ func ReloadTemplates() { name: "user_profile", file: filepath.Join("user", "profile.html"), }, + templateLoader{ + templ: &viewProfileNotifTemplate, + name: "user_profile", + file: filepath.Join("user", "profile_notifications.html"), + }, templateLoader{ templ: &viewProfileEditTemplate, name: "user_profile", diff --git a/router/template_variables.go b/router/template_variables.go index 7217af7c..bc6d20e2 100644 --- a/router/template_variables.go +++ b/router/template_variables.go @@ -97,6 +97,14 @@ type UserProfileVariables struct { Route *mux.Route // For getting current route in templates } +type UserProfileNotifVariables struct { + Search SearchForm + Navigation Navigation + User *model.User + URL *url.URL // For parsing Url in templates + Route *mux.Route // For getting current route in templates +} + type HomeTemplateVariables struct { ListTorrents []model.TorrentJSON Search SearchForm diff --git a/router/upload_handler.go b/router/upload_handler.go index f857474f..19f3dae2 100644 --- a/router/upload_handler.go +++ b/router/upload_handler.go @@ -1,6 +1,7 @@ package router import ( + "fmt" "net/http" "strconv" "time" @@ -77,13 +78,15 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) { UploaderID: user.ID} db.ORM.Create(&torrent) + url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.ID), 10)) + if (user.ID > 0) { // If we are a member - userService.GetLiked(user) // We populate the liked field for users - if len(user.Liked) > 0 { // If we are followed by at least someone - for _, follower := range user.Liked { + userService.GetLikings(user) // We populate the liked field for users + if len(user.Likings) > 0 { // If we are followed by at least someone + for _, follower := range user.Likings { T, _, _ := languages.TfuncAndLanguageWithFallback(user.Language, user.Language) // We need to send the notification to every user in their language - notifierService.NotifyUser(&follower, torrent.Identifier(), T("new_torrent_uploaded", torrent.Name, user.Username)) + notifierService.NotifyUser(&follower, torrent.Identifier(), fmt.Sprintf(T("new_torrent_uploaded"), torrent.Name, user.Username), url.String()) } } @@ -101,7 +104,6 @@ func UploadPostHandler(w http.ResponseWriter, r *http.Request) { } } - url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.ID), 10)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/router/user_handler.go b/router/user_handler.go index 5e642173..cf6cb0e7 100644 --- a/router/user_handler.go +++ b/router/user_handler.go @@ -145,7 +145,7 @@ func UserProfileFormHandler(w http.ResponseWriter, r *http.Request) { id := vars["id"] currentUser := GetUser(r) userProfile, _, errorUser := userService.RetrieveUserForAdmin(id) - if errorUser != nil || !userPermission.CurrentOrAdmin(currentUser, userProfile.ID) { + if errorUser != nil || !userPermission.CurrentOrAdmin(currentUser, userProfile.ID) || userProfile.ID == 0 { NotFoundHandler(w, r) return } @@ -311,7 +311,7 @@ func UserFollowHandler(w http.ResponseWriter, r *http.Request) { id := vars["id"] currentUser := GetUser(r) user, _, errorUser := userService.RetrieveUserForAdmin(id) - if errorUser == nil { + if errorUser == nil && user.ID > 0 { if !userPermission.IsFollower(&user, currentUser) { followAction = "followed" userService.SetFollow(&user, currentUser) @@ -323,3 +323,17 @@ func UserFollowHandler(w http.ResponseWriter, r *http.Request) { url, _ := Router.Get("user_profile").URL("id", strconv.Itoa(int(user.ID)), "username", user.Username) http.Redirect(w, r, url.String()+"?"+followAction, http.StatusSeeOther) } + +func UserNotificationsHandler(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if currentUser.ID > 0 { + languages.SetTranslationFromRequest(viewProfileNotifTemplate, r) + htv := UserProfileNotifVariables{NewSearchForm(), NewNavigation(), currentUser, r.URL, mux.CurrentRoute(r)} + err := viewProfileNotifTemplate.ExecuteTemplate(w, "index.html", htv) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + } else { + NotFoundHandler(w, r) + } +} \ No newline at end of file diff --git a/service/notifier/notifier.go b/service/notifier/notifier.go index 0ea1fa01..1aad0ad7 100644 --- a/service/notifier/notifier.go +++ b/service/notifier/notifier.go @@ -6,13 +6,19 @@ import ( ) -func NotifyUser(user *model.User, name string, msg string) { +func NotifyUser(user *model.User, name string, msg string, url string) { if (user.ID > 0) { - user.Notifications = append(user.Notifications, model.NewNotification(name, msg)) + notification := model.NewNotification(name, msg, url) + notification.UserID = user.ID + db.ORM.Save(notification) // TODO: Email notification } } func ToggleReadNotification(identifier string, id uint) { // db.ORM.Model(&model.Notification{}).Where("identifier = ? AND user_id = ?", identifier, id).Updates(model.Notification{Read: true}) +} + +func DeleteAllNotifications(id uint) { // + db.ORM.Where("user_id = ?", id).Delete(&model.Notification{}) } \ No newline at end of file diff --git a/templates/_badgemenu.html b/templates/_badgemenu.html index 9ee8497c..bdc3f5f5 100644 --- a/templates/_badgemenu.html +++ b/templates/_badgemenu.html @@ -4,12 +4,13 @@ {{if gt $.User.ID 0 }} + {{ if CurrentUserIdentical $.User .ID }} +
  • + {{T "my_notifications"}} +
  • + {{end}} {{if CurrentOrAdmin $.User .ID }}
  • {{T "settings"}} diff --git a/templates/user/profile_edit.html b/templates/user/profile_edit.html index 7b6f9373..4976cef2 100755 --- a/templates/user/profile_edit.html +++ b/templates/user/profile_edit.html @@ -41,6 +41,11 @@ {{T "torrents"}}
  • {{if gt $.User.ID 0 }} + {{ if CurrentUserIdentical $.User .ID }} +
  • + {{T "my_notifications"}} +
  • + {{end}} {{if CurrentOrAdmin $.User .ID }}
  • {{T "settings"}} diff --git a/templates/user/profile_notifications.html b/templates/user/profile_notifications.html new file mode 100644 index 00000000..30048228 --- /dev/null +++ b/templates/user/profile_notifications.html @@ -0,0 +1,54 @@ +{{define "title"}}{{ T "profile_edit_page" .User.Username }}{{end}} +{{define "contclass"}}cont-view{{end}} +{{define "content"}} +
    +{{with .User }} +
    +
    + +
    + {{.Username}} +
    + + +
    +
    + {{.Username}} +
    +
    + {{GetRole . }} +
    +
    + + +
    + +
    + + +
    + +
    + +
    +
    +{{end}} +
    +
    + {{ block "profile_notifications_content" . }}{{end}} +
    +
    +
    +{{end}} diff --git a/translations/en-us.all.json b/translations/en-us.all.json index 064170b3..bcaa2473 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -758,5 +758,13 @@ { "id": "no_database_dumps_available", "translation": "No database dumps are available at this moment." + }, + { + "id": "my_notifications", + "translation": "My Notifications" + }, + { + "id": "new_torrent_uploaded", + "translation": "New torrent: \"%s\" from %s" } ] From 42de73fa78024b1b5e575561d8dfbab296875e61 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 21 May 2017 00:29:07 +0200 Subject: [PATCH 3/5] Fixed unassigned value --- service/notifier/notifier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/notifier/notifier.go b/service/notifier/notifier.go index 1aad0ad7..cd930d24 100644 --- a/service/notifier/notifier.go +++ b/service/notifier/notifier.go @@ -10,7 +10,7 @@ func NotifyUser(user *model.User, name string, msg string, url string) { if (user.ID > 0) { notification := model.NewNotification(name, msg, url) notification.UserID = user.ID - db.ORM.Save(notification) + db.ORM.Create(¬ification) // TODO: Email notification } } From 5f60970eb901a0f4476e048399eac0bcb4dc8968 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 21 May 2017 01:06:40 +0200 Subject: [PATCH 4/5] Finished --- router/template_variables.go | 1 + router/user_handler.go | 12 ++++++++++-- router/view_torrent_handler.go | 8 +++++++- templates/_user_notifications.html | 10 ++++++---- translations/en-us.all.json | 4 ++++ 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/router/template_variables.go b/router/template_variables.go index bc6d20e2..a33585a4 100644 --- a/router/template_variables.go +++ b/router/template_variables.go @@ -98,6 +98,7 @@ type UserProfileVariables struct { } type UserProfileNotifVariables struct { + Infos map[string][]string Search SearchForm Navigation Navigation User *model.User diff --git a/router/user_handler.go b/router/user_handler.go index cf6cb0e7..e4670aa2 100644 --- a/router/user_handler.go +++ b/router/user_handler.go @@ -7,10 +7,12 @@ import ( "github.com/NyaaPantsu/nyaa/model" "github.com/NyaaPantsu/nyaa/service/captcha" + "github.com/NyaaPantsu/nyaa/service/notifier" "github.com/NyaaPantsu/nyaa/service/user" "github.com/NyaaPantsu/nyaa/service/user/form" "github.com/NyaaPantsu/nyaa/service/user/permission" "github.com/NyaaPantsu/nyaa/util/languages" + msg "github.com/NyaaPantsu/nyaa/util/messages" "github.com/NyaaPantsu/nyaa/util/modelHelper" "github.com/gorilla/mux" ) @@ -327,8 +329,14 @@ func UserFollowHandler(w http.ResponseWriter, r *http.Request) { func UserNotificationsHandler(w http.ResponseWriter, r *http.Request) { currentUser := GetUser(r) if currentUser.ID > 0 { - languages.SetTranslationFromRequest(viewProfileNotifTemplate, r) - htv := UserProfileNotifVariables{NewSearchForm(), NewNavigation(), currentUser, r.URL, mux.CurrentRoute(r)} + messages := msg.GetMessages(r) + T := languages.SetTranslationFromRequest(viewProfileNotifTemplate, r) + if r.URL.Query()["clear"] != nil { + notifierService.DeleteAllNotifications(currentUser.ID) + messages.AddInfo("infos", T("notifications_cleared")) + currentUser.Notifications = []model.Notification{} + } + htv := UserProfileNotifVariables{messages.GetAllInfos(), NewSearchForm(), NewNavigation(), currentUser, r.URL, mux.CurrentRoute(r)} err := viewProfileNotifTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/router/view_torrent_handler.go b/router/view_torrent_handler.go index 97c726f3..bb1fb599 100644 --- a/router/view_torrent_handler.go +++ b/router/view_torrent_handler.go @@ -9,6 +9,7 @@ import ( "github.com/NyaaPantsu/nyaa/db" "github.com/NyaaPantsu/nyaa/model" "github.com/NyaaPantsu/nyaa/service/captcha" + "github.com/NyaaPantsu/nyaa/service/notifier" "github.com/NyaaPantsu/nyaa/service/torrent" "github.com/NyaaPantsu/nyaa/service/user/permission" "github.com/NyaaPantsu/nyaa/util" @@ -22,19 +23,24 @@ func ViewHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] messages := msg.GetMessages(r) + user := GetUser(r) if (r.URL.Query()["success"] != nil) { messages.AddInfo("infos", "Torrent uploaded successfully!") } torrent, err := torrentService.GetTorrentById(id) + + if (r.URL.Query()["notif"] != nil) { + notifierService.ToggleReadNotification(torrent.Identifier(), user.ID) + } + if err != nil { NotFoundHandler(w, r) return } b := torrent.ToJSON() captchaID := "" - user := GetUser(r) if userPermission.NeedsCaptcha(user) { captchaID = captcha.GetID() } diff --git a/templates/_user_notifications.html b/templates/_user_notifications.html index fec1a2ed..1db0583c 100644 --- a/templates/_user_notifications.html +++ b/templates/_user_notifications.html @@ -1,15 +1,17 @@ {{define "profile_notifications_content"}} {{with .User}} + {{ range (index $.Infos "infos")}} +
    × {{ . }}
    + {{end}} {{ range .Notifications }} {{end}} diff --git a/translations/en-us.all.json b/translations/en-us.all.json index bcaa2473..6af5cb6a 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -759,6 +759,10 @@ "id": "no_database_dumps_available", "translation": "No database dumps are available at this moment." }, + { + "id": "notifications_cleared", + "translation": "Notifications erased!" + }, { "id": "my_notifications", "translation": "My Notifications" From c7d3c49daa8bfd1e459bc20131a7aa0c371a8b12 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 21 May 2017 01:22:07 +0200 Subject: [PATCH 5/5] fix --- router/user_handler.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/router/user_handler.go b/router/user_handler.go index 805212f8..57ee1afc 100644 --- a/router/user_handler.go +++ b/router/user_handler.go @@ -326,13 +326,14 @@ func UserNotificationsHandler(w http.ResponseWriter, r *http.Request) { currentUser := GetUser(r) if currentUser.ID > 0 { messages := msg.GetMessages(r) - T := languages.SetTranslationFromRequest(viewProfileNotifTemplate, r) + Ts, _ := languages.GetTfuncAndLanguageFromRequest(r) + T := languages.GetTfuncFromRequest(r) if r.URL.Query()["clear"] != nil { notifierService.DeleteAllNotifications(currentUser.ID) - messages.AddInfo("infos", T("notifications_cleared")) + messages.AddInfo("infos", Ts("notifications_cleared")) currentUser.Notifications = []model.Notification{} } - htv := UserProfileNotifVariables{messages.GetAllInfos(), NewSearchForm(), NewNavigation(), currentUser, r.URL, mux.CurrentRoute(r)} + htv := UserProfileNotifVariables{messages.GetAllInfos(), NewSearchForm(), NewNavigation(), T, currentUser, r.URL, mux.CurrentRoute(r)} err := viewProfileNotifTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)
    -