mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
feat: rework notification table (#9926)
This change is motivated by 5e300a2a87
- Drop the `updated_by` and `commit_id` column, they are unused and have a index for no reason.
- Drop the index on `status` and `created_unix` and make a index on `(user_id, status)`.
## Test
1. Run migration.
2. Confirm the migration succeeds.
3. Check that `notification` table has the correct indexes.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9926
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
parent
482ba3a4e5
commit
d1cef852ee
12 changed files with 175 additions and 113 deletions
|
|
@ -6,7 +6,6 @@ package activities
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"forgejo.org/models/db"
|
"forgejo.org/models/db"
|
||||||
|
|
@ -51,24 +50,21 @@ const (
|
||||||
// Notification represents a notification
|
// Notification represents a notification
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
ID int64 `xorm:"pk autoincr"`
|
ID int64 `xorm:"pk autoincr"`
|
||||||
UserID int64 `xorm:"INDEX NOT NULL"`
|
UserID int64 `xorm:"NOT NULL INDEX(s)"`
|
||||||
RepoID int64 `xorm:"INDEX NOT NULL"`
|
RepoID int64 `xorm:"INDEX NOT NULL"`
|
||||||
|
|
||||||
Status NotificationStatus `xorm:"SMALLINT INDEX NOT NULL"`
|
Status NotificationStatus `xorm:"SMALLINT NOT NULL INDEX(s)"`
|
||||||
Source NotificationSource `xorm:"SMALLINT INDEX NOT NULL"`
|
Source NotificationSource `xorm:"SMALLINT INDEX NOT NULL"`
|
||||||
|
|
||||||
IssueID int64 `xorm:"INDEX NOT NULL"`
|
IssueID int64 `xorm:"INDEX NOT NULL"`
|
||||||
CommitID string `xorm:"INDEX"`
|
|
||||||
CommentID int64
|
CommentID int64
|
||||||
|
|
||||||
UpdatedBy int64 `xorm:"INDEX NOT NULL"`
|
|
||||||
|
|
||||||
Issue *issues_model.Issue `xorm:"-"`
|
Issue *issues_model.Issue `xorm:"-"`
|
||||||
Repository *repo_model.Repository `xorm:"-"`
|
Repository *repo_model.Repository `xorm:"-"`
|
||||||
Comment *issues_model.Comment `xorm:"-"`
|
Comment *issues_model.Comment `xorm:"-"`
|
||||||
User *user_model.User `xorm:"-"`
|
User *user_model.User `xorm:"-"`
|
||||||
|
|
||||||
CreatedUnix timeutil.TimeStamp `xorm:"created INDEX NOT NULL"`
|
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
|
||||||
UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
|
UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,20 +84,18 @@ func CreateRepoTransferNotification(ctx context.Context, doer, newOwner *user_mo
|
||||||
}
|
}
|
||||||
for i := range users {
|
for i := range users {
|
||||||
notify = append(notify, &Notification{
|
notify = append(notify, &Notification{
|
||||||
UserID: i,
|
UserID: i,
|
||||||
RepoID: repo.ID,
|
RepoID: repo.ID,
|
||||||
Status: NotificationStatusUnread,
|
Status: NotificationStatusUnread,
|
||||||
UpdatedBy: doer.ID,
|
Source: NotificationSourceRepository,
|
||||||
Source: NotificationSourceRepository,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
notify = []*Notification{{
|
notify = []*Notification{{
|
||||||
UserID: newOwner.ID,
|
UserID: newOwner.ID,
|
||||||
RepoID: repo.ID,
|
RepoID: repo.ID,
|
||||||
Status: NotificationStatusUnread,
|
Status: NotificationStatusUnread,
|
||||||
UpdatedBy: doer.ID,
|
Source: NotificationSourceRepository,
|
||||||
Source: NotificationSourceRepository,
|
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,14 +103,13 @@ func CreateRepoTransferNotification(ctx context.Context, doer, newOwner *user_mo
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func createIssueNotification(ctx context.Context, userID int64, issue *issues_model.Issue, commentID, updatedByID int64) error {
|
func createIssueNotification(ctx context.Context, userID int64, issue *issues_model.Issue, commentID int64) error {
|
||||||
notification := &Notification{
|
notification := &Notification{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
RepoID: issue.RepoID,
|
RepoID: issue.RepoID,
|
||||||
Status: NotificationStatusUnread,
|
Status: NotificationStatusUnread,
|
||||||
IssueID: issue.ID,
|
IssueID: issue.ID,
|
||||||
CommentID: commentID,
|
CommentID: commentID,
|
||||||
UpdatedBy: updatedByID,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if issue.IsPull {
|
if issue.IsPull {
|
||||||
|
|
@ -128,25 +121,23 @@ func createIssueNotification(ctx context.Context, userID int64, issue *issues_mo
|
||||||
return db.Insert(ctx, notification)
|
return db.Insert(ctx, notification)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateIssueNotification(ctx context.Context, userID, issueID, commentID, updatedByID int64) error {
|
func updateIssueNotification(ctx context.Context, userID, issueID, commentID int64) error {
|
||||||
notification, err := GetIssueNotification(ctx, userID, issueID)
|
notification, err := GetIssueNotification(ctx, userID, issueID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTICE: Only update comment id when the before notification on this issue is read, otherwise you may miss some old comments.
|
// If the notification is not yet read, then only update the update_unix column,
|
||||||
// But we need update update_by so that the notification will be reorder
|
// so that the notification does not point to a newer comment.
|
||||||
var cols []string
|
if notification.Status != NotificationStatusRead {
|
||||||
if notification.Status == NotificationStatusRead {
|
notification.UpdatedUnix = timeutil.TimeStampNow()
|
||||||
notification.Status = NotificationStatusUnread
|
_, err = db.GetEngine(ctx).ID(notification.ID).Cols("updated_unix").NoAutoTime().Update(notification)
|
||||||
notification.CommentID = commentID
|
return err
|
||||||
cols = []string{"status", "update_by", "comment_id"}
|
|
||||||
} else {
|
|
||||||
notification.UpdatedBy = updatedByID
|
|
||||||
cols = []string{"update_by"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.GetEngine(ctx).ID(notification.ID).Cols(cols...).Update(notification)
|
notification.Status = NotificationStatusUnread
|
||||||
|
notification.CommentID = commentID
|
||||||
|
_, err = db.GetEngine(ctx).ID(notification.ID).Cols("status", "comment_id").Update(notification)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,8 +233,6 @@ func (n *Notification) HTMLURL(ctx context.Context) string {
|
||||||
return n.Comment.HTMLURL(ctx)
|
return n.Comment.HTMLURL(ctx)
|
||||||
}
|
}
|
||||||
return n.Issue.HTMLURL()
|
return n.Issue.HTMLURL()
|
||||||
case NotificationSourceCommit:
|
|
||||||
return n.Repository.HTMLURL() + "/commit/" + url.PathEscape(n.CommitID)
|
|
||||||
case NotificationSourceRepository:
|
case NotificationSourceRepository:
|
||||||
return n.Repository.HTMLURL()
|
return n.Repository.HTMLURL()
|
||||||
}
|
}
|
||||||
|
|
@ -258,8 +247,6 @@ func (n *Notification) Link(ctx context.Context) string {
|
||||||
return n.Comment.Link(ctx)
|
return n.Comment.Link(ctx)
|
||||||
}
|
}
|
||||||
return n.Issue.Link()
|
return n.Issue.Link()
|
||||||
case NotificationSourceCommit:
|
|
||||||
return n.Repository.Link() + "/commit/" + url.PathEscape(n.CommitID)
|
|
||||||
case NotificationSourceRepository:
|
case NotificationSourceRepository:
|
||||||
return n.Repository.Link()
|
return n.Repository.Link()
|
||||||
}
|
}
|
||||||
|
|
@ -370,10 +357,10 @@ func GetNotificationByID(ctx context.Context, notificationID int64) (*Notificati
|
||||||
|
|
||||||
// UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
|
// UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
|
||||||
func UpdateNotificationStatuses(ctx context.Context, user *user_model.User, currentStatus, desiredStatus NotificationStatus) error {
|
func UpdateNotificationStatuses(ctx context.Context, user *user_model.User, currentStatus, desiredStatus NotificationStatus) error {
|
||||||
n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
|
n := &Notification{Status: desiredStatus}
|
||||||
_, err := db.GetEngine(ctx).
|
_, err := db.GetEngine(ctx).
|
||||||
Where("user_id = ? AND status = ?", user.ID, currentStatus).
|
Where("user_id = ? AND status = ?", user.ID, currentStatus).
|
||||||
Cols("status", "updated_by", "updated_unix").
|
Cols("status", "updated_unix").
|
||||||
Update(n)
|
Update(n)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -164,12 +164,12 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
|
||||||
}
|
}
|
||||||
|
|
||||||
if notificationExists(notifications, issue.ID, userID) {
|
if notificationExists(notifications, issue.ID, userID) {
|
||||||
if err = updateIssueNotification(ctx, userID, issue.ID, commentID, notificationAuthorID); err != nil {
|
if err = updateIssueNotification(ctx, userID, issue.ID, commentID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err = createIssueNotification(ctx, userID, issue, commentID, notificationAuthorID); err != nil {
|
if err = createIssueNotification(ctx, userID, issue, commentID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,19 @@
|
||||||
// Copyright 2017 The Gitea Authors. All rights reserved.
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||||||
|
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
package activities_test
|
package activities
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
activities_model "forgejo.org/models/activities"
|
|
||||||
"forgejo.org/models/db"
|
"forgejo.org/models/db"
|
||||||
issues_model "forgejo.org/models/issues"
|
issues_model "forgejo.org/models/issues"
|
||||||
"forgejo.org/models/unittest"
|
"forgejo.org/models/unittest"
|
||||||
user_model "forgejo.org/models/user"
|
user_model "forgejo.org/models/user"
|
||||||
|
"forgejo.org/modules/timeutil"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
@ -21,25 +23,25 @@ func TestCreateOrUpdateIssueNotifications(t *testing.T) {
|
||||||
require.NoError(t, unittest.PrepareTestDatabase())
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
||||||
|
|
||||||
require.NoError(t, activities_model.CreateOrUpdateIssueNotifications(db.DefaultContext, issue.ID, 0, 2, 0))
|
require.NoError(t, CreateOrUpdateIssueNotifications(db.DefaultContext, issue.ID, 0, 2, 0))
|
||||||
|
|
||||||
// User 9 is inactive, thus notifications for user 1 and 4 are created
|
// User 9 is inactive, thus notifications for user 1 and 4 are created
|
||||||
notf := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{UserID: 1, IssueID: issue.ID})
|
notf := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID})
|
||||||
assert.Equal(t, activities_model.NotificationStatusUnread, notf.Status)
|
assert.Equal(t, NotificationStatusUnread, notf.Status)
|
||||||
unittest.CheckConsistencyFor(t, &issues_model.Issue{ID: issue.ID})
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{ID: issue.ID})
|
||||||
|
|
||||||
notf = unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{UserID: 4, IssueID: issue.ID})
|
notf = unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID})
|
||||||
assert.Equal(t, activities_model.NotificationStatusUnread, notf.Status)
|
assert.Equal(t, NotificationStatusUnread, notf.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotificationsForUser(t *testing.T) {
|
func TestNotificationsForUser(t *testing.T) {
|
||||||
require.NoError(t, unittest.PrepareTestDatabase())
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
notfs, err := db.Find[activities_model.Notification](db.DefaultContext, activities_model.FindNotificationOptions{
|
notfs, err := db.Find[Notification](db.DefaultContext, FindNotificationOptions{
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
Status: []activities_model.NotificationStatus{
|
Status: []NotificationStatus{
|
||||||
activities_model.NotificationStatusRead,
|
NotificationStatusRead,
|
||||||
activities_model.NotificationStatusUnread,
|
NotificationStatusUnread,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
@ -55,7 +57,7 @@ func TestNotificationsForUser(t *testing.T) {
|
||||||
|
|
||||||
func TestNotification_GetRepo(t *testing.T) {
|
func TestNotification_GetRepo(t *testing.T) {
|
||||||
require.NoError(t, unittest.PrepareTestDatabase())
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
notf := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{RepoID: 1})
|
notf := unittest.AssertExistsAndLoadBean(t, &Notification{RepoID: 1})
|
||||||
repo, err := notf.GetRepo(db.DefaultContext)
|
repo, err := notf.GetRepo(db.DefaultContext)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, repo, notf.Repository)
|
assert.Equal(t, repo, notf.Repository)
|
||||||
|
|
@ -64,7 +66,7 @@ func TestNotification_GetRepo(t *testing.T) {
|
||||||
|
|
||||||
func TestNotification_GetIssue(t *testing.T) {
|
func TestNotification_GetIssue(t *testing.T) {
|
||||||
require.NoError(t, unittest.PrepareTestDatabase())
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
notf := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{RepoID: 1})
|
notf := unittest.AssertExistsAndLoadBean(t, &Notification{RepoID: 1})
|
||||||
issue, err := notf.GetIssue(db.DefaultContext)
|
issue, err := notf.GetIssue(db.DefaultContext)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, issue, notf.Issue)
|
assert.Equal(t, issue, notf.Issue)
|
||||||
|
|
@ -74,19 +76,19 @@ func TestNotification_GetIssue(t *testing.T) {
|
||||||
func TestGetNotificationCount(t *testing.T) {
|
func TestGetNotificationCount(t *testing.T) {
|
||||||
require.NoError(t, unittest.PrepareTestDatabase())
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||||
cnt, err := db.Count[activities_model.Notification](db.DefaultContext, activities_model.FindNotificationOptions{
|
cnt, err := db.Count[Notification](db.DefaultContext, FindNotificationOptions{
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
Status: []activities_model.NotificationStatus{
|
Status: []NotificationStatus{
|
||||||
activities_model.NotificationStatusRead,
|
NotificationStatusRead,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.EqualValues(t, 0, cnt)
|
assert.EqualValues(t, 0, cnt)
|
||||||
|
|
||||||
cnt, err = db.Count[activities_model.Notification](db.DefaultContext, activities_model.FindNotificationOptions{
|
cnt, err = db.Count[Notification](db.DefaultContext, FindNotificationOptions{
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
Status: []activities_model.NotificationStatus{
|
Status: []NotificationStatus{
|
||||||
activities_model.NotificationStatusUnread,
|
NotificationStatusUnread,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
@ -97,15 +99,15 @@ func TestSetNotificationStatus(t *testing.T) {
|
||||||
require.NoError(t, unittest.PrepareTestDatabase())
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
notf := unittest.AssertExistsAndLoadBean(t,
|
notf := unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusRead})
|
&Notification{UserID: user.ID, Status: NotificationStatusRead})
|
||||||
_, err := activities_model.SetNotificationStatus(db.DefaultContext, notf.ID, user, activities_model.NotificationStatusPinned)
|
_, err := SetNotificationStatus(db.DefaultContext, notf.ID, user, NotificationStatusPinned)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
unittest.AssertExistsAndLoadBean(t,
|
unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{ID: notf.ID, Status: activities_model.NotificationStatusPinned})
|
&Notification{ID: notf.ID, Status: NotificationStatusPinned})
|
||||||
|
|
||||||
_, err = activities_model.SetNotificationStatus(db.DefaultContext, 1, user, activities_model.NotificationStatusRead)
|
_, err = SetNotificationStatus(db.DefaultContext, 1, user, NotificationStatusRead)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
_, err = activities_model.SetNotificationStatus(db.DefaultContext, unittest.NonexistentID, user, activities_model.NotificationStatusRead)
|
_, err = SetNotificationStatus(db.DefaultContext, unittest.NonexistentID, user, NotificationStatusRead)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,18 +115,18 @@ func TestUpdateNotificationStatuses(t *testing.T) {
|
||||||
require.NoError(t, unittest.PrepareTestDatabase())
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
notfUnread := unittest.AssertExistsAndLoadBean(t,
|
notfUnread := unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusUnread})
|
&Notification{UserID: user.ID, Status: NotificationStatusUnread})
|
||||||
notfRead := unittest.AssertExistsAndLoadBean(t,
|
notfRead := unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusRead})
|
&Notification{UserID: user.ID, Status: NotificationStatusRead})
|
||||||
notfPinned := unittest.AssertExistsAndLoadBean(t,
|
notfPinned := unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{UserID: user.ID, Status: activities_model.NotificationStatusPinned})
|
&Notification{UserID: user.ID, Status: NotificationStatusPinned})
|
||||||
require.NoError(t, activities_model.UpdateNotificationStatuses(db.DefaultContext, user, activities_model.NotificationStatusUnread, activities_model.NotificationStatusRead))
|
require.NoError(t, UpdateNotificationStatuses(db.DefaultContext, user, NotificationStatusUnread, NotificationStatusRead))
|
||||||
unittest.AssertExistsAndLoadBean(t,
|
unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{ID: notfUnread.ID, Status: activities_model.NotificationStatusRead})
|
&Notification{ID: notfUnread.ID, Status: NotificationStatusRead})
|
||||||
unittest.AssertExistsAndLoadBean(t,
|
unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{ID: notfRead.ID, Status: activities_model.NotificationStatusRead})
|
&Notification{ID: notfRead.ID, Status: NotificationStatusRead})
|
||||||
unittest.AssertExistsAndLoadBean(t,
|
unittest.AssertExistsAndLoadBean(t,
|
||||||
&activities_model.Notification{ID: notfPinned.ID, Status: activities_model.NotificationStatusPinned})
|
&Notification{ID: notfPinned.ID, Status: NotificationStatusPinned})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetIssueReadBy(t *testing.T) {
|
func TestSetIssueReadBy(t *testing.T) {
|
||||||
|
|
@ -132,10 +134,44 @@ func TestSetIssueReadBy(t *testing.T) {
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
||||||
require.NoError(t, db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
require.NoError(t, db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
||||||
return activities_model.SetIssueReadBy(ctx, issue.ID, user.ID)
|
return SetIssueReadBy(ctx, issue.ID, user.ID)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
nt, err := activities_model.GetIssueNotification(db.DefaultContext, user.ID, issue.ID)
|
nt, err := GetIssueNotification(db.DefaultContext, user.ID, issue.ID)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, activities_model.NotificationStatusRead, nt.Status)
|
assert.Equal(t, NotificationStatusRead, nt.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateIssueNotification(t *testing.T) {
|
||||||
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
now := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
timeutil.MockSet(now)
|
||||||
|
defer timeutil.MockUnset()
|
||||||
|
|
||||||
|
t.Run("Read notification", func(t *testing.T) {
|
||||||
|
require.NoError(t, updateIssueNotification(t.Context(), 1, 1, 1001))
|
||||||
|
|
||||||
|
notification := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: 1})
|
||||||
|
assert.Equal(t, NotificationStatusUnread, notification.Status)
|
||||||
|
assert.EqualValues(t, 0, notification.CommentID)
|
||||||
|
assert.Equal(t, timeutil.TimeStamp(now.Unix()), notification.UpdatedUnix)
|
||||||
|
})
|
||||||
|
t.Run("Unread notification", func(t *testing.T) {
|
||||||
|
beforeUpdateUnix := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 2, IssueID: 2}).UpdatedUnix
|
||||||
|
require.NoError(t, updateIssueNotification(t.Context(), 2, 2, 1001))
|
||||||
|
|
||||||
|
notification := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 2, IssueID: 2})
|
||||||
|
assert.Equal(t, NotificationStatusUnread, notification.Status)
|
||||||
|
assert.EqualValues(t, 1001, notification.CommentID)
|
||||||
|
assert.NotEqual(t, beforeUpdateUnix, notification.UpdatedUnix)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Pinned notification", func(t *testing.T) {
|
||||||
|
require.NoError(t, updateIssueNotification(t.Context(), 1, 1, 1001))
|
||||||
|
|
||||||
|
notification := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: 1})
|
||||||
|
assert.Equal(t, NotificationStatusUnread, notification.Status)
|
||||||
|
assert.EqualValues(t, 0, notification.CommentID)
|
||||||
|
assert.Equal(t, timeutil.TimeStamp(now.Unix()), notification.UpdatedUnix)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
repo_id: 1
|
repo_id: 1
|
||||||
status: 1 # unread
|
status: 1 # unread
|
||||||
source: 1 # issue
|
source: 1 # issue
|
||||||
updated_by: 2
|
|
||||||
issue_id: 1
|
issue_id: 1
|
||||||
created_unix: 946684800
|
created_unix: 946684800
|
||||||
updated_unix: 946684820
|
updated_unix: 946684820
|
||||||
|
|
@ -15,7 +14,6 @@
|
||||||
repo_id: 1
|
repo_id: 1
|
||||||
status: 2 # read
|
status: 2 # read
|
||||||
source: 1 # issue
|
source: 1 # issue
|
||||||
updated_by: 1
|
|
||||||
issue_id: 2
|
issue_id: 2
|
||||||
created_unix: 946685800
|
created_unix: 946685800
|
||||||
updated_unix: 946685820
|
updated_unix: 946685820
|
||||||
|
|
@ -26,7 +24,6 @@
|
||||||
repo_id: 1
|
repo_id: 1
|
||||||
status: 3 # pinned
|
status: 3 # pinned
|
||||||
source: 1 # issue
|
source: 1 # issue
|
||||||
updated_by: 1
|
|
||||||
issue_id: 3
|
issue_id: 3
|
||||||
created_unix: 946686800
|
created_unix: 946686800
|
||||||
updated_unix: 946686800
|
updated_unix: 946686800
|
||||||
|
|
@ -37,7 +34,6 @@
|
||||||
repo_id: 1
|
repo_id: 1
|
||||||
status: 1 # unread
|
status: 1 # unread
|
||||||
source: 1 # issue
|
source: 1 # issue
|
||||||
updated_by: 1
|
|
||||||
issue_id: 5
|
issue_id: 5
|
||||||
created_unix: 946687800
|
created_unix: 946687800
|
||||||
updated_unix: 946687800
|
updated_unix: 946687800
|
||||||
|
|
@ -48,7 +44,6 @@
|
||||||
repo_id: 2
|
repo_id: 2
|
||||||
status: 1 # unread
|
status: 1 # unread
|
||||||
source: 1 # issue
|
source: 1 # issue
|
||||||
updated_by: 5
|
|
||||||
issue_id: 4
|
issue_id: 4
|
||||||
created_unix: 946688800
|
created_unix: 946688800
|
||||||
updated_unix: 946688820
|
updated_unix: 946688820
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ func TestDropIndexIfExists(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
x, deferable := migration_tests.PrepareTestEnv(t, 0, new(Table))
|
x, deferable := migration_tests.PrepareTestEnv(t, 0, new(Table))
|
||||||
x.ShowSQL(true)
|
|
||||||
defer deferable()
|
defer deferable()
|
||||||
if x == nil || t.Failed() {
|
if x == nil || t.Failed() {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
65
models/forgejo_migrations/v14a_rework-notification.go
Normal file
65
models/forgejo_migrations/v14a_rework-notification.go
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package forgejo_migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
activity_model "forgejo.org/models/activities"
|
||||||
|
"forgejo.org/modules/setting"
|
||||||
|
|
||||||
|
"xorm.io/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registerMigration(&Migration{
|
||||||
|
Description: "remove columns and rework indexes for notification table",
|
||||||
|
Upgrade: reworkNotification,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func reworkNotification(x *xorm.Engine) error {
|
||||||
|
type Notification struct {
|
||||||
|
UserID int64 `xorm:"NOT NULL INDEX(s)"`
|
||||||
|
Status activity_model.NotificationStatus `xorm:"SMALLINT NOT NULL INDEX(s)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_user_id"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_created_unix"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_updated_by"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_commit_id"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_status"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case setting.Database.Type.IsSQLite3():
|
||||||
|
|
||||||
|
if _, err := x.Exec("ALTER TABLE `notification` DROP COLUMN `updated_by`"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := x.Exec("ALTER TABLE `notification` DROP COLUMN `commit_id`"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
case setting.Database.Type.IsMySQL(), setting.Database.Type.IsPostgreSQL():
|
||||||
|
if _, err := x.Exec("ALTER TABLE `notification` DROP COLUMN `updated_by`, DROP COLUMN `commit_id`"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := x.SyncWithOptions(xorm.SyncOptions{IgnoreDropIndices: true}, new(Notification))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
@ -67,8 +67,6 @@ func subjectToSource(value []string) (result []activities_model.NotificationSour
|
||||||
result = append(result, activities_model.NotificationSourceIssue)
|
result = append(result, activities_model.NotificationSourceIssue)
|
||||||
case "pull":
|
case "pull":
|
||||||
result = append(result, activities_model.NotificationSourcePullRequest)
|
result = append(result, activities_model.NotificationSourcePullRequest)
|
||||||
case "commit":
|
|
||||||
result = append(result, activities_model.NotificationSourceCommit)
|
|
||||||
case "repository":
|
case "repository":
|
||||||
result = append(result, activities_model.NotificationSourceRepository)
|
result = append(result, activities_model.NotificationSourceRepository)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ func ListRepoNotifications(ctx *context.APIContext) {
|
||||||
// collectionFormat: multi
|
// collectionFormat: multi
|
||||||
// items:
|
// items:
|
||||||
// type: string
|
// type: string
|
||||||
// enum: [issue,pull,commit,repository]
|
// enum: [issue,pull,repository]
|
||||||
// - name: since
|
// - name: since
|
||||||
// in: query
|
// in: query
|
||||||
// description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
|
// description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ func ListNotifications(ctx *context.APIContext) {
|
||||||
// collectionFormat: multi
|
// collectionFormat: multi
|
||||||
// items:
|
// items:
|
||||||
// type: string
|
// type: string
|
||||||
// enum: [issue,pull,commit,repository]
|
// enum: [issue,pull,repository]
|
||||||
// - name: since
|
// - name: since
|
||||||
// in: query
|
// in: query
|
||||||
// description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
|
// description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
|
||||||
|
|
|
||||||
|
|
@ -109,24 +109,17 @@ func getNotifications(ctx *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := db.GetEngine(ctx).Table("notification")
|
statuses := []activities_model.NotificationStatus{status, activities_model.NotificationStatusPinned}
|
||||||
if setting.Database.Type.IsMySQL() {
|
nls, err := db.Find[activities_model.Notification](ctx, activities_model.FindNotificationOptions{
|
||||||
sess = sess.IndexHint("USE", "", "IDX_notification_user_id")
|
ListOptions: db.ListOptions{
|
||||||
}
|
PageSize: perPage,
|
||||||
sess.Where("user_id = ?", ctx.Doer.ID).
|
Page: page,
|
||||||
And("status = ? OR status = ?", status, activities_model.NotificationStatusPinned).
|
},
|
||||||
OrderBy("notification.updated_unix DESC")
|
UserID: ctx.Doer.ID,
|
||||||
|
Status: statuses,
|
||||||
if perPage > 0 {
|
})
|
||||||
if page == 0 {
|
if err != nil {
|
||||||
page = 1
|
ctx.ServerError("db.Find[activities_model.Notification]", err)
|
||||||
}
|
|
||||||
sess.Limit(perPage, (page-1)*perPage)
|
|
||||||
}
|
|
||||||
|
|
||||||
nls := make([]*activities_model.Notification, 0, perPage)
|
|
||||||
if err := sess.Find(&nls); err != nil {
|
|
||||||
ctx.ServerError("FindNotifications", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
notifications := activities_model.NotificationList(nls)
|
notifications := activities_model.NotificationList(nls)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ package convert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
|
||||||
|
|
||||||
activities_model "forgejo.org/models/activities"
|
activities_model "forgejo.org/models/activities"
|
||||||
"forgejo.org/models/perm"
|
"forgejo.org/models/perm"
|
||||||
|
|
@ -67,14 +66,6 @@ func ToNotificationThread(ctx context.Context, n *activities_model.Notification)
|
||||||
result.Subject.State = "merged"
|
result.Subject.State = "merged"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case activities_model.NotificationSourceCommit:
|
|
||||||
url := n.Repository.HTMLURL() + "/commit/" + url.PathEscape(n.CommitID)
|
|
||||||
result.Subject = &api.NotificationSubject{
|
|
||||||
Type: api.NotifySubjectCommit,
|
|
||||||
Title: n.CommitID,
|
|
||||||
URL: url,
|
|
||||||
HTMLURL: url,
|
|
||||||
}
|
|
||||||
case activities_model.NotificationSourceRepository:
|
case activities_model.NotificationSourceRepository:
|
||||||
result.Subject = &api.NotificationSubject{
|
result.Subject = &api.NotificationSubject{
|
||||||
Type: api.NotifySubjectRepository,
|
Type: api.NotifySubjectRepository,
|
||||||
|
|
|
||||||
2
templates/swagger/v1_json.tmpl
generated
2
templates/swagger/v1_json.tmpl
generated
|
|
@ -2192,7 +2192,6 @@
|
||||||
"enum": [
|
"enum": [
|
||||||
"issue",
|
"issue",
|
||||||
"pull",
|
"pull",
|
||||||
"commit",
|
|
||||||
"repository"
|
"repository"
|
||||||
],
|
],
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
|
@ -13520,7 +13519,6 @@
|
||||||
"enum": [
|
"enum": [
|
||||||
"issue",
|
"issue",
|
||||||
"pull",
|
"pull",
|
||||||
"commit",
|
|
||||||
"repository"
|
"repository"
|
||||||
],
|
],
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue