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

Commit

Permalink
feat(user): Admin email sending isolation between sites.
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode committed Apr 28, 2022
1 parent 3120b81 commit 9a8b979
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 38 deletions.
24 changes: 16 additions & 8 deletions cmd/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,23 @@ func syncConfWithDB() {
// 导入配置文件的管理员用户
for _, admin := range config.Instance.AdminUsers {
user := model.FindUser(admin.Name, admin.Email)
receiveEmail := true // 默认允许接收邮件
if admin.ReceiveEmail != nil {
receiveEmail = *admin.ReceiveEmail
}
if user.IsEmpty() {
// create
user = model.User{
Name: admin.Name,
Email: admin.Email,
Link: admin.Link,
Password: admin.Password,
BadgeName: admin.BadgeName,
BadgeColor: admin.BadgeColor,
IsAdmin: true,
IsInConf: true,
Name: admin.Name,
Email: admin.Email,
Link: admin.Link,
Password: admin.Password,
BadgeName: admin.BadgeName,
BadgeColor: admin.BadgeColor,
IsAdmin: true,
IsInConf: true,
ReceiveEmail: receiveEmail,
SiteNames: strings.Join(admin.Sites, ","),
}
model.CreateUser(&user)
} else {
Expand All @@ -175,6 +181,8 @@ func syncConfWithDB() {
user.BadgeColor = admin.BadgeColor
user.IsAdmin = true
user.IsInConf = true
user.ReceiveEmail = receiveEmail
user.SiteNames = strings.Join(admin.Sites, ",")
model.UpdateUser(&user)
}
}
Expand Down
14 changes: 8 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ type SSLConf struct {
}

type AdminUserConf struct {
Name string `mapstructure:"name" json:"name"`
Email string `mapstructure:"email" json:"email"`
Link string `mapstructure:"link" json:"link"`
Password string `mapstructure:"password" json:"password"`
BadgeName string `mapstructure:"badge_name" json:"badge_name"`
BadgeColor string `mapstructure:"badge_color" json:"badge_color"`
Name string `mapstructure:"name" json:"name"`
Email string `mapstructure:"email" json:"email"`
Link string `mapstructure:"link" json:"link"`
Password string `mapstructure:"password" json:"password"`
BadgeName string `mapstructure:"badge_name" json:"badge_name"`
BadgeColor string `mapstructure:"badge_color" json:"badge_color"`
ReceiveEmail *bool `mapstructure:"receive_email" json:"receive_email"`
Sites []string `mapstructure:"sites" json:"sites"`
}

type ModeratorConf struct {
Expand Down
13 changes: 12 additions & 1 deletion model/notify_launcher/notify_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func SendNotify(comment *model.Comment, pComment *model.Comment) {
// ==============
// 回复对方
// ==============
if !comment.IsPending { // 待审状态评论禁止回复对方
if !comment.IsPending && pComment.FetchUser().ReceiveEmail {
// 不是待审状态评论 && 对方开启接收邮件
notify := model.FindCreateNotify(pComment.UserID, comment.ID)
notify.SetComment(*comment)
notify.SetInitial()
Expand All @@ -53,6 +54,16 @@ func SendNotify(comment *model.Comment, pComment *model.Comment) {
continue
}

// 只发送给对应站点管理员
if admin.SiteNames != "" && !lib.ContainsStr(admin.ToCooked().SiteNames, comment.SiteName) {
continue
}

// 关闭接收邮件
if !admin.ReceiveEmail {
continue
}

notify := model.FindCreateNotify(admin.ID, comment.ID)
notify.SetComment(*comment)
notify.SetInitial()
Expand Down
57 changes: 34 additions & 23 deletions model/user.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package model

import (
"github.com/ArtalkJS/ArtalkGo/lib"
"gorm.io/gorm"
)

type User struct {
gorm.Model
Name string `gorm:"index;size:255"`
Email string `gorm:"index;size:255"`
Link string
Password string
BadgeName string
BadgeColor string
LastIP string
LastUA string
IsAdmin bool
Name string `gorm:"index;size:255"`
Email string `gorm:"index;size:255"`
Link string
Password string
BadgeName string
BadgeColor string
LastIP string
LastUA string
IsAdmin bool
SiteNames string
ReceiveEmail bool `gorm:"default:true"`

// 配置文件中添加的
IsInConf bool
Expand All @@ -25,23 +28,31 @@ func (u User) IsEmpty() bool {
}

type CookedUser struct {
ID uint `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Link string `json:"link"`
BadgeName string `json:"badge_name"`
BadgeColor string `json:"badge_color"`
IsAdmin bool `json:"is_admin"`
ID uint `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Link string `json:"link"`
BadgeName string `json:"badge_name"`
BadgeColor string `json:"badge_color"`
IsAdmin bool `json:"is_admin"`
SiteNames []string `json:"site_names"`
SiteNamesRaw string `json:"site_names_raw"`
ReceiveEmail bool `json:"receive_email"`
}

func (u User) ToCooked() CookedUser {
splitSites := lib.RemoveBlankStrings(lib.SplitAndTrimSpace(u.SiteNames, ","))

return CookedUser{
ID: u.ID,
Name: u.Name,
Email: u.Email,
Link: u.Link,
BadgeName: u.BadgeName,
BadgeColor: u.BadgeColor,
IsAdmin: u.IsAdmin,
ID: u.ID,
Name: u.Name,
Email: u.Email,
Link: u.Link,
BadgeName: u.BadgeName,
BadgeColor: u.BadgeColor,
IsAdmin: u.IsAdmin,
SiteNames: splitSites,
SiteNamesRaw: u.SiteNames,
ReceiveEmail: u.ReceiveEmail,
}
}

0 comments on commit 9a8b979

Please sign in to comment.