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.

point_operate.go 1.3 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package point
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/redis/redis_key"
  5. "code.gitea.io/gitea/modules/redis/redis_lock"
  6. "code.gitea.io/gitea/services/reward"
  7. "code.gitea.io/gitea/services/reward/limiter"
  8. "code.gitea.io/gitea/services/reward/point/account"
  9. "errors"
  10. "time"
  11. )
  12. type PointOperator struct {
  13. }
  14. func (operator *PointOperator) IsLimited(ctx reward.RewardOperateContext) bool {
  15. if err := limiter.CheckLimit(ctx.Reward.Type, ctx.TargetUserId, ctx.Reward.Amount); err != nil {
  16. return false
  17. }
  18. return true
  19. }
  20. func (operator *PointOperator) Operate(ctx reward.RewardOperateContext) error {
  21. a, err := account.GetAccount(ctx.TargetUserId)
  22. if err != nil || a == nil {
  23. return errors.New("get account error")
  24. }
  25. lock := redis_lock.NewDistributeLock(redis_key.PointAccountOperateLock(a.AccountCode))
  26. if lock.LockWithWait(3*time.Second, 3*time.Second) {
  27. defer lock.UnLock()
  28. na, _ := account.GetAccount(ctx.TargetUserId)
  29. if ctx.OperateType == models.OperateTypeIncrease {
  30. na.Increase(ctx.Reward.Amount, ctx.SourceId)
  31. } else if ctx.OperateType == models.OperateTypeDecrease {
  32. na.Decrease(ctx.Reward.Amount, ctx.SourceId)
  33. }
  34. } else {
  35. return errors.New("Get account operate lock failed")
  36. }
  37. return nil
  38. }