加入挑战

This commit is contained in:
marvzhang
2020-03-19 18:56:59 +08:00
parent e3a059eb7c
commit e33dfd7992
30 changed files with 504 additions and 21 deletions

View File

@@ -19,10 +19,36 @@ func GetService(name string, uid bson.ObjectId) Service {
switch name {
case constants.ChallengeLogin7d:
return &Login7dService{UserId: uid}
case constants.ChallengeLogin30d:
return &Login30dService{UserId: uid}
case constants.ChallengeLogin90d:
return &Login90dService{UserId: uid}
case constants.ChallengeLogin180d:
return &Login180dService{UserId: uid}
case constants.ChallengeCreateCustomizedSpider:
return &CreateCustomizedSpiderService{UserId: uid}
case constants.ChallengeCreateConfigurableSpider:
return &CreateConfigurableSpiderService{UserId: uid}
case constants.ChallengeCreateSchedule:
return &CreateScheduleService{UserId: uid}
case constants.ChallengeCreateNodes:
return &CreateNodesService{UserId: uid}
case constants.ChallengeRunRandom:
return &RunRandomService{UserId: uid}
case constants.ChallengeScrape1k:
return &Scrape1kService{UserId: uid}
case constants.ChallengeScrape10k:
return &Scrape10kService{UserId: uid}
case constants.ChallengeScrape100k:
return &Scrape100kService{UserId: uid}
case constants.ChallengeInstallDep:
return &InstallDepService{UserId: uid}
case constants.ChallengeInstallLang:
return &InstallLangService{UserId: uid}
case constants.ChallengeViewDisclaimer:
return &ViewDisclaimerService{UserId: uid}
case constants.ChallengeCreateUser:
return &CreateUserService{UserId: uid}
}
return nil
}
@@ -98,6 +124,8 @@ func InitChallengeService() error {
continue
}
} else {
ch.Id = chDb.Id
ch.CreateTs = chDb.CreateTs
if err := ch.Save(); err != nil {
log.Errorf(err.Error())
debug.PrintStack()

View File

@@ -0,0 +1,23 @@
package challenge
import (
"crawlab/constants"
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type CreateConfigurableSpiderService struct {
UserId bson.ObjectId
}
func (s *CreateConfigurableSpiderService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"type": constants.Configurable,
}
_, count, err := model.GetSpiderList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return count > 0, nil
}

View File

@@ -0,0 +1,22 @@
package challenge
import (
"crawlab/constants"
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type CreateNodesService struct {
UserId bson.ObjectId
}
func (s *CreateNodesService) Check() (bool, error) {
query := bson.M{
"status": constants.StatusOnline,
}
list, err := model.GetScheduleList(query)
if err != nil {
return false, err
}
return len(list) >= 3, nil
}

View File

@@ -0,0 +1,21 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type CreateScheduleService struct {
UserId bson.ObjectId
}
func (s *CreateScheduleService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
}
list, err := model.GetScheduleList(query)
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -0,0 +1,21 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type CreateUserService struct {
UserId bson.ObjectId
}
func (s *CreateUserService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
}
list, err := model.GetUserList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -0,0 +1,23 @@
package challenge
import (
"crawlab/constants"
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type InstallDepService struct {
UserId bson.ObjectId
}
func (s *InstallDepService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"type": constants.ActionTypeInstallDep,
}
list, err := model.GetActionList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -0,0 +1,23 @@
package challenge
import (
"crawlab/constants"
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type InstallLangService struct {
UserId bson.ObjectId
}
func (s *InstallLangService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"type": constants.ActionTypeInstallLang,
}
list, err := model.GetActionList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -0,0 +1,18 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type Login180dService struct {
UserId bson.ObjectId
}
func (s *Login180dService) Check() (bool, error) {
days, err := model.GetVisitDays(s.UserId)
if err != nil {
return false, err
}
return days >= 180, nil
}

View File

@@ -0,0 +1,18 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type Login30dService struct {
UserId bson.ObjectId
}
func (s *Login30dService) Check() (bool, error) {
days, err := model.GetVisitDays(s.UserId)
if err != nil {
return false, err
}
return days >= 30, nil
}

View File

@@ -0,0 +1,18 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type Login90dService struct {
UserId bson.ObjectId
}
func (s *Login90dService) Check() (bool, error) {
days, err := model.GetVisitDays(s.UserId)
if err != nil {
return false, err
}
return days >= 90, nil
}

View File

@@ -14,6 +14,7 @@ func (s *RunRandomService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"run_type": constants.RunTypeRandom,
"status": constants.StatusFinished,
}
list, err := model.GetTaskList(query, 0, 1, "-_id")
if err != nil {

View File

@@ -0,0 +1,24 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type Scrape100kService struct {
UserId bson.ObjectId
}
func (s *Scrape100kService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"result_count": bson.M{
"$gte": 100000,
},
}
list, err := model.GetTaskList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -0,0 +1,24 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type Scrape10kService struct {
UserId bson.ObjectId
}
func (s *Scrape10kService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"result_count": bson.M{
"$gte": 10000,
},
}
list, err := model.GetTaskList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -0,0 +1,24 @@
package challenge
import (
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type Scrape1kService struct {
UserId bson.ObjectId
}
func (s *Scrape1kService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"result_count": bson.M{
"$gte": 1000,
},
}
list, err := model.GetTaskList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -0,0 +1,23 @@
package challenge
import (
"crawlab/constants"
"crawlab/model"
"github.com/globalsign/mgo/bson"
)
type ViewDisclaimerService struct {
UserId bson.ObjectId
}
func (s *ViewDisclaimerService) Check() (bool, error) {
query := bson.M{
"user_id": s.UserId,
"type": constants.ActionTypeViewDisclaimer,
}
list, err := model.GetActionList(query, 0, 1, "-_id")
if err != nil {
return false, err
}
return len(list) > 0, nil
}

View File

@@ -14,7 +14,7 @@ import (
)
func InitUserService() error {
_ = CreateNewUser("admin", "admin", constants.RoleAdmin, "")
_ = CreateNewUser("admin", "admin", constants.RoleAdmin, "", bson.ObjectIdHex(constants.ObjectIdNull))
return nil
}
@@ -90,12 +90,13 @@ func CheckToken(tokenStr string) (user model.User, err error) {
return
}
func CreateNewUser(username string, password string, role string, email string) error {
func CreateNewUser(username string, password string, role string, email string, uid bson.ObjectId) error {
user := model.User{
Username: strings.ToLower(username),
Password: utils.EncryptPassword(password),
Role: role,
Email: email,
UserId: uid,
Setting: model.UserSetting{
NotificationTrigger: constants.NotificationTriggerNever,
EnabledNotifications: []string{
@@ -112,11 +113,10 @@ func CreateNewUser(username string, password string, role string, email string)
}
func GetCurrentUser(c *gin.Context) *model.User {
data, _ := c.Get("currentUser")
data, _ := c.Get(constants.ContextUser)
return data.(*model.User)
}
func GetCurrentUserId(c *gin.Context) bson.ObjectId {
return GetCurrentUser(c).Id
}