From 5a438ee3c0303efcb9d1935ff521917fe8a109e8 Mon Sep 17 00:00:00 2001
From: Lunny Xiao <xiaolunwen@gmail.com>
Date: Tue, 24 Sep 2019 13:02:49 +0800
Subject: [PATCH] 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
---
 models/context.go                             | 55 ++++++++++++++++++
 models/error.go                               | 15 +++++
 models/issue.go                               |  8 +--
 models/issue_comment.go                       | 35 ------------
 models/issue_user.go                          |  8 +--
 models/issue_user_test.go                     |  2 +-
 modules/notification/mail/mail.go             | 11 ++--
 routers/admin/admin.go                        |  3 +-
 routers/admin/users.go                        |  5 +-
 routers/api/v1/admin/user.go                  |  6 +-
 routers/init.go                               |  2 +-
 routers/repo/setting.go                       |  3 +-
 routers/routes/routes.go                      |  3 +-
 routers/user/auth.go                          | 12 ++--
 routers/user/auth_openid.go                   |  4 +-
 routers/user/setting/account.go               |  3 +-
 {models => services/mailer}/mail.go           | 57 ++++++++++---------
 services/mailer/mail_comment.go               | 47 +++++++++++++++
 .../mailer/mail_issue.go                      | 50 ++++++++--------
 {models => services/mailer}/mail_test.go      | 42 +++++++-------
 {modules => services}/mailer/mailer.go        |  0
 services/mailer/main_test.go                  | 16 ++++++
 22 files changed, 253 insertions(+), 134 deletions(-)
 create mode 100644 models/context.go
 rename {models => services/mailer}/mail.go (72%)
 create mode 100644 services/mailer/mail_comment.go
 rename models/issue_mail.go => services/mailer/mail_issue.go (65%)
 rename {models => services/mailer}/mail_test.go (57%)
 rename {modules => services}/mailer/mailer.go (100%)
 create mode 100644 services/mailer/main_test.go

diff --git a/models/context.go b/models/context.go
new file mode 100644
index 0000000000..5f47c595a2
--- /dev/null
+++ b/models/context.go
@@ -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
+}
diff --git a/models/error.go b/models/error.go
index c025437c50..9974287a0a 100644
--- a/models/error.go
+++ b/models/error.go
@@ -11,6 +11,21 @@ import (
 	"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.
 type ErrNameReserved struct {
 	Name string
diff --git a/models/issue.go b/models/issue.go
index bb2db94407..c4b15e9ddf 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -1503,7 +1503,7 @@ func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
 
 // UpdateIssueMentions extracts mentioned people from content and
 // 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 {
 		return nil
 	}
@@ -1513,7 +1513,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
 	}
 	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)
 	}
 
@@ -1525,7 +1525,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
 		}
 
 		memberIDs := make([]int64, 0, user.NumMembers)
-		orgUsers, err := getOrgUsersByOrgID(e, user.ID)
+		orgUsers, err := getOrgUsersByOrgID(ctx.e, user.ID)
 		if err != nil {
 			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...)
 	}
 
