Skip to content

Commit b19ec55

Browse files
committed
Creating notifications on new issue
1 parent 0f1b484 commit b19ec55

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

models/notification.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,83 @@ func (n *Notification) BeforeUpdate() {
5252
n.Updated = now
5353
n.UpdatedUnix = nowUnix
5454
}
55+
56+
func CreateOrUpdateIssueNotifications(issue *Issue) error {
57+
watches, err := getWatchers(x, issue.RepoID)
58+
if err != nil {
59+
return err
60+
}
61+
62+
sess := x.NewSession()
63+
if err := sess.Begin(); err != nil {
64+
return err
65+
}
66+
67+
defer sess.Close()
68+
69+
for _, watch := range watches {
70+
exists, err := issueNotificationExists(sess, watch.UserID, watch.RepoID)
71+
if err != nil {
72+
return err
73+
}
74+
75+
if exists {
76+
err = updateIssueNotification(sess, watch.UserID, issue.ID)
77+
} else {
78+
err = createIssueNotification(sess, watch.UserID, issue)
79+
}
80+
81+
if err != nil {
82+
return err
83+
}
84+
}
85+
86+
return sess.Commit()
87+
}
88+
89+
func issueNotificationExists(e Engine, userID, issueID int64) (bool, error) {
90+
count, err := e.
91+
Where("user_id = ?", userID).
92+
And("issue_id = ?", issueID).
93+
Count(Notification{})
94+
return count > 0, err
95+
}
96+
97+
func createIssueNotification(e Engine, userID int64, issue *Issue) error {
98+
notification := &Notification{
99+
UserID: userID,
100+
RepoID: issue.RepoID,
101+
Status: NotificationStatusUnread,
102+
IssueID: issue.ID,
103+
}
104+
105+
if issue.IsPull {
106+
notification.Source = NotificationSourcePullRequest
107+
} else {
108+
notification.Source = NotificationSourceIssue
109+
}
110+
111+
_, err := e.Insert(notification)
112+
return err
113+
}
114+
115+
func updateIssueNotification(e Engine, userID, issueID int64) error {
116+
notification, err := getIssueNotification(e, userID, issueID)
117+
if err != nil {
118+
return err
119+
}
120+
121+
notification.Status = NotificationStatusUnread
122+
123+
_, err = e.Id(notification.ID).Update(notification)
124+
return err
125+
}
126+
127+
func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error) {
128+
notification := new(Notification)
129+
_, err := e.
130+
Where("user_id = ?").
131+
And("issue_id = ?", issueID).
132+
Get(notification)
133+
return notification, err
134+
}

routers/repo/issue.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
453453
return
454454
}
455455

456+
if err := models.CreateOrUpdateIssueNotifications(issue); err != nil {
457+
ctx.Handle(500, "CreateOrUpdateIssueNotifications", err)
458+
return
459+
}
460+
456461
log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
457462
ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
458463
}

0 commit comments

Comments
 (0)