Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
refactor(conf): Rename notify to admin_notify & admin_notify.email op…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: qwqcode <qwqcode@gmail.com>
  • Loading branch information
qwqcode committed May 22, 2022
1 parent 37112f9 commit 924a080
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 59 deletions.
38 changes: 10 additions & 28 deletions artalk-go.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ email:
enabled: false # 总开关
send_type: "smtp" # 发送方式 [smtp, ali_dm, sendmail]
send_name: '{{reply_nick}}' # 发信人昵称
send_addr: "example@qq.com" # 发信人地址
send_addr: "noreply@example.com" # 发信人地址
mail_subject: "[{{site_name}}] 您收到了来自 @{{reply_nick}} 的回复"
mail_subject_to_admin: '[{{site_name}}] 您的文章 "{{page_title}}" 有新回复'

This comment has been minimized.

Copy link
@qwqcode

qwqcode May 22, 2022

Author Member

mail_subject_to_admin 弃用,变更为 admin_notify.email.mail_subject

mail_tpl: "default" # 邮件模板文件
smtp:
host: "smtp.qq.com"
Expand All @@ -118,7 +117,7 @@ email:
ali_dm: # https://help.aliyun.com/document_detail/29444.html
access_key_id: "" # 阿里云颁发给用户的访问服务所用的密钥 ID
access_key_secret: "" # 用于加密的密钥
account_name: "example@example.com" # 管理控制台中配置的发信地址
account_name: "noreply@example.com" # 管理控制台中配置的发信地址

# 图片上传
img_upload:
Expand All @@ -132,38 +131,21 @@ img_upload:
exec: "./upgit -c <upgit配置文件路径> -t /artalk-img"
del_local: true # 上传后删除本地的图片

# 其他通知方式
notify:
# 管理员多元推送
admin_notify:
# 邮件通知管理员
email:
enabled: true
mail_subject: '[{{site_name}}] 您的文章 "{{page_title}}" 有新回复'

This comment has been minimized.

Copy link
@qwqcode

qwqcode May 22, 2022

Author Member

这里可以独立控制管理员是否接收邮件通知

