Update notifier.go
Cette révision appartient à :
Parent
1f67f36831
révision
e60b2afa62
1 fichiers modifiés avec 28 ajouts et 5 suppressions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket