webhook.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package model
  2. import (
  3. "errors"
  4. )
  5. // WebhookConstructRule Keep compatible with Message
  6. type WebhookConstructRule struct {
  7. Title string `json:"title"`
  8. Description string `json:"description"`
  9. Content string `json:"content"`
  10. URL string `json:"url"`
  11. }
  12. type Webhook struct {
  13. Id int `json:"id"`
  14. UserId int `json:"user_id" gorm:"index"`
  15. Name string `json:"name" gorm:"type:varchar(32);index"`
  16. Status int `json:"status" gorm:"default:1"` // enabled, disabled
  17. Link string `json:"link" gorm:"type:char(32);uniqueIndex"`
  18. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  19. ExtractRule string `json:"extract_rule" gorm:"not null"` // how we extract key info from the request
  20. ConstructRule string `json:"construct_rule" gorm:"not null"` // how we construct message with the extracted info
  21. Channel string `json:"channel" gorm:"type:varchar(32); not null"` // which channel to send our message
  22. }
  23. func GetWebhookById(id int, userId int) (*Webhook, error) {
  24. if id == 0 || userId == 0 {
  25. return nil, errors.New("id 或 userId 为空!")
  26. }
  27. c := Webhook{Id: id, UserId: userId}
  28. err := DB.Where(c).First(&c).Error
  29. return &c, err
  30. }
  31. func GetWebhookByLink(link string) (*Webhook, error) {
  32. if link == "" {
  33. return nil, errors.New("link 为空!")
  34. }
  35. c := Webhook{Link: link}
  36. err := DB.Where(c).First(&c).Error
  37. return &c, err
  38. }
  39. func GetWebhooksByUserId(userId int, startIdx int, num int) (webhooks []*Webhook, err error) {
  40. err = DB.Where("user_id = ?", userId).Order("id desc").Limit(num).Offset(startIdx).Find(&webhooks).Error
  41. return webhooks, err
  42. }
  43. func SearchWebhooks(userId int, keyword string) (webhooks []*Webhook, err error) {
  44. err = DB.Where("user_id = ?", userId).Where("id = ? or link = ? or name LIKE ?", keyword, keyword, keyword+"%").Find(&webhooks).Error
  45. return webhooks, err
  46. }
  47. func DeleteWebhookById(id int, userId int) (c *Webhook, err error) {
  48. // Why we need userId here? In case user want to delete other's c.
  49. if id == 0 || userId == 0 {
  50. return nil, errors.New("id 或 userId 为空!")
  51. }
  52. c = &Webhook{Id: id, UserId: userId}
  53. err = DB.Where(c).First(&c).Error
  54. if err != nil {
  55. return nil, err
  56. }
  57. return c, c.Delete()
  58. }
  59. func (webhook *Webhook) Insert() error {
  60. var err error
  61. err = DB.Create(webhook).Error
  62. return err
  63. }
  64. func (webhook *Webhook) UpdateStatus(status int) error {
  65. err := DB.Model(webhook).Update("status", status).Error
  66. return err
  67. }
  68. // Update Make sure your token's fields is completed, because this will update zero values
  69. func (webhook *Webhook) Update() error {
  70. var err error
  71. err = DB.Model(webhook).Select("status", "name", "extract_rule", "construct_rule", "channel").Updates(webhook).Error
  72. return err
  73. }
  74. func (webhook *Webhook) Delete() error {
  75. err := DB.Delete(webhook).Error
  76. return err
  77. }