# Telegram
telegram:
enabled: false
api_token: ""
receivers:
- 7777777
# 飞书
lark:
enabled: false
webhook_url: ""
# 钉钉
ding_talk:
enabled: false
token: ""
secret: ""
# Bark
bark:
enabled: false
server: "http://day.app/xxxxxxx/"
# Slack
slack:
enabled: false
oauth_token: ""
receivers:
- "CHANNEL_ID"
# LINE
line:
enabled: false
channel_secret: ""
channel_access_token: ""
receivers:
- "USER_ID_1"
- "GROUP_ID_1"
# 另支持 飞书机器人、钉钉、Slack、LINE 等更多推送方式
# 详情参考:https://artalk.js.org/guide/backend/notify.html
46 changes: 24 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import "time"
// Config 配置
// @link https://godoc.org/github.com/mitchellh/mapstructure
type Config struct {
AppKey string `mapstructure:"app_key" json:"app_key"` // 加密密钥
Debug bool `mapstructure:"debug" json:"debug"` // 调试模式
TimeZone string `mapstructure:"timezone" json:"timezone"` // 时区
Host string `mapstructure:"host" json:"host"` // HTTP Server 监听 IP
Port int `mapstructure:"port" json:"port"` // HTTP Server 监听 Port
DB DBConf `mapstructure:"db" json:"db"` // 数据文件
Cache CacheConf `mapstructure:"cache" json:"cache"` // 缓存
Log LogConf `mapstructure:"log" json:"log"` // 日志文件
AllowOrigins []string `mapstructure:"allow_origins" json:"allow_origins"` // @deprecated 已废弃 (请使用 TrustedDomains)
TrustedDomains []string `mapstructure:"trusted_domains" json:"trusted_domains"` // 可信任的域名 (新)
SSL SSLConf `mapstructure:"ssl" json:"ssl"` // SSL
SiteDefault string `mapstructure:"site_default" json:"site_default"` // 默认站点名(当请求无指定 site_name 时使用)
AdminUsers []AdminUserConf `mapstructure:"admin_users" json:"admin_users"` // 管理员账户
LoginTimeout int `mapstructure:"login_timeout" json:"login_timeout"` // 登陆超时
Moderator ModeratorConf `mapstructure:"moderator" json:"moderator"` // 评论审查
Captcha CaptchaConf `mapstructure:"captcha" json:"captcha"` // 验证码
Email EmailConf `mapstructure:"email" json:"email"` // 邮箱提醒
ImgUpload ImgUploadConf `mapstructure:"img_upload" json:"img_upload"` // 图片上传
Notify NotifyConf `mapstructure:"notify" json:"notify"` // 其他通知方式
Frontend FrontendConf `mapstructure:"frontend" json:"frontend"`
AppKey string `mapstructure:"app_key" json:"app_key"` // 加密密钥
Debug bool `mapstructure:"debug" json:"debug"` // 调试模式
TimeZone string `mapstructure:"timezone" json:"timezone"` // 时区
Host string `mapstructure:"host" json:"host"` // HTTP Server 监听 IP
Port int `mapstructure:"port" json:"port"` // HTTP Server 监听 Port
DB DBConf `mapstructure:"db" json:"db"` // 数据文件
Cache CacheConf `mapstructure:"cache" json:"cache"` // 缓存
Log LogConf `mapstructure:"log" json:"log"` // 日志文件
AllowOrigins []string `mapstructure:"allow_origins" json:"allow_origins"` // @deprecated 已废弃 (请使用 TrustedDomains)
TrustedDomains []string `mapstructure:"trusted_domains" json:"trusted_domains"` // 可信任的域名 (新)
SSL SSLConf `mapstructure:"ssl" json:"ssl"` // SSL
SiteDefault string `mapstructure:"site_default" json:"site_default"` // 默认站点名(当请求无指定 site_name 时使用)
AdminUsers []AdminUserConf `mapstructure:"admin_users" json:"admin_users"` // 管理员账户
LoginTimeout int `mapstructure:"login_timeout" json:"login_timeout"` // 登陆超时
Moderator ModeratorConf `mapstructure:"moderator" json:"moderator"` // 评论审查
Captcha CaptchaConf `mapstructure:"captcha" json:"captcha"` // 验证码
Email EmailConf `mapstructure:"email" json:"email"` // 邮箱提醒
ImgUpload ImgUploadConf `mapstructure:"img_upload" json:"img_upload"` // 图片上传
AdminNotify AdminNotifyConf `mapstructure:"admin_notify" json:"admin_notify"` // 其他通知方式
Notify *AdminNotifyConf `mapstructure:"notify" json:"notify"` // @deprecated 已废弃 (请使用 AdminNotify)

This comment has been minimized.

Copy link
@qwqcode

qwqcode May 22, 2022

Author Member

仅增加了 AdminNotify 配置项;修改 Notify 类型为指针,并弃用

除此之外,其他没有变

Frontend FrontendConf `mapstructure:"frontend" json:"frontend"`
}

