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 bugtokarchuk/v1.17
							parent
							
								
									730065a3dc
								
							
						
					
					
						commit
						5a438ee3c0
					
				@ -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 | 
				
			||||||
 | 
					} | 
				
			||||||
@ -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 | 
				
			||||||
 | 
					} | 
				
			||||||
@ -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("..", "..")) | 
				
			||||||
 | 
					} | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue