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.4 kB

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