mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 15:04:00 +01:00 
			
		
		
		
	Refactor
This commit is contained in:
		
							parent
							
								
									229235f99d
								
							
						
					
					
						commit
						c8b64c7686
					
				@ -171,3 +171,39 @@ func fromDisplayName(u *user_model.User) string {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return u.GetCompleteName()
 | 
						return u.GetCompleteName()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func generateMetadataHeaders(repo *repo_model.Repository) map[string]string {
 | 
				
			||||||
 | 
						return map[string]string{
 | 
				
			||||||
 | 
							// https://datatracker.ietf.org/doc/html/rfc2919
 | 
				
			||||||
 | 
							"List-ID": fmt.Sprintf("%s <%s.%s.%s>", repo.FullName(), repo.Name, repo.OwnerName, setting.Domain),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// https://datatracker.ietf.org/doc/html/rfc2369
 | 
				
			||||||
 | 
							"List-Archive": fmt.Sprintf("<%s>", repo.HTMLURL()),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							"X-Mailer": "Gitea",
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							"X-Gitea-Repository":      repo.Name,
 | 
				
			||||||
 | 
							"X-Gitea-Repository-Path": repo.FullName(),
 | 
				
			||||||
 | 
							"X-Gitea-Repository-Link": repo.HTMLURL(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							"X-GitLab-Project":      repo.Name,
 | 
				
			||||||
 | 
							"X-GitLab-Project-Path": repo.FullName(),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func generateSenderRecipientHeaders(doer, recipient *user_model.User) map[string]string {
 | 
				
			||||||
 | 
						return map[string]string{
 | 
				
			||||||
 | 
							"X-Gitea-Sender":     doer.Name,
 | 
				
			||||||
 | 
							"X-Gitea-Recipient":  recipient.Name,
 | 
				
			||||||
 | 
							"X-GitHub-Sender":    doer.Name,
 | 
				
			||||||
 | 
							"X-GitHub-Recipient": recipient.Name,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func generateReasonHeaders(reason string) map[string]string {
 | 
				
			||||||
 | 
						return map[string]string{
 | 
				
			||||||
 | 
							"X-Gitea-Reason":              reason,
 | 
				
			||||||
 | 
							"X-GitHub-Reason":             reason,
 | 
				
			||||||
 | 
							"X-GitLab-NotificationReason": reason,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -29,7 +29,7 @@ import (
 | 
				
			|||||||
// Many e-mail service providers have limitations on the size of the email body, it's usually from 10MB to 25MB
 | 
					// Many e-mail service providers have limitations on the size of the email body, it's usually from 10MB to 25MB
 | 
				
			||||||
const maxEmailBodySize = 9_000_000
 | 
					const maxEmailBodySize = 9_000_000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func fallbackMailSubject(issue *issues_model.Issue) string {
 | 
					func fallbackIssueMailSubject(issue *issues_model.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)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -86,7 +86,7 @@ func composeIssueCommentMessages(ctx context.Context, comment *mailComment, lang
 | 
				
			|||||||
	if actName != "new" {
 | 
						if actName != "new" {
 | 
				
			||||||
		prefix = "Re: "
 | 
							prefix = "Re: "
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	fallback = prefix + fallbackMailSubject(comment.Issue)
 | 
						fallback = prefix + fallbackIssueMailSubject(comment.Issue)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if comment.Comment != nil && comment.Comment.Review != nil {
 | 
						if comment.Comment != nil && comment.Comment.Review != nil {
 | 
				
			||||||
		reviewComments = make([]*issues_model.Comment, 0, 10)
 | 
							reviewComments = make([]*issues_model.Comment, 0, 10)
 | 
				
			||||||
@ -202,7 +202,7 @@ func composeIssueCommentMessages(ctx context.Context, comment *mailComment, lang
 | 
				
			|||||||
		msg.SetHeader("References", references...)
 | 
							msg.SetHeader("References", references...)
 | 
				
			||||||
		msg.SetHeader("List-Unsubscribe", listUnsubscribe...)
 | 
							msg.SetHeader("List-Unsubscribe", listUnsubscribe...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for key, value := range generateAdditionalHeaders(comment, actType, recipient) {
 | 
							for key, value := range generateAdditionalHeadersForIssue(comment, actType, recipient) {
 | 
				
			||||||
			msg.SetHeader(key, value)
 | 
								msg.SetHeader(key, value)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -302,35 +302,24 @@ func generateMessageIDForIssue(issue *issues_model.Issue, comment *issues_model.
 | 
				
			|||||||
	return fmt.Sprintf("<%s/%s/%d%s@%s>", issue.Repo.FullName(), path, issue.Index, extra, setting.Domain)
 | 
						return fmt.Sprintf("<%s/%s/%d%s@%s>", issue.Repo.FullName(), path, issue.Index, extra, setting.Domain)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func generateAdditionalHeaders(ctx *mailComment, reason string, recipient *user_model.User) map[string]string {
 | 
					func generateAdditionalHeadersForIssue(ctx *mailComment, reason string, recipient *user_model.User) map[string]string {
 | 
				
			||||||
	repo := ctx.Issue.Repo
 | 
						repo := ctx.Issue.Repo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return map[string]string{
 | 
						issueID := strconv.FormatInt(ctx.Issue.Index, 10)
 | 
				
			||||||
		// https://datatracker.ietf.org/doc/html/rfc2919
 | 
						headers := generateMetadataHeaders(repo)
 | 
				
			||||||
		"List-ID": fmt.Sprintf("%s <%s.%s.%s>", repo.FullName(), repo.Name, repo.OwnerName, setting.Domain),
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// https://datatracker.ietf.org/doc/html/rfc2369
 | 
						for k, v := range generateSenderRecipientHeaders(ctx.Doer, recipient) {
 | 
				
			||||||
		"List-Archive": fmt.Sprintf("<%s>", repo.HTMLURL()),
 | 
							headers[k] = v
 | 
				
			||||||
 | 
					 | 
				
			||||||
		"X-Mailer":                  "Gitea",
 | 
					 | 
				
			||||||
		"X-Gitea-Reason":            reason,
 | 
					 | 
				
			||||||
		"X-Gitea-Sender":            ctx.Doer.Name,
 | 
					 | 
				
			||||||
		"X-Gitea-Recipient":         recipient.Name,
 | 
					 | 
				
			||||||
		"X-Gitea-Recipient-Address": recipient.Email,
 | 
					 | 
				
			||||||
		"X-Gitea-Repository":        repo.Name,
 | 
					 | 
				
			||||||
		"X-Gitea-Repository-Path":   repo.FullName(),
 | 
					 | 
				
			||||||
		"X-Gitea-Repository-Link":   repo.HTMLURL(),
 | 
					 | 
				
			||||||
		"X-Gitea-Issue-ID":          strconv.FormatInt(ctx.Issue.Index, 10),
 | 
					 | 
				
			||||||
		"X-Gitea-Issue-Link":        ctx.Issue.HTMLURL(),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		"X-GitHub-Reason":            reason,
 | 
					 | 
				
			||||||
		"X-GitHub-Sender":            ctx.Doer.Name,
 | 
					 | 
				
			||||||
		"X-GitHub-Recipient":         recipient.Name,
 | 
					 | 
				
			||||||
		"X-GitHub-Recipient-Address": recipient.Email,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		"X-GitLab-NotificationReason": reason,
 | 
					 | 
				
			||||||
		"X-GitLab-Project":            repo.Name,
 | 
					 | 
				
			||||||
		"X-GitLab-Project-Path":       repo.FullName(),
 | 
					 | 
				
			||||||
		"X-GitLab-Issue-IID":          strconv.FormatInt(ctx.Issue.Index, 10),
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						for k, v := range generateReasonHeaders(reason) {
 | 
				
			||||||
 | 
							headers[k] = v
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						headers["X-Gitea-Recipient-Address"] = recipient.Email
 | 
				
			||||||
 | 
						headers["X-Gitea-Issue-ID"] = issueID
 | 
				
			||||||
 | 
						headers["X-Gitea-Issue-Link"] = ctx.Issue.HTMLURL()
 | 
				
			||||||
 | 
						headers["X-GitHub-Recipient-Address"] = recipient.Email
 | 
				
			||||||
 | 
						headers["X-GitLab-Issue-IID"] = issueID
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return headers
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -297,13 +297,13 @@ func testComposeIssueCommentMessage(t *testing.T, ctx *mailComment, recipients [
 | 
				
			|||||||
	return msgs[0]
 | 
						return msgs[0]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestGenerateAdditionalHeaders(t *testing.T) {
 | 
					func TestGenerateAdditionalHeadersForIssue(t *testing.T) {
 | 
				
			||||||
	doer, _, issue, _ := prepareMailerTest(t)
 | 
						doer, _, issue, _ := prepareMailerTest(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	comment := &mailComment{Issue: issue, Doer: doer}
 | 
						comment := &mailComment{Issue: issue, Doer: doer}
 | 
				
			||||||
	recipient := &user_model.User{Name: "test", Email: "test@gitea.com"}
 | 
						recipient := &user_model.User{Name: "test", Email: "test@gitea.com"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	headers := generateAdditionalHeaders(comment, "dummy-reason", recipient)
 | 
						headers := generateAdditionalHeadersForIssue(comment, "dummy-reason", recipient)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	expected := map[string]string{
 | 
						expected := map[string]string{
 | 
				
			||||||
		"List-ID":                   "user2/repo1 <repo1.user2.localhost>",
 | 
							"List-ID":                   "user2/repo1 <repo1.user2.localhost>",
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user