|
- package point
-
- import (
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/redis/redis_key"
- "code.gitea.io/gitea/modules/redis/redis_lock"
- "code.gitea.io/gitea/services/reward"
- "code.gitea.io/gitea/services/reward/limiter"
- "code.gitea.io/gitea/services/reward/point/account"
- "errors"
- "time"
- )
-
- type PointOperator struct {
- }
-
- func (operator *PointOperator) IsLimited(ctx reward.RewardOperateContext) bool {
- if err := limiter.CheckLimit(ctx.Reward.Type, ctx.TargetUserId, ctx.Reward.Amount); err != nil {
- return false
- }
- return true
- }
-
- func (operator *PointOperator) Operate(ctx reward.RewardOperateContext) error {
- a, err := account.GetAccount(ctx.TargetUserId)
- if err != nil || a == nil {
- return errors.New("get account error")
- }
-
- lock := redis_lock.NewDistributeLock(redis_key.PointAccountOperateLock(a.AccountCode))
- if lock.LockWithWait(3*time.Second, 3*time.Second) {
- defer lock.UnLock()
- na, _ := account.GetAccount(ctx.TargetUserId)
- if ctx.OperateType == models.OperateTypeIncrease {
- na.Increase(ctx.Reward.Amount, ctx.SourceId)
- } else if ctx.OperateType == models.OperateTypeDecrease {
- na.Decrease(ctx.Reward.Amount, ctx.SourceId)
- }
-
- } else {
- return errors.New("Get account operate lock failed")
- }
- return nil
- }
|