feat: now server part supports multiple channels for the same type (#50)

This commit is contained in:
JustSong
2023-05-05 10:43:56 +08:00
parent b291ed43ca
commit 3a8d625201
21 changed files with 601 additions and 298 deletions
+106
View File
@@ -0,0 +1,106 @@
package model
import (
"errors"
)
const (
TypeEmail = "email"
TypeWeChatTestAccount = "test"
TypeWeChatCorpAccount = "corp_app"
TypeCorp = "corp"
TypeLark = "lark"
TypeDing = "ding"
TypeTelegram = "telegram"
TypeDiscord = "discord"
TypeBark = "bark"
TypeClient = "client"
TypeNone = "none"
)
type Channel struct {
Id int `json:"id"`
Type string `json:"type" gorm:"type:varchar(32)"`
UserId int `json:"user_id" gorm:"uniqueIndex:name_user_id"`
Name string `json:"name" gorm:"type:varchar(32);uniqueIndex:name_user_id"`
Status int `json:"status" gorm:"default:1"` // enabled, disabled
Secret string `json:"secret"`
AppId string `json:"app_id"`
AccountId string `json:"account_id"`
URL string `json:"url" gorm:"column:url"`
Other string `json:"other"`
}
func GetChannelById(id int, userId int) (*Channel, error) {
if id == 0 || userId == 0 {
return nil, errors.New("id 或 userId 为空!")
}
c := Channel{Id: id, UserId: userId}
err := DB.Where(c).First(&c).Error
return &c, err
}
func GetChannelByName(name string, userId int) (*Channel, error) {
if name == "" || userId == 0 {
return nil, errors.New("name 或 userId 为空!")
}
c := Channel{Name: name, UserId: userId}
err := DB.Where(c).First(&c).Error
return &c, err
}
func GetTokenStoreChannels() (channels []*Channel, err error) {
err = DB.Where("type = ? or type = ?", TypeWeChatCorpAccount, TypeWeChatTestAccount).Find(&channels).Error
return channels, err
}
func GetTokenStoreChannelsByUserId(userId int) (channels []*Channel, err error) {
err = DB.Where("user_id = ?", userId).Where("type = ? or type = ?", TypeWeChatCorpAccount, TypeWeChatTestAccount).Find(&channels).Error
return channels, err
}
func GetChannelsByUserId(userId int, startIdx int, num int) (channels []*Channel, err error) {
err = DB.Where("user_id = ?", userId).Order("id desc").Limit(num).Offset(startIdx).Find(&channels).Error
return channels, err
}
func SearchChannels(userId int, keyword string) (channels []*Channel, err error) {
err = DB.Where("user_id = ?", userId).Select([]string{"id", "name"}).Where("id = ? or name LIKE", keyword, keyword+"%").Find(&channels).Error
return channels, err
}
func DeleteChannelById(id int, userId int) (c *Channel, err error) {
// Why we need userId here? In case user want to delete other's c.
if id == 0 || userId == 0 {
return nil, errors.New("id 或 userId 为空!")
}
c = &Channel{Id: id, UserId: userId}
err = DB.Where(c).First(&c).Error
if err != nil {
return nil, err
}
return c, c.Delete()
}
func (channel *Channel) Insert() error {
var err error
err = DB.Create(channel).Error
return err
}
func (channel *Channel) UpdateStatus(status int) error {
err := DB.Model(channel).Update("status", status).Error
return err
}
// Update Make sure your token's fields is completed, because this will update non-zero values
func (channel *Channel) Update() error {
var err error
err = DB.Model(channel).Select("type", "name", "secret", "app_id", "account_id", "url", "other").Updates(channel).Error
return err
}
func (channel *Channel) Delete() error {
err := DB.Delete(channel).Error
return err
}
+4
View File
@@ -64,6 +64,10 @@ func InitDB() (err error) {
if err != nil {
return err
}
err = db.AutoMigrate(&Channel{})
if err != nil {
return err
}
err = createRootAccountIfNeed()
return err
} else {
+15 -44
View File
@@ -9,41 +9,20 @@ import (
// User if you add sensitive fields, don't forget to clean them in setupLogin function.
// Otherwise, the sensitive information will be saved on local storage in plain text!
type User struct {
Id int `json:"id"`
Username string `json:"username" gorm:"unique;index" validate:"max=12"`
Password string `json:"password" gorm:"not null;" validate:"min=8,max=20"`
DisplayName string `json:"display_name" gorm:"index" validate:"max=20"`
Role int `json:"role" gorm:"type:int;default:1"` // admin, common
Status int `json:"status" gorm:"type:int;default:1"` // enabled, disabled
Token string `json:"token"`
Email string `json:"email" gorm:"index" validate:"max=50"`
GitHubId string `json:"github_id" gorm:"column:github_id;index"`
WeChatId string `json:"wechat_id" gorm:"column:wechat_id;index"`
VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
Channel string `json:"channel"`
WeChatTestAccountId string `json:"wechat_test_account_id" gorm:"column:wechat_test_account_id"`
WeChatTestAccountSecret string `json:"wechat_test_account_secret" gorm:"column:wechat_test_account_secret"`
WeChatTestAccountTemplateId string `json:"wechat_test_account_template_id" gorm:"column:wechat_test_account_template_id"`
WeChatTestAccountOpenId string `json:"wechat_test_account_open_id" gorm:"column:wechat_test_account_open_id"`
WeChatTestAccountVerificationToken string `json:"wechat_test_account_verification_token" gorm:"column:wechat_test_account_verification_token"`
WeChatCorpAccountId string `json:"wechat_corp_account_id" gorm:"column:wechat_corp_account_id"`
WeChatCorpAccountAgentSecret string `json:"wechat_corp_account_agent_secret" gorm:"column:wechat_corp_account_agent_secret"`
WeChatCorpAccountAgentId string `json:"wechat_corp_account_agent_id" gorm:"column:wechat_corp_account_agent_id"`
WeChatCorpAccountUserId string `json:"wechat_corp_account_user_id" gorm:"column:wechat_corp_account_user_id"`
WeChatCorpAccountClientType string `json:"wechat_corp_account_client_type" gorm:"column:wechat_corp_account_client_type;default=plugin"`
CorpWebhookURL string `json:"corp_webhook_url" gorm:"corp_webhook_url"`
LarkWebhookURL string `json:"lark_webhook_url"`
LarkWebhookSecret string `json:"lark_webhook_secret"`
DingWebhookURL string `json:"ding_webhook_url"`
DingWebhookSecret string `json:"ding_webhook_secret"`
BarkServer string `json:"bark_server"`
BarkSecret string `json:"bark_secret"`
ClientSecret string `json:"client_secret"`
TelegramBotToken string `json:"telegram_bot_token"`
TelegramChatId string `json:"telegram_chat_id"`
DiscordWebhookURL string `json:"discord_webhook_url"`
SendEmailToOthers int `json:"send_email_to_others" gorm:"type:int;default:0"`
SaveMessageToDatabase int `json:"save_message_to_database" gorm:"type:int;default:0"`
Id int `json:"id"`
Username string `json:"username" gorm:"unique;index" validate:"max=12"`
Password string `json:"password" gorm:"not null;" validate:"min=8,max=20"`
DisplayName string `json:"display_name" gorm:"index" validate:"max=20"`
Role int `json:"role" gorm:"type:int;default:1"` // admin, common
Status int `json:"status" gorm:"type:int;default:1"` // enabled, disabled
Token string `json:"token"`
Email string `json:"email" gorm:"index" validate:"max=50"`
GitHubId string `json:"github_id" gorm:"column:github_id;index"`
WeChatId string `json:"wechat_id" gorm:"column:wechat_id;index"`
VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
Channel string `json:"channel"`
SendEmailToOthers int `json:"send_email_to_others" gorm:"type:int;default:0"`
SaveMessageToDatabase int `json:"save_message_to_database" gorm:"type:int;default:0"`
}
func GetMaxUserId() int {
@@ -57,11 +36,6 @@ func GetAllUsers(startIdx int, num int) (users []*User, err error) {
return users, err
}
func GetAllUsersWithSecrets() (users []*User, err error) {
err = DB.Where("status = ?", common.UserStatusEnabled).Where("wechat_test_account_id != '' or wechat_corp_account_id != ''").Find(&users).Error
return users, err
}
func SearchUsers(keyword string) (users []*User, err error) {
err = DB.Select([]string{"id", "username", "display_name", "role", "status", "email"}).Where("id = ? or username LIKE ? or email LIKE ? or display_name LIKE ?", keyword, keyword+"%", keyword+"%", keyword+"%").Find(&users).Error
return users, err
@@ -77,10 +51,7 @@ func GetUserById(id int, selectAll bool) (*User, error) {
err = DB.First(&user, "id = ?", id).Error
} else {
err = DB.Select([]string{"id", "username", "display_name", "role", "status", "email", "wechat_id", "github_id",
"channel", "token",
"wechat_test_account_id", "wechat_test_account_template_id", "wechat_test_account_open_id",
"wechat_corp_account_id", "wechat_corp_account_agent_id", "wechat_corp_account_user_id", "wechat_corp_account_client_type",
"bark_server", "telegram_chat_id", "save_message_to_database",
"channel", "token", "save_message_to_database",
}).First(&user, "id = ?", id).Error
}
return &user, err