summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorSasha Varlamov <sasha@sashavarlamov.com>2017-12-07 12:52:57 +0700
committerLunny Xiao <xiaolunwen@gmail.com>2017-12-07 13:52:57 +0800
commit7ec6cddd27f36f50f06669b208674feb0dc19535 (patch)
treed29dbb6b7fc8f956a9745d5610a9228231759d66 /models
parent1ed7f1881589b3831b2fe36fa9228f82c1b9b83e (diff)
downloadgitea-7ec6cddd27f36f50f06669b208674feb0dc19535.tar.gz
gitea-7ec6cddd27f36f50f06669b208674feb0dc19535.zip
Add 'mark all read' option to notifications (#3097)
* Add 'mark all read' option to notifications Signed-off-by: Sasha Varlamov <sasha@sashavarlamov.com> * Fix exported comment Signed-off-by: Sasha Varlamov <sasha@sashavarlamov.com> * Format method comments Signed-off-by: Sasha Varlamov <sasha@sashavarlamov.com> * Fix exported comment Signed-off-by: Sasha Varlamov <sasha@sashavarlamov.com> Format method comments Signed-off-by: Sasha Varlamov <sasha@sashavarlamov.com> Tests for reactions (#3083) * Unit tests for reactions * Fix import order Signed-off-by: Lauris Bukšis-Haberkorns <lauris@nix.lv> Fix reaction possition when there is attachments (#3099) Refactor notifications swap function * Accept change to drop beforeupdate call * Update purge notifications error message for consistency * Drop unnecessary check for mark all as read button * Remove debugging comment
Diffstat (limited to 'models')
-rw-r--r--models/fixtures/notification.yml22
-rw-r--r--models/notification.go10
-rw-r--r--models/notification_test.go28
3 files changed, 56 insertions, 4 deletions
diff --git a/models/fixtures/notification.yml b/models/fixtures/notification.yml
index d90936709e..fe5c47287d 100644
--- a/models/fixtures/notification.yml
+++ b/models/fixtures/notification.yml
@@ -19,3 +19,25 @@
issue_id: 2
created_unix: 946684800
updated_unix: 946684800
+
+-
+ id: 3
+ user_id: 2
+ repo_id: 1
+ status: 3 # pinned
+ source: 1 # issue
+ updated_by: 1
+ issue_id: 2
+ created_unix: 946684800
+ updated_unix: 946684800
+
+-
+ id: 4
+ user_id: 2
+ repo_id: 1
+ status: 1 # unread
+ source: 1 # issue
+ updated_by: 1
+ issue_id: 2
+ created_unix: 946684800
+ updated_unix: 946684800 \ No newline at end of file
diff --git a/models/notification.go b/models/notification.go
index 46da059852..dcd624e514 100644
--- a/models/notification.go
+++ b/models/notification.go
@@ -311,3 +311,13 @@ func getNotificationByID(notificationID int64) (*Notification, error) {
return notification, nil
}
+
+// UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
+func UpdateNotificationStatuses(user *User, currentStatus NotificationStatus, desiredStatus NotificationStatus) error {
+ n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
+ _, err := x.
+ Where("user_id = ? AND status = ?", user.ID, currentStatus).
+ Cols("status", "updated_by", "updated_unix").
+ Update(n)
+ return err
+}
diff --git a/models/notification_test.go b/models/notification_test.go
index 07d9ee764c..01c960c929 100644
--- a/models/notification_test.go
+++ b/models/notification_test.go
@@ -31,9 +31,11 @@ func TestNotificationsForUser(t *testing.T) {
statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread}
notfs, err := NotificationsForUser(user, statuses, 1, 10)
assert.NoError(t, err)
- if assert.Len(t, notfs, 1) {
+ if assert.Len(t, notfs, 2) {
assert.EqualValues(t, 2, notfs[0].ID)
assert.EqualValues(t, user.ID, notfs[0].UserID)
+ assert.EqualValues(t, 4, notfs[1].ID)
+ assert.EqualValues(t, user.ID, notfs[1].UserID)
}
}
@@ -57,12 +59,12 @@ func TestNotification_GetIssue(t *testing.T) {
func TestGetNotificationCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
- cnt, err := GetNotificationCount(user, NotificationStatusUnread)
+ user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+ cnt, err := GetNotificationCount(user, NotificationStatusRead)
assert.NoError(t, err)
assert.EqualValues(t, 0, cnt)
- cnt, err = GetNotificationCount(user, NotificationStatusRead)
+ cnt, err = GetNotificationCount(user, NotificationStatusUnread)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}
@@ -79,3 +81,21 @@ func TestSetNotificationStatus(t *testing.T) {
assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead))
assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead))
}
+
+func TestUpdateNotificationStatuses(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+ user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ notfUnread := AssertExistsAndLoadBean(t,
+ &Notification{UserID: user.ID, Status: NotificationStatusUnread}).(*Notification)
+ notfRead := AssertExistsAndLoadBean(t,
+ &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
+ notfPinned := AssertExistsAndLoadBean(t,
+ &Notification{UserID: user.ID, Status: NotificationStatusPinned}).(*Notification)
+ assert.NoError(t, UpdateNotificationStatuses(user, NotificationStatusUnread, NotificationStatusRead))
+ AssertExistsAndLoadBean(t,
+ &Notification{ID: notfUnread.ID, Status: NotificationStatusRead})
+ AssertExistsAndLoadBean(t,
+ &Notification{ID: notfRead.ID, Status: NotificationStatusRead})
+ AssertExistsAndLoadBean(t,
+ &Notification{ID: notfPinned.ID, Status: NotificationStatusPinned})
+}