mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-29 05:06:11 +01:00
Move all mail related codes from models to services/mailer (#7200)
* move all mail related codes from models to modules/mailer * fix lint * use DBContext instead Engine * use WithContext not WithEngine * Use DBContext instead of Engine * don't use defer when sess.Close() * move DBContext to context.go and add some methods * move mailer from modules/ to services * fix lint * fix tests * fix fmt * add gitea copyright * fix tests * don't expose db functions * make code clear * add DefaultDBContext * fix build * fix bug
This commit is contained in:
parent
730065a3dc
commit
5a438ee3c0
55
models/context.go
Normal file
55
models/context.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// Copyright 2019 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 models
|
||||||
|
|
||||||
|
// DBContext represents a db context
|
||||||
|
type DBContext struct {
|
||||||
|
e Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultDBContext represents a DBContext with default Engine
|
||||||
|
func DefaultDBContext() DBContext {
|
||||||
|
return DBContext{x}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Committer represents an interface to Commit or Close the dbcontext
|
||||||
|
type Committer interface {
|
||||||
|
Commit() error
|
||||||
|
Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxDBContext represents a transaction DBContext
|
||||||
|
func TxDBContext() (DBContext, Committer, error) {
|
||||||
|
sess := x.NewSession()
|
||||||
|
if err := sess.Begin(); err != nil {
|
||||||
|
sess.Close()
|
||||||
|
return DBContext{}, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return DBContext{sess}, sess, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithContext represents executing database operations
|
||||||
|
func WithContext(f func(ctx DBContext) error) error {
|
||||||
|
return f(DBContext{x})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithTx represents executing database operations on a trasaction
|
||||||
|
func WithTx(f func(ctx DBContext) error) error {
|
||||||
|
sess := x.NewSession()
|
||||||
|
if err := sess.Begin(); err != nil {
|
||||||
|
sess.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := f(DBContext{sess}); err != nil {
|
||||||
|
sess.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err := sess.Commit()
|
||||||
|
sess.Close()
|
||||||
|
return err
|
||||||
|
}
|
|
@ -11,6 +11,21 @@ import (
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrNotExist represents a non-exist error.
|
||||||
|
type ErrNotExist struct {
|
||||||
|
ID int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsErrNotExist checks if an error is an ErrNotExist
|
||||||
|
func IsErrNotExist(err error) bool {
|
||||||
|
_, ok := err.(ErrNotExist)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err ErrNotExist) Error() string {
|
||||||
|
return fmt.Sprintf("record does not exist [id: %d]", err.ID)
|
||||||
|
}
|
||||||
|
|
||||||
// ErrNameReserved represents a "reserved name" error.
|
// ErrNameReserved represents a "reserved name" error.
|
||||||
type ErrNameReserved struct {
|
type ErrNameReserved struct {
|
||||||
Name string
|
Name string
|
||||||
|
|
|
@ -1503,7 +1503,7 @@ func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
|
||||||
|
|
||||||
// UpdateIssueMentions extracts mentioned people from content and
|
// UpdateIssueMentions extracts mentioned people from content and
|
||||||
// updates issue-user relations for them.
|
// updates issue-user relations for them.
|
||||||
func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
func UpdateIssueMentions(ctx DBContext, issueID int64, mentions []string) error {
|
||||||
if len(mentions) == 0 {
|
if len(mentions) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1513,7 +1513,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
||||||
}
|
}
|
||||||
users := make([]*User, 0, len(mentions))
|
users := make([]*User, 0, len(mentions))
|
||||||
|
|
||||||
if err := e.In("lower_name", mentions).Asc("lower_name").Find(&users); err != nil {
|
if err := ctx.e.In("lower_name", mentions).Asc("lower_name").Find(&users); err != nil {
|
||||||
return fmt.Errorf("find mentioned users: %v", err)
|
return fmt.Errorf("find mentioned users: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1525,7 +1525,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
memberIDs := make([]int64, 0, user.NumMembers)
|
memberIDs := make([]int64, 0, user.NumMembers)
|
||||||
orgUsers, err := getOrgUsersByOrgID(e, user.ID)
|
orgUsers, err := getOrgUsersByOrgID(ctx.e, user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("GetOrgUsersByOrgID [%d]: %v", user.ID, err)
|
return fmt.Errorf("GetOrgUsersByOrgID [%d]: %v", user.ID, err)
|
||||||
}
|
}
|
||||||
|
@ -1537,7 +1537,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
||||||
ids = append(ids, memberIDs...)
|
ids = append(ids, memberIDs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := UpdateIssueUsersByMentions(e, issueID, ids); err != nil {
|
if err := UpdateIssueUsersByMentions(ctx, issueID, ids); err != nil {
|
||||||
return fmt.Errorf("UpdateIssueUsersByMentions: %v", err)
|
return fmt.Errorf("UpdateIssueUsersByMentions: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/markup"
|
|
||||||
"code.gitea.io/gitea/modules/markup/markdown"
|
"code.gitea.io/gitea/modules/markup/markdown"
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
|
@ -395,40 +394,6 @@ func (c *Comment) LoadDepIssueDetails() (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// MailParticipants sends new comment emails to repository watchers
|
|
||||||
// and mentioned people.
|
|
||||||
func (c *Comment) MailParticipants(opType ActionType, issue *Issue) (err error) {
|
|
||||||
return c.mailParticipants(x, opType, issue)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Comment) mailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
|
|
||||||
mentions := markup.FindAllMentions(c.Content)
|
|
||||||
if err = UpdateIssueMentions(e, c.IssueID, mentions); err != nil {
|
|
||||||
return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(c.Content) > 0 {
|
|
||||||
if err = mailIssueCommentToParticipants(e, issue, c.Poster, c.Content, c, mentions); err != nil {
|
|
||||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch opType {
|
|
||||||
case ActionCloseIssue:
|
|
||||||
ct := fmt.Sprintf("Closed #%d.", issue.Index)
|
|
||||||
if err = mailIssueCommentToParticipants(e, issue, c.Poster, ct, c, mentions); err != nil {
|
|
||||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
|
||||||
}
|
|
||||||
case ActionReopenIssue:
|
|
||||||
ct := fmt.Sprintf("Reopened #%d.", issue.Index)
|
|
||||||
if err = mailIssueCommentToParticipants(e, issue, c.Poster, ct, c, mentions); err != nil {
|
|
||||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Comment) loadReactions(e Engine) (err error) {
|
func (c *Comment) loadReactions(e Engine) (err error) {
|
||||||
if c.Reactions != nil {
|
if c.Reactions != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -94,22 +94,22 @@ func UpdateIssueUserByRead(uid, issueID int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
|
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
|
||||||
func UpdateIssueUsersByMentions(e Engine, issueID int64, uids []int64) error {
|
func UpdateIssueUsersByMentions(ctx DBContext, issueID int64, uids []int64) error {
|
||||||
for _, uid := range uids {
|
for _, uid := range uids {
|
||||||
iu := &IssueUser{
|
iu := &IssueUser{
|
||||||
UID: uid,
|
UID: uid,
|
||||||
IssueID: issueID,
|
IssueID: issueID,
|
||||||
}
|
}
|
||||||
has, err := e.Get(iu)
|
has, err := ctx.e.Get(iu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
iu.IsMentioned = true
|
iu.IsMentioned = true
|
||||||
if has {
|
if has {
|
||||||
_, err = e.ID(iu.ID).Cols("is_mentioned").Update(iu)
|
_, err = ctx.e.ID(iu.ID).Cols("is_mentioned").Update(iu)
|
||||||
} else {
|
} else {
|
||||||
_, err = e.Insert(iu)
|
_, err = ctx.e.Insert(iu)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -50,7 +50,7 @@ func TestUpdateIssueUsersByMentions(t *testing.T) {
|
||||||
issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
||||||
|
|
||||||
uids := []int64{2, 5}
|
uids := []int64{2, 5}
|
||||||
assert.NoError(t, UpdateIssueUsersByMentions(x, issue.ID, uids))
|
assert.NoError(t, UpdateIssueUsersByMentions(DefaultDBContext(), issue.ID, uids))
|
||||||
for _, uid := range uids {
|
for _, uid := range uids {
|
||||||
AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1")
|
AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1")
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/notification/base"
|
"code.gitea.io/gitea/modules/notification/base"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mailNotifier struct {
|
type mailNotifier struct {
|
||||||
|
@ -36,13 +37,13 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.
|
||||||
act = models.ActionCommentIssue
|
act = models.ActionCommentIssue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := comment.MailParticipants(act, issue); err != nil {
|
if err := mailer.MailParticipantsComment(comment, act, issue); err != nil {
|
||||||
log.Error("MailParticipants: %v", err)
|
log.Error("MailParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mailNotifier) NotifyNewIssue(issue *models.Issue) {
|
func (m *mailNotifier) NotifyNewIssue(issue *models.Issue) {
|
||||||
if err := issue.MailParticipants(issue.Poster, models.ActionCreateIssue); err != nil {
|
if err := mailer.MailParticipants(issue, issue.Poster, models.ActionCreateIssue); err != nil {
|
||||||
log.Error("MailParticipants: %v", err)
|
log.Error("MailParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,13 +64,13 @@ func (m *mailNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := issue.MailParticipants(doer, actionType); err != nil {
|
if err := mailer.MailParticipants(issue, doer, actionType); err != nil {
|
||||||
log.Error("MailParticipants: %v", err)
|
log.Error("MailParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mailNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
|
func (m *mailNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
|
||||||
if err := pr.Issue.MailParticipants(pr.Issue.Poster, models.ActionCreatePullRequest); err != nil {
|
if err := mailer.MailParticipants(pr.Issue, pr.Issue.Poster, models.ActionCreatePullRequest); err != nil {
|
||||||
log.Error("MailParticipants: %v", err)
|
log.Error("MailParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +84,7 @@ func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models
|
||||||
} else if comment.Type == models.CommentTypeComment {
|
} else if comment.Type == models.CommentTypeComment {
|
||||||
act = models.ActionCommentIssue
|
act = models.ActionCommentIssue
|
||||||
}
|
}
|
||||||
if err := comment.MailParticipants(act, pr.Issue); err != nil {
|
if err := mailer.MailParticipantsComment(comment, act, pr.Issue); err != nil {
|
||||||
log.Error("MailParticipants: %v", err)
|
log.Error("MailParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/process"
|
"code.gitea.io/gitea/modules/process"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
|
|
||||||
"gitea.com/macaron/macaron"
|
"gitea.com/macaron/macaron"
|
||||||
"github.com/unknwon/com"
|
"github.com/unknwon/com"
|
||||||
|
@ -197,7 +198,7 @@ func Dashboard(ctx *context.Context) {
|
||||||
func SendTestMail(ctx *context.Context) {
|
func SendTestMail(ctx *context.Context) {
|
||||||
email := ctx.Query("email")
|
email := ctx.Query("email")
|
||||||
// Send a test email to the user's email address and redirect back to Config
|
// Send a test email to the user's email address and redirect back to Config
|
||||||
if err := models.SendTestMail(email); err != nil {
|
if err := mailer.SendTestMail(email); err != nil {
|
||||||
ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
|
ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
|
||||||
} else {
|
} else {
|
||||||
ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
|
ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/routers"
|
"code.gitea.io/gitea/routers"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
|
|
||||||
"github.com/unknwon/com"
|
"github.com/unknwon/com"
|
||||||
)
|
)
|
||||||
|
@ -116,8 +117,8 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) {
|
||||||
log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
|
log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
|
||||||
|
|
||||||
// Send email notification.
|
// Send email notification.
|
||||||
if form.SendNotify && setting.MailService != nil {
|
if form.SendNotify {
|
||||||
models.SendRegisterNotifyMail(ctx.Context, u)
|
mailer.SendRegisterNotifyMail(ctx.Locale, u)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
|
ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/routers/api/v1/convert"
|
"code.gitea.io/gitea/routers/api/v1/convert"
|
||||||
"code.gitea.io/gitea/routers/api/v1/user"
|
"code.gitea.io/gitea/routers/api/v1/user"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) {
|
func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) {
|
||||||
|
@ -88,8 +88,8 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
|
||||||
log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
|
log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
|
||||||
|
|
||||||
// Send email notification.
|
// Send email notification.
|
||||||
if form.SendNotify && setting.MailService != nil {
|
if form.SendNotify {
|
||||||
models.SendRegisterNotifyMail(ctx.Context.Context, u)
|
mailer.SendRegisterNotifyMail(ctx.Locale, u)
|
||||||
}
|
}
|
||||||
ctx.JSON(201, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
|
ctx.JSON(201, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,11 @@ import (
|
||||||
"code.gitea.io/gitea/modules/highlight"
|
"code.gitea.io/gitea/modules/highlight"
|
||||||
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
|
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/mailer"
|
|
||||||
"code.gitea.io/gitea/modules/markup"
|
"code.gitea.io/gitea/modules/markup"
|
||||||
"code.gitea.io/gitea/modules/markup/external"
|
"code.gitea.io/gitea/modules/markup/external"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/ssh"
|
"code.gitea.io/gitea/modules/ssh"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
|
|
||||||
"gitea.com/macaron/macaron"
|
"gitea.com/macaron/macaron"
|
||||||
)
|
)
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
"code.gitea.io/gitea/modules/validation"
|
"code.gitea.io/gitea/modules/validation"
|
||||||
"code.gitea.io/gitea/routers/utils"
|
"code.gitea.io/gitea/routers/utils"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
|
|
||||||
"github.com/unknwon/com"
|
"github.com/unknwon/com"
|
||||||
"mvdan.cc/xurls/v2"
|
"mvdan.cc/xurls/v2"
|
||||||
|
@ -549,7 +550,7 @@ func CollaborationPost(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if setting.Service.EnableNotifyMail {
|
if setting.Service.EnableNotifyMail {
|
||||||
models.SendCollaboratorMail(u, ctx.User, ctx.Repo.Repository)
|
mailer.SendCollaboratorMail(u, ctx.User, ctx.Repo.Repository)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
|
||||||
|
|
|
@ -34,6 +34,7 @@ import (
|
||||||
"code.gitea.io/gitea/routers/repo"
|
"code.gitea.io/gitea/routers/repo"
|
||||||
"code.gitea.io/gitea/routers/user"
|
"code.gitea.io/gitea/routers/user"
|
||||||
userSetting "code.gitea.io/gitea/routers/user/setting"
|
userSetting "code.gitea.io/gitea/routers/user/setting"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
|
|
||||||
// to registers all internal adapters
|
// to registers all internal adapters
|
||||||
_ "code.gitea.io/gitea/modules/session"
|
_ "code.gitea.io/gitea/modules/session"
|
||||||
|
@ -166,7 +167,7 @@ func NewMacaron() *macaron.Macaron {
|
||||||
))
|
))
|
||||||
|
|
||||||
m.Use(templates.HTMLRenderer())
|
m.Use(templates.HTMLRenderer())
|
||||||
models.InitMailRender(templates.Mailer())
|
mailer.InitMailRender(templates.Mailer())
|
||||||
|
|
||||||
localeNames, err := options.Dir("locale")
|
localeNames, err := options.Dir("locale")
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
|
|
||||||
"gitea.com/macaron/captcha"
|
"gitea.com/macaron/captcha"
|
||||||
"github.com/markbates/goth"
|
"github.com/markbates/goth"
|
||||||
|
@ -948,7 +949,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
|
||||||
|
|
||||||
// Send confirmation email
|
// Send confirmation email
|
||||||
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
|
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
|
||||||
models.SendActivateAccountMail(ctx.Context, u)
|
mailer.SendActivateAccountMail(ctx.Locale, u)
|
||||||
|
|
||||||
ctx.Data["IsSendRegisterMail"] = true
|
ctx.Data["IsSendRegisterMail"] = true
|
||||||
ctx.Data["Email"] = u.Email
|
ctx.Data["Email"] = u.Email
|
||||||
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
||||||
|
@ -1094,7 +1096,8 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
|
||||||
|
|
||||||
// Send confirmation email, no need for social account.
|
// Send confirmation email, no need for social account.
|
||||||
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
|
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
|
||||||
models.SendActivateAccountMail(ctx.Context, u)
|
mailer.SendActivateAccountMail(ctx.Locale, u)
|
||||||
|
|
||||||
ctx.Data["IsSendRegisterMail"] = true
|
ctx.Data["IsSendRegisterMail"] = true
|
||||||
ctx.Data["Email"] = u.Email
|
ctx.Data["Email"] = u.Email
|
||||||
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
||||||
|
@ -1125,7 +1128,7 @@ func Activate(ctx *context.Context) {
|
||||||
ctx.Data["ResendLimited"] = true
|
ctx.Data["ResendLimited"] = true
|
||||||
} else {
|
} else {
|
||||||
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
||||||
models.SendActivateAccountMail(ctx.Context, ctx.User)
|
mailer.SendActivateAccountMail(ctx.Locale, ctx.User)
|
||||||
|
|
||||||
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
|
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
|
||||||
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
||||||
|
@ -1247,7 +1250,8 @@ func ForgotPasswdPost(ctx *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
models.SendResetPasswordMail(ctx.Context, u)
|
mailer.SendResetPasswordMail(ctx.Locale, u)
|
||||||
|
|
||||||
if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
|
if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
|
||||||
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/recaptcha"
|
"code.gitea.io/gitea/modules/recaptcha"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
|
|
||||||
"gitea.com/macaron/captcha"
|
"gitea.com/macaron/captcha"
|
||||||
)
|
)
|
||||||
|
@ -444,7 +445,8 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
|
||||||
|
|
||||||
// Send confirmation email, no need for social account.
|
// Send confirmation email, no need for social account.
|
||||||
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
|
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
|
||||||
models.SendActivateAccountMail(ctx.Context, u)
|
mailer.SendActivateAccountMail(ctx.Locale, u)
|
||||||
|
|
||||||
ctx.Data["IsSendRegisterMail"] = true
|
ctx.Data["IsSendRegisterMail"] = true
|
||||||
ctx.Data["Email"] = u.Email
|
ctx.Data["Email"] = u.Email
|
||||||
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
|
"code.gitea.io/gitea/services/mailer"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -130,7 +131,7 @@ func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
|
||||||
|
|
||||||
// Send confirmation email
|
// Send confirmation email
|
||||||
if setting.Service.RegisterEmailConfirm {
|
if setting.Service.RegisterEmailConfirm {
|
||||||
models.SendActivateEmailMail(ctx.Context, ctx.User, email)
|
mailer.SendActivateEmailMail(ctx.Locale, ctx.User, email)
|
||||||
|
|
||||||
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
|
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
|
||||||
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
// Copyright 2016 The Gogs Authors. All rights reserved.
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
||||||
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a MIT-style
|
// Use of this source code is governed by a MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package models
|
package mailer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -10,9 +11,9 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/base"
|
"code.gitea.io/gitea/modules/base"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/mailer"
|
|
||||||
"code.gitea.io/gitea/modules/markup"
|
"code.gitea.io/gitea/modules/markup"
|
||||||
"code.gitea.io/gitea/modules/markup/markdown"
|
"code.gitea.io/gitea/modules/markup/markdown"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
@ -42,11 +43,11 @@ func InitMailRender(tmpls *template.Template) {
|
||||||
|
|
||||||
// SendTestMail sends a test mail
|
// SendTestMail sends a test mail
|
||||||
func SendTestMail(email string) error {
|
func SendTestMail(email string) error {
|
||||||
return gomail.Send(mailer.Sender, mailer.NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
|
return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendUserMail sends a mail to the user
|
// SendUserMail sends a mail to the user
|
||||||
func SendUserMail(language string, u *User, tpl base.TplName, code, subject, info string) {
|
func SendUserMail(language string, u *models.User, tpl base.TplName, code, subject, info string) {
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"DisplayName": u.DisplayName(),
|
"DisplayName": u.DisplayName(),
|
||||||
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
|
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
|
||||||
|
@ -61,10 +62,10 @@ func SendUserMail(language string, u *User, tpl base.TplName, code, subject, inf
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
|
msg := NewMessage([]string{u.Email}, subject, content.String())
|
||||||
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
|
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
|
||||||
|
|
||||||
mailer.SendAsync(msg)
|
SendAsync(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Locale represents an interface to translation
|
// Locale represents an interface to translation
|
||||||
|
@ -74,17 +75,17 @@ type Locale interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendActivateAccountMail sends an activation mail to the user (new user registration)
|
// SendActivateAccountMail sends an activation mail to the user (new user registration)
|
||||||
func SendActivateAccountMail(locale Locale, u *User) {
|
func SendActivateAccountMail(locale Locale, u *models.User) {
|
||||||
SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
|
SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendResetPasswordMail sends a password reset mail to the user
|
// SendResetPasswordMail sends a password reset mail to the user
|
||||||
func SendResetPasswordMail(locale Locale, u *User) {
|
func SendResetPasswordMail(locale Locale, u *models.User) {
|
||||||
SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
|
SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendActivateEmailMail sends confirmation email to confirm new email address
|
// SendActivateEmailMail sends confirmation email to confirm new email address
|
||||||
func SendActivateEmailMail(locale Locale, u *User, email *EmailAddress) {
|
func SendActivateEmailMail(locale Locale, u *models.User, email *models.EmailAddress) {
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"DisplayName": u.DisplayName(),
|
"DisplayName": u.DisplayName(),
|
||||||
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
|
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
|
||||||
|
@ -99,14 +100,19 @@ func SendActivateEmailMail(locale Locale, u *User, email *EmailAddress) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := mailer.NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
|
msg := NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
|
||||||
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
|
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
|
||||||
|
|
||||||
mailer.SendAsync(msg)
|
SendAsync(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
|
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
|
||||||
func SendRegisterNotifyMail(locale Locale, u *User) {
|
func SendRegisterNotifyMail(locale Locale, u *models.User) {
|
||||||
|
if setting.MailService == nil {
|
||||||
|
log.Warn("SendRegisterNotifyMail is being invoked but mail service hasn't been initialized")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"DisplayName": u.DisplayName(),
|
"DisplayName": u.DisplayName(),
|
||||||
"Username": u.Name,
|
"Username": u.Name,
|
||||||
|
@ -119,14 +125,14 @@ func SendRegisterNotifyMail(locale Locale, u *User) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := mailer.NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
|
msg := NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
|
||||||
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
|
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
|
||||||
|
|
||||||
mailer.SendAsync(msg)
|
SendAsync(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendCollaboratorMail sends mail notification to new collaborator.
|
// SendCollaboratorMail sends mail notification to new collaborator.
|
||||||
func SendCollaboratorMail(u, doer *User, repo *Repository) {
|
func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
|
||||||
repoName := path.Join(repo.Owner.Name, repo.Name)
|
repoName := path.Join(repo.Owner.Name, repo.Name)
|
||||||
subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
|
subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
|
||||||
|
|
||||||
|
@ -143,10 +149,10 @@ func SendCollaboratorMail(u, doer *User, repo *Repository) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
|
msg := NewMessage([]string{u.Email}, subject, content.String())
|
||||||
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
|
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
|
||||||
|
|
||||||
mailer.SendAsync(msg)
|
SendAsync(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func composeTplData(subject, body, link string) map[string]interface{} {
|
func composeTplData(subject, body, link string) map[string]interface{} {
|
||||||
|
@ -157,14 +163,13 @@ func composeTplData(subject, body, link string) map[string]interface{} {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func composeIssueCommentMessage(issue *Issue, doer *User, content string, comment *Comment, tplName base.TplName, tos []string, info string) *mailer.Message {
|
func composeIssueCommentMessage(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tplName base.TplName, tos []string, info string) *Message {
|
||||||
var subject string
|
var subject string
|
||||||
if comment != nil {
|
if comment != nil {
|
||||||
subject = "Re: " + issue.mailSubject()
|
subject = "Re: " + mailSubject(issue)
|
||||||
} else {
|
} else {
|
||||||
subject = issue.mailSubject()
|
subject = mailSubject(issue)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := issue.LoadRepo()
|
err := issue.LoadRepo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("LoadRepo: %v", err)
|
log.Error("LoadRepo: %v", err)
|
||||||
|
@ -185,7 +190,7 @@ func composeIssueCommentMessage(issue *Issue, doer *User, content string, commen
|
||||||
log.Error("Template: %v", err)
|
log.Error("Template: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := mailer.NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
|
msg := NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
|
||||||
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
|
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
|
||||||
|
|
||||||
// Set Message-ID on first message so replies know what to reference
|
// Set Message-ID on first message so replies know what to reference
|
||||||
|
@ -200,18 +205,18 @@ func composeIssueCommentMessage(issue *Issue, doer *User, content string, commen
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendIssueCommentMail composes and sends issue comment emails to target receivers.
|
// SendIssueCommentMail composes and sends issue comment emails to target receivers.
|
||||||
func SendIssueCommentMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
|
func SendIssueCommentMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
|
||||||
if len(tos) == 0 {
|
if len(tos) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
|
SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendIssueMentionMail composes and sends issue mention emails to target receivers.
|
// SendIssueMentionMail composes and sends issue mention emails to target receivers.
|
||||||
func SendIssueMentionMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
|
func SendIssueMentionMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
|
||||||
if len(tos) == 0 {
|
if len(tos) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
|
SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
|
||||||
}
|
}
|
47
services/mailer/mail_comment.go
Normal file
47
services/mailer/mail_comment.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
// Copyright 2019 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 mailer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models"
|
||||||
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"code.gitea.io/gitea/modules/markup"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MailParticipantsComment sends new comment emails to repository watchers
|
||||||
|
// and mentioned people.
|
||||||
|
func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue) error {
|
||||||
|
return mailParticipantsComment(models.DefaultDBContext(), c, opType, issue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mailParticipantsComment(ctx models.DBContext, c *models.Comment, opType models.ActionType, issue *models.Issue) (err error) {
|
||||||
|
mentions := markup.FindAllMentions(c.Content)
|
||||||
|
if err = models.UpdateIssueMentions(ctx, c.IssueID, mentions); err != nil {
|
||||||
|
return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.Content) > 0 {
|
||||||
|
if err = mailIssueCommentToParticipants(issue, c.Poster, c.Content, c, mentions); err != nil {
|
||||||
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch opType {
|
||||||
|
case models.ActionCloseIssue:
|
||||||
|
ct := fmt.Sprintf("Closed #%d.", issue.Index)
|
||||||
|
if err = mailIssueCommentToParticipants(issue, c.Poster, ct, c, mentions); err != nil {
|
||||||
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||||
|
}
|
||||||
|
case models.ActionReopenIssue:
|
||||||
|
ct := fmt.Sprintf("Reopened #%d.", issue.Index)
|
||||||
|
if err = mailIssueCommentToParticipants(issue, c.Poster, ct, c, mentions); err != nil {
|
||||||
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -1,13 +1,13 @@
|
||||||
// Copyright 2016 The Gogs Authors. All rights reserved.
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||||
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a MIT-style
|
// Use of this source code is governed by a MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package models
|
package mailer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/markup"
|
"code.gitea.io/gitea/modules/markup"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
@ -15,7 +15,7 @@ import (
|
||||||
"github.com/unknwon/com"
|
"github.com/unknwon/com"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (issue *Issue) mailSubject() string {
|
func mailSubject(issue *models.Issue) string {
|
||||||
return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index)
|
return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,23 +23,23 @@ func (issue *Issue) mailSubject() string {
|
||||||
// This function sends two list of emails:
|
// This function sends two list of emails:
|
||||||
// 1. Repository watchers and users who are participated in comments.
|
// 1. Repository watchers and users who are participated in comments.
|
||||||
// 2. Users who are not in 1. but get mentioned in current issue/comment.
|
// 2. Users who are not in 1. but get mentioned in current issue/comment.
|
||||||
func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content string, comment *Comment, mentions []string) error {
|
func mailIssueCommentToParticipants(issue *models.Issue, doer *models.User, content string, comment *models.Comment, mentions []string) error {
|
||||||
if !setting.Service.EnableNotifyMail {
|
if !setting.Service.EnableNotifyMail {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
watchers, err := getWatchers(e, issue.RepoID)
|
watchers, err := models.GetWatchers(issue.RepoID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err)
|
return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err)
|
||||||
}
|
}
|
||||||
participants, err := getParticipantsByIssueID(e, issue.ID)
|
participants, err := models.GetParticipantsByIssueID(issue.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
|
return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case the issue poster is not watching the repository and is active,
|
// In case the issue poster is not watching the repository and is active,
|
||||||
// even if we have duplicated in watchers, can be safely filtered out.
|
// even if we have duplicated in watchers, can be safely filtered out.
|
||||||
err = issue.loadPoster(e)
|
err = issue.LoadPoster()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
|
return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assignees must receive any communications
|
// Assignees must receive any communications
|
||||||
assignees, err := getAssigneesByIssue(e, issue)
|
assignees, err := models.GetAssigneesByIssue(issue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -66,11 +66,11 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
to, err := getUserByID(e, watchers[i].UserID)
|
to, err := models.GetUserByID(watchers[i].UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
|
return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
|
||||||
}
|
}
|
||||||
if to.IsOrganization() || to.EmailNotifications() != EmailNotificationsEnabled {
|
if to.IsOrganization() || to.EmailNotifications() != models.EmailNotificationsEnabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
|
||||||
for i := range participants {
|
for i := range participants {
|
||||||
if participants[i].ID == doer.ID ||
|
if participants[i].ID == doer.ID ||
|
||||||
com.IsSliceContainsStr(names, participants[i].Name) ||
|
com.IsSliceContainsStr(names, participants[i].Name) ||
|
||||||
participants[i].EmailNotifications() != EmailNotificationsEnabled {
|
participants[i].EmailNotifications() != models.EmailNotificationsEnabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
|
||||||
names = append(names, participants[i].Name)
|
names = append(names, participants[i].Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := issue.loadRepo(e); err != nil {
|
if err := issue.LoadRepo(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
|
||||||
tos = append(tos, mentions[i])
|
tos = append(tos, mentions[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
emails := getUserEmailsByNames(e, tos)
|
emails := models.GetUserEmailsByNames(tos)
|
||||||
|
|
||||||
for _, to := range emails {
|
for _, to := range emails {
|
||||||
SendIssueMentionMail(issue, doer, content, comment, []string{to})
|
SendIssueMentionMail(issue, doer, content, comment, []string{to})
|
||||||
|
@ -118,39 +118,39 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
|
||||||
|
|
||||||
// MailParticipants sends new issue thread created emails to repository watchers
|
// MailParticipants sends new issue thread created emails to repository watchers
|
||||||
// and mentioned people.
|
// and mentioned people.
|
||||||
func (issue *Issue) MailParticipants(doer *User, opType ActionType) (err error) {
|
func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType) error {
|
||||||
return issue.mailParticipants(x, doer, opType)
|
return mailParticipants(models.DefaultDBContext(), issue, doer, opType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (issue *Issue) mailParticipants(e Engine, doer *User, opType ActionType) (err error) {
|
func mailParticipants(ctx models.DBContext, issue *models.Issue, doer *models.User, opType models.ActionType) (err error) {
|
||||||
mentions := markup.FindAllMentions(issue.Content)
|
mentions := markup.FindAllMentions(issue.Content)
|
||||||
|
|
||||||
if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
|
if err = models.UpdateIssueMentions(ctx, issue.ID, mentions); err != nil {
|
||||||
return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
|
return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(issue.Content) > 0 {
|
if len(issue.Content) > 0 {
|
||||||
if err = mailIssueCommentToParticipants(e, issue, doer, issue.Content, nil, mentions); err != nil {
|
if err = mailIssueCommentToParticipants(issue, doer, issue.Content, nil, mentions); err != nil {
|
||||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch opType {
|
switch opType {
|
||||||
case ActionCreateIssue, ActionCreatePullRequest:
|
case models.ActionCreateIssue, models.ActionCreatePullRequest:
|
||||||
if len(issue.Content) == 0 {
|
if len(issue.Content) == 0 {
|
||||||
ct := fmt.Sprintf("Created #%d.", issue.Index)
|
ct := fmt.Sprintf("Created #%d.", issue.Index)
|
||||||
if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
|
if err = mailIssueCommentToParticipants(issue, doer, ct, nil, mentions); err != nil {
|
||||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case ActionCloseIssue, ActionClosePullRequest:
|
case models.ActionCloseIssue, models.ActionClosePullRequest:
|
||||||
ct := fmt.Sprintf("Closed #%d.", issue.Index)
|
ct := fmt.Sprintf("Closed #%d.", issue.Index)
|
||||||
if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
|
if err = mailIssueCommentToParticipants(issue, doer, ct, nil, mentions); err != nil {
|
||||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||||
}
|
}
|
||||||
case ActionReopenIssue, ActionReopenPullRequest:
|
case models.ActionReopenIssue, models.ActionReopenPullRequest:
|
||||||
ct := fmt.Sprintf("Reopened #%d.", issue.Index)
|
ct := fmt.Sprintf("Reopened #%d.", issue.Index)
|
||||||
if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
|
if err = mailIssueCommentToParticipants(issue, doer, ct, nil, mentions); err != nil {
|
||||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,12 +2,13 @@
|
||||||
// Use of this source code is governed by a MIT-style
|
// Use of this source code is governed by a MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package models
|
package mailer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -33,16 +34,18 @@ const tmpl = `
|
||||||
`
|
`
|
||||||
|
|
||||||
func TestComposeIssueCommentMessage(t *testing.T) {
|
func TestComposeIssueCommentMessage(t *testing.T) {
|
||||||
assert.NoError(t, PrepareTestDatabase())
|
assert.NoError(t, models.PrepareTestDatabase())
|
||||||
var MailService setting.Mailer
|
var mailService = setting.Mailer{
|
||||||
|
From: "test@gitea.com",
|
||||||
|
}
|
||||||
|
|
||||||
MailService.From = "test@gitea.com"
|
setting.MailService = &mailService
|
||||||
setting.MailService = &MailService
|
setting.Domain = "localhost"
|
||||||
|
|
||||||
doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
|
||||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, Owner: doer}).(*Repository)
|
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
|
||||||
issue := AssertExistsAndLoadBean(t, &Issue{ID: 1, Repo: repo, Poster: doer}).(*Issue)
|
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
|
||||||
comment := AssertExistsAndLoadBean(t, &Comment{ID: 2, Issue: issue}).(*Comment)
|
comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
|
||||||
|
|
||||||
email := template.Must(template.New("issue/comment").Parse(tmpl))
|
email := template.Must(template.New("issue/comment").Parse(tmpl))
|
||||||
InitMailRender(email)
|
InitMailRender(email)
|
||||||
|
@ -54,22 +57,23 @@ func TestComposeIssueCommentMessage(t *testing.T) {
|
||||||
inreplyTo := msg.GetHeader("In-Reply-To")
|
inreplyTo := msg.GetHeader("In-Reply-To")
|
||||||
references := msg.GetHeader("References")
|
references := msg.GetHeader("References")
|
||||||
|
|
||||||
assert.Equal(t, subject[0], "Re: "+issue.mailSubject(), "Comment reply subject should contain Re:")
|
assert.Equal(t, subject[0], "Re: "+mailSubject(issue), "Comment reply subject should contain Re:")
|
||||||
assert.Equal(t, inreplyTo[0], "<user2/repo1/issues/1@localhost>", "In-Reply-To header doesn't match")
|
assert.Equal(t, inreplyTo[0], "<user2/repo1/issues/1@localhost>", "In-Reply-To header doesn't match")
|
||||||
assert.Equal(t, references[0], "<user2/repo1/issues/1@localhost>", "References header doesn't match")
|
assert.Equal(t, references[0], "<user2/repo1/issues/1@localhost>", "References header doesn't match")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestComposeIssueMessage(t *testing.T) {
|
func TestComposeIssueMessage(t *testing.T) {
|
||||||
assert.NoError(t, PrepareTestDatabase())
|
assert.NoError(t, models.PrepareTestDatabase())
|
||||||
var MailService setting.Mailer
|
var mailService = setting.Mailer{
|
||||||
|
From: "test@gitea.com",
|
||||||
|
}
|
||||||
|
|
||||||
MailService.From = "test@gitea.com"
|
setting.MailService = &mailService
|
||||||
setting.MailService = &MailService
|
setting.Domain = "localhost"
|
||||||
|
|
||||||
doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
|
||||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, Owner: doer}).(*Repository)
|
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
|
||||||
issue := AssertExistsAndLoadBean(t, &Issue{ID: 1, Repo: repo, Poster: doer}).(*Issue)
|
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
|
||||||
|
|
||||||
email := template.Must(template.New("issue/comment").Parse(tmpl))
|
email := template.Must(template.New("issue/comment").Parse(tmpl))
|
||||||
InitMailRender(email)
|
InitMailRender(email)
|
||||||
|
@ -80,7 +84,7 @@ func TestComposeIssueMessage(t *testing.T) {
|
||||||
subject := msg.GetHeader("Subject")
|
subject := msg.GetHeader("Subject")
|
||||||
messageID := msg.GetHeader("Message-ID")
|
messageID := msg.GetHeader("Message-ID")
|
||||||
|
|
||||||
assert.Equal(t, subject[0], issue.mailSubject(), "Subject not equal to issue.mailSubject()")
|
assert.Equal(t, subject[0], mailSubject(issue), "Subject not equal to issue.mailSubject()")
|
||||||
assert.Nil(t, msg.GetHeader("In-Reply-To"))
|
assert.Nil(t, msg.GetHeader("In-Reply-To"))
|
||||||
assert.Nil(t, msg.GetHeader("References"))
|
assert.Nil(t, msg.GetHeader("References"))
|
||||||
assert.Equal(t, messageID[0], "<user2/repo1/issues/1@localhost>", "Message-ID header doesn't match")
|
assert.Equal(t, messageID[0], "<user2/repo1/issues/1@localhost>", "Message-ID header doesn't match")
|
16
services/mailer/main_test.go
Normal file
16
services/mailer/main_test.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2019 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 mailer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
models.MainTest(m, filepath.Join("..", ".."))
|
||||||
|
}
|
Loading…
Reference in a new issue