From 4163c6dc68d88df590d58b46f9d343f3b34afc30 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 20 Jun 2025 15:54:29 -0700 Subject: [PATCH] Fix bug --- models/activities/notification.go | 15 +++++++- models/activities/notification_list.go | 1 + routers/web/repo/commit.go | 9 +++++ services/uinotification/notify.go | 37 ++++++------------- .../user/notification/notification_div.tmpl | 2 +- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/models/activities/notification.go b/models/activities/notification.go index 4abacf47793..5931bd3cb2d 100644 --- a/models/activities/notification.go +++ b/models/activities/notification.go @@ -149,10 +149,11 @@ func CreateCommitNotifications(ctx context.Context, doerID, repoID int64, commit return db.Insert(ctx, notification) } -func CreateOrUpdateReleaseNotifications(ctx context.Context, doerID, releaseID, receiverID int64) error { +func CreateOrUpdateReleaseNotifications(ctx context.Context, doerID, repoID, releaseID, receiverID int64) error { notification := new(Notification) if _, err := db.GetEngine(ctx). Where("user_id = ?", receiverID). + And("repo_id = ?", repoID). And("release_id = ?", releaseID). Get(notification); err != nil { return err @@ -166,6 +167,7 @@ func CreateOrUpdateReleaseNotifications(ctx context.Context, doerID, releaseID, notification = &Notification{ Source: NotificationSourceRelease, + RepoID: repoID, UserID: receiverID, Status: NotificationStatusUnread, ReleaseID: releaseID, @@ -427,6 +429,17 @@ func SetReleaseReadBy(ctx context.Context, releaseID, userID int64) error { return err } +// SetCommitReadBy sets issue to be read by given user. +func SetCommitReadBy(ctx context.Context, repoID, userID int64, commitID string) error { + _, err := db.GetEngine(ctx).Where(builder.Eq{ + "user_id": userID, + "status": NotificationStatusUnread, + "source": NotificationSourceCommit, + "commit_id": commitID, + }).Cols("status").Update(&Notification{Status: NotificationStatusRead}) + return err +} + // SetNotificationStatus change the notification status func SetNotificationStatus(ctx context.Context, notificationID int64, user *user_model.User, status NotificationStatus) (*Notification, error) { notification, err := GetNotificationByID(ctx, notificationID) diff --git a/models/activities/notification_list.go b/models/activities/notification_list.go index 6f3c3812205..a95ad638b65 100644 --- a/models/activities/notification_list.go +++ b/models/activities/notification_list.go @@ -492,6 +492,7 @@ func (nl NotificationList) LoadReleases(ctx context.Context) ([]int, error) { failures = append(failures, i) continue } + notification.Release.Repo = notification.Repository } } return failures, nil diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index b3af138461c..51e10ffc47e 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -12,6 +12,7 @@ import ( "path" "strings" + activities_model "code.gitea.io/gitea/models/activities" asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" @@ -301,6 +302,14 @@ func Diff(ctx *context.Context) { commitID = commit.ID.String() } + if ctx.IsSigned { + err = activities_model.SetCommitReadBy(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID, commitID) + if err != nil { + ctx.ServerError("SetReleaseReadBy", err) + return + } + } + fileOnly := ctx.FormBool("file-only") maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles files := ctx.FormStrings("files") diff --git a/services/uinotification/notify.go b/services/uinotification/notify.go index 47353d20211..80e6b40aeaa 100644 --- a/services/uinotification/notify.go +++ b/services/uinotification/notify.go @@ -68,11 +68,11 @@ func handler(items ...notificationOpts) []notificationOpts { log.Error("CreateRepoTransferNotification: %v", err) } case activities_model.NotificationSourceCommit: - if err := activities_model.CreateCommitNotifications(db.DefaultContext, opts.RepoID, opts.NotificationAuthorID, opts.CommitID, opts.ReceiverID); err != nil { + if err := activities_model.CreateCommitNotifications(db.DefaultContext, opts.NotificationAuthorID, opts.RepoID, opts.CommitID, opts.ReceiverID); err != nil { log.Error("Was unable to create commit notification: %v", err) } case activities_model.NotificationSourceRelease: - if err := activities_model.CreateOrUpdateReleaseNotifications(db.DefaultContext, opts.NotificationAuthorID, opts.ReleaseID, opts.ReceiverID); err != nil { + if err := activities_model.CreateOrUpdateReleaseNotifications(db.DefaultContext, opts.NotificationAuthorID, opts.RepoID, opts.ReleaseID, opts.ReceiverID); err != nil { log.Error("Was unable to create release notification: %v", err) } case activities_model.NotificationSourceIssue, activities_model.NotificationSourcePullRequest: @@ -96,6 +96,7 @@ func (ns *notificationService) CreateIssueComment(ctx context.Context, doer *use opts := notificationOpts{ Source: util.Iif(issue.IsPull, activities_model.NotificationSourcePullRequest, activities_model.NotificationSourceIssue), IssueID: issue.ID, + RepoID: issue.RepoID, NotificationAuthorID: doer.ID, } if comment != nil { @@ -103,30 +104,22 @@ func (ns *notificationService) CreateIssueComment(ctx context.Context, doer *use } _ = ns.queue.Push(opts) for _, mention := range mentions { - opts := notificationOpts{ - IssueID: issue.ID, - NotificationAuthorID: doer.ID, - ReceiverID: mention.ID, - } - if comment != nil { - opts.CommentID = comment.ID - } + opts.ReceiverID = mention.ID _ = ns.queue.Push(opts) } } func (ns *notificationService) NewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) { - _ = ns.queue.Push(notificationOpts{ + opts := notificationOpts{ Source: activities_model.NotificationSourceIssue, + RepoID: issue.RepoID, IssueID: issue.ID, NotificationAuthorID: issue.Poster.ID, - }) + } + _ = ns.queue.Push(opts) for _, mention := range mentions { - _ = ns.queue.Push(notificationOpts{ - IssueID: issue.ID, - NotificationAuthorID: issue.Poster.ID, - ReceiverID: mention.ID, - }) + opts.ReceiverID = mention.ID + _ = ns.queue.Push(opts) } } @@ -212,14 +205,7 @@ func (ns *notificationService) PullRequestReview(ctx context.Context, pr *issues } _ = ns.queue.Push(opts) for _, mention := range mentions { - opts := notificationOpts{ - IssueID: pr.Issue.ID, - NotificationAuthorID: r.Reviewer.ID, - ReceiverID: mention.ID, - } - if c != nil { - opts.CommentID = c.ID - } + opts.ReceiverID = mention.ID _ = ns.queue.Push(opts) } } @@ -366,6 +352,7 @@ func (ns *notificationService) NewRelease(ctx context.Context, rel *repo_model.R func (ns *notificationService) UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { opts := notificationOpts{ Source: activities_model.NotificationSourceRelease, + RepoID: rel.RepoID, ReleaseID: rel.ID, NotificationAuthorID: rel.PublisherID, } diff --git a/templates/user/notification/notification_div.tmpl b/templates/user/notification/notification_div.tmpl index 342f432ae0f..eb24288a4b8 100644 --- a/templates/user/notification/notification_div.tmpl +++ b/templates/user/notification/notification_div.tmpl @@ -72,7 +72,7 @@ {{if .Issue}} {{DateUtils.TimeSince .Issue.UpdatedUnix}} {{else if .Release}} - {{DateUtils.TimeSince .Release.UpdatedUnix}} + {{DateUtils.TimeSince .Release.CreatedUnix}} {{else if .Commit}} {{DateUtils.TimeSince .Commit.Committer.When}} {{else}}