Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

134 linhas
3.1 KiB

  1. package model
  2. import (
  3. "strings"
  4. "sync"
  5. "github.com/QuantumNous/new-api/common"
  6. )
  7. type EmailQuotaRule struct {
  8. Id int `json:"id" gorm:"primaryKey"`
  9. EmailSuffix string `json:"email_suffix" gorm:"size:128;not null;uniqueIndex"`
  10. Quota int64 `json:"quota" gorm:"not null"`
  11. Enabled bool `json:"enabled" gorm:"default:1"`
  12. Description string `json:"description" gorm:"size:256"`
  13. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  14. UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
  15. }
  16. var (
  17. emailQuotaCache map[string]int64
  18. emailQuotaCacheMu sync.RWMutex
  19. )
  20. // LoadEmailQuotaCache loads all enabled rules into memory cache.
  21. func LoadEmailQuotaCache() {
  22. var rules []EmailQuotaRule
  23. DB.Where("enabled = ?", true).Find(&rules)
  24. cache := make(map[string]int64, len(rules))
  25. for _, r := range rules {
  26. cache[strings.ToLower(r.EmailSuffix)] = r.Quota
  27. }
  28. emailQuotaCacheMu.Lock()
  29. emailQuotaCache = cache
  30. emailQuotaCacheMu.Unlock()
  31. }
  32. // MatchEmailQuotaRule checks if the email matches any enabled suffix rule.
  33. // Returns the matched quota, or -1 if no match.
  34. func MatchEmailQuotaRule(email string) int64 {
  35. if email == "" {
  36. return -1
  37. }
  38. at := strings.LastIndex(email, "@")
  39. if at < 0 {
  40. return -1
  41. }
  42. suffix := strings.ToLower(email[at:])
  43. emailQuotaCacheMu.RLock()
  44. defer emailQuotaCacheMu.RUnlock()
  45. if quota, ok := emailQuotaCache[suffix]; ok {
  46. return quota
  47. }
  48. return -1
  49. }
  50. func boolToInt(b bool) int {
  51. if b {
  52. return 1
  53. }
  54. return 0
  55. }
  56. func (r *EmailQuotaRule) Insert() error {
  57. r.CreatedTime = common.GetTimestamp()
  58. r.UpdatedTime = r.CreatedTime
  59. err := DB.Model(&EmailQuotaRule{}).Create(map[string]interface{}{
  60. "email_suffix": r.EmailSuffix,
  61. "quota": r.Quota,
  62. "enabled": boolToInt(r.Enabled),
  63. "description": r.Description,
  64. "created_time": r.CreatedTime,
  65. "updated_time": r.UpdatedTime,
  66. }).Error
  67. if err != nil {
  68. return err
  69. }
  70. var last EmailQuotaRule
  71. if err := DB.Where("email_suffix = ?", r.EmailSuffix).First(&last).Error; err == nil {
  72. r.Id = last.Id
  73. }
  74. LoadEmailQuotaCache()
  75. return nil
  76. }
  77. func (r *EmailQuotaRule) Update() error {
  78. r.UpdatedTime = common.GetTimestamp()
  79. err := DB.Model(&EmailQuotaRule{}).Where("id = ?", r.Id).Updates(map[string]interface{}{
  80. "email_suffix": r.EmailSuffix,
  81. "quota": r.Quota,
  82. "enabled": boolToInt(r.Enabled),
  83. "description": r.Description,
  84. "updated_time": r.UpdatedTime,
  85. }).Error
  86. if err != nil {
  87. return err
  88. }
  89. LoadEmailQuotaCache()
  90. return nil
  91. }
  92. func (r *EmailQuotaRule) Delete() error {
  93. err := DB.Delete(r).Error
  94. if err != nil {
  95. return err
  96. }
  97. LoadEmailQuotaCache()
  98. return nil
  99. }
  100. func GetAllEmailQuotaRules() ([]EmailQuotaRule, error) {
  101. var list []EmailQuotaRule
  102. err := DB.Order("id ASC").Find(&list).Error
  103. return list, err
  104. }
  105. func GetEmailQuotaRuleById(id int) (*EmailQuotaRule, error) {
  106. var r EmailQuotaRule
  107. err := DB.First(&r, id).Error
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &r, nil
  112. }
  113. func GetEmailQuotaRuleBySuffix(suffix string) (*EmailQuotaRule, error) {
  114. var r EmailQuotaRule
  115. err := DB.Where("email_suffix = ?", suffix).First(&r).Error
  116. if err != nil {
  117. return nil, err
  118. }
  119. return &r, nil
  120. }