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

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