You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 2.8 kB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package limiter
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/redis/redis_client"
  6. "code.gitea.io/gitea/modules/redis/redis_key"
  7. )
  8. func GetSingleDailyPointLimitConfig() (*models.LimitConfigVO, error) {
  9. r, err := GetLimitConfigList(models.LimitConfigQueryOpts{
  10. RefreshRate: models.PeriodDaily,
  11. Scope: models.LimitScopeSingleUser,
  12. LimitCode: models.SourceTypeAccomplishTask.Name(),
  13. LimitType: models.LimitTypeRewardPoint,
  14. })
  15. if err != nil {
  16. return nil, err
  17. }
  18. if r == nil || len(r) == 0 {
  19. return nil, nil
  20. }
  21. return r[0], nil
  22. }
  23. func SetSingleDailyPointLimitConfig(limitNum int64, doer *models.User) error {
  24. l := &models.LimitConfigVO{
  25. RefreshRate: models.PeriodDaily,
  26. Scope: models.LimitScopeSingleUser.Name(),
  27. LimitCode: models.SourceTypeAccomplishTask.Name(),
  28. LimitType: models.LimitTypeRewardPoint.Name(),
  29. LimitNum: limitNum,
  30. }
  31. return AddLimitConfig(l, doer)
  32. }
  33. func GetLimitConfigList(opts models.LimitConfigQueryOpts) ([]*models.LimitConfigVO, error) {
  34. r, err := GetLimitersByLimitType(opts.LimitType)
  35. if err != nil {
  36. log.Error("GetLimitConfigList error when getting limiters by limit type.err=%v", err)
  37. return nil, err
  38. }
  39. result := make([]*models.LimitConfigVO, 0)
  40. for _, v := range r {
  41. if opts.LimitCode != "" && opts.LimitCode != v.LimitCode {
  42. continue
  43. }
  44. if opts.Scope != "" && opts.Scope.Name() != v.Scope {
  45. continue
  46. }
  47. if opts.RefreshRate != "" && opts.RefreshRate != v.RefreshRate {
  48. continue
  49. }
  50. if opts.LimitType != "" && opts.LimitType.Name() != v.LimitType {
  51. continue
  52. }
  53. result = append(result, v.ToLimitConfigVO())
  54. }
  55. return result, nil
  56. }
  57. func GetLimitConfigById(id int64) (*models.LimitConfig, error) {
  58. return models.GetLimitConfigById(id)
  59. }
  60. func AddLimitConfig(config *models.LimitConfigVO, doer *models.User) error {
  61. r := &models.LimitConfig{
  62. Tittle: config.Tittle,
  63. RefreshRate: config.RefreshRate,
  64. Scope: config.Scope,
  65. LimitNum: config.LimitNum,
  66. LimitCode: config.LimitCode,
  67. LimitType: config.LimitType,
  68. CreatorId: doer.ID,
  69. CreatorName: doer.Name,
  70. }
  71. err := models.AddLimitConfig(r)
  72. if err != nil {
  73. log.Error("add limit config error,config:%v err:%v", config, err)
  74. return err
  75. }
  76. redis_client.Del(redis_key.LimitConfig(config.LimitType))
  77. return nil
  78. }
  79. func DeleteLimitConfig(id int64, doer *models.User) error {
  80. config, err := GetLimitConfigById(id)
  81. if err != nil {
  82. log.Error("GetLimitConfigById err,e=%v", err)
  83. return err
  84. }
  85. err = models.DeleteLimitConfig(*config, doer.ID, doer.Name)
  86. if err != nil {
  87. log.Error("add limit config error,config:%v err:%v", config, err)
  88. return err
  89. }
  90. redis_client.Del(redis_key.LimitConfig(config.LimitType))
  91. return nil
  92. }