-	if err := UpdateIssueUsersByMentions(e, issueID, ids); err != nil {
+	if err := UpdateIssueUsersByMentions(ctx, issueID, ids); err != nil {
 		return fmt.Errorf("UpdateIssueUsersByMentions: %v", err)
 	}
 
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 0bb313c30b..6786032f41 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -12,7 +12,6 @@ import (
 
 	"code.gitea.io/gitea/modules/git"
 	"code.gitea.io/gitea/modules/log"
-	"code.gitea.io/gitea/modules/markup"
 	"code.gitea.io/gitea/modules/markup/markdown"
 	api "code.gitea.io/gitea/modules/structs"
 	"code.gitea.io/gitea/modules/timeutil"
@@ -395,40 +394,6 @@ func (c *Comment) LoadDepIssueDetails() (err error) {
 	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) {
 	if c.Reactions != nil {
 		return nil
diff --git a/models/issue_user.go b/models/issue_user.go
index 58eb5117f8..d55a0dc2fb 100644
--- a/models/issue_user.go
+++ b/models/issue_user.go
@@ -94,22 +94,22 @@ func UpdateIssueUserByRead(uid, issueID int64) error {
 }
 
 // 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 {
 		iu := &IssueUser{
 			UID:     uid,
 			IssueID: issueID,
 		}
-		has, err := e.Get(iu)
+		has, err := ctx.e.Get(iu)
 		if err != nil {
 			return err
 		}
 
 		iu.IsMentioned = true
 		if has {
-			_, err = e.ID(iu.ID).Cols("is_mentioned").Update(iu)
+			_, err = ctx.e.ID(iu.ID).Cols("is_mentioned").Update(iu)
 		} else {
-			_, err = e.Insert(iu)
+			_, err = ctx.e.Insert(iu)
 		}
 		if err != nil {
 			return err
diff --git a/models/issue_user_test.go b/models/issue_user_test.go
index a333bc8619..a57ab33f9e 100644
--- a/models/issue_user_test.go
+++ b/models/issue_user_test.go
@@ -50,7 +50,7 @@ func TestUpdateIssueUsersByMentions(t *testing.T) {
 	issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
 
 	uids := []int64{2, 5}
-	assert.NoError(t, UpdateIssueUsersByMentions(x, issue.ID, uids))
+	assert.NoError(t, UpdateIssueUsersByMentions(DefaultDBContext(), issue.ID, uids))
 	for _, uid := range uids {
 		AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1")
 	}
diff --git a/modules/notification/mail/mail.go b/modules/notification/mail/mail.go
index 9d0db4f415..e1ae391f78 100644
--- a/modules/notification/mail/mail.go
+++ b/modules/notification/mail/mail.go
@@ -8,6 +8,7 @@ import (
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/notification/base"
+	"code.gitea.io/gitea/services/mailer"
 )
 
 type mailNotifier struct {
@@ -36,13 +37,13 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.
 		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)
 	}
 }
 
 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)
 	}
 }
@@ -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)
 	}
 }
 
 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)
 	}
 }
@@ -83,7 +84,7 @@ func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models
 	} else if comment.Type == models.CommentTypeComment {
 		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)
 	}
 }
