Fix bug with sqlite load read (#26305) (#26339)

Backport #26305 by @lunny

Possible fix #26280

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
(cherry picked from commit 9be9042479)
This commit is contained in:
Giteabot 2023-08-05 15:35:08 +08:00 committed by Earl Warren
parent e5c26e38f5
commit 33c52556a3
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
4 changed files with 25 additions and 10 deletions

View file

@ -319,7 +319,7 @@ func createIssueNotification(ctx context.Context, userID int64, issue *issues_mo
} }
func updateIssueNotification(ctx context.Context, userID, issueID, commentID, updatedByID int64) error { func updateIssueNotification(ctx context.Context, userID, issueID, commentID, updatedByID int64) error {
notification, err := getIssueNotification(ctx, userID, issueID) notification, err := GetIssueNotification(ctx, userID, issueID)
if err != nil { if err != nil {
return err return err
} }
@ -340,7 +340,8 @@ func updateIssueNotification(ctx context.Context, userID, issueID, commentID, up
return err return err
} }
func getIssueNotification(ctx context.Context, userID, issueID int64) (*Notification, error) { // GetIssueNotification return the notification about an issue
func GetIssueNotification(ctx context.Context, userID, issueID int64) (*Notification, error) {
notification := new(Notification) notification := new(Notification)
_, err := db.GetEngine(ctx). _, err := db.GetEngine(ctx).
Where("user_id = ?", userID). Where("user_id = ?", userID).
@ -751,7 +752,7 @@ func GetUIDsAndNotificationCounts(since, until timeutil.TimeStamp) ([]UserIDCoun
// SetIssueReadBy sets issue to be read by given user. // SetIssueReadBy sets issue to be read by given user.
func SetIssueReadBy(ctx context.Context, issueID, userID int64) error { func SetIssueReadBy(ctx context.Context, issueID, userID int64) error {
if err := issues_model.UpdateIssueUserByRead(userID, issueID); err != nil { if err := issues_model.UpdateIssueUserByRead(ctx, userID, issueID); err != nil {
return err return err
} }
@ -759,7 +760,7 @@ func SetIssueReadBy(ctx context.Context, issueID, userID int64) error {
} }
func setIssueNotificationStatusReadIfUnread(ctx context.Context, userID, issueID int64) error { func setIssueNotificationStatusReadIfUnread(ctx context.Context, userID, issueID int64) error {
notification, err := getIssueNotification(ctx, userID, issueID) notification, err := GetIssueNotification(ctx, userID, issueID)
// ignore if not exists // ignore if not exists
if err != nil { if err != nil {
return nil return nil
@ -771,7 +772,7 @@ func setIssueNotificationStatusReadIfUnread(ctx context.Context, userID, issueID
notification.Status = NotificationStatusRead notification.Status = NotificationStatusRead
_, err = db.GetEngine(ctx).ID(notification.ID).Update(notification) _, err = db.GetEngine(ctx).ID(notification.ID).Cols("status").Update(notification)
return err return err
} }

View file

@ -4,6 +4,7 @@
package activities_test package activities_test
import ( import (
"context"
"testing" "testing"
activities_model "code.gitea.io/gitea/models/activities" activities_model "code.gitea.io/gitea/models/activities"
@ -109,3 +110,16 @@ func TestUpdateNotificationStatuses(t *testing.T) {
unittest.AssertExistsAndLoadBean(t, unittest.AssertExistsAndLoadBean(t,
&activities_model.Notification{ID: notfPinned.ID, Status: activities_model.NotificationStatusPinned}) &activities_model.Notification{ID: notfPinned.ID, Status: activities_model.NotificationStatusPinned})
} }
func TestSetIssueReadBy(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
assert.NoError(t, db.WithTx(db.DefaultContext, func(ctx context.Context) error {
return activities_model.SetIssueReadBy(ctx, issue.ID, user.ID)
}))
nt, err := activities_model.GetIssueNotification(db.DefaultContext, user.ID, issue.ID)
assert.NoError(t, err)
assert.EqualValues(t, activities_model.NotificationStatusRead, nt.Status)
}

View file

@ -55,8 +55,8 @@ func NewIssueUsers(ctx context.Context, repo *repo_model.Repository, issue *Issu
} }
// UpdateIssueUserByRead updates issue-user relation for reading. // UpdateIssueUserByRead updates issue-user relation for reading.
func UpdateIssueUserByRead(uid, issueID int64) error { func UpdateIssueUserByRead(ctx context.Context, uid, issueID int64) error {
_, err := db.GetEngine(db.DefaultContext).Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID) _, err := db.GetEngine(ctx).Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
return err return err
} }

View file

@ -40,13 +40,13 @@ func TestUpdateIssueUserByRead(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}) issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
assert.NoError(t, issues_model.UpdateIssueUserByRead(4, issue.ID)) assert.NoError(t, issues_model.UpdateIssueUserByRead(db.DefaultContext, 4, issue.ID))
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1")
assert.NoError(t, issues_model.UpdateIssueUserByRead(4, issue.ID)) assert.NoError(t, issues_model.UpdateIssueUserByRead(db.DefaultContext, 4, issue.ID))
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1")
assert.NoError(t, issues_model.UpdateIssueUserByRead(unittest.NonexistentID, unittest.NonexistentID)) assert.NoError(t, issues_model.UpdateIssueUserByRead(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID))
} }
func TestUpdateIssueUsersByMentions(t *testing.T) { func TestUpdateIssueUsersByMentions(t *testing.T) {