@ -5,6 +5,7 @@
package models
package models
import (
import (
"fmt"
"time"
"time"
)
)
@ -20,6 +21,8 @@ const (
NotificationStatusUnread NotificationStatus = iota + 1
NotificationStatusUnread NotificationStatus = iota + 1
// NotificationStatusRead represents a read notification
// NotificationStatusRead represents a read notification
NotificationStatusRead
NotificationStatusRead
// NotificationStatusPinned represents a pinned notification
NotificationStatusPinned
)
)
const (
const (
@ -182,13 +185,19 @@ func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error
}
}
// NotificationsForUser returns notifications for a given user and status
// NotificationsForUser returns notifications for a given user and status
func NotificationsForUser ( user * User , status NotificationStatus , page , perPage int ) ( [ ] * Notification , error ) {
func NotificationsForUser ( user * User , statuses [ ] NotificationStatus , page , perPage int ) ( [ ] * Notification , error ) {
return notificationsForUser ( x , user , status , page , perPage )
return notificationsForUser ( x , user , statuses , page , perPage )
}
}
func notificationsForUser ( e Engine , user * User , status NotificationStatus , page , perPage int ) ( notifications [ ] * Notification , err error ) {
func notificationsForUser ( e Engine , user * User , statuses [ ] NotificationStatus , page , perPage int ) ( notifications [ ] * Notification , err error ) {
// FIXME: Xorm does not support aliases types (like NotificationStatus) on In() method
s := make ( [ ] uint8 , len ( statuses ) )
for i , status := range statuses {
s [ i ] = uint8 ( status )
}
sess := e .
sess := e .
Where ( "user_id = ?" , user . ID ) .
Where ( "user_id = ?" , user . ID ) .
And ( "status = ?" , status ) .
In ( "status" , s ) .
OrderBy ( "updated_unix DESC" )
OrderBy ( "updated_unix DESC" )
if page > 0 && perPage > 0 {
if page > 0 && perPage > 0 {
@ -241,15 +250,53 @@ func getNotificationCount(e Engine, user *User, status NotificationStatus) (coun
return
return
}
}
func setNotificationStatusRead ( e Engine , userID , issueID int64 ) error {
func setNotificationStatusReadIfUnread ( e Engine , userID , issueID int64 ) error {
notification , err := getIssueNotification ( e , userID , issueID )
notification , err := getIssueNotification ( e , userID , issueID )
// ignore if not exists
// ignore if not exists
if err != nil {
if err != nil {
return nil
return nil
}
}
if notification . Status != NotificationStatusUnread {
return nil
}
notification . Status = NotificationStatusRead
notification . Status = NotificationStatusRead
_ , err = e . Id ( notification . ID ) . Update ( notification )
_ , err = e . Id ( notification . ID ) . Update ( notification )
return err
return err
}
}
// SetNotificationStatus change the notification status
func SetNotificationStatus ( notificationID int64 , user * User , status NotificationStatus ) error {
notification , err := getNotificationByID ( notificationID )
if err != nil {
return err
}
if notification . UserID != user . ID {
return fmt . Errorf ( "Can't change notification of another user: %d, %d" , notification . UserID , user . ID )
}
notification . Status = status
_ , err = x . Id ( notificationID ) . Update ( notification )
return err
}
func getNotificationByID ( notificationID int64 ) ( * Notification , error ) {
notification := new ( Notification )
ok , err := x .
Where ( "id = ?" , notificationID ) .
Get ( notification )
if err != nil {
return nil , err
}
if ! ok {
return nil , fmt . Errorf ( "Notification %d does not exists" , notificationID )
}
return notification , nil
}