Skip to content

Lint/issue &mail #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Label struct {
IsChecked bool `xorm:"-"`
}

// APIFormat converts a Label to the api.Label format
func (label *Label) APIFormat() *api.Label {
return &api.Label{
ID: label.ID,
Expand All @@ -77,9 +78,9 @@ func (label *Label) CalOpenIssues() {

// ForegroundColor calculates the text color for labels based
// on their background color.
func (l *Label) ForegroundColor() template.CSS {
if strings.HasPrefix(l.Color, "#") {
if color, err := strconv.ParseUint(l.Color[1:], 16, 64); err == nil {
func (label *Label) ForegroundColor() template.CSS {
if strings.HasPrefix(label.Color, "#") {
if color, err := strconv.ParseUint(label.Color[1:], 16, 64); err == nil {
r := float32(0xFF & (color >> 16))
g := float32(0xFF & (color >> 8))
b := float32(0xFF & color)
Expand Down
2 changes: 1 addition & 1 deletion models/issue_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"code.gitea.io/gitea/modules/setting"
)

func (issue *Issue) MailSubject() string {
func (issue *Issue) mailSubject() string {
return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.Name, issue.Title, issue.Index)
}

Expand Down
41 changes: 23 additions & 18 deletions models/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ import (
)

const (
MailAuthActivate base.TplName = "auth/activate"
MailAuthActivateEmail base.TplName = "auth/activate_email"
MailAuthResetPassword base.TplName = "auth/reset_passwd"
MailAuthRegisterNotify base.TplName = "auth/register_notify"
mailAuthActivate base.TplName = "auth/activate"
mailAuthActivateEmail base.TplName = "auth/activate_email"
mailAuthResetPassword base.TplName = "auth/reset_passwd"
mailAuthRegisterNotify base.TplName = "auth/register_notify"

MailIssueComment base.TplName = "issue/comment"
MailIssueMention base.TplName = "issue/mention"
mailIssueComment base.TplName = "issue/comment"
mailIssueMention base.TplName = "issue/mention"

MailNotifyCollaborator base.TplName = "notify/collaborator"
mailNotifyCollaborator base.TplName = "notify/collaborator"
)

type MailRender interface {
type mailRenderInterface interface {
HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error)
}

var mailRender MailRender
var mailRender mailRenderInterface

// InitMailRender initializes the macaron mail renderer
func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
opt := &macaron.RenderOptions{
Directory: dir,
Expand All @@ -53,10 +54,12 @@ func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
}
}

// SendTestMail sends a test mail
func SendTestMail(email string) error {
return gomail.Send(&mailer.Sender{}, mailer.NewMessage([]string{email}, "Gogs Test Email!", "Gogs Test Email!").Message)
}

// SendUserMail sends a mail to the user
func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject, info string) {
data := map[string]interface{}{
"Username": u.DisplayName(),
Expand All @@ -76,23 +79,25 @@ func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject,
mailer.SendAsync(msg)
}

// SendActivateAccountMail sends an activation mail to the user
func SendActivateAccountMail(c *macaron.Context, u *User) {
SendUserMail(c, u, MailAuthActivate, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
SendUserMail(c, u, mailAuthActivate, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
}

// SendResetPasswordMail sends a password reset mail to the user
func SendResetPasswordMail(c *macaron.Context, u *User) {
SendUserMail(c, u, MailAuthResetPassword, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
SendUserMail(c, u, mailAuthResetPassword, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
}

// SendActivateAccountMail sends confirmation email.
// SendActivateEmailMail sends confirmation email.
func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
data := map[string]interface{}{
"Username": u.DisplayName(),
"ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
"Code": u.GenerateEmailActivateCode(email.Email),
"Email": email.Email,
}
body, err := mailRender.HTMLString(string(MailAuthActivateEmail), data)
body, err := mailRender.HTMLString(string(mailAuthActivateEmail), data)
if err != nil {
log.Error(3, "HTMLString: %v", err)
return
Expand All @@ -109,7 +114,7 @@ func SendRegisterNotifyMail(c *macaron.Context, u *User) {
data := map[string]interface{}{
"Username": u.DisplayName(),
}
body, err := mailRender.HTMLString(string(MailAuthRegisterNotify), data)
body, err := mailRender.HTMLString(string(mailAuthRegisterNotify), data)
if err != nil {
log.Error(3, "HTMLString: %v", err)
return
Expand All @@ -131,7 +136,7 @@ func SendCollaboratorMail(u, doer *User, repo *Repository) {
"RepoName": repoName,
"Link": repo.HTMLURL(),
}
body, err := mailRender.HTMLString(string(MailNotifyCollaborator), data)
body, err := mailRender.HTMLString(string(mailNotifyCollaborator), data)
if err != nil {
log.Error(3, "HTMLString: %v", err)
return
Expand All @@ -152,7 +157,7 @@ func composeTplData(subject, body, link string) map[string]interface{} {
}

func composeIssueMessage(issue *Issue, doer *User, tplName base.TplName, tos []string, info string) *mailer.Message {
subject := issue.MailSubject()
subject := issue.mailSubject()
body := string(markdown.RenderSpecialLink([]byte(issue.Content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
data := composeTplData(subject, body, issue.HTMLURL())
data["Doer"] = doer
Expand All @@ -171,13 +176,13 @@ func SendIssueCommentMail(issue *Issue, doer *User, tos []string) {
return
}

mailer.SendAsync(composeIssueMessage(issue, doer, MailIssueComment, tos, "issue comment"))
mailer.SendAsync(composeIssueMessage(issue, doer, mailIssueComment, tos, "issue comment"))
}

// SendIssueMentionMail composes and sends issue mention emails to target receivers.
func SendIssueMentionMail(issue *Issue, doer *User, tos []string) {
if len(tos) == 0 {
return
}
mailer.SendAsync(composeIssueMessage(issue, doer, MailIssueMention, tos, "issue mention"))
mailer.SendAsync(composeIssueMessage(issue, doer, mailIssueMention, tos, "issue mention"))
}
12 changes: 7 additions & 5 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ type Release struct {
CreatedUnix int64
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (r *Release) BeforeInsert() {
if r.CreatedUnix == 0 {
r.CreatedUnix = time.Now().Unix()
}
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (r *Release) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
Expand Down Expand Up @@ -151,29 +153,29 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err
return rels, err
}

type ReleaseSorter struct {
type releaseSorter struct {
rels []*Release
}

func (rs *ReleaseSorter) Len() int {
func (rs *releaseSorter) Len() int {
return len(rs.rels)
}

func (rs *ReleaseSorter) Less(i, j int) bool {
func (rs *releaseSorter) Less(i, j int) bool {
diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
if diffNum != 0 {
return diffNum > 0
}
return rs.rels[i].Created.After(rs.rels[j].Created)
}

func (rs *ReleaseSorter) Swap(i, j int) {
func (rs *releaseSorter) Swap(i, j int) {
rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
}

// SortReleases sorts releases by number of commits and created time.
func SortReleases(rels []*Release) {
sorter := &ReleaseSorter{rels: rels}
sorter := &releaseSorter{rels: rels}
sort.Sort(sorter)
}

Expand Down