mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package mailer
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
|
|
org_model "github.com/hanzoai/git/models/organization"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/templates"
|
|
"github.com/hanzoai/git/modules/translation"
|
|
sender_service "github.com/hanzoai/git/services/mailer/sender"
|
|
)
|
|
|
|
const tplTeamInviteMail templates.TplName = "org/team_invite"
|
|
|
|
// MailTeamInvite sends team invites
|
|
func MailTeamInvite(ctx context.Context, inviter *user_model.User, team *org_model.Team, invite *org_model.TeamInvite) error {
|
|
if setting.MailService == nil {
|
|
return nil
|
|
}
|
|
|
|
org, err := user_model.GetUserByID(ctx, team.OrgID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
locale := translation.NewLocale(inviter.Language)
|
|
|
|
// check if a user with this email already exists
|
|
user, err := user_model.GetUserByEmail(ctx, invite.Email)
|
|
if err != nil && !user_model.IsErrUserNotExist(err) {
|
|
return err
|
|
} else if user != nil && user.ProhibitLogin {
|
|
return errors.New("login is prohibited for the invited user")
|
|
}
|
|
|
|
inviteRedirect := url.QueryEscape("/org/invite/" + invite.Token)
|
|
inviteURL := fmt.Sprintf("%suser/sign_up?redirect_to=%s", setting.AppURL, inviteRedirect)
|
|
|
|
if (err == nil && user != nil) || setting.Service.DisableRegistration || setting.Service.AllowOnlyExternalRegistration {
|
|
// user account exists or registration disabled
|
|
inviteURL = fmt.Sprintf("%suser/login?redirect_to=%s", setting.AppURL, inviteRedirect)
|
|
}
|
|
|
|
subject := locale.TrString("mail.team_invite.subject", inviter.DisplayName(), org.DisplayName())
|
|
mailMeta := map[string]any{
|
|
"locale": locale,
|
|
"Inviter": inviter,
|
|
"Organization": org,
|
|
"Team": team,
|
|
"Invite": invite,
|
|
"Subject": subject,
|
|
"InviteURL": inviteURL,
|
|
}
|
|
|
|
var mailBody bytes.Buffer
|
|
if err := LoadedTemplates().BodyTemplates.ExecuteTemplate(&mailBody, string(tplTeamInviteMail), mailMeta); err != nil {
|
|
log.Error("ExecuteTemplate [%s]: %v", string(tplTeamInviteMail)+"/body", err)
|
|
return err
|
|
}
|
|
|
|
msg := sender_service.NewMessage(invite.Email, subject, mailBody.String())
|
|
msg.Info = subject
|
|
|
|
SendAsync(msg)
|
|
|
|
return nil
|
|
}
|