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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. action.OpType = models.GetTaskOptType(action)
  11. switch action.OpType {
  12. case models.ActionCreateRepo,
  13. models.ActionCreateImage:
  14. if action.Repo.IsPrivate {
  15. return
  16. }
  17. }
  18. go accomplish(action)
  19. }
  20. func accomplish(action models.Action) error {
  21. defer func() {
  22. if err := recover(); err != nil {
  23. combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
  24. log.Error("PANIC:%v", combinedErr)
  25. }
  26. }()
  27. userId := action.ActUserID
  28. taskType := fmt.Sprint(action.OpType)
  29. //get task config
  30. config, err := GetTaskConfig(taskType)
  31. if err != nil {
  32. log.Error("GetTaskConfig error,%v", err)
  33. return err
  34. }
  35. if config == nil {
  36. log.Info("task config not exist,userId=%d taskType=%s", userId, taskType)
  37. return nil
  38. }
  39. //is limited?
  40. if isLimited(userId, config, models.JustReject) {
  41. log.Info("task accomplish maximum times are reached,userId=%d taskType=%s", userId, taskType)
  42. return nil
  43. }
  44. //add log
  45. _, err = models.InsertTaskAccomplishLog(&models.TaskAccomplishLog{
  46. ConfigId: config.ID,
  47. TaskCode: config.TaskCode,
  48. UserId: userId,
  49. ActionId: action.ID,
  50. })
  51. if err != nil {
  52. log.Error("InsertTaskAccomplishLog error,%v", err)
  53. return err
  54. }
  55. //reward
  56. reward.Operate(&models.RewardOperateContext{
  57. SourceType: models.SourceTypeAccomplishTask,
  58. SourceId: fmt.Sprint(action.ID),
  59. Tittle: config.Tittle,
  60. Reward: models.Reward{
  61. Amount: config.AwardAmount,
  62. Type: models.GetRewardTypeInstance(config.AwardType),
  63. },
  64. TargetUserId: userId,
  65. RequestId: fmt.Sprint(action.ID),
  66. OperateType: models.OperateTypeIncrease,
  67. RejectPolicy: models.FillUp,
  68. })
  69. log.Debug("accomplish success,action=%v", action)
  70. return nil
  71. }
  72. func isLimited(userId int64, config *models.TaskConfig, rejectPolicy models.LimiterRejectPolicy) bool {
  73. if _, err := limiter.CheckLimit(config.TaskCode, models.LimitTypeTask, userId, 1, rejectPolicy); err != nil {
  74. log.Error(" isLimited CheckLimit error. %v", err)
  75. return true
  76. }
  77. return false
  78. }