| @@ -45,12 +45,21 @@ type LimitConfig struct { | |||
| Scope string `xorm:"NOT NULL"` | |||
| LimitNum int64 `xorm:"NOT NULL"` | |||
| LimitCode string | |||
| LimitType string `xorm:"NOT NULL"` | |||
| Creator int64 `xorm:"NOT NULL"` | |||
| LimitType string `xorm:"NOT NULL"` | |||
| CreatorId int64 `xorm:"NOT NULL"` | |||
| CreatorName string | |||
| CreatedUnix timeutil.TimeStamp `xorm:"created"` | |||
| DeletedAt timeutil.TimeStamp `xorm:"deleted"` | |||
| } | |||
| type LimitConfigVO struct { | |||
| RefreshRate string | |||
| Scope string | |||
| LimitNum int64 | |||
| Creator string | |||
| CreatedUnix timeutil.TimeStamp | |||
| } | |||
| func GetLimitConfigByLimitType(limitType LimitType) ([]LimitConfig, error) { | |||
| r := make([]LimitConfig, 0) | |||
| err := x.Where(" limit_type = ?", limitType.Name()).Find(&r) | |||
| @@ -45,13 +45,24 @@ type TaskConfig struct { | |||
| ID int64 `xorm:"pk autoincr"` | |||
| TaskCode string `xorm:"NOT NULL"` | |||
| Tittle string | |||
| AwardType string `xorm:"NOT NULL"` | |||
| AwardAmount int64 `xorm:"NOT NULL"` | |||
| Creator int64 `xorm:"NOT NULL"` | |||
| AwardType string `xorm:"NOT NULL"` | |||
| AwardAmount int64 `xorm:"NOT NULL"` | |||
| CreatorId int64 `xorm:"NOT NULL"` | |||
| CreatorName string | |||
| CreatedUnix timeutil.TimeStamp `xorm:"created"` | |||
| DeletedAt timeutil.TimeStamp `xorm:"deleted"` | |||
| } | |||
| type TaskConfigWithLimit struct { | |||
| TaskCode string | |||
| Tittle string | |||
| AwardType string | |||
| AwardAmount int64 | |||
| Creator string | |||
| CreatedUnix timeutil.TimeStamp | |||
| Limiters []LimitConfigVO | |||
| } | |||
| func getTaskConfig(t *TaskConfig) (*TaskConfig, error) { | |||
| has, err := x.Get(t) | |||
| if err != nil { | |||
| @@ -68,3 +79,14 @@ func GetTaskConfigByTaskCode(taskCode string) (*TaskConfig, error) { | |||
| } | |||
| return getTaskConfig(t) | |||
| } | |||
| func GetTaskConfigList() ([]*TaskConfig, error) { | |||
| r := make([]*TaskConfig, 0) | |||
| err := x.Find(&r) | |||
| if err != nil { | |||
| return nil, err | |||
| } | |||
| if len(r) == 0 { | |||
| return nil, ErrRecordNotExist{} | |||
| } | |||
| return r, nil | |||
| } | |||
| @@ -6,6 +6,9 @@ func TaskAccomplishLock(sourceId string, taskType string) string { | |||
| return KeyJoin(TASK_REDIS_PREFIX, sourceId, taskType, "accomplish") | |||
| } | |||
| func TaskConfig(taskType string) string { | |||
| return KeyJoin(TASK_REDIS_PREFIX, "config", taskType) | |||
| func TaskConfigList() string { | |||
| return KeyJoin(TASK_REDIS_PREFIX, "config", "list") | |||
| } | |||
| func TaskConfigWithLimiterList() string { | |||
| return KeyJoin(TASK_REDIS_PREFIX, "config", "limiter", "list") | |||
| } | |||
| @@ -25,7 +25,7 @@ func ServerError(msg string) *AiforgeResponse { | |||
| } | |||
| func SuccessWithData(data interface{}) *AiforgeResponse { | |||
| return &AiforgeResponse{Code: RESPONSE_CODE_ERROR_DEFAULT, Msg: RESPONSE_MSG_SUCCESS, Data: data} | |||
| return &AiforgeResponse{Code: RESPONSE_CODE_SUCCESS, Msg: RESPONSE_MSG_SUCCESS, Data: data} | |||
| } | |||
| func ErrorWithData(code int, msg string, data interface{}) *AiforgeResponse { | |||
| return &AiforgeResponse{Code: code, Msg: msg, Data: data} | |||
| @@ -0,0 +1,31 @@ | |||
| package point | |||
| import ( | |||
| "code.gitea.io/gitea/modules/context" | |||
| "code.gitea.io/gitea/routers/response" | |||
| "code.gitea.io/gitea/services/reward/point/account" | |||
| "net/http" | |||
| ) | |||
| type AccountResponse struct { | |||
| AccountCode string | |||
| Balance int64 | |||
| TotalEarned int64 | |||
| TotalConsumed int64 | |||
| } | |||
| func GetPointAccount(ctx *context.Context) { | |||
| userId := ctx.User.ID | |||
| a, err := account.GetAccount(userId) | |||
| if err != nil { | |||
| ctx.JSON(http.StatusOK, response.ServerError(err.Error())) | |||
| return | |||
| } | |||
| res := &AccountResponse{ | |||
| AccountCode: a.AccountCode, | |||
| Balance: a.Balance, | |||
| TotalEarned: a.TotalEarned, | |||
| TotalConsumed: a.TotalConsumed, | |||
| } | |||
| ctx.JSON(http.StatusOK, response.SuccessWithData(res)) | |||
| } | |||
| @@ -6,6 +6,8 @@ package routes | |||
| import ( | |||
| "bytes" | |||
| "code.gitea.io/gitea/routers/reward/point" | |||
| "code.gitea.io/gitea/routers/task" | |||
| "encoding/gob" | |||
| "net/http" | |||
| "path" | |||
| @@ -1314,6 +1316,14 @@ func RegisterRoutes(m *macaron.Macaron) { | |||
| m.Post("/purge", user.NotificationPurgePost) | |||
| }, reqSignIn) | |||
| m.Group("/reward/point", func() { | |||
| m.Get("/account", point.GetPointAccount) | |||
| }, reqSignIn) | |||
| m.Group("/task/config", func() { | |||
| m.Get("/list", task.GetTaskConfigList) | |||
| }, reqSignIn) | |||
| if setting.API.EnableSwagger { | |||
| m.Get("/swagger.v1.json", templates.JSONRenderer(), routers.SwaggerV1Json) | |||
| } | |||
| @@ -0,0 +1,17 @@ | |||
| package task | |||
| import ( | |||
| "code.gitea.io/gitea/modules/context" | |||
| "code.gitea.io/gitea/routers/response" | |||
| "code.gitea.io/gitea/services/task" | |||
| "net/http" | |||
| ) | |||
| func GetTaskConfigList(ctx *context.Context) { | |||
| r, err := task.GetTaskConfigWithLimitList() | |||
| if err != nil { | |||
| ctx.JSON(http.StatusOK, response.ServerError(err.Error())) | |||
| return | |||
| } | |||
| ctx.JSON(http.StatusOK, response.SuccessWithData(r)) | |||
| } | |||
| @@ -4,6 +4,7 @@ import ( | |||
| "code.gitea.io/gitea/models" | |||
| "code.gitea.io/gitea/modules/redis/redis_client" | |||
| "code.gitea.io/gitea/modules/redis/redis_key" | |||
| "code.gitea.io/gitea/services/reward/limiter" | |||
| "encoding/json" | |||
| "time" | |||
| ) | |||
| @@ -11,17 +12,30 @@ import ( | |||
| //GetTaskConfig get task config from redis cache first | |||
| // if not exist in redis, find in db and refresh the redis key | |||
| func GetTaskConfig(taskType string) (*models.TaskConfig, error) { | |||
| redisKey := redis_key.TaskConfig(taskType) | |||
| list, err := GetTaskConfigList() | |||
| if err != nil { | |||
| return nil, err | |||
| } | |||
| for _, v := range list { | |||
| if v.TaskCode == taskType { | |||
| return v, nil | |||
| } | |||
| } | |||
| return nil, nil | |||
| } | |||
| func GetTaskConfigList() ([]*models.TaskConfig, error) { | |||
| redisKey := redis_key.TaskConfigList() | |||
| configStr, _ := redis_client.Get(redisKey) | |||
| if configStr != "" { | |||
| if configStr == redis_key.EMPTY_REDIS_VAL { | |||
| return nil, nil | |||
| } | |||
| config := new(models.TaskConfig) | |||
| json.Unmarshal([]byte(configStr), config) | |||
| config := make([]*models.TaskConfig, 0) | |||
| json.Unmarshal([]byte(configStr), &config) | |||
| return config, nil | |||
| } | |||
| config, err := models.GetTaskConfigByTaskCode(taskType) | |||
| config, err := models.GetTaskConfigList() | |||
| if err != nil { | |||
| if models.IsErrRecordNotExist(err) { | |||
| redis_client.Setex(redisKey, redis_key.EMPTY_REDIS_VAL, 5*time.Second) | |||
| @@ -33,3 +47,45 @@ func GetTaskConfig(taskType string) (*models.TaskConfig, error) { | |||
| redis_client.Setex(redisKey, string(jsonStr), 30*24*time.Hour) | |||
| return config, nil | |||
| } | |||
| func GetTaskConfigWithLimitList() ([]*models.TaskConfigWithLimit, error) { | |||
| list, err := GetTaskConfigList() | |||
| if err != nil { | |||
| return nil, err | |||
| } | |||
| if len(list) == 0 { | |||
| return nil, nil | |||
| } | |||
| r := make([]*models.TaskConfigWithLimit, 0) | |||
| l, err := limiter.GetLimitersByLimitType(models.LimitTypeTask) | |||
| if err != nil { | |||
| return nil, err | |||
| } | |||
| for i := 0; i < len(list); i++ { | |||
| li := list[i] | |||
| t := &models.TaskConfigWithLimit{ | |||
| TaskCode: li.TaskCode, | |||
| Tittle: li.Tittle, | |||
| AwardType: li.AwardType, | |||
| AwardAmount: li.AwardAmount, | |||
| Creator: li.CreatorName, | |||
| CreatedUnix: li.CreatedUnix, | |||
| } | |||
| lv := make([]models.LimitConfigVO, 0) | |||
| for j := 0; j < len(l); j++ { | |||
| lj := l[j] | |||
| if lj.LimitCode == li.TaskCode { | |||
| lv = append(lv, models.LimitConfigVO{ | |||
| RefreshRate: lj.RefreshRate, | |||
| Scope: lj.Scope, | |||
| LimitNum: lj.LimitNum, | |||
| Creator: lj.CreatorName, | |||
| CreatedUnix: lj.CreatedUnix, | |||
| }) | |||
| } | |||
| } | |||
| t.Limiters = lv | |||
| r = append(r, t) | |||
| } | |||
| return r, nil | |||
| } | |||