Add mail notify for new collaborator
This commit is contained in:
		
							parent
							
								
									07c3d497a7
								
							
						
					
					
						commit
						02687cbdf3
					
				
					 7 changed files with 93 additions and 18 deletions
				
			
		|  | @ -71,7 +71,7 @@ There are 4 ways to install Gogs: | ||||||
| 
 | 
 | ||||||
| ## Contributors | ## Contributors | ||||||
| 
 | 
 | ||||||
| This project was launched by [Unknwon](https://github.com/Unknwon) and [lunny](https://github.com/lunny); [fuxiaohei](https://github.com/fuxiaohei), [slene](https://github.com/slene) and [codeskyblue](https://github.com/codeskyblue) joined the team soon after. See [contributors page](https://github.com/gogits/gogs/graphs/contributors) for full list of contributors. | The [core team](http://gogs.io/team) of this project. See [contributors page](https://github.com/gogits/gogs/graphs/contributors) for full list of contributors. | ||||||
| 
 | 
 | ||||||
| [][koding] | [][koding] | ||||||
| [koding]: https://koding.com/Teamwork?import=https://github.com/gogits/gogs/archive/master.zip&c=git1 | [koding]: https://koding.com/Teamwork?import=https://github.com/gogits/gogs/archive/master.zip&c=git1 | ||||||
|  |  | ||||||
|  | @ -63,7 +63,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 | ||||||
| 
 | 
 | ||||||
| ## 贡献成员 | ## 贡献成员 | ||||||
| 
 | 
 | ||||||
| 本项目最初由 [Unknown](https://github.com/Unknwon) 和 [lunny](https://github.com/lunny) 发起,随后 [fuxiaohei](https://github.com/fuxiaohei)、[slene](https://github.com/slene) 以及 [codeskyblue](https://github.com/codeskyblue) 加入到开发团队。您可以通过查看 [贡献者页面](https://github.com/gogits/gogs/graphs/contributors) 获取完整的贡献者列表。 | 本项目的 [开发团队](http://gogs.io/team)。您可以通过查看 [贡献者页面](https://github.com/gogits/gogs/graphs/contributors) 获取完整的贡献者列表。 | ||||||
| 
 | 
 | ||||||
| [][koding] | [][koding] | ||||||
| [koding]: https://koding.com/Teamwork?import=https://github.com/gogits/gogs/archive/master.zip&c=git1 | [koding]: https://koding.com/Teamwork?import=https://github.com/gogits/gogs/archive/master.zip&c=git1 | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ import ( | ||||||
| 	"encoding/hex" | 	"encoding/hex" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"path" | ||||||
| 
 | 
 | ||||||
| 	"github.com/gogits/gogs/models" | 	"github.com/gogits/gogs/models" | ||||||
| 	"github.com/gogits/gogs/modules/base" | 	"github.com/gogits/gogs/modules/base" | ||||||
|  | @ -135,7 +136,7 @@ func SendIssueNotifyMail(user, owner *models.User, repo *models.Repository, issu | ||||||
| 		return tos, nil | 		return tos, nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name) | 	subject := fmt.Sprintf("[%s] %s(#%d)", repo.Name, issue.Name, issue.Index) | ||||||
| 	content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.", | 	content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.", | ||||||
| 		base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name), | 		base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name), | ||||||
| 		base.AppUrl, owner.Name, repo.Name, issue.Index) | 		base.AppUrl, owner.Name, repo.Name, issue.Index) | ||||||
|  | @ -146,17 +147,48 @@ func SendIssueNotifyMail(user, owner *models.User, repo *models.Repository, issu | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // SendIssueMentionMail sends mail notification for who are mentioned in issue.
 | // SendIssueMentionMail sends mail notification for who are mentioned in issue.
 | ||||||
| func SendIssueMentionMail(user, owner *models.User, repo *models.Repository, issue *models.Issue, tos []string) error { | func SendIssueMentionMail(r *middleware.Render, user, owner *models.User, | ||||||
|  | 	repo *models.Repository, issue *models.Issue, tos []string) error { | ||||||
|  | 
 | ||||||
| 	if len(tos) == 0 { | 	if len(tos) == 0 { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	issueLink := fmt.Sprintf("%s%s/%s/issues/%d", base.AppUrl, owner.Name, repo.Name, issue.Index) | 	subject := fmt.Sprintf("[%s] %s(#%d)", repo.Name, issue.Name, issue.Index) | ||||||
| 	body := fmt.Sprintf(`%s mentioned you.`, user.Name) | 
 | ||||||
| 	subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name) | 	data := GetMailTmplData(nil) | ||||||
| 	content := fmt.Sprintf("%s<br>-<br> <a href=\"%s\">View it on Gogs</a>.", body, issueLink) | 	data["IssueLink"] = fmt.Sprintf("%s/%s/issues/%d", owner.Name, repo.Name, issue.Index) | ||||||
| 	msg := NewMailMessageFrom(tos, user.Name, subject, content) | 	data["Subject"] = subject | ||||||
|  | 
 | ||||||
|  | 	body, err := r.HTMLString("mail/notify/mention", data) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return fmt.Errorf("mail.SendIssueMentionMail(fail to render): %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	msg := NewMailMessageFrom(tos, user.Name, subject, body) | ||||||
| 	msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject) | 	msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject) | ||||||
| 	SendAsync(&msg) | 	SendAsync(&msg) | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // SendCollaboratorMail sends mail notification to new collaborator.
 | ||||||
|  | func SendCollaboratorMail(r *middleware.Render, user, owner *models.User, | ||||||
|  | 	repo *models.Repository) error { | ||||||
|  | 
 | ||||||
|  | 	subject := fmt.Sprintf("%s added you to %s", owner.Name, repo.Name) | ||||||
|  | 
 | ||||||
|  | 	data := GetMailTmplData(nil) | ||||||
|  | 	data["RepoLink"] = path.Join(owner.Name, repo.Name) | ||||||
|  | 	data["Subject"] = subject | ||||||
|  | 
 | ||||||
|  | 	body, err := r.HTMLString("mail/notify/collaborator", data) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return fmt.Errorf("mail.SendCollaboratorMail(fail to render): %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	msg := NewMailMessage([]string{user.Email}, subject, body) | ||||||
|  | 	msg.Info = fmt.Sprintf("UID: %d, send register mail", user.Id) | ||||||
|  | 
 | ||||||
|  | 	SendAsync(&msg) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -132,8 +132,8 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C | ||||||
| 
 | 
 | ||||||
| 			newTos = append(newTos, m[1:]) | 			newTos = append(newTos, m[1:]) | ||||||
| 		} | 		} | ||||||
| 		if err = mailer.SendIssueMentionMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, | 		if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, | ||||||
| 			issue, models.GetUserEmailsByNames(newTos)); err != nil { | 			ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { | ||||||
| 			ctx.Handle(500, "issue.CreateIssue(SendIssueMentionMail)", err) | 			ctx.Handle(500, "issue.CreateIssue(SendIssueMentionMail)", err) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -13,6 +13,7 @@ import ( | ||||||
| 	"github.com/gogits/gogs/models" | 	"github.com/gogits/gogs/models" | ||||||
| 	"github.com/gogits/gogs/modules/base" | 	"github.com/gogits/gogs/modules/base" | ||||||
| 	"github.com/gogits/gogs/modules/log" | 	"github.com/gogits/gogs/modules/log" | ||||||
|  | 	"github.com/gogits/gogs/modules/mailer" | ||||||
| 	"github.com/gogits/gogs/modules/middleware" | 	"github.com/gogits/gogs/modules/middleware" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -185,22 +186,30 @@ func CollaborationPost(ctx *middleware.Context) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	isExist, err := models.IsUserExist(name) | 	u, err := models.GetUserByName(name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.Handle(500, "repo.CollaborationPost(IsUserExist)", err) | 		if err == models.ErrUserNotExist { | ||||||
| 		return | 			ctx.Flash.Error("Given user does not exist.") | ||||||
| 	} else if !isExist { | 			ctx.Redirect(ctx.Req.RequestURI) | ||||||
| 		ctx.Flash.Error("Given user does not exist.") | 		} else { | ||||||
| 		ctx.Redirect(ctx.Req.RequestURI) | 			ctx.Handle(500, "repo.CollaborationPost(GetUserByName)", err) | ||||||
|  | 		} | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if err := models.AddAccess(&models.Access{UserName: name, RepoName: repoLink, | 	if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink, | ||||||
| 		Mode: models.AU_WRITABLE}); err != nil { | 		Mode: models.AU_WRITABLE}); err != nil { | ||||||
| 		ctx.Handle(500, "repo.CollaborationPost(AddAccess)", err) | 		ctx.Handle(500, "repo.CollaborationPost(AddAccess)", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if base.Service.NotifyMail { | ||||||
|  | 		if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil { | ||||||
|  | 			ctx.Handle(500, "repo.CollaborationPost(SendCollaboratorMail)", err) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	ctx.Flash.Success("New collaborator has been added.") | 	ctx.Flash.Success("New collaborator has been added.") | ||||||
| 	ctx.Redirect(ctx.Req.RequestURI) | 	ctx.Redirect(ctx.Req.RequestURI) | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										18
									
								
								templates/mail/notify/collaborator.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								templates/mail/notify/collaborator.tmpl
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,18 @@ | ||||||
|  | <!DOCTYPE html> | ||||||
|  | <html> | ||||||
|  | <head> | ||||||
|  |     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||||||
|  |     <title>{{.Subject}}</title> | ||||||
|  | </head> | ||||||
|  | 
 | ||||||
|  | <body> | ||||||
|  |     <p>You can now push to this repository.</p> | ||||||
|  |     <p> | ||||||
|  |         --- | ||||||
|  |         <br> | ||||||
|  |         View it on Gogs:  | ||||||
|  |         <br> | ||||||
|  |         <a href="{{.AppUrl}}{{.RepoLink}}">{{.AppUrl}}{{.RepoLink}}</a> | ||||||
|  |     </p> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
							
								
								
									
										16
									
								
								templates/mail/notify/mention.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								templates/mail/notify/mention.tmpl
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | ||||||
|  | <!DOCTYPE html> | ||||||
|  | <html> | ||||||
|  | <head> | ||||||
|  |     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||||||
|  |     <title>{{.Subject}}</title> | ||||||
|  | </head> | ||||||
|  | 
 | ||||||
|  | <body> | ||||||
|  |     <p>{{.ActUserName}} mentioned you.</p> | ||||||
|  |     <p> | ||||||
|  |         --- | ||||||
|  |         <br> | ||||||
|  |         <a href="{{.AppUrl}}{{.IssueLink}}">View it on Gogs</a>. | ||||||
|  |     </p> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
		Loading…
	
		Reference in a new issue