diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-04-20 06:25:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-19 18:25:08 -0400 |
commit | 9d99f6ab19ac3f97af3ca126720e9075c127a652 (patch) | |
tree | b817b4582a871f83b91ad7977fe772fc3501c1e8 /services | |
parent | c9cc6698d2172625854cd063301e63602204a2a1 (diff) | |
download | gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.tar.gz gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.zip |
Refactor renders (#15175)
* Refactor renders
* Some performance optimization
* Fix comment
* Transform reader
* Fix csv test
* Fix test
* Fix tests
* Improve optimaziation
* Fix test
* Fix test
* Detect file encoding with reader
* Improve optimaziation
* reduce memory usage
* improve code
* fix build
* Fix test
* Fix for go1.15
* Fix render
* Fix comment
* Fix lint
* Fix test
* Don't use NormalEOF when unnecessary
* revert change on util.go
* Apply suggestions from code review
Co-authored-by: zeripath <art27@cantab.net>
* rename function
* Take NormalEOF back
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'services')
-rw-r--r-- | services/gitdiff/csv_test.go | 10 | ||||
-rw-r--r-- | services/mailer/mail.go | 32 | ||||
-rw-r--r-- | services/mailer/mail_issue.go | 6 | ||||
-rw-r--r-- | services/mailer/mail_release.go | 11 | ||||
-rw-r--r-- | services/mailer/mail_test.go | 9 |
5 files changed, 50 insertions, 18 deletions
diff --git a/services/gitdiff/csv_test.go b/services/gitdiff/csv_test.go index 17edea582c..f3dc0c2a2c 100644 --- a/services/gitdiff/csv_test.go +++ b/services/gitdiff/csv_test.go @@ -95,11 +95,17 @@ func TestCSVDiff(t *testing.T) { var baseReader *csv.Reader if len(c.base) > 0 { - baseReader = csv_module.CreateReaderAndGuessDelimiter([]byte(c.base)) + baseReader, err = csv_module.CreateReaderAndGuessDelimiter(strings.NewReader(c.base)) + if err != nil { + t.Errorf("CreateReaderAndGuessDelimiter failed: %s", err) + } } var headReader *csv.Reader if len(c.head) > 0 { - headReader = csv_module.CreateReaderAndGuessDelimiter([]byte(c.head)) + headReader, err = csv_module.CreateReaderAndGuessDelimiter(strings.NewReader(c.head)) + if err != nil { + t.Errorf("CreateReaderAndGuessDelimiter failed: %s", err) + } } result, err := CreateCsvDiff(diff.Files[0], baseReader, headReader) diff --git a/services/mailer/mail.go b/services/mailer/mail.go index c50795968a..f22140c9f7 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -174,8 +174,7 @@ func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) { SendAsync(msg) } -func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []string, fromMention bool, info string) []*Message { - +func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []string, fromMention bool, info string) ([]*Message, error) { var ( subject string link string @@ -199,7 +198,14 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []str } // This is the body of the new issue or comment, not the mail body - body := string(markup.RenderByType(markdown.MarkupName, []byte(ctx.Content), ctx.Issue.Repo.HTMLURL(), ctx.Issue.Repo.ComposeMetas())) + body, err := markdown.RenderString(&markup.RenderContext{ + URLPrefix: ctx.Issue.Repo.HTMLURL(), + Metas: ctx.Issue.Repo.ComposeMetas(), + }, ctx.Content) + if err != nil { + return nil, err + } + actType, actName, tplName := actionToTemplate(ctx.Issue, ctx.ActionType, commentType, reviewType) if actName != "new" { @@ -240,14 +246,13 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []str // TODO: i18n templates? if err := subjectTemplates.ExecuteTemplate(&mailSubject, string(tplName), mailMeta); err == nil { subject = sanitizeSubject(mailSubject.String()) + if subject == "" { + subject = fallback + } } else { log.Error("ExecuteTemplate [%s]: %v", tplName+"/subject", err) } - if subject == "" { - subject = fallback - } - subject = emoji.ReplaceAliases(subject) mailMeta["Subject"] = subject @@ -275,7 +280,7 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []str msgs = append(msgs, msg) } - return msgs + return msgs, nil } func sanitizeSubject(subject string) string { @@ -288,21 +293,26 @@ func sanitizeSubject(subject string) string { } // SendIssueAssignedMail composes and sends issue assigned email -func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, recipients []*models.User) { +func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, recipients []*models.User) error { langMap := make(map[string][]string) for _, user := range recipients { langMap[user.Language] = append(langMap[user.Language], user.Email) } for lang, tos := range langMap { - SendAsyncs(composeIssueCommentMessages(&mailCommentContext{ + msgs, err := composeIssueCommentMessages(&mailCommentContext{ Issue: issue, Doer: doer, ActionType: models.ActionType(0), Content: content, Comment: comment, - }, lang, tos, false, "issue assigned")) + }, lang, tos, false, "issue assigned") + if err != nil { + return err + } + SendAsyncs(msgs) } + return nil } // actionToTemplate returns the type and name of the action facing the user diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index 9786a06f62..bb541d27a0 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -146,7 +146,11 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*models.User, visite // working backwards from the last (possibly) incomplete batch. If len(receivers) can be 0 this // starting condition will need to be changed slightly for i := ((len(receivers) - 1) / MailBatchSize) * MailBatchSize; i >= 0; i -= MailBatchSize { - SendAsyncs(composeIssueCommentMessages(ctx, lang, receivers[i:], fromMention, "issue comments")) + msgs, err := composeIssueCommentMessages(ctx, lang, receivers[i:], fromMention, "issue comments") + if err != nil { + return err + } + SendAsyncs(msgs) receivers = receivers[:i] } } diff --git a/services/mailer/mail_release.go b/services/mailer/mail_release.go index 22efe2f046..1e12fe13ac 100644 --- a/services/mailer/mail_release.go +++ b/services/mailer/mail_release.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/translation" @@ -48,7 +49,15 @@ func MailNewRelease(rel *models.Release) { func mailNewRelease(lang string, tos []string, rel *models.Release) { locale := translation.NewLocale(lang) - rel.RenderedNote = markdown.RenderString(rel.Note, rel.Repo.Link(), rel.Repo.ComposeMetas()) + var err error + rel.RenderedNote, err = markdown.RenderString(&markup.RenderContext{ + URLPrefix: rel.Repo.Link(), + Metas: rel.Repo.ComposeMetas(), + }, rel.Note) + if err != nil { + log.Error("markdown.RenderString(%d): %v", rel.RepoID, err) + return + } subject := locale.Tr("mail.release.new.subject", rel.TagName, rel.Repo.FullName()) mailMeta := map[string]interface{}{ diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index 9eef084408..813e51c0d2 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -58,8 +58,9 @@ func TestComposeIssueCommentMessage(t *testing.T) { InitMailRender(stpl, btpl) tos := []string{"test@gitea.com", "test2@gitea.com"} - msgs := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue, + msgs, err := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue, Content: "test body", Comment: comment}, "en-US", tos, false, "issue comment") + assert.NoError(t, err) assert.Len(t, msgs, 2) gomailMsg := msgs[0].ToMessage() mailto := gomailMsg.GetHeader("To") @@ -92,8 +93,9 @@ func TestComposeIssueMessage(t *testing.T) { InitMailRender(stpl, btpl) tos := []string{"test@gitea.com", "test2@gitea.com"} - msgs := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue, + msgs, err := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue, Content: "test body"}, "en-US", tos, false, "issue create") + assert.NoError(t, err) assert.Len(t, msgs, 2) gomailMsg := msgs[0].ToMessage() @@ -218,7 +220,8 @@ func TestTemplateServices(t *testing.T) { } func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, tos []string, fromMention bool, info string) *Message { - msgs := composeIssueCommentMessages(ctx, "en-US", tos, fromMention, info) + msgs, err := composeIssueCommentMessages(ctx, "en-US", tos, fromMention, info) + assert.NoError(t, err) assert.Len(t, msgs, 1) return msgs[0] } |