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 2.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package point
  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. "code.gitea.io/gitea/modules/redis/redis_lock"
  8. "code.gitea.io/gitea/services/reward/limiter"
  9. "code.gitea.io/gitea/services/reward/point/account"
  10. "errors"
  11. "time"
  12. )
  13. type PointOperator struct {
  14. }
  15. func (operator *PointOperator) IsLimited(ctx *models.RewardOperateContext) error {
  16. realAmount, err := limiter.CheckLimit(ctx.SourceType.Name(), models.LimitTypeRewardPoint, ctx.TargetUserId, ctx.Reward.Amount, ctx.RejectPolicy)
  17. if err != nil {
  18. log.Error("PointOperator IsLimited error,err=%v", err)
  19. return err
  20. }
  21. if realAmount < ctx.Reward.Amount {
  22. ctx.LossAmount = ctx.Reward.Amount - realAmount
  23. ctx.Reward.Amount = realAmount
  24. }
  25. return nil
  26. }
  27. func (operator *PointOperator) Operate(ctx *models.RewardOperateContext) error {
  28. lock := redis_lock.NewDistributeLock(redis_key.PointAccountOperateLock(ctx.TargetUserId))
  29. isOk, err := lock.LockWithWait(3*time.Second, 3*time.Second)
  30. if err != nil {
  31. log.Error("Get PointAccountOperateLock error,err=%v", err)
  32. return err
  33. }
  34. if isOk {
  35. defer lock.UnLock()
  36. na, err := account.GetAccount(ctx.TargetUserId)
  37. if err != nil || na == nil {
  38. log.Error("operator get account error error,err=%v", err)
  39. return errors.New("get account error")
  40. }
  41. if ctx.OperateType == models.OperateTypeIncrease {
  42. err = na.Increase(ctx.Reward.Amount, ctx.SourceId)
  43. } else if ctx.OperateType == models.OperateTypeDecrease {
  44. if !ctx.PermittedNegative && na.Balance < ctx.Reward.Amount {
  45. log.Info("account balance is not enough,ctx=%v", ctx)
  46. return models.ErrInsufficientPointsBalance{}
  47. }
  48. err = na.Decrease(ctx.Reward.Amount, ctx.SourceId)
  49. }
  50. if err != nil {
  51. log.Error("operate account balance error,err=%v", err)
  52. return err
  53. }
  54. redis_client.Del(redis_key.PointAccountInfo(ctx.TargetUserId))
  55. } else {
  56. log.Error("Get account operate lock failed,ctx=%v", ctx)
  57. return errors.New("Get account operate lock failed")
  58. }
  59. return nil
  60. }