1
0

channel.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package model
  2. import (
  3. "errors"
  4. "message-pusher/common"
  5. )
  6. const (
  7. TypeEmail = "email"
  8. TypeWeChatTestAccount = "test"
  9. TypeWeChatCorpAccount = "corp_app"
  10. TypeCorp = "corp"
  11. TypeLark = "lark"
  12. TypeDing = "ding"
  13. TypeTelegram = "telegram"
  14. TypeDiscord = "discord"
  15. TypeBark = "bark"
  16. TypeClient = "client"
  17. TypeNone = "none"
  18. TypeOneBot = "one_bot"
  19. TypeGroup = "group"
  20. TypeLarkApp = "lark_app"
  21. TypeCustom = "custom"
  22. TypeTencentAlarm = "tencent_alarm"
  23. )
  24. type Channel struct {
  25. Id int `json:"id"`
  26. Type string `json:"type" gorm:"type:varchar(32)"`
  27. UserId int `json:"user_id" gorm:"uniqueIndex:name_user_id;index"`
  28. Name string `json:"name" gorm:"type:varchar(32);uniqueIndex:name_user_id"`
  29. Description string `json:"description"`
  30. Status int `json:"status" gorm:"default:1"` // enabled, disabled
  31. Secret string `json:"secret" gorm:"index"`
  32. AppId string `json:"app_id"`
  33. AccountId string `json:"account_id"`
  34. URL string `json:"url" gorm:"column:url"`
  35. Other string `json:"other"`
  36. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  37. Token *string `json:"token" gorm:"token"`
  38. }
  39. type BriefChannel struct {
  40. Id int `json:"id"`
  41. Name string `json:"name"`
  42. Description string `json:"description"`
  43. }
  44. func GetChannelById(id int, userId int, selectAll bool) (*Channel, error) {
  45. if id == 0 || userId == 0 {
  46. return nil, errors.New("id 或 userId 为空!")
  47. }
  48. c := Channel{Id: id, UserId: userId}
  49. var err error
  50. if selectAll {
  51. err = DB.Where(c).First(&c).Error
  52. } else {
  53. err = DB.Omit("secret").Where(c).First(&c).Error
  54. }
  55. return &c, err
  56. }
  57. func GetChannelByName(name string, userId int) (*Channel, error) {
  58. if name == "" || userId == 0 {
  59. return nil, errors.New("name 或 userId 为空!")
  60. }
  61. c := Channel{Name: name, UserId: userId}
  62. err := DB.Where(c).First(&c).Error
  63. return &c, err
  64. }
  65. func GetTokenStoreChannels() (channels []*Channel, err error) {
  66. err = DB.Where("type in ?", []string{TypeWeChatCorpAccount, TypeWeChatTestAccount, TypeLarkApp}).Find(&channels).Error
  67. return channels, err
  68. }
  69. func GetTokenStoreChannelsByUserId(userId int) (channels []*Channel, err error) {
  70. err = DB.Where("user_id = ?", userId).Where("type = ? or type = ?", TypeWeChatCorpAccount, TypeWeChatTestAccount).Find(&channels).Error
  71. return channels, err
  72. }
  73. func GetChannelsByUserId(userId int, startIdx int, num int) (channels []*Channel, err error) {
  74. err = DB.Omit("secret").Where("user_id = ?", userId).Order("id desc").Limit(num).Offset(startIdx).Find(&channels).Error
  75. return channels, err
  76. }
  77. func GetBriefChannelsByUserId(userId int) (channels []*BriefChannel, err error) {
  78. err = DB.Model(&Channel{}).Select("id", "name", "description").Where("user_id = ? and status = ?", userId, common.ChannelStatusEnabled).Find(&channels).Error
  79. return channels, err
  80. }
  81. func SearchChannels(userId int, keyword string) (channels []*Channel, err error) {
  82. err = DB.Omit("secret").Where("user_id = ?", userId).Where("id = ? or name LIKE ?", keyword, keyword+"%").Find(&channels).Error
  83. return channels, err
  84. }
  85. func DeleteChannelById(id int, userId int) (c *Channel, err error) {
  86. // Why we need userId here? In case user want to delete other's c.
  87. if id == 0 || userId == 0 {
  88. return nil, errors.New("id 或 userId 为空!")
  89. }
  90. c = &Channel{Id: id, UserId: userId}
  91. err = DB.Where(c).First(&c).Error
  92. if err != nil {
  93. return nil, err
  94. }
  95. return c, c.Delete()
  96. }
  97. func (channel *Channel) Insert() error {
  98. var err error
  99. err = DB.Create(channel).Error
  100. return err
  101. }
  102. func (channel *Channel) UpdateStatus(status int) error {
  103. err := DB.Model(channel).Update("status", status).Error
  104. return err
  105. }
  106. // Update Make sure your token's fields is completed, because this will update non-zero values
  107. func (channel *Channel) Update() error {
  108. var err error
  109. err = DB.Model(channel).Select("type", "name", "description", "secret", "app_id", "account_id", "url", "other", "status", "token").Updates(channel).Error
  110. return err
  111. }
  112. func (channel *Channel) Delete() error {
  113. err := DB.Delete(channel).Error
  114. return err
  115. }