channel.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. )
  23. type Channel struct {
  24. Id int `json:"id"`
  25. Type string `json:"type" gorm:"type:varchar(32)"`
  26. UserId int `json:"user_id" gorm:"uniqueIndex:name_user_id;index"`
  27. Name string `json:"name" gorm:"type:varchar(32);uniqueIndex:name_user_id"`
  28. Description string `json:"description"`
  29. Status int `json:"status" gorm:"default:1"` // enabled, disabled
  30. Secret string `json:"secret" gorm:"index"`
  31. AppId string `json:"app_id"`
  32. AccountId string `json:"account_id"`
  33. URL string `json:"url" gorm:"column:url"`
  34. Other string `json:"other"`
  35. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  36. }
  37. type BriefChannel struct {
  38. Id int `json:"id"`
  39. Name string `json:"name"`
  40. Description string `json:"description"`
  41. }
  42. func GetChannelById(id int, userId int) (*Channel, error) {
  43. if id == 0 || userId == 0 {
  44. return nil, errors.New("id 或 userId 为空!")
  45. }
  46. c := Channel{Id: id, UserId: userId}
  47. err := DB.Where(c).First(&c).Error
  48. return &c, err
  49. }
  50. func GetChannelByName(name string, userId int) (*Channel, error) {
  51. if name == "" || userId == 0 {
  52. return nil, errors.New("name 或 userId 为空!")
  53. }
  54. c := Channel{Name: name, UserId: userId}
  55. err := DB.Where(c).First(&c).Error
  56. return &c, err
  57. }
  58. func GetTokenStoreChannels() (channels []*Channel, err error) {
  59. err = DB.Where("type in ?", []string{TypeWeChatCorpAccount, TypeWeChatTestAccount, TypeLarkApp}).Find(&channels).Error
  60. return channels, err
  61. }
  62. func GetTokenStoreChannelsByUserId(userId int) (channels []*Channel, err error) {
  63. err = DB.Where("user_id = ?", userId).Where("type = ? or type = ?", TypeWeChatCorpAccount, TypeWeChatTestAccount).Find(&channels).Error
  64. return channels, err
  65. }
  66. func GetChannelsByUserId(userId int, startIdx int, num int) (channels []*Channel, err error) {
  67. err = DB.Omit("secret").Where("user_id = ?", userId).Order("id desc").Limit(num).Offset(startIdx).Find(&channels).Error
  68. return channels, err
  69. }
  70. func GetBriefChannelsByUserId(userId int) (channels []*BriefChannel, err error) {
  71. err = DB.Model(&Channel{}).Select("id", "name", "description").Where("user_id = ? and status = ?", userId, common.ChannelStatusEnabled).Find(&channels).Error
  72. return channels, err
  73. }
  74. func SearchChannels(userId int, keyword string) (channels []*Channel, err error) {
  75. err = DB.Omit("secret").Where("user_id = ?", userId).Where("id = ? or name LIKE ?", keyword, keyword+"%").Find(&channels).Error
  76. return channels, err
  77. }
  78. func DeleteChannelById(id int, userId int) (c *Channel, err error) {
  79. // Why we need userId here? In case user want to delete other's c.
  80. if id == 0 || userId == 0 {
  81. return nil, errors.New("id 或 userId 为空!")
  82. }
  83. c = &Channel{Id: id, UserId: userId}
  84. err = DB.Where(c).First(&c).Error
  85. if err != nil {
  86. return nil, err
  87. }
  88. return c, c.Delete()
  89. }
  90. func (channel *Channel) Insert() error {
  91. var err error
  92. err = DB.Create(channel).Error
  93. return err
  94. }
  95. func (channel *Channel) UpdateStatus(status int) error {
  96. err := DB.Model(channel).Update("status", status).Error
  97. return err
  98. }
  99. // Update Make sure your token's fields is completed, because this will update non-zero values
  100. func (channel *Channel) Update() error {
  101. var err error
  102. err = DB.Model(channel).Select("type", "name", "description", "secret", "app_id", "account_id", "url", "other", "status").Updates(channel).Error
  103. return err
  104. }
  105. func (channel *Channel) Delete() error {
  106. err := DB.Delete(channel).Error
  107. return err
  108. }