rename to notifier

This commit is contained in:
Lunny Xiao 2018-05-21 22:42:22 +08:00
parent fc62dea471
commit 3888095a74
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
13 changed files with 174 additions and 133 deletions

View File

@ -16,7 +16,6 @@ import (
// register supported doc types
_ "code.gitea.io/gitea/modules/markup/markdown"
_ "code.gitea.io/gitea/modules/markup/orgmode"
_ "code.gitea.io/gitea/modules/notification/init"
"github.com/urfave/cli"
)

View File

@ -715,8 +715,9 @@ func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
return transferRepoAction(x, doer, oldOwner, repo)
}
func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
return notifyWatchers(e, &Action{
// MergePullRequestAction adds new action for merging pull request.
func MergePullRequestAction(doer *User, repo *Repository, issue *Issue) error {
return notifyWatchers(x, &Action{
ActUserID: doer.ID,
ActUser: doer,
OpType: ActionMergePullRequest,
@ -727,11 +728,6 @@ func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue
})
}
// MergePullRequestAction adds new action for merging pull request.
func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
return mergePullRequestAction(x, actUser, repo, pull)
}
// GetFeedsOptions options for retrieving feeds
type GetFeedsOptions struct {
RequestedUser *User

View File

@ -720,36 +720,9 @@ func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
}
// UpdateComment updates information of comment.
func UpdateComment(doer *User, c *Comment, oldContent string) error {
func UpdateComment(doer *User, c *Comment) error {
if _, err := x.ID(c.ID).AllCols().Update(c); err != nil {
return err
} else if c.Type == CommentTypeComment {
UpdateIssueIndexer(c.IssueID)
}
if err := c.LoadIssue(); err != nil {
return err
}
if err := c.Issue.LoadAttributes(); err != nil {
return err
}
mode, _ := AccessLevel(doer.ID, c.Issue.Repo)
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: c.Issue.APIFormat(),
Comment: c.APIFormat(),
Changes: &api.ChangesPayload{
Body: &api.ChangesFromPayload{
From: oldContent,
},
},
Repository: c.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error(2, "PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
} else {
go HookQueue.Add(c.Issue.Repo.ID)
}
return nil

View File

@ -10,28 +10,29 @@ import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/base"
)
type actionReceiver struct {
type actionNotifier struct {
}
var (
receiver notification.NotifyReceiver = &actionReceiver{}
_ base.Notifier = &actionNotifier{}
)
func init() {
notification.RegisterReceiver(receiver)
// NewNotifier returns a new actionNotifier
func NewNotifier() *actionNotifier {
return &actionNotifier{}
}
func (r *actionReceiver) Run() {}
func (r *actionNotifier) Run() {}
func (r *actionReceiver) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
func (r *actionNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
panic("not implementation")
}
func (r *actionReceiver) NotifyNewIssue(issue *models.Issue) {
func (r *actionNotifier) NotifyNewIssue(issue *models.Issue) {
if err := models.NotifyWatchers(&models.Action{
ActUserID: issue.Poster.ID,
ActUser: issue.Poster,
@ -45,11 +46,11 @@ func (r *actionReceiver) NotifyNewIssue(issue *models.Issue) {
}
}
func (r *actionReceiver) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
func (r *actionNotifier) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
panic("not implementation")
}
func (r *actionReceiver) NotifyNewPullRequest(pr *models.PullRequest) {
func (r *actionNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
issue := pr.Issue
if err := models.NotifyWatchers(&models.Action{
ActUserID: issue.Poster.ID,
@ -64,8 +65,11 @@ func (r *actionReceiver) NotifyNewPullRequest(pr *models.PullRequest) {
}
}
func (r *actionReceiver) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
func (r *actionNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
if err := models.MergePullRequestAction(doer, pr.Issue.Repo, pr.Issue); err != nil {
log.Error(4, "MergePullRequestAction [%d]: %v", pr.ID, err)
}
}
func (r *actionNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
}

View File

@ -0,0 +1,22 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package base
import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
)
// Notifier defines an interface to notify receiver
type Notifier interface {
Run()
NotifyCreateIssueComment(*models.User, *models.Repository,
*models.Issue, *models.Comment)
NotifyNewIssue(*models.Issue)
NotifyCloseIssue(*models.Issue, *models.User)
NotifyMergePullRequest(*models.PullRequest, *models.User, *git.Repository)
NotifyNewPullRequest(*models.PullRequest)
NotifyUpdateComment(*models.User, *models.Comment, string)
}

View File

@ -7,39 +7,45 @@ package indexer
import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/base"
)
type indexerReceiver struct {
type indexerNotifier struct {
}
var (
receiver notification.NotifyReceiver = &indexerReceiver{}
_ base.Notifier = &indexerNotifier{}
)
func init() {
notification.RegisterReceiver(receiver)
// NewNotifier create a new indexerNotifier notifier
func NewNotifier() *indexerNotifier {
return &indexerNotifier{}
}
func (r *indexerReceiver) Run() {
func (r *indexerNotifier) Run() {
}
func (r *indexerReceiver) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
func (r *indexerNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
}
func (r *indexerReceiver) NotifyNewIssue(issue *models.Issue) {
func (r *indexerNotifier) NotifyNewIssue(issue *models.Issue) {
models.UpdateIssueIndexer(issue.ID)
}
func (r *indexerReceiver) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
func (r *indexerNotifier) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
}
func (r *indexerReceiver) NotifyNewPullRequest(pr *models.PullRequest) {
func (r *indexerNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
models.UpdateIssueIndexer(pr.Issue.ID)
}
func (r *indexerReceiver) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
func (r *indexerNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
}
func (r *indexerNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
if c.Type == models.CommentTypeComment {
models.UpdateIssueIndexer(c.IssueID)
}
}

View File

@ -1,10 +0,0 @@
package init
import (
// Import all needed receivers
_ "code.gitea.io/gitea/modules/notification/action"
_ "code.gitea.io/gitea/modules/notification/indexer"
_ "code.gitea.io/gitea/modules/notification/mail"
_ "code.gitea.io/gitea/modules/notification/ui"
_ "code.gitea.io/gitea/modules/notification/webhook"
)

View File

@ -8,45 +8,49 @@ import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/base"
)
type mailReceiver struct {
type mailNotifier struct {
}
var (
receiver notification.NotifyReceiver = &mailReceiver{}
_ base.Notifier = &mailNotifier{}
)
func init() {
notification.RegisterReceiver(receiver)
// NewNotifier create a new mailNotifier notifier
func NewNotifier() *mailNotifier {
return &mailNotifier{}
}
func (m *mailReceiver) Run() {
func (m *mailNotifier) Run() {
}
func (m *mailReceiver) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
panic("not implementation")
}
func (m *mailReceiver) NotifyNewIssue(issue *models.Issue) {
func (m *mailNotifier) NotifyNewIssue(issue *models.Issue) {
if err := issue.MailParticipants(); err != nil {
log.Error(4, "MailParticipants: %v", err)
}
}
func (m *mailReceiver) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
func (m *mailNotifier) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
if err := issue.MailParticipants(); err != nil {
log.Error(4, "MailParticipants: %v", err)
}
}
func (m *mailReceiver) NotifyNewPullRequest(pr *models.PullRequest) {
func (m *mailNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
if err := pr.Issue.MailParticipants(); err != nil {
log.Error(4, "MailParticipants: %v", err)
}
}
func (m *mailReceiver) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
func (m *mailNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
}
func (m *mailNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
}

View File

@ -7,61 +7,71 @@ package notification
import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/notification/action"
"code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/notification/indexer"
"code.gitea.io/gitea/modules/notification/mail"
"code.gitea.io/gitea/modules/notification/ui"
"code.gitea.io/gitea/modules/notification/webhook"
)
// NotifyReceiver defines an interface to notify receiver
type NotifyReceiver interface {
Run()
NotifyCreateIssueComment(*models.User, *models.Repository,
*models.Issue, *models.Comment)
NotifyNewIssue(*models.Issue)
NotifyCloseIssue(*models.Issue, *models.User)
NotifyMergePullRequest(*models.PullRequest, *models.User, *git.Repository)
NotifyNewPullRequest(*models.PullRequest)
}
var (
notifyReceivers []NotifyReceiver
notifiers []base.Notifier
)
// RegisterReceiver providers method to receive notify messages
func RegisterReceiver(receiver NotifyReceiver) {
go receiver.Run()
notifyReceivers = append(notifyReceivers, receiver)
// RegisterNotifier providers method to receive notify messages
func RegisterNotifier(notifier base.Notifier) {
go notifier.Run()
notifiers = append(notifiers, notifier)
}
// NotifyCreateIssueComment notifies issue comment related message to receivers
func init() {
RegisterNotifier(webhook.NewNotifier())
RegisterNotifier(ui.NewNotifier())
RegisterNotifier(mail.NewNotifier())
RegisterNotifier(indexer.NewNotifier())
RegisterNotifier(action.NewNotifier())
}
// NotifyCreateIssueComment notifies issue comment related message to notifiers
func NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
for _, receiver := range notifyReceivers {
receiver.NotifyCreateIssueComment(doer, repo, issue, comment)
for _, notifier := range notifiers {
notifier.NotifyCreateIssueComment(doer, repo, issue, comment)
}
}
// NotifyNewIssue notifies new issue to receivers
// NotifyNewIssue notifies new issue to notifiers
func NotifyNewIssue(issue *models.Issue) {
for _, receiver := range notifyReceivers {
receiver.NotifyNewIssue(issue)
for _, notifier := range notifiers {
notifier.NotifyNewIssue(issue)
}
}
// NotifyCloseIssue notifies close issue to receivers
// NotifyCloseIssue notifies close issue to notifiers
func NotifyCloseIssue(issue *models.Issue, doer *models.User) {
for _, receiver := range notifyReceivers {
receiver.NotifyCloseIssue(issue, doer)
for _, notifier := range notifiers {
notifier.NotifyCloseIssue(issue, doer)
}
}
// NotifyMergePullRequest notifies merge pull request to receivers
// NotifyMergePullRequest notifies merge pull request to notifiers
func NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository) {
for _, receiver := range notifyReceivers {
receiver.NotifyMergePullRequest(pr, doer, baseGitRepo)
for _, notifier := range notifiers {
notifier.NotifyMergePullRequest(pr, doer, baseGitRepo)
}
}
// NotifyNewPullRequest notifies new pull request to receivers
// NotifyNewPullRequest notifies new pull request to notifiers
func NotifyNewPullRequest(pr *models.PullRequest) {
for _, receiver := range notifyReceivers {
receiver.NotifyNewPullRequest(pr)
for _, notifier := range notifiers {
notifier.NotifyNewPullRequest(pr)
}
}
// NotifyUpdateComment notifies update comment to notifiers
func NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
for _, notifier := range notifiers {
notifier.NotifyUpdateComment(doer, c, oldContent)
}
}

View File

@ -8,7 +8,7 @@ import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/base"
)
type (
@ -23,21 +23,20 @@ type (
)
var (
// service is the notification service
service = &notificationService{
issueQueue: make(chan issueNotificationOpts, 100),
}
_ notification.NotifyReceiver = &notificationService{}
_ base.Notifier = &notificationService{}
)
func init() {
notification.RegisterReceiver(service)
// NewNotifier create a new notificationService notifier
func NewNotifier() *notificationService {
return &notificationService{
issueQueue: make(chan issueNotificationOpts, 100),
}
}
func (ns *notificationService) Run() {
for {
select {
case opts := <-service.issueQueue:
case opts := <-ns.issueQueue:
if err := models.CreateOrUpdateIssueNotifications(opts.issue, opts.notificationAuthorID); err != nil {
log.Error(4, "Was unable to create issue notification: %v", err)
}
@ -76,3 +75,6 @@ func (ns *notificationService) NotifyNewPullRequest(pr *models.PullRequest) {
pr.Issue.PosterID,
}
}
func (ns *notificationService) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
}

View File

@ -8,27 +8,28 @@ import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/sdk/gitea"
)
type webhookReceiver struct {
type webhookNotifier struct {
}
var (
receiver notification.NotifyReceiver = &webhookReceiver{}
_ base.Notifier = &webhookNotifier{}
)
func init() {
notification.RegisterReceiver(receiver)
// NewNotifier returns a new webhookNotifier
func NewNotifier() *webhookNotifier {
return &webhookNotifier{}
}
func (w *webhookReceiver) Run() {
func (w *webhookNotifier) Run() {
}
func (w *webhookReceiver) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
func (w *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
mode, _ := models.AccessLevel(doer.ID, repo)
if err := models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{
@ -45,7 +46,7 @@ func (w *webhookReceiver) NotifyCreateIssueComment(doer *models.User, repo *mode
}
// NotifyNewIssue implements notification.Receiver
func (w *webhookReceiver) NotifyNewIssue(issue *models.Issue) {
func (w *webhookNotifier) NotifyNewIssue(issue *models.Issue) {
mode, _ := models.AccessLevel(issue.Poster.ID, issue.Repo)
if err := models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
Action: api.HookIssueOpened,
@ -61,11 +62,11 @@ func (w *webhookReceiver) NotifyNewIssue(issue *models.Issue) {
}
// NotifyCloseIssue implements notification.Receiver
func (w *webhookReceiver) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
func (w *webhookNotifier) NotifyCloseIssue(issue *models.Issue, doer *models.User) {
panic("not implements")
}
func (w *webhookReceiver) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository) {
func (w *webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository) {
mode, _ := models.AccessLevel(doer.ID, pr.Issue.Repo)
if err := models.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueClosed,
@ -111,7 +112,7 @@ func (w *webhookReceiver) NotifyMergePullRequest(pr *models.PullRequest, doer *m
}
}
func (w *webhookReceiver) NotifyNewPullRequest(pr *models.PullRequest) {
func (w *webhookNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
mode, _ := models.AccessLevel(pr.Issue.Poster.ID, pr.Issue.Repo)
if err := models.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueOpened,
@ -125,3 +126,32 @@ func (w *webhookReceiver) NotifyNewPullRequest(pr *models.PullRequest) {
go models.HookQueue.Add(pr.Issue.Repo.ID)
}
}
func (w *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
if err := c.LoadIssue(); err != nil {
log.Error(2, "LoadIssue [comment_id: %d]: %v", c.ID, err)
return
}
if err := c.Issue.LoadAttributes(); err != nil {
log.Error(2, "Issue.LoadAttributes [comment_id: %d]: %v", c.ID, err)
return
}
mode, _ := models.AccessLevel(doer.ID, c.Issue.Repo)
if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: c.Issue.APIFormat(),
Comment: c.APIFormat(),
Changes: &api.ChangesPayload{
Body: &api.ChangesFromPayload{
From: oldContent,
},
},
Repository: c.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error(2, "PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
} else {
go models.HookQueue.Add(c.Issue.Repo.ID)
}
}

View File

@ -266,10 +266,13 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
oldContent := comment.Content
comment.Content = form.Body
if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil {
if err := models.UpdateComment(ctx.User, comment); err != nil {
ctx.Error(500, "UpdateComment", err)
return
}
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
ctx.JSON(200, comment.APIFormat())
}

View File

@ -1094,11 +1094,13 @@ func UpdateCommentContent(ctx *context.Context) {
})
return
}
if err = models.UpdateComment(ctx.User, comment, oldContent); err != nil {
if err = models.UpdateComment(ctx.User, comment); err != nil {
ctx.ServerError("UpdateComment", err)
return
}
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
ctx.JSON(200, map[string]interface{}{
"content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
})