Skip to content

Commit

Permalink
Split mail sender sub package from mailer service package (#32618)
Browse files Browse the repository at this point in the history
Move all mail sender related codes into a sub package of
services/mailer. Just move, no code change.
Then we just have dependencies on go-mail package in the new sub
package. We can use other package to replace it because it's
unmaintainable. ref #18664
  • Loading branch information
lunny authored Nov 30, 2024
1 parent fd3aa5b commit 79d593a
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 405 deletions.
3 changes: 2 additions & 1 deletion routers/private/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/mailer"
sender_service "code.gitea.io/gitea/services/mailer/sender"
)

// SendEmail pushes messages to mail queue
Expand Down Expand Up @@ -81,7 +82,7 @@ func SendEmail(ctx *context.PrivateContext) {

func sendEmail(ctx *context.PrivateContext, subject, message string, to []string) {
for _, email := range to {
msg := mailer.NewMessage(email, subject, message)
msg := sender_service.NewMessage(email, subject, message)
mailer.SendAsync(msg)
}

Expand Down
19 changes: 9 additions & 10 deletions services/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/translation"
incoming_payload "code.gitea.io/gitea/services/mailer/incoming/payload"
sender_service "code.gitea.io/gitea/services/mailer/sender"
"code.gitea.io/gitea/services/mailer/token"

"gopkg.in/gomail.v2"
)

const (
Expand Down Expand Up @@ -60,7 +59,7 @@ func SendTestMail(email string) error {
// No mail service configured
return nil
}
return gomail.Send(Sender, NewMessage(email, "Gitea Test Email!", "Gitea Test Email!").ToMessage())
return sender_service.Send(sender, sender_service.NewMessage(email, "Gitea Test Email!", "Gitea Test Email!"))
}

// sendUserMail sends a mail to the user
Expand All @@ -82,7 +81,7 @@ func sendUserMail(language string, u *user_model.User, tpl base.TplName, code, s
return
}

msg := NewMessage(u.EmailTo(), subject, content.String())
msg := sender_service.NewMessage(u.EmailTo(), subject, content.String())
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)

SendAsync(msg)
Expand Down Expand Up @@ -130,7 +129,7 @@ func SendActivateEmailMail(u *user_model.User, email string) {
return
}

msg := NewMessage(email, locale.TrString("mail.activate_email"), content.String())
msg := sender_service.NewMessage(email, locale.TrString("mail.activate_email"), content.String())
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)

SendAsync(msg)
Expand Down Expand Up @@ -158,7 +157,7 @@ func SendRegisterNotifyMail(u *user_model.User) {
return
}

msg := NewMessage(u.EmailTo(), locale.TrString("mail.register_notify", setting.AppName), content.String())
msg := sender_service.NewMessage(u.EmailTo(), locale.TrString("mail.register_notify", setting.AppName), content.String())
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)

SendAsync(msg)
Expand Down Expand Up @@ -189,13 +188,13 @@ func SendCollaboratorMail(u, doer *user_model.User, repo *repo_model.Repository)
return
}

msg := NewMessage(u.EmailTo(), subject, content.String())
msg := sender_service.NewMessage(u.EmailTo(), subject, content.String())
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)

SendAsync(msg)
}

func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipients []*user_model.User, fromMention bool, info string) ([]*Message, error) {
func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipients []*user_model.User, fromMention bool, info string) ([]*sender_service.Message, error) {
var (
subject string
link string
Expand Down Expand Up @@ -304,9 +303,9 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
return nil, err
}

msgs := make([]*Message, 0, len(recipients))
msgs := make([]*sender_service.Message, 0, len(recipients))
for _, recipient := range recipients {
msg := NewMessageFrom(
msg := sender_service.NewMessageFrom(
recipient.Email,
fromDisplayName(ctx.Doer),
setting.MailService.FromEmail,
Expand Down
5 changes: 3 additions & 2 deletions services/mailer/mail_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
sender_service "code.gitea.io/gitea/services/mailer/sender"
)

const (
Expand Down Expand Up @@ -80,11 +81,11 @@ func mailNewRelease(ctx context.Context, lang string, tos []*user_model.User, re
return
}

msgs := make([]*Message, 0, len(tos))
msgs := make([]*sender_service.Message, 0, len(tos))
publisherName := fromDisplayName(rel.Publisher)
msgID := generateMessageIDForRelease(rel)
for _, to := range tos {
msg := NewMessageFrom(to.EmailTo(), publisherName, setting.MailService.FromEmail, subject, mailBody.String())
msg := sender_service.NewMessageFrom(to.EmailTo(), publisherName, setting.MailService.FromEmail, subject, mailBody.String())
msg.Info = subject
msg.SetHeader("Message-ID", msgID)
msgs = append(msgs, msg)
Expand Down
3 changes: 2 additions & 1 deletion services/mailer/mail_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
sender_service "code.gitea.io/gitea/services/mailer/sender"
)

// SendRepoTransferNotifyMail triggers a notification e-mail when a pending repository transfer was created
Expand Down Expand Up @@ -79,7 +80,7 @@ func sendRepoTransferNotifyMailPerLang(lang string, newOwner, doer *user_model.U
}

for _, to := range emailTos {
msg := NewMessageFrom(to.EmailTo(), fromDisplayName(doer), setting.MailService.FromEmail, subject, content.String())
msg := sender_service.NewMessageFrom(to.EmailTo(), fromDisplayName(doer), setting.MailService.FromEmail, subject, content.String())
msg.Info = fmt.Sprintf("UID: %d, repository pending transfer notification", newOwner.ID)

SendAsync(msg)
Expand Down
3 changes: 2 additions & 1 deletion services/mailer/mail_team_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
sender_service "code.gitea.io/gitea/services/mailer/sender"
)

const (
Expand Down Expand Up @@ -67,7 +68,7 @@ func MailTeamInvite(ctx context.Context, inviter *user_model.User, team *org_mod
return err
}

msg := NewMessage(invite.Email, subject, mailBody.String())
msg := sender_service.NewMessage(invite.Email, subject, mailBody.String())
msg.Info = subject

SendAsync(msg)
Expand Down
5 changes: 3 additions & 2 deletions services/mailer/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
sender_service "code.gitea.io/gitea/services/mailer/sender"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestTemplateSelection(t *testing.T) {
template.Must(bodyTemplates.New("pull/comment").Parse("pull/comment/body"))
template.Must(bodyTemplates.New("issue/close").Parse("issue/close/body"))

expect := func(t *testing.T, msg *Message, expSubject, expBody string) {
expect := func(t *testing.T, msg *sender_service.Message, expSubject, expBody string) {
subject := msg.ToMessage().GetHeader("Subject")
msgbuf := new(bytes.Buffer)
_, _ = msg.ToMessage().WriteTo(msgbuf)
Expand Down Expand Up @@ -252,7 +253,7 @@ func TestTemplateServices(t *testing.T) {
"//Re: //")
}

func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recipients []*user_model.User, fromMention bool, info string) *Message {
func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recipients []*user_model.User, fromMention bool, info string) *sender_service.Message {
msgs, err := composeIssueCommentMessages(ctx, "en-US", recipients, fromMention, info)
assert.NoError(t, err)
assert.Len(t, msgs, 1)
Expand Down
Loading

0 comments on commit 79d593a

Please sign in to comment.