code.gitea.io/gitea@v1.21.7/services/mailer/mail_release.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package mailer
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  
    10  	repo_model "code.gitea.io/gitea/models/repo"
    11  	user_model "code.gitea.io/gitea/models/user"
    12  	"code.gitea.io/gitea/modules/base"
    13  	"code.gitea.io/gitea/modules/log"
    14  	"code.gitea.io/gitea/modules/markup"
    15  	"code.gitea.io/gitea/modules/markup/markdown"
    16  	"code.gitea.io/gitea/modules/setting"
    17  	"code.gitea.io/gitea/modules/translation"
    18  )
    19  
    20  const (
    21  	tplNewReleaseMail base.TplName = "release"
    22  )
    23  
    24  // MailNewRelease send new release notify to all repo watchers.
    25  func MailNewRelease(ctx context.Context, rel *repo_model.Release) {
    26  	if setting.MailService == nil {
    27  		// No mail service configured
    28  		return
    29  	}
    30  
    31  	watcherIDList, err := repo_model.GetRepoWatchersIDs(ctx, rel.RepoID)
    32  	if err != nil {
    33  		log.Error("GetRepoWatchersIDs(%d): %v", rel.RepoID, err)
    34  		return
    35  	}
    36  
    37  	recipients, err := user_model.GetMaileableUsersByIDs(ctx, watcherIDList, false)
    38  	if err != nil {
    39  		log.Error("user_model.GetMaileableUsersByIDs: %v", err)
    40  		return
    41  	}
    42  
    43  	langMap := make(map[string][]string)
    44  	for _, user := range recipients {
    45  		if user.ID != rel.PublisherID {
    46  			langMap[user.Language] = append(langMap[user.Language], user.Email)
    47  		}
    48  	}
    49  
    50  	for lang, tos := range langMap {
    51  		mailNewRelease(ctx, lang, tos, rel)
    52  	}
    53  }
    54  
    55  func mailNewRelease(ctx context.Context, lang string, tos []string, rel *repo_model.Release) {
    56  	locale := translation.NewLocale(lang)
    57  
    58  	var err error
    59  	rel.RenderedNote, err = markdown.RenderString(&markup.RenderContext{
    60  		Ctx: ctx,
    61  		Links: markup.Links{
    62  			Base: rel.Repo.HTMLURL(),
    63  		},
    64  		Metas: rel.Repo.ComposeMetas(),
    65  	}, rel.Note)
    66  	if err != nil {
    67  		log.Error("markdown.RenderString(%d): %v", rel.RepoID, err)
    68  		return
    69  	}
    70  
    71  	subject := locale.Tr("mail.release.new.subject", rel.TagName, rel.Repo.FullName())
    72  	mailMeta := map[string]any{
    73  		"locale":   locale,
    74  		"Release":  rel,
    75  		"Subject":  subject,
    76  		"Language": locale.Language(),
    77  		"Link":     rel.HTMLURL(),
    78  	}
    79  
    80  	var mailBody bytes.Buffer
    81  
    82  	if err := bodyTemplates.ExecuteTemplate(&mailBody, string(tplNewReleaseMail), mailMeta); err != nil {
    83  		log.Error("ExecuteTemplate [%s]: %v", string(tplNewReleaseMail)+"/body", err)
    84  		return
    85  	}
    86  
    87  	msgs := make([]*Message, 0, len(tos))
    88  	publisherName := rel.Publisher.DisplayName()
    89  	relURL := "<" + rel.HTMLURL() + ">"
    90  	for _, to := range tos {
    91  		msg := NewMessageFrom(to, publisherName, setting.MailService.FromEmail, subject, mailBody.String())
    92  		msg.Info = subject
    93  		msg.SetHeader("Message-ID", relURL)
    94  		msgs = append(msgs, msg)
    95  	}
    96  
    97  	SendAsync(msgs...)
    98  }