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.

notify.go 1.5 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package reward
  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/timeutil"
  8. "encoding/json"
  9. "fmt"
  10. "time"
  11. )
  12. func NotifyRewardOperation(userId, amount int64, rewardType models.RewardType, operateType models.RewardOperateType) {
  13. data := &models.UserRewardOperationRedis{
  14. UserId: userId,
  15. Amount: amount,
  16. RewardType: rewardType,
  17. OperateType: operateType,
  18. }
  19. b, _ := json.Marshal(data)
  20. redis_client.ZAdd(redis_key.RewardOperateNotification(), string(b), float64(time.Now().Unix()))
  21. }
  22. func GetRewardOperation(since, until timeutil.TimeStamp) []models.UserRewardOperation {
  23. list, err := redis_client.ZRangeByScore(redis_key.RewardOperateNotification(), float64(since), float64(until))
  24. if err != nil {
  25. log.Error("GetRewardOperation ZRangeByScore error. %v", err)
  26. return nil
  27. }
  28. if len(list) == 0 {
  29. log.Debug("GetRewardOperation list length = 0")
  30. return nil
  31. }
  32. r := make([]models.UserRewardOperation, len(list))
  33. for _, v := range list {
  34. t := models.UserRewardOperationRedis{}
  35. json.Unmarshal([]byte(v), &t)
  36. r = append(r, models.UserRewardOperation{
  37. UserId: t.UserId,
  38. Msg: v,
  39. })
  40. }
  41. redis_client.ZRemRangeByScore(redis_key.RewardOperateNotification(), float64(since), float64(until))
  42. return r
  43. }
  44. func GetRewardOperateMsg(u models.UserRewardOperationRedis) string {
  45. return u.OperateType.Show() + fmt.Sprint(u.Amount) + u.RewardType.Show()
  46. }