mirror of
https://gitee.com/gitea/gitea
synced 2025-11-21 00:40:26 +08:00
Creating notifications on new issue
This commit is contained in:
@@ -52,3 +52,83 @@ func (n *Notification) BeforeUpdate() {
|
|||||||
n.Updated = now
|
n.Updated = now
|
||||||
n.UpdatedUnix = nowUnix
|
n.UpdatedUnix = nowUnix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateOrUpdateIssueNotifications(issue *Issue) error {
|
||||||
|
watches, err := getWatchers(x, issue.RepoID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sess := x.NewSession()
|
||||||
|
if err := sess.Begin(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer sess.Close()
|
||||||
|
|
||||||
|
for _, watch := range watches {
|
||||||
|
exists, err := issueNotificationExists(sess, watch.UserID, watch.RepoID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
err = updateIssueNotification(sess, watch.UserID, issue.ID)
|
||||||
|
} else {
|
||||||
|
err = createIssueNotification(sess, watch.UserID, issue)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sess.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func issueNotificationExists(e Engine, userID, issueID int64) (bool, error) {
|
||||||
|
count, err := e.
|
||||||
|
Where("user_id = ?", userID).
|
||||||
|
And("issue_id = ?", issueID).
|
||||||
|
Count(Notification{})
|
||||||
|
return count > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func createIssueNotification(e Engine, userID int64, issue *Issue) error {
|
||||||
|
notification := &Notification{
|
||||||
|
UserID: userID,
|
||||||
|
RepoID: issue.RepoID,
|
||||||
|
Status: NotificationStatusUnread,
|
||||||
|
IssueID: issue.ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
if issue.IsPull {
|
||||||
|
notification.Source = NotificationSourcePullRequest
|
||||||
|
} else {
|
||||||
|
notification.Source = NotificationSourceIssue
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := e.Insert(notification)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateIssueNotification(e Engine, userID, issueID int64) error {
|
||||||
|
notification, err := getIssueNotification(e, userID, issueID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
notification.Status = NotificationStatusUnread
|
||||||
|
|
||||||
|
_, err = e.Id(notification.ID).Update(notification)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error) {
|
||||||
|
notification := new(Notification)
|
||||||
|
_, err := e.
|
||||||
|
Where("user_id = ?").
|
||||||
|
And("issue_id = ?", issueID).
|
||||||
|
Get(notification)
|
||||||
|
return notification, err
|
||||||
|
}
|
||||||
|
|||||||
@@ -453,6 +453,11 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := models.CreateOrUpdateIssueNotifications(issue); err != nil {
|
||||||
|
ctx.Handle(500, "CreateOrUpdateIssueNotifications", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
|
log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
|
||||||
ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
|
ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user