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.

task.go 2.7 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
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package task
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/services/reward"
  6. "code.gitea.io/gitea/services/reward/limiter"
  7. "fmt"
  8. )
  9. func Accomplish(action models.Action) {
  10. defer func() {
  11. if err := recover(); err != nil {
  12. combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
  13. log.Error("PANIC:%v", combinedErr)
  14. }
  15. }()
  16. action.OpType = models.GetTaskOptType(action)
  17. switch action.OpType {
  18. case models.ActionCreateRepo:
  19. if action.Repo.IsPrivate {
  20. return
  21. }
  22. case models.ActionCreateImage:
  23. if action.IsPrivate {
  24. return
  25. }
  26. case models.ActionBindWechat:
  27. n, err := models.CountWechatBindLog(action.Content, models.WECHAT_BIND)
  28. if err != nil {
  29. log.Error("CountWechatBindLog error when accomplish task,err=%v", err)
  30. return
  31. }
  32. //if wechatOpenId has been bound before,the action can not get reward
  33. if n > 1 {
  34. return
  35. }
  36. }
  37. go accomplish(action)
  38. }
  39. func accomplish(action models.Action) error {
  40. defer func() {
  41. if err := recover(); err != nil {
  42. combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
  43. log.Error("PANIC:%v", combinedErr)
  44. }
  45. }()
  46. userId := action.ActUserID
  47. taskType := fmt.Sprint(action.OpType)
  48. //get task config
  49. config, err := GetTaskConfig(taskType)
  50. if err != nil {
  51. log.Error("GetTaskConfig error,%v", err)
  52. return err
  53. }
  54. if config == nil {
  55. log.Info("task config not exist,userId=%d taskType=%s", userId, taskType)
  56. return nil
  57. }
  58. //is limited?
  59. if isLimited(userId, config, models.JustReject) {
  60. log.Info("task accomplish maximum times are reached,userId=%d taskType=%s", userId, taskType)
  61. return nil
  62. }
  63. //add log
  64. _, err = models.InsertTaskAccomplishLog(&models.TaskAccomplishLog{
  65. ConfigId: config.ID,
  66. TaskCode: config.TaskCode,
  67. UserId: userId,
  68. ActionId: action.ID,
  69. })
  70. if err != nil {
  71. log.Error("InsertTaskAccomplishLog error,%v", err)
  72. return err
  73. }
  74. //reward
  75. reward.Operate(&models.RewardOperateContext{
  76. SourceType: models.SourceTypeAccomplishTask,
  77. SourceId: fmt.Sprint(action.ID),
  78. Tittle: config.Tittle,
  79. Reward: models.Reward{
  80. Amount: config.AwardAmount,
  81. Type: models.GetRewardTypeInstance(config.AwardType),
  82. },
  83. TargetUserId: userId,
  84. RequestId: fmt.Sprint(action.ID),
  85. OperateType: models.OperateTypeIncrease,
  86. RejectPolicy: models.FillUp,
  87. })
  88. log.Debug("accomplish success,action=%v", action)
  89. return nil
  90. }
  91. func isLimited(userId int64, config *models.TaskConfig, rejectPolicy models.LimiterRejectPolicy) bool {
  92. if _, err := limiter.CheckLimit(config.TaskCode, models.LimitTypeTask, userId, 1, rejectPolicy); err != nil {
  93. log.Error(" isLimited CheckLimit error. %v", err)
  94. return true
  95. }
  96. return false
  97. }