From e60b2afa6262a4c3d832f288a7abe5eece033ab6 Mon Sep 17 00:00:00 2001 From: kilo Date: Sun, 29 Oct 2017 19:28:25 +0100 Subject: [PATCH] Update notifier.go --- models/notifications/notifier.go | 33 +++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/models/notifications/notifier.go b/models/notifications/notifier.go index 1c03aa75..60fb0383 100644 --- a/models/notifications/notifier.go +++ b/models/notifications/notifier.go @@ -18,15 +18,38 @@ func NotifyUser(user *models.User, name string, msg string, url string, email bo } // ToggleReadNotification : Make a notification as read according to its identifier -func ToggleReadNotification(identifier string, id uint) { // - models.ORM.Model(&models.Notification{}).Where("identifier = ? AND user_id = ?", identifier, id).Updates(models.Notification{Read: true}) +func ToggleReadNotification(identifier string, user *models.User) { // + models.ORM.Model(&models.Notification{}).Where("identifier = ? AND user_id = ?", identifier, user.ID).Updates(models.Notification{Read: true}) + for i, notif := range user.Notifications { + if notif.Identifier == identifier { + user.Notifications[i].Read = true + } + } + //Need to update both DB and variable, otherwise when the function is called the user still needs to do an additional refresh to see the notification gone/read +} + +// MarkAllNotificationsAsRead : Force every notification as read +func MarkAllNotificationsAsRead(user *models.User) { // + models.ORM.Model(&models.Notification{}).Where("user_id = ?", user.ID).Updates(models.Notification{Read: true}) + for i := range user.Notifications { + user.Notifications[i].Read = true + } + //Need to update both DB and variable, otherwise when the function is called the user still needs to do an additional refresh to see the notification gone/read } // DeleteNotifications : Erase notifications from a user -func DeleteNotifications(id uint, all bool) { // +func DeleteNotifications(user *models.User, all bool) { // if all { - models.ORM.Where("user_id = ?", id).Delete(&models.Notification{}) + models.ORM.Where("user_id = ?", user.ID).Delete(&models.Notification{}) + user.Notifications = []models.Notification{} } else { - models.ORM.Where("user_id = ? AND read = ?", id, true).Delete(&models.Notification{}) + models.ORM.Where("user_id = ? AND read = ?", user.ID, true).Delete(&models.Notification{}) + NewNotifications := []models.Notification{} + for _, notif := range user.Notifications { + if !notif.Read { + NewNotifications = append(NewNotifications, notif) + } + } + user.Notifications = NewNotifications } }