|
|
|
@@ -39,6 +39,7 @@ type Settings struct {
|
|
|
|
|
From string `json:"from"`
|
|
|
|
|
Recipients []string `json:"recipients"`
|
|
|
|
|
Cc []string `json:"cc_recipients"`
|
|
|
|
|
Review string `json:"review_address"`
|
|
|
|
|
Weekday int `json:"weekday"`
|
|
|
|
|
Hour int `json:"hour"`
|
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
@@ -58,6 +59,7 @@ type SettingsInput struct {
|
|
|
|
|
From string `json:"from"`
|
|
|
|
|
Recipients []string `json:"recipients"`
|
|
|
|
|
Cc []string `json:"cc_recipients"`
|
|
|
|
|
Review string `json:"review_address"`
|
|
|
|
|
Weekday int `json:"weekday"`
|
|
|
|
|
Hour int `json:"hour"`
|
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
@@ -196,6 +198,7 @@ func normalize(input SettingsInput) (SettingsInput, error) {
|
|
|
|
|
input.Host = strings.TrimSpace(input.Host)
|
|
|
|
|
input.Username = strings.TrimSpace(input.Username)
|
|
|
|
|
input.From = strings.TrimSpace(input.From)
|
|
|
|
|
input.Review = strings.TrimSpace(input.Review)
|
|
|
|
|
seen := map[string]bool{}
|
|
|
|
|
recipients := make([]string, 0, len(input.Recipients))
|
|
|
|
|
for _, raw := range input.Recipients {
|
|
|
|
@@ -236,6 +239,9 @@ func normalize(input SettingsInput) (SettingsInput, error) {
|
|
|
|
|
if input.Port < 1 || input.Port > 65535 || input.Weekday < 1 || input.Weekday > 7 || input.Hour < 0 || input.Hour > 23 {
|
|
|
|
|
return input, ErrInvalid
|
|
|
|
|
}
|
|
|
|
|
if input.Review != "" && !validAddress(input.Review) {
|
|
|
|
|
return input, ErrInvalid
|
|
|
|
|
}
|
|
|
|
|
if input.Enabled {
|
|
|
|
|
if input.Host == "" || input.From == "" || !validAddress(input.From) || len(input.Recipients) == 0 {
|
|
|
|
|
return input, ErrInvalid
|
|
|
|
@@ -250,8 +256,8 @@ func (s *Store) ReadSettings(ctx context.Context) (Settings, error) {
|
|
|
|
|
return out, ErrUnavailable
|
|
|
|
|
}
|
|
|
|
|
var sealed []byte
|
|
|
|
|
err := s.pool.QueryRow(ctx, `SELECT smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,send_weekday,send_hour,enabled,revision,updated_by,updated_at FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).
|
|
|
|
|
Scan(&out.Host, &out.Port, &out.Username, &sealed, &out.From, &out.Recipients, &out.Cc, &out.Weekday, &out.Hour, &out.Enabled, &out.Revision, &out.UpdatedBy, &out.UpdatedAt)
|
|
|
|
|
err := s.pool.QueryRow(ctx, `SELECT smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).
|
|
|
|
|
Scan(&out.Host, &out.Port, &out.Username, &sealed, &out.From, &out.Recipients, &out.Cc, &out.Review, &out.Weekday, &out.Hour, &out.Enabled, &out.Revision, &out.UpdatedBy, &out.UpdatedAt)
|
|
|
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
|
|
|
return out, nil
|
|
|
|
|
}
|
|
|
|
@@ -304,10 +310,10 @@ func (s *Store) SaveSettings(ctx context.Context, actor string, input SettingsIn
|
|
|
|
|
return Settings{}, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if _, err = tx.Exec(ctx, `INSERT INTO qa_weekly_email_settings(tenant_key,smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,send_weekday,send_hour,enabled,revision,updated_by,updated_at)
|
|
|
|
|
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,now())
|
|
|
|
|
ON CONFLICT (tenant_key) DO UPDATE SET smtp_host=excluded.smtp_host,smtp_port=excluded.smtp_port,smtp_username=excluded.smtp_username,smtp_password_sealed=excluded.smtp_password_sealed,from_address=excluded.from_address,recipients=excluded.recipients,cc_recipients=excluded.cc_recipients,send_weekday=excluded.send_weekday,send_hour=excluded.send_hour,enabled=excluded.enabled,revision=excluded.revision,updated_by=excluded.updated_by,updated_at=now()`,
|
|
|
|
|
s.tenant, normalized.Host, normalized.Port, normalized.Username, sealed, normalized.From, normalized.Recipients, normalized.Cc, normalized.Weekday, normalized.Hour, normalized.Enabled, next, actor); err != nil {
|
|
|
|
|
if _, err = tx.Exec(ctx, `INSERT INTO qa_weekly_email_settings(tenant_key,smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at)
|
|
|
|
|
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,now())
|
|
|
|
|
ON CONFLICT (tenant_key) DO UPDATE SET smtp_host=excluded.smtp_host,smtp_port=excluded.smtp_port,smtp_username=excluded.smtp_username,smtp_password_sealed=excluded.smtp_password_sealed,from_address=excluded.from_address,recipients=excluded.recipients,cc_recipients=excluded.cc_recipients,review_address=excluded.review_address,send_weekday=excluded.send_weekday,send_hour=excluded.send_hour,enabled=excluded.enabled,revision=excluded.revision,updated_by=excluded.updated_by,updated_at=now()`,
|
|
|
|
|
s.tenant, normalized.Host, normalized.Port, normalized.Username, sealed, normalized.From, normalized.Recipients, normalized.Cc, normalized.Review, normalized.Weekday, normalized.Hour, normalized.Enabled, next, actor); err != nil {
|
|
|
|
|
return Settings{}, ErrUnavailable
|
|
|
|
|
}
|
|
|
|
|
if err = tx.Commit(ctx); err != nil {
|
|
|
|
@@ -569,8 +575,14 @@ func (s *Store) TestSend(ctx context.Context, week string) (string, error) {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
// A review copy goes only to the operator's own review address so testing never
|
|
|
|
|
// reaches colleagues who are on the real distribution list.
|
|
|
|
|
target := settings.Recipients
|
|
|
|
|
if settings.Review != "" {
|
|
|
|
|
target = []string{settings.Review}
|
|
|
|
|
}
|
|
|
|
|
return s.mailer.Send(ctx, SMTPConfig{Host: settings.Host, Port: settings.Port, Username: settings.Username, Password: password},
|
|
|
|
|
Message{To: settings.Recipients, Cc: settings.Cc, From: settings.From, Subject: fmt.Sprintf("知识库周报 %s(测试发送)", week), HTML: html, Text: text, Images: images})
|
|
|
|
|
Message{To: target, Cc: nil, From: settings.From, Subject: fmt.Sprintf("知识库周报 %s(测试发送)", week), HTML: html, Text: text, Images: images})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) send(ctx context.Context, settings Settings, week string, revision int64, markdown string, now time.Time) (DueResult, error) {
|
|
|
|
|