type DBConf struct {
Expand Down Expand Up @@ -141,7 +142,7 @@ type EmailConf struct {
SendName string `mapstructure:"send_name" json:"send_name"` // 发件人名
SendAddr string `mapstructure:"send_addr" json:"send_addr"` // 发件人地址
MailSubject string `mapstructure:"mail_subject" json:"mail_subject"` // 邮件标题
MailSubjectToAdmin string `mapstructure:"mail_subject_to_admin" json:"mail_subject_to_admin"` // 邮件标题 (发送给管理员用)
MailSubjectToAdmin string `mapstructure:"mail_subject_to_admin" json:"mail_subject_to_admin"` // @deprecated 已废弃 (请使用 AdminNotify.Email.MailSubject) - 邮件标题 (发送给管理员用)
MailTpl string `mapstructure:"mail_tpl" json:"mail_tpl"` // 邮件模板
SMTP SMTPConf `mapstructure:"smtp" json:"smtp"` // SMTP 配置
AliDM AliDMConf `mapstructure:"ali_dm" json:"ali_dm"` // 阿里云邮件配置
Expand Down Expand Up @@ -216,7 +217,8 @@ type UpgitConf struct {
}

// 其他通知方式
type NotifyConf struct {
type AdminNotifyConf struct {
Email EmailConf `mapstructure:"email" json:"email"` // 邮件通知
Telegram NotifyTelegramConf `mapstructure:"telegram" json:"telegram"` // TG
Lark NotifyLarkConf `mapstructure:"lark" json:"lark"` // 飞书
DingTalk NotifyDingTalkConf `mapstructure:"ding_talk" json:"ding_talk"` // 钉钉
Expand Down
12 changes: 11 additions & 1 deletion config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func postInit() {
Instance.Captcha.Always = true
}

// 检查废弃需更新配置
/* 检查废弃需更新配置 */
if Instance.Captcha.ActionTimeout != 0 {
logrus.Warn("captcha.action_timeout 配置项已废弃,请使用 captcha.action_reset 代替")
if Instance.Captcha.ActionReset == 0 {
Expand All @@ -101,4 +101,14 @@ func postInit() {
Instance.TrustedDomains = Instance.AllowOrigins
}
}

// @version < 2.2.0
if Instance.Notify != nil {
logrus.Warn("notify 配置项已废弃,请使用 admin_notify 代替")
Instance.AdminNotify = *Instance.Notify
}
if Instance.Email.MailSubjectToAdmin != "" {
logrus.Warn("email.mail_subject_to_admin 配置项已废弃,请使用 admin_notify.email.mail_subject 代替")
Instance.AdminNotify.Email.MailSubject = Instance.Email.MailSubjectToAdmin
}

This comment has been minimized.

Copy link
@qwqcode

qwqcode May 22, 2022

Author Member

兼容旧版配置,提示弃用信息

}
2 changes: 1 addition & 1 deletion lib/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func AsyncSendToAdmin(notify *model.Notify, admin *model.User) {
}

fromName := RenderCommon(config.Instance.Email.SendName, notify, from, to)
subject := RenderCommon(config.Instance.Email.MailSubjectToAdmin, notify, from, to)
subject := RenderCommon(config.Instance.AdminNotify.Email.MailSubjectToAdmin, notify, from, to)
body := RenderEmailTpl(notify, from, to)

AddToQueue(Email{
Expand Down
10 changes: 5 additions & 5 deletions model/notify_launcher/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ func Init() {
email.InitQueue()

// Telegram
tgConf := config.Instance.Notify.Telegram
tgConf := config.Instance.AdminNotify.Telegram
if tgConf.Enabled {
telegramService, _ := telegram.New(tgConf.ApiToken)
telegramService.AddReceivers(tgConf.Receivers...)
notify.UseServices(telegramService)
}

// 钉钉
dingTalkConf := config.Instance.Notify.DingTalk
dingTalkConf := config.Instance.AdminNotify.DingTalk
if dingTalkConf.Enabled {
dingTalkService := dingding.New(&dingding.Config{Token: dingTalkConf.Token, Secret: dingTalkConf.Secret})
notify.UseServices(dingTalkService)
}

// Slack
slackConf := config.Instance.Notify.Slack
slackConf := config.Instance.AdminNotify.Slack
if slackConf.Enabled {
slackService := slack.New(slackConf.OauthToken)
slackService.AddReceivers(slackConf.Receivers...)
notify.UseServices(slackService)
}

// LINE
LINEConf := config.Instance.Notify.LINE
LINEConf := config.Instance.AdminNotify.LINE
if LINEConf.Enabled {
lineService, _ := line.New(config.Instance.Notify.LINE.ChannelSecret, config.Instance.Notify.LINE.ChannelAccessToken)
lineService, _ := line.New(config.Instance.AdminNotify.LINE.ChannelSecret, config.Instance.AdminNotify.LINE.ChannelAccessToken)
lineService.AddReceivers(LINEConf.Receivers...)
notify.UseServices(lineService)
}
Expand Down
4 changes: 2 additions & 2 deletions model/notify_launcher/notify_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func AdminNotify(comment *model.Comment, pComment *model.Comment) {

// 飞书发送
func SendLark(msg string) {
larkConf := config.Instance.Notify.Lark
larkConf := config.Instance.AdminNotify.Lark
if !larkConf.Enabled {
return
}
Expand All @@ -139,7 +139,7 @@ func SendLark(msg string) {

// Bark 发送
func SendBark(title string, msg string) {
barkConf := config.Instance.Notify.Bark
barkConf := config.Instance.AdminNotify.Bark
if !barkConf.Enabled {
return
}
Expand Down

0 comments on commit 924a080

Please sign in to comment.