diff --git a/routers/admin/admin.go b/routers/admin/admin.go
index 496aca375f..538d01f9a4 100644
--- a/routers/admin/admin.go
+++ b/routers/admin/admin.go
@@ -22,6 +22,7 @@ import (
 	"code.gitea.io/gitea/modules/process"
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/modules/timeutil"
+	"code.gitea.io/gitea/services/mailer"
 
 	"gitea.com/macaron/macaron"
 	"github.com/unknwon/com"
@@ -197,7 +198,7 @@ func Dashboard(ctx *context.Context) {
 func SendTestMail(ctx *context.Context) {
 	email := ctx.Query("email")
 	// 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))
 	} else {
 		ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
diff --git a/routers/admin/users.go b/routers/admin/users.go
index aa595edad9..660f116682 100644
--- a/routers/admin/users.go
+++ b/routers/admin/users.go
@@ -14,6 +14,7 @@ import (
 	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/routers"
+	"code.gitea.io/gitea/services/mailer"
 
 	"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)
 
 	// Send email notification.
-	if form.SendNotify && setting.MailService != nil {
-		models.SendRegisterNotifyMail(ctx.Context, u)
+	if form.SendNotify {
+		mailer.SendRegisterNotifyMail(ctx.Locale, u)
 	}
 
 	ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go
index f4b694aa22..70076b626b 100644
--- a/routers/api/v1/admin/user.go
+++ b/routers/api/v1/admin/user.go
@@ -9,10 +9,10 @@ import (
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/context"
 	"code.gitea.io/gitea/modules/log"
-	"code.gitea.io/gitea/modules/setting"
 	api "code.gitea.io/gitea/modules/structs"
 	"code.gitea.io/gitea/routers/api/v1/convert"
 	"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) {
@@ -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)
 
 	// Send email notification.
-	if form.SendNotify && setting.MailService != nil {
-		models.SendRegisterNotifyMail(ctx.Context.Context, u)
+	if form.SendNotify {
+		mailer.SendRegisterNotifyMail(ctx.Locale, u)
 	}
 	ctx.JSON(201, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
 }
diff --git a/routers/init.go b/routers/init.go
index fdf90904ce..5c8741f244 100644
--- a/routers/init.go
+++ b/routers/init.go
@@ -16,11 +16,11 @@ import (
 	"code.gitea.io/gitea/modules/highlight"
 	issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
 	"code.gitea.io/gitea/modules/log"
-	"code.gitea.io/gitea/modules/mailer"
 	"code.gitea.io/gitea/modules/markup"
 	"code.gitea.io/gitea/modules/markup/external"
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/modules/ssh"
+	"code.gitea.io/gitea/services/mailer"
 
 	"gitea.com/macaron/macaron"
 )
diff --git a/routers/repo/setting.go b/routers/repo/setting.go
index 91db519d62..b0af396e7d 100644
--- a/routers/repo/setting.go
+++ b/routers/repo/setting.go
@@ -24,6 +24,7 @@ import (
 	"code.gitea.io/gitea/modules/timeutil"
 	"code.gitea.io/gitea/modules/validation"
 	"code.gitea.io/gitea/routers/utils"
+	"code.gitea.io/gitea/services/mailer"
 
 	"github.com/unknwon/com"
 	"mvdan.cc/xurls/v2"
@@ -549,7 +550,7 @@ func CollaborationPost(ctx *context.Context) {
 	}
 
 	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"))
diff --git a/routers/routes/routes.go b/routers/routes/routes.go
index 93ce220b00..e18ac0923e 100644
--- a/routers/routes/routes.go
+++ b/routers/routes/routes.go
@@ -34,6 +34,7 @@ import (
 	"code.gitea.io/gitea/routers/repo"
 	"code.gitea.io/gitea/routers/user"
 	userSetting "code.gitea.io/gitea/routers/user/setting"
+	"code.gitea.io/gitea/services/mailer"
 
 	// to registers all internal adapters
 	_ "code.gitea.io/gitea/modules/session"
@@ -166,7 +167,7 @@ func NewMacaron() *macaron.Macaron {
 	))
 
 	m.Use(templates.HTMLRenderer())
-	models.InitMailRender(templates.Mailer())
+	mailer.InitMailRender(templates.Mailer())
 
 	localeNames, err := options.Dir("locale")
 
diff --git a/routers/user/auth.go b/routers/user/auth.go
index dedb9c7b0d..3def867f64 100644
--- a/routers/user/auth.go
+++ b/routers/user/auth.go
@@ -21,6 +21,7 @@ import (
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/modules/timeutil"
 	"code.gitea.io/gitea/modules/util"
+	"code.gitea.io/gitea/services/mailer"
 
 	"gitea.com/macaron/captcha"
 	"github.com/markbates/goth"
@@ -948,7 +949,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
 
 	// Send confirmation email
 	if setting.Service.RegisterEmailConfirm && u.ID > 1 {
-		models.SendActivateAccountMail(ctx.Context, u)
+		mailer.SendActivateAccountMail(ctx.Locale, u)
+
 		ctx.Data["IsSendRegisterMail"] = true
 		ctx.Data["Email"] = u.Email
 		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.
 	if setting.Service.RegisterEmailConfirm && u.ID > 1 {
-		models.SendActivateAccountMail(ctx.Context, u)
+		mailer.SendActivateAccountMail(ctx.Locale, u)
+
 		ctx.Data["IsSendRegisterMail"] = true
 		ctx.Data["Email"] = u.Email
 		ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
@@ -1125,7 +1128,7 @@ func Activate(ctx *context.Context) {
 				ctx.Data["ResendLimited"] = true
 			} else {
 				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 {
 					log.Error("Set cache(MailResendLimit) fail: %v", err)
@@ -1247,7 +1250,8 @@ func ForgotPasswdPost(ctx *context.Context) {
 		return
 	}
 
-	models.SendResetPasswordMail(ctx.Context, u)
+	mailer.SendResetPasswordMail(ctx.Locale, u)
+
 	if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
 		log.Error("Set cache(MailResendLimit) fail: %v", err)
 	}
diff --git a/routers/user/auth_openid.go b/routers/user/auth_openid.go
index 832766b16c..ccaea8264f 100644
--- a/routers/user/auth_openid.go
+++ b/routers/user/auth_openid.go
@@ -18,6 +18,7 @@ import (
 	"code.gitea.io/gitea/modules/recaptcha"
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/modules/timeutil"
+	"code.gitea.io/gitea/services/mailer"
 
 	"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.
 	if setting.Service.RegisterEmailConfirm && u.ID > 1 {
-		models.SendActivateAccountMail(ctx.Context, u)
+		mailer.SendActivateAccountMail(ctx.Locale, u)
+
 		ctx.Data["IsSendRegisterMail"] = true
 		ctx.Data["Email"] = u.Email
 		ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
diff --git a/routers/user/setting/account.go b/routers/user/setting/account.go
index 674606cce8..71d98fd3b9 100644
--- a/routers/user/setting/account.go
+++ b/routers/user/setting/account.go
@@ -15,6 +15,7 @@ import (
 	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/modules/timeutil"
+	"code.gitea.io/gitea/services/mailer"
 )
 
 const (
@@ -130,7 +131,7 @@ func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
 
 	// Send confirmation email
 	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 {
 			log.Error("Set cache(MailResendLimit) fail: %v", err)
diff --git a/models/mail.go b/services/mailer/mail.go
similarity index 72%
rename from models/mail.go
rename to services/mailer/mail.go
index 2c72818cec..3f0a789dc4 100644
--- a/models/mail.go
+++ b/services/mailer/mail.go
@@ -1,8 +1,9 @@
 // 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
 // license that can be found in the LICENSE file.
 
-package models
+package mailer
 
 import (
 	"bytes"
@@ -10,9 +11,9 @@ import (
 	"html/template"
 	"path"
 
+	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/base"
 	"code.gitea.io/gitea/modules/log"
-	"code.gitea.io/gitea/modules/mailer"
 	"code.gitea.io/gitea/modules/markup"
 	"code.gitea.io/gitea/modules/markup/markdown"
 	"code.gitea.io/gitea/modules/setting"
@@ -42,11 +43,11 @@ func InitMailRender(tmpls *template.Template) {
 
 // SendTestMail sends a test mail
 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
-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{}{
 		"DisplayName":       u.DisplayName(),
 		"ActiveCodeLives":   timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
@@ -61,10 +62,10 @@ func SendUserMail(language string, u *User, tpl base.TplName, code, subject, inf
 		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)
 
-	mailer.SendAsync(msg)
+	SendAsync(msg)
 }
 
 // Locale represents an interface to translation
@@ -74,17 +75,17 @@ type Locale interface {
 }
 
 // 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")
 }
 
 // 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")
 }
 
 // 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{}{
 		"DisplayName":     u.DisplayName(),
 		"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
@@ -99,14 +100,19 @@ func SendActivateEmailMail(locale Locale, u *User, email *EmailAddress) {
 		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)
 
-	mailer.SendAsync(msg)
+	SendAsync(msg)
 }
 
 // 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{}{
 		"DisplayName": u.DisplayName(),
 		"Username":    u.Name,
@@ -119,14 +125,14 @@ func SendRegisterNotifyMail(locale Locale, u *User) {
 		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)
 
-	mailer.SendAsync(msg)
+	SendAsync(msg)
 }
 
 // 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)
 	subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
 
@@ -143,10 +149,10 @@ func SendCollaboratorMail(u, doer *User, repo *Repository) {
 		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)
 
-	mailer.SendAsync(msg)
+	SendAsync(msg)
 }
 
 func composeTplData(subject, body, link string) map[string]interface{} {
@@ -157,14 +163,13 @@ func composeTplData(subject, body, link string) map[string]interface{} {
 	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
 	if comment != nil {
-		subject = "Re: " + issue.mailSubject()
+		subject = "Re: " + mailSubject(issue)
 	} else {
-		subject = issue.mailSubject()
+		subject = mailSubject(issue)
 	}
-
 	err := issue.LoadRepo()
 	if err != nil {
 		log.Error("LoadRepo: %v", err)
@@ -185,7 +190,7 @@ func composeIssueCommentMessage(issue *Issue, doer *User, content string, commen
 		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)
 
 	// 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.
-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 {
 		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.
-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 {
 		return
 	}
-	mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
+	SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
 }
diff --git a/services/mailer/mail_comment.go b/services/mailer/mail_comment.go
new file mode 100644
index 0000000000..cb477f887b
--- /dev/null
+++ b/services/mailer/mail_comment.go
@@ -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
+}
diff --git a/models/issue_mail.go b/services/mailer/mail_issue.go
similarity index 65%
rename from models/issue_mail.go
rename to services/mailer/mail_issue.go
index 87d991e500..92d2c5a879 100644
--- a/models/issue_mail.go
+++ b/services/mailer/mail_issue.go
@@ -1,13 +1,13 @@
-// Copyright 2016 The Gogs Authors. All rights reserved.
-// Copyright 2018 The Gitea Authors. All rights reserved.
+// 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
+package mailer
 
 import (
 	"fmt"
 
+	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/markup"
 	"code.gitea.io/gitea/modules/setting"
@@ -15,7 +15,7 @@ import (
 	"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)
 }
 
@@ -23,23 +23,23 @@ func (issue *Issue) mailSubject() string {
 // This function sends two list of emails:
 // 1. Repository watchers and users who are participated in comments.
 // 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 {
 		return nil
 	}
 
-	watchers, err := getWatchers(e, issue.RepoID)
+	watchers, err := models.GetWatchers(issue.RepoID)
 	if err != nil {
 		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 {
 		return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
 	}
 
 	// 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.
-	err = issue.loadPoster(e)
+	err = issue.LoadPoster()
 	if err != nil {
 		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, err := getAssigneesByIssue(e, issue)
+	assignees, err := models.GetAssigneesByIssue(issue)
 	if err != nil {
 		return err
 	}
@@ -66,11 +66,11 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
 			continue
 		}
 
-		to, err := getUserByID(e, watchers[i].UserID)
+		to, err := models.GetUserByID(watchers[i].UserID)
 		if err != nil {
 			return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
 		}
-		if to.IsOrganization() || to.EmailNotifications() != EmailNotificationsEnabled {
+		if to.IsOrganization() || to.EmailNotifications() != models.EmailNotificationsEnabled {
 			continue
 		}
 
@@ -80,7 +80,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
 	for i := range participants {
 		if participants[i].ID == doer.ID ||
 			com.IsSliceContainsStr(names, participants[i].Name) ||
-			participants[i].EmailNotifications() != EmailNotificationsEnabled {
+			participants[i].EmailNotifications() != models.EmailNotificationsEnabled {
 			continue
 		}
 
@@ -88,7 +88,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
 		names = append(names, participants[i].Name)
 	}
 
-	if err := issue.loadRepo(e); err != nil {
+	if err := issue.LoadRepo(); err != nil {
 		return err
 	}
 
@@ -107,7 +107,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
 		tos = append(tos, mentions[i])
 	}
 
-	emails := getUserEmailsByNames(e, tos)
+	emails := models.GetUserEmailsByNames(tos)
 
 	for _, to := range emails {
 		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
 // and mentioned people.
-func (issue *Issue) MailParticipants(doer *User, opType ActionType) (err error) {
-	return issue.mailParticipants(x, doer, opType)
+func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType) error {
+	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)
 
-	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)
 	}
 
 	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)
 		}
 	}
 
 	switch opType {
-	case ActionCreateIssue, ActionCreatePullRequest:
+	case models.ActionCreateIssue, models.ActionCreatePullRequest:
 		if len(issue.Content) == 0 {
 			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)
 			}
 		}
-	case ActionCloseIssue, ActionClosePullRequest:
+	case models.ActionCloseIssue, models.ActionClosePullRequest:
 		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)
 		}
-	case ActionReopenIssue, ActionReopenPullRequest:
+	case models.ActionReopenIssue, models.ActionReopenPullRequest:
 		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)
 		}
 	}
diff --git a/models/mail_test.go b/services/mailer/mail_test.go
similarity index 57%
rename from models/mail_test.go
rename to services/mailer/mail_test.go
index 51c52427f0..c7a84d6b33 100644
--- a/models/mail_test.go
+++ b/services/mailer/mail_test.go
@@ -2,12 +2,13 @@
 // Use of this source code is governed by a MIT-style
 // license that can be found in the LICENSE file.
 
-package models
+package mailer
 
 import (
 	"html/template"
 	"testing"
 
+	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/setting"
 
 	"github.com/stretchr/testify/assert"
@@ -33,16 +34,18 @@ const tmpl = `
 `
 
 func TestComposeIssueCommentMessage(t *testing.T) {
-	assert.NoError(t, PrepareTestDatabase())
-	var MailService setting.Mailer
+	assert.NoError(t, models.PrepareTestDatabase())
+	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)
-	repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, Owner: doer}).(*Repository)
-	issue := AssertExistsAndLoadBean(t, &Issue{ID: 1, Repo: repo, Poster: doer}).(*Issue)
-	comment := AssertExistsAndLoadBean(t, &Comment{ID: 2, Issue: issue}).(*Comment)
+	doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+	repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
+	issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
+	comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment)
 
 	email := template.Must(template.New("issue/comment").Parse(tmpl))
 	InitMailRender(email)
@@ -54,22 +57,23 @@ func TestComposeIssueCommentMessage(t *testing.T) {
 	inreplyTo := msg.GetHeader("In-Reply-To")
 	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, references[0], "<user2/repo1/issues/1@localhost>", "References header doesn't match")
-
 }
 
 func TestComposeIssueMessage(t *testing.T) {
-	assert.NoError(t, PrepareTestDatabase())
-	var MailService setting.Mailer
+	assert.NoError(t, models.PrepareTestDatabase())
+	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)
-	repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, Owner: doer}).(*Repository)
-	issue := AssertExistsAndLoadBean(t, &Issue{ID: 1, Repo: repo, Poster: doer}).(*Issue)
+	doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+	repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository)
+	issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue)
 
 	email := template.Must(template.New("issue/comment").Parse(tmpl))
 	InitMailRender(email)
@@ -80,7 +84,7 @@ func TestComposeIssueMessage(t *testing.T) {
 	subject := msg.GetHeader("Subject")
 	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("References"))
 	assert.Equal(t, messageID[0], "<user2/repo1/issues/1@localhost>", "Message-ID header doesn't match")
diff --git a/modules/mailer/mailer.go b/services/mailer/mailer.go
similarity index 100%
rename from modules/mailer/mailer.go
rename to services/mailer/mailer.go
diff --git a/services/mailer/main_test.go b/services/mailer/main_test.go
new file mode 100644
index 0000000000..bbd121024e
--- /dev/null
+++ b/services/mailer/main_test.go
@@ -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("..", ".."))
+}