2023-05-05 10:43:56 +08:00
package model
import (
"errors"
2023-05-06 21:19:49 +08:00
"message-pusher/common"
2023-05-05 10:43:56 +08:00
)
const (
TypeEmail = "email"
TypeWeChatTestAccount = "test"
TypeWeChatCorpAccount = "corp_app"
TypeCorp = "corp"
TypeLark = "lark"
TypeDing = "ding"
TypeTelegram = "telegram"
TypeDiscord = "discord"
TypeBark = "bark"
TypeClient = "client"
TypeNone = "none"
2023-05-06 15:21:17 +08:00
TypeOneBot = "one_bot"
2023-05-07 10:33:48 +08:00
TypeGroup = "group"
2023-05-08 10:39:30 +08:00
TypeLarkApp = "lark_app"
2023-05-08 22:20:14 +08:00
TypeCustom = "custom"
2023-05-15 22:57:12 +08:00
TypeTencentAlarm = "tencent_alarm"
2023-05-05 10:43:56 +08:00
)
type Channel struct {
2024-11-10 10:46:28 +08:00
Id int `json:"id"`
Type string `json:"type" gorm:"type:varchar(32)"`
UserId int `json:"user_id" gorm:"uniqueIndex:name_user_id;index"`
Name string `json:"name" gorm:"type:varchar(32);uniqueIndex:name_user_id"`
Description string `json:"description"`
Status int `json:"status" gorm:"default:1"` // enabled, disabled
Secret string `json:"secret" gorm:"index"`
AppId string `json:"app_id"`
AccountId string `json:"account_id"`
URL string `json:"url" gorm:"column:url"`
Other string `json:"other"`
CreatedTime int64 `json:"created_time" gorm:"bigint"`
Token * string `json:"token" gorm:"token"`
2023-05-05 10:43:56 +08:00
}
2023-05-06 21:19:49 +08:00
type BriefChannel struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
2023-05-15 23:30:29 +08:00
func GetChannelById ( id int , userId int , selectAll bool ) ( * Channel , error ) {
2023-05-05 10:43:56 +08:00
if id == 0 || userId == 0 {
return nil , errors . New ( "id 或 userId 为空!" )
}
c := Channel { Id : id , UserId : userId }
2023-05-15 23:30:29 +08:00
var err error
if selectAll {
err = DB . Where ( c ). First ( & c ). Error
} else {
err = DB . Omit ( "secret" ). Where ( c ). First ( & c ). Error
}
2023-05-05 10:43:56 +08:00
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 ) {
2023-05-08 10:39:30 +08:00
err = DB . Where ( "type in ?" , [] string { TypeWeChatCorpAccount , TypeWeChatTestAccount , TypeLarkApp }). Find ( & channels ). Error
2023-05-05 10:43:56 +08:00
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 ) {
2023-05-06 10:15:18 +08:00
err = DB . Omit ( "secret" ). Where ( "user_id = ?" , userId ). Order ( "id desc" ). Limit ( num ). Offset ( startIdx ). Find ( & channels ). Error
2023-05-05 10:43:56 +08:00
return channels , err
}
2023-05-06 21:19:49 +08:00
func GetBriefChannelsByUserId ( userId int ) ( channels [] * BriefChannel , err error ) {
err = DB . Model ( & Channel {}). Select ( "id" , "name" , "description" ). Where ( "user_id = ? and status = ?" , userId , common . ChannelStatusEnabled ). Find ( & channels ). Error
return channels , err
}
2023-05-05 10:43:56 +08:00
func SearchChannels ( userId int , keyword string ) ( channels [] * Channel , err error ) {
2023-05-06 10:15:18 +08:00
err = DB . Omit ( "secret" ). Where ( "user_id = ?" , userId ). Where ( "id = ? or name LIKE ?" , keyword , keyword + "%" ). Find ( & channels ). Error
2023-05-05 10:43:56 +08:00
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
2024-11-10 10:46:28 +08:00
err = DB . Model ( channel ). Select ( "type" , "name" , "description" , "secret" , "app_id" , "account_id" , "url" , "other" , "status" , "token" ). Updates ( channel ). Error
2023-05-05 10:43:56 +08:00
return err
}
func ( channel * Channel ) Delete () error {
err := DB . Delete ( channel ). Error
return err
}