Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
taskcenter
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Odysseus
taskcenter
Commits
99adfe02
Commit
99adfe02
authored
Oct 24, 2024
by
贾浩@五瓣科技
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update ve
parent
c5675226
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
224 additions
and
319 deletions
+224
-319
dao.go
dao/dao.go
+1
-1
db.go
dao/db.go
+1
-1
tw_task.go
dao/tw_task.go
+23
-9
jwt.go
middleware/jwt.go
+25
-10
api.go
model/api/api.go
+30
-4
taskcenter.go
model/db/taskcenter.go
+15
-5
group.go
server/group.go
+0
-42
project.go
server/project.go
+0
-61
router.go
server/router.go
+7
-13
task.go
server/task.go
+27
-0
group.go
service/group.go
+12
-65
project.go
service/project.go
+0
-108
task.go
service/task.go
+83
-0
No files found.
dao/dao.go
View file @
99adfe02
...
@@ -56,7 +56,7 @@ func New(_c *config.Config) (dao *Dao, err error) {
...
@@ -56,7 +56,7 @@ func New(_c *config.Config) (dao *Dao, err error) {
sqlDB
.
SetConnMaxIdleTime
(
time
.
Hour
)
sqlDB
.
SetConnMaxIdleTime
(
time
.
Hour
)
if
os
.
Getenv
(
"MIGRATE"
)
==
"true"
{
if
os
.
Getenv
(
"MIGRATE"
)
==
"true"
{
err
=
dao
.
db
.
AutoMigrate
(
&
dbModel
.
TaskAction
{},
&
dbModel
.
Project
{},
&
dbModel
.
TaskGroup
{},
&
dbModel
.
Task
{},
&
dbModel
.
TaskHistory
{})
err
=
dao
.
db
.
AutoMigrate
(
&
dbModel
.
TaskAction
{},
&
dbModel
.
TaskGroup
{},
&
dbModel
.
Task
{},
&
dbModel
.
TaskHistory
{},
&
dbModel
.
AdminUser
{})
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
...
...
dao/db.go
View file @
99adfe02
...
@@ -81,7 +81,7 @@ func (d *Dao) GetGroupTasks(gid int) (list []*dbModel.Task, err error) {
...
@@ -81,7 +81,7 @@ func (d *Dao) GetGroupTasks(gid int) (list []*dbModel.Task, err error) {
return
list
,
d
.
db
.
Where
(
"group_id = ?"
,
gid
)
.
Find
(
&
list
)
.
Error
return
list
,
d
.
db
.
Where
(
"group_id = ?"
,
gid
)
.
Find
(
&
list
)
.
Error
}
}
func
(
d
*
Dao
)
Create
Group
Task
(
gt
*
dbModel
.
Task
)
(
err
error
)
{
func
(
d
*
Dao
)
CreateTask
(
gt
*
dbModel
.
Task
)
(
err
error
)
{
return
d
.
db
.
Create
(
gt
)
.
Error
return
d
.
db
.
Create
(
gt
)
.
Error
}
}
...
...
dao/tw_task.go
View file @
99adfe02
...
@@ -14,14 +14,21 @@ import (
...
@@ -14,14 +14,21 @@ import (
"github.com/tidwall/gjson"
"github.com/tidwall/gjson"
)
)
func
(
d
*
Dao
)
CreateTwitterProject
(
apiKey
,
apiSecret
,
accessToken
,
accessSecret
,
token
,
projectName
string
)
(
twitterUserId
int
,
twitterHandle
,
twitterName
string
,
err
error
)
{
type
TwitterConfig
struct
{
APIKey
string
APISecret
string
AccessToken
string
AccessTokenSecret
string
TwitterToken
string
}
func
(
d
*
Dao
)
GetTwitterInfo
(
config
TwitterConfig
)
(
twitterUserId
int
,
twitterHandle
,
twitterName
string
,
err
error
)
{
body
:=
map
[
string
]
string
{
body
:=
map
[
string
]
string
{
"api_key"
:
apiKey
,
"api_key"
:
config
.
APIKey
,
"api_key_secret"
:
apiSecret
,
"api_key_secret"
:
config
.
APISecret
,
"access_token"
:
accessToken
,
"access_token"
:
config
.
AccessToken
,
"access_token_secret"
:
accessSecret
,
"access_token_secret"
:
config
.
AccessTokenSecret
,
"token"
:
token
,
"token"
:
config
.
TwitterToken
,
"project"
:
projectName
,
}
}
buf
:=
new
(
bytes
.
Buffer
)
buf
:=
new
(
bytes
.
Buffer
)
err
=
json
.
NewEncoder
(
buf
)
.
Encode
(
body
)
err
=
json
.
NewEncoder
(
buf
)
.
Encode
(
body
)
...
@@ -59,11 +66,18 @@ func (d *Dao) CreateTwitterProject(apiKey, apiSecret, accessToken, accessSecret,
...
@@ -59,11 +66,18 @@ func (d *Dao) CreateTwitterProject(apiKey, apiSecret, accessToken, accessSecret,
return
userId
,
respTemp
.
Data
.
Username
,
respTemp
.
Data
.
Name
,
nil
return
userId
,
respTemp
.
Data
.
Username
,
respTemp
.
Data
.
Name
,
nil
}
}
func
(
d
*
Dao
)
DoTweetTask
(
twitterUserId
int
,
tweetId
int
,
twitterHandle
,
action
string
,
start
bool
)
(
err
error
)
{
func
(
d
*
Dao
)
DoTweetTask
(
twitterUserId
int
,
tweetId
int
,
twitterHandle
,
action
string
,
start
bool
,
config
TwitterConfig
)
(
err
error
)
{
body
:=
map
[
string
]
string
{
body
:=
map
[
string
]
interface
{}
{
"user_id"
:
fmt
.
Sprintf
(
"%d"
,
twitterUserId
),
"user_id"
:
fmt
.
Sprintf
(
"%d"
,
twitterUserId
),
"task_id"
:
fmt
.
Sprintf
(
"%d"
,
tweetId
),
"task_id"
:
fmt
.
Sprintf
(
"%d"
,
tweetId
),
"task_type"
:
action
,
"task_type"
:
action
,
"config"
:
map
[
string
]
string
{
"api_key"
:
config
.
APIKey
,
"api_key_secret"
:
config
.
APISecret
,
"access_token"
:
config
.
AccessToken
,
"access_token_secret"
:
config
.
AccessTokenSecret
,
"token"
:
config
.
TwitterToken
,
},
}
}
if
twitterHandle
!=
""
&&
action
==
constant
.
TwitterAPIActionFollow
{
if
twitterHandle
!=
""
&&
action
==
constant
.
TwitterAPIActionFollow
{
...
...
middleware/jwt.go
View file @
99adfe02
package
middleware
package
middleware
import
(
import
(
"taskcenter/constant"
"taskcenter/dao"
"taskcenter/util"
"taskcenter/util"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
log
"github.com/sirupsen/logrus"
log
"github.com/sirupsen/logrus"
)
)
func
JWTMiddleware
(
secret
string
,
needAdmin
bool
)
gin
.
HandlerFunc
{
func
JWTMiddleware
(
d
*
dao
.
Dao
,
secret
string
,
needAdmin
bool
)
gin
.
HandlerFunc
{
return
func
(
c
*
gin
.
Context
)
{
return
func
(
c
*
gin
.
Context
)
{
tokenString
:=
c
.
GetHeader
(
"Authorization"
)
tokenString
:=
c
.
GetHeader
(
"Authorization"
)
if
tokenString
==
""
||
len
(
tokenString
)
<
7
{
if
tokenString
==
""
||
len
(
tokenString
)
<
7
{
...
@@ -20,7 +22,7 @@ func JWTMiddleware(secret string, needAdmin bool) gin.HandlerFunc {
...
@@ -20,7 +22,7 @@ func JWTMiddleware(secret string, needAdmin bool) gin.HandlerFunc {
return
return
}
}
ok
,
expired
,
uid
,
role
:=
util
.
ParseJWT
(
secret
,
tokenString
[
7
:
])
ok
,
expired
,
uid
,
_
:=
util
.
ParseJWT
(
secret
,
tokenString
[
7
:
])
if
!
ok
{
if
!
ok
{
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
200
,
gin
.
H
{
"code"
:
1
,
"code"
:
1
,
...
@@ -41,14 +43,27 @@ func JWTMiddleware(secret string, needAdmin bool) gin.HandlerFunc {
...
@@ -41,14 +43,27 @@ func JWTMiddleware(secret string, needAdmin bool) gin.HandlerFunc {
return
return
}
}
if
needAdmin
&&
role
!=
"admin"
{
if
needAdmin
{
c
.
JSON
(
200
,
gin
.
H
{
ok
,
err
:=
d
.
IsAdminUser
(
uid
)
"code"
:
1
,
if
err
!=
nil
{
"msg"
:
"permission denied"
,
log
.
WithError
(
err
)
.
Error
(
"auth is admin"
)
"data"
:
""
,
c
.
JSON
(
200
,
gin
.
H
{
})
"code"
:
1
,
c
.
Abort
()
"msg"
:
constant
.
InternalError
,
return
"data"
:
""
,
})
c
.
Abort
()
return
}
if
!
ok
{
c
.
JSON
(
200
,
gin
.
H
{
"code"
:
1
,
"msg"
:
"no access permissions"
,
"data"
:
""
,
})
c
.
Abort
()
return
}
}
}
log
.
WithField
(
"userId"
,
uid
)
.
Debug
(
"jwt uid"
)
log
.
WithField
(
"userId"
,
uid
)
.
Debug
(
"jwt uid"
)
...
...
model/api/api.go
View file @
99adfe02
...
@@ -39,11 +39,10 @@ type GetProjectResponse struct {
...
@@ -39,11 +39,10 @@ type GetProjectResponse struct {
}
}
type
CreateGroupRequest
struct
{
type
CreateGroupRequest
struct
{
ProjectId
int
`json:"projectId" binding:"required"`
GroupId
int
`json:"groupId,omitempty"`
GroupId
int
`json:"groupId,omitempty"`
Description
string
`json:"description"`
Description
string
`json:"description"`
Tasks
[]
Task
`json:"tasks"`
//
Tasks []Task `json:"tasks"`
UserId
string
`json:"-"`
UserId
string
`json:"-"`
}
}
type
Task
struct
{
type
Task
struct
{
...
@@ -61,7 +60,34 @@ type Task struct {
...
@@ -61,7 +60,34 @@ type Task struct {
TelegramActiveThreshold
int
`json:"telegramActiveThreshold"`
// 仅电报活跃可用
TelegramActiveThreshold
int
`json:"telegramActiveThreshold"`
// 仅电报活跃可用
}
}
type
GetGroupResponse
CreateGroupRequest
type
CreateTaskRequest
struct
{
Platform
string
`json:"platform" binding:"required"`
Action
string
`json:"action" binding:"required"`
Url
string
`json:"url"`
TweetId
int
`json:"tweetId"`
Description
string
`json:"description" binding:"required"`
Reward
int
`json:"reward" binding:"required"`
Start
int
`json:"start" binding:"required"`
End
int
`json:"end" binding:"required"`
Daily
bool
`json:"daily" binding:"required"`
TelegramActiveThreshold
int
`json:"telegramActiveThreshold"`
// 仅电报活跃可用
TwitterAPIKey
string
`json:"twitterAPIKey"`
TwitterAPISecret
string
`json:"twitterAPISecret"`
TwitterAccessToken
string
`json:"twitterAccessToken"`
TwitterAccessSecret
string
`json:"twitterAccessSecret"`
TwitterToken
string
`json:"twitterToken"`
TelegramChatId
int
`json:"telegramChatId"`
UserId
string
`json:"-"`
TaskId
int
`json:"-"`
GroupId
int
`json:"groupId"`
}
type
GetGroupResponse
struct
{
GroupId
int
`json:"groupId,omitempty"`
Description
string
`json:"description"`
UserId
string
`json:"-"`
Tasks
[]
Task
`json:"tasks"`
}
type
GetGroupListResponse
struct
{
type
GetGroupListResponse
struct
{
TotalCount
int
`json:"totalCount"`
TotalCount
int
`json:"totalCount"`
...
...
model/db/taskcenter.go
View file @
99adfe02
...
@@ -46,7 +46,6 @@ func (p *Project) TableName() string {
...
@@ -46,7 +46,6 @@ func (p *Project) TableName() string {
type
TaskGroup
struct
{
type
TaskGroup
struct
{
Id
int
`gorm:"primaryKey;autoIncrement:false"`
Id
int
`gorm:"primaryKey;autoIncrement:false"`
UserId
string
`gorm:"type:uuid;not null;comment:关联用户id"`
UserId
string
`gorm:"type:uuid;not null;comment:关联用户id"`
ProjectId
int
`gorm:"type:int;not null;comment:项目id"`
Description
string
`gorm:"type:text;not null;comment:任务组描述"`
Description
string
`gorm:"type:text;not null;comment:任务组描述"`
gorm
.
Model
gorm
.
Model
}
}
...
@@ -55,17 +54,27 @@ func (t *TaskGroup) TableName() string {
...
@@ -55,17 +54,27 @@ func (t *TaskGroup) TableName() string {
return
"taskcenter.task_group"
return
"taskcenter.task_group"
}
}
type
Config
struct
{
TwitterAPIKey
string
`gorm:"type:text;not null;comment:tweet api key"`
TwitterAPISecret
string
`gorm:"type:text;not null;comment:tweet api secret"`
TwitterAccessToken
string
`gorm:"type:text;not null;comment:tweet access token"`
TwitterAccessSecret
string
`gorm:"type:text;not null;comment:tweet access secret"`
TwitterToken
string
`gorm:"type:text;not null;comment:tweet token"`
TwitterHandle
string
`gorm:"type:text;not null;comment:tweet handle"`
TwitterName
string
`gorm:"type:text;not null;comment:tweet name"`
TwitterUserId
int
`gorm:"type:int;not null;comment:tweet user id,用于关注"`
TelegramChatId
int
`gorm:"type:int;not null;comment:telegram chat id"`
TelegramChatUsername
string
`gorm:"type:text;not null;comment:telegram chat username"`
TelegramChatTitle
string
`gorm:"type:text;not null;comment:telegram chat title"`
}
type
Task
struct
{
type
Task
struct
{
Id
int
`gorm:"primaryKey;autoIncrement:false"`
Id
int
`gorm:"primaryKey;autoIncrement:false"`
GroupId
int
`gorm:"type:int;index;not null;comment:任务组id"`
GroupId
int
`gorm:"type:int;index;not null;comment:任务组id"`
Platform
string
`gorm:"type:text;not null;comment:任务平台"`
Platform
string
`gorm:"type:text;not null;comment:任务平台"`
Action
string
`gorm:"type:text;not null;comment:任务动作"`
Action
string
`gorm:"type:text;not null;comment:任务动作"`
Url
string
`gorm:"type:text;not null;comment:任务链接"`
Url
string
`gorm:"type:text;not null;comment:任务链接"`
TwitterUserId
int
`gorm:"type:int;not null;comment:tweet user id,用于关注"`
TwitterHandle
string
`gorm:"type:text;not null;comment:tweet handle,用于关注"`
TweetId
int
`gorm:"type:int;not null;comment:推文id,用于转发点赞"`
TweetId
int
`gorm:"type:int;not null;comment:推文id,用于转发点赞"`
TelegramChatId
int
`gorm:"type:int;not null;comment:telegram群id"`
TelegramChatUsername
string
`gorm:"type:text;not null;comment:telegram群用户名"`
Description
string
`gorm:"type:text;not null;comment:任务描述"`
Description
string
`gorm:"type:text;not null;comment:任务描述"`
Reward
int
`gorm:"type:int;not null;comment:任务奖励"`
Reward
int
`gorm:"type:int;not null;comment:任务奖励"`
Start
int
`gorm:"type:int;not null;comment:任务开始时间"`
Start
int
`gorm:"type:int;not null;comment:任务开始时间"`
...
@@ -73,6 +82,7 @@ type Task struct {
...
@@ -73,6 +82,7 @@ type Task struct {
Daily
bool
`gorm:"type:bool;not null;comment:是否是每日任务"`
Daily
bool
`gorm:"type:bool;not null;comment:是否是每日任务"`
TwitterTaskEndAt
sql
.
NullTime
`gorm:"index;comment:推特可用,推特任务中心已停止"`
TwitterTaskEndAt
sql
.
NullTime
`gorm:"index;comment:推特可用,推特任务中心已停止"`
TelegramActiveThreshold
int
`gorm:"type:int;not null;comment:telegram群活跃阈值"`
TelegramActiveThreshold
int
`gorm:"type:int;not null;comment:telegram群活跃阈值"`
Config
gorm
.
Model
gorm
.
Model
}
}
...
...
server/group.go
View file @
99adfe02
...
@@ -16,48 +16,6 @@ func createGroup(c *gin.Context) {
...
@@ -16,48 +16,6 @@ func createGroup(c *gin.Context) {
}
}
req
.
UserId
=
c
.
GetString
(
"userId"
)
req
.
UserId
=
c
.
GetString
(
"userId"
)
if
len
(
req
.
Tasks
)
==
0
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
project
,
err
:=
srv
.
GetProject
(
req
.
ProjectId
)
if
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
return
}
if
project
==
nil
{
c
.
JSON
(
200
,
withError
(
"project not found"
))
return
}
if
project
.
UserId
!=
req
.
UserId
{
c
.
JSON
(
200
,
withError
(
"permission denied"
))
return
}
for
_
,
task
:=
range
req
.
Tasks
{
if
!
constant
.
IsValidAction
(
task
.
Action
)
||
!
constant
.
IsValidPlatform
(
task
.
Platform
)
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
// go wait retry done
// if task.Platform == constant.TaskPlatformTelegram && task.TelegramChatId == 0 {
// c.JSON(200, withError(constant.InvalidParam))
// return
// }
// if task.Platform == constant.TaskPlatformTwitter && task.TweetId == 0 && task.TwitterUserId == 0 {
// c.JSON(200, withError(constant.InvalidParam))
// return
// }
if
task
.
Platform
==
constant
.
TaskPlatformTwitter
&&
task
.
Action
!=
constant
.
TaskActionFollow
&&
task
.
TweetId
==
0
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
}
gid
,
err
:=
srv
.
CreateGroup
(
req
)
gid
,
err
:=
srv
.
CreateGroup
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
...
...
server/project.go
deleted
100644 → 0
View file @
c5675226
package
server
import
(
"strconv"
"taskcenter/constant"
apiModel
"taskcenter/model/api"
"github.com/gin-gonic/gin"
)
func
createProject
(
c
*
gin
.
Context
)
{
req
:=
&
apiModel
.
CreateProjectRequest
{}
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
req
.
UserId
=
c
.
GetString
(
"userId"
)
resp
,
err
:=
srv
.
CreateProject
(
req
)
if
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
return
}
c
.
JSON
(
200
,
withSuccess
(
resp
))
}
func
listProject
(
c
*
gin
.
Context
)
{
_page
:=
c
.
DefaultQuery
(
"page"
,
"1"
)
_pageSize
:=
c
.
DefaultQuery
(
"pageSize"
,
"10"
)
page
,
_
:=
strconv
.
Atoi
(
_page
)
pageSize
,
_
:=
strconv
.
Atoi
(
_pageSize
)
if
page
<
1
||
pageSize
<
1
||
pageSize
>
100
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
resp
,
err
:=
srv
.
GetProjectList
(
page
,
pageSize
)
if
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
return
}
c
.
JSON
(
200
,
withSuccess
(
resp
))
}
func
getProject
(
c
*
gin
.
Context
)
{
_pid
:=
c
.
Param
(
"pid"
)
pid
,
_
:=
strconv
.
Atoi
(
_pid
)
if
pid
==
0
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
resp
,
err
:=
srv
.
GetProject
(
pid
)
if
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
return
}
c
.
JSON
(
200
,
withSuccess
(
resp
))
}
server/router.go
View file @
99adfe02
...
@@ -11,24 +11,18 @@ func initRouter(e *gin.Engine) {
...
@@ -11,24 +11,18 @@ func initRouter(e *gin.Engine) {
e
.
Use
(
middleware
.
PrintRequestResponseBodyMiddleware
())
e
.
Use
(
middleware
.
PrintRequestResponseBodyMiddleware
())
v1
:=
e
.
Group
(
"/api/v1"
)
v1
:=
e
.
Group
(
"/api/v1"
)
{
project
:=
v1
.
Group
(
"/project"
,
middleware
.
JWTMiddleware
(
conf
.
Supabase
.
JWTSecret
,
true
))
project
.
GET
(
"/:pid"
,
getProject
)
// 获取项目详情
project
.
GET
(
"/list"
,
listProject
)
// 获取项目列表
project
.
POST
(
"/create"
,
createProject
)
// 创建项目
}
{
{
group
:=
v1
.
Group
(
"/group"
)
group
:=
v1
.
Group
(
"/group"
)
group
.
GET
(
"/:gid"
,
middleware
.
JWTMiddleware
(
conf
.
Supabase
.
JWTSecret
,
false
),
getGroup
)
// 获取任务组任务详情
group
.
GET
(
"/:gid"
,
middleware
.
JWTMiddleware
(
d
,
conf
.
Supabase
.
JWTSecret
,
false
),
getGroup
)
// 获取任务组任务详情
group
.
GET
(
"/list"
,
middleware
.
JWTMiddleware
(
conf
.
Supabase
.
JWTSecret
,
true
),
listGroup
)
// 获取任务组任务详情
group
.
GET
(
"/list"
,
middleware
.
JWTMiddleware
(
d
,
conf
.
Supabase
.
JWTSecret
,
true
),
listGroup
)
// 获取任务组任务详情
group
.
POST
(
"/create"
,
middleware
.
JWTMiddleware
(
conf
.
Supabase
.
JWTSecret
,
true
),
createGroup
)
// 创建任务组
group
.
POST
(
"/create"
,
middleware
.
JWTMiddleware
(
d
,
conf
.
Supabase
.
JWTSecret
,
true
),
createGroup
)
// 创建任务组
}
}
{
{
task
:=
v1
.
Group
(
"/task"
,
middleware
.
JWTMiddleware
(
conf
.
Supabase
.
JWTSecret
,
false
))
task
:=
v1
.
Group
(
"/task"
)
task
.
POST
(
"/submit/:tid"
,
submitTask
)
// 提交任务
task
.
POST
(
"/create"
,
createTask
,
middleware
.
JWTMiddleware
(
d
,
conf
.
Supabase
.
JWTSecret
,
true
))
// 创建任务
task
.
GET
(
"/check/:tid"
,
checkTask
)
// 检查任务是否完成
task
.
POST
(
"/submit/:tid"
,
submitTask
,
middleware
.
JWTMiddleware
(
d
,
conf
.
Supabase
.
JWTSecret
,
false
))
// 提交任务
task
.
GET
(
"/check/:tid"
,
checkTask
,
middleware
.
JWTMiddleware
(
d
,
conf
.
Supabase
.
JWTSecret
,
false
))
// 检查任务是否完成
}
}
}
}
server/task.go
View file @
99adfe02
...
@@ -3,10 +3,37 @@ package server
...
@@ -3,10 +3,37 @@ package server
import
(
import
(
"strconv"
"strconv"
"taskcenter/constant"
"taskcenter/constant"
apiModel
"taskcenter/model/api"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
)
func
createTask
(
c
*
gin
.
Context
)
{
req
:=
&
apiModel
.
CreateTaskRequest
{}
if
err
:=
c
.
ShouldBindJSON
(
req
);
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
req
.
UserId
=
c
.
GetString
(
"userId"
)
ok
,
err
:=
srv
.
CheckGroup
(
req
.
GroupId
,
req
.
UserId
)
if
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
return
}
if
!
ok
{
c
.
JSON
(
200
,
withError
(
constant
.
InvalidParam
))
return
}
taskId
,
err
:=
srv
.
CreateTask
(
req
)
if
err
!=
nil
{
c
.
JSON
(
200
,
withError
(
constant
.
InternalError
))
return
}
c
.
JSON
(
200
,
withSuccess
(
gin
.
H
{
"taskId"
:
taskId
}))
}
func
checkTask
(
c
*
gin
.
Context
)
{
func
checkTask
(
c
*
gin
.
Context
)
{
_taskId
:=
c
.
Param
(
"tid"
)
_taskId
:=
c
.
Param
(
"tid"
)
taskId
,
_
:=
strconv
.
Atoi
(
_taskId
)
taskId
,
_
:=
strconv
.
Atoi
(
_taskId
)
...
...
service/group.go
View file @
99adfe02
package
service
package
service
import
(
import
(
"fmt"
"taskcenter/constant"
apiModel
"taskcenter/model/api"
apiModel
"taskcenter/model/api"
dbModel
"taskcenter/model/db"
dbModel
"taskcenter/model/db"
"taskcenter/util"
"taskcenter/util"
...
@@ -14,7 +12,6 @@ func (s *Service) CreateGroup(req *apiModel.CreateGroupRequest) (gid int, err er
...
@@ -14,7 +12,6 @@ func (s *Service) CreateGroup(req *apiModel.CreateGroupRequest) (gid int, err er
g
:=
&
dbModel
.
TaskGroup
{
g
:=
&
dbModel
.
TaskGroup
{
Id
:
util
.
GenFlakeID
(),
Id
:
util
.
GenFlakeID
(),
UserId
:
req
.
UserId
,
UserId
:
req
.
UserId
,
ProjectId
:
req
.
ProjectId
,
Description
:
req
.
Description
,
Description
:
req
.
Description
,
}
}
err
=
s
.
d
.
CreateGroup
(
g
)
err
=
s
.
d
.
CreateGroup
(
g
)
...
@@ -22,71 +19,23 @@ func (s *Service) CreateGroup(req *apiModel.CreateGroupRequest) (gid int, err er
...
@@ -22,71 +19,23 @@ func (s *Service) CreateGroup(req *apiModel.CreateGroupRequest) (gid int, err er
log
.
WithError
(
err
)
.
Error
(
"create group error"
)
log
.
WithError
(
err
)
.
Error
(
"create group error"
)
return
return
}
}
return
g
.
Id
,
nil
}
project
,
err
:=
s
.
d
.
GetProject
(
req
.
ProjectId
)
func
(
s
*
Service
)
CheckGroup
(
gid
int
,
userId
string
)
(
exist
bool
,
err
error
)
{
g
,
err
:=
s
.
d
.
GetGroup
(
gid
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"get
project
error"
)
log
.
WithError
(
err
)
.
Error
(
"get
group
error"
)
return
0
,
err
return
}
}
for
_
,
task
:=
range
req
.
Tasks
{
if
g
==
nil
{
gt
:=
&
dbModel
.
Task
{
return
false
,
nil
Id
:
util
.
GenFlakeID
(),
GroupId
:
g
.
Id
,
Platform
:
task
.
Platform
,
Action
:
task
.
Action
,
Description
:
task
.
Description
,
Reward
:
task
.
Reward
,
Start
:
task
.
Start
,
End
:
task
.
End
,
Daily
:
task
.
Daily
,
}
if
task
.
Platform
==
constant
.
TaskPlatformTwitter
{
gt
.
TwitterHandle
=
project
.
TwitterHandle
gt
.
TwitterUserId
=
project
.
TwitterUserId
var
twitterAPIAction
string
switch
task
.
Action
{
case
constant
.
TaskActionFollow
:
twitterAPIAction
=
constant
.
TwitterAPIActionFollow
gt
.
Url
=
fmt
.
Sprintf
(
"https://x.com/intent/follow?screen_name=%s"
,
project
.
TwitterHandle
)
case
constant
.
TaskActionLike
:
twitterAPIAction
=
constant
.
TwitterAPIActionLike
gt
.
TweetId
=
task
.
TweetId
gt
.
Url
=
fmt
.
Sprintf
(
"https://x.com/intent/like?tweet_id=%d"
,
task
.
TweetId
)
case
constant
.
TaskActionRetweet
:
twitterAPIAction
=
constant
.
TwitterAPIActionRetweet
gt
.
TweetId
=
task
.
TweetId
gt
.
Url
=
fmt
.
Sprintf
(
"https://x.com/intent/retweet?tweet_id=%d"
,
task
.
TweetId
)
}
var
twitterHandle
string
// 关注时传handle
if
task
.
Action
==
constant
.
TaskActionFollow
{
twitterHandle
=
project
.
TwitterHandle
}
// 推特任务中心,启动任务
err
=
s
.
d
.
DoTweetTask
(
gt
.
TwitterUserId
,
gt
.
TweetId
,
twitterHandle
,
twitterAPIAction
,
true
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"do tweet task error"
)
return
0
,
err
}
}
if
task
.
Platform
==
constant
.
TaskPlatformTelegram
{
gt
.
TelegramChatId
=
project
.
TelegramChatId
gt
.
TelegramChatUsername
=
project
.
TelegramChatUsername
gt
.
Url
=
fmt
.
Sprintf
(
"https://t.me/%s"
,
project
.
TelegramChatUsername
)
if
task
.
Action
==
constant
.
TaskActionActive
{
gt
.
TelegramActiveThreshold
=
task
.
TelegramActiveThreshold
}
}
err
=
s
.
d
.
CreateGroupTask
(
gt
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"create group task error"
)
return
0
,
err
}
}
}
return
g
.
Id
,
nil
if
g
.
UserId
!=
userId
{
return
false
,
nil
}
return
true
,
nil
}
}
func
(
s
*
Service
)
GetGroup
(
gid
int
,
userId
string
)
(
resp
*
apiModel
.
GetGroupResponse
,
err
error
)
{
func
(
s
*
Service
)
GetGroup
(
gid
int
,
userId
string
)
(
resp
*
apiModel
.
GetGroupResponse
,
err
error
)
{
...
@@ -101,7 +50,6 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
...
@@ -101,7 +50,6 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
return
nil
,
nil
return
nil
,
nil
}
}
resp
.
ProjectId
=
g
.
ProjectId
resp
.
Description
=
g
.
Description
resp
.
Description
=
g
.
Description
tasks
,
err
:=
s
.
d
.
GetGroupTasks
(
g
.
Id
)
tasks
,
err
:=
s
.
d
.
GetGroupTasks
(
g
.
Id
)
...
@@ -147,7 +95,6 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR
...
@@ -147,7 +95,6 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR
for
_
,
v
:=
range
list
{
for
_
,
v
:=
range
list
{
resp
.
Items
=
append
(
resp
.
Items
,
&
apiModel
.
GetGroupResponse
{
resp
.
Items
=
append
(
resp
.
Items
,
&
apiModel
.
GetGroupResponse
{
GroupId
:
v
.
Id
,
GroupId
:
v
.
Id
,
ProjectId
:
v
.
ProjectId
,
Description
:
v
.
Description
,
Description
:
v
.
Description
,
})
})
...
...
service/project.go
deleted
100644 → 0
View file @
c5675226
package
service
import
(
apiModel
"taskcenter/model/api"
dbModel
"taskcenter/model/db"
"taskcenter/util"
log
"github.com/sirupsen/logrus"
)
func
(
s
*
Service
)
CreateProject
(
req
*
apiModel
.
CreateProjectRequest
)
(
resp
*
apiModel
.
CreateProjectResponse
,
err
error
)
{
resp
=
&
apiModel
.
CreateProjectResponse
{}
twitterUserId
,
twitterHandle
,
twitterName
,
err
:=
s
.
d
.
CreateTwitterProject
(
req
.
TwitterAPIKey
,
req
.
TwitterAPISecret
,
req
.
TwitterAccessToken
,
req
.
TwitterAccessSecret
,
req
.
TwitterToken
,
req
.
ProjectName
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"create twitter project error"
)
return
}
tgTitle
,
tgUsername
,
err
:=
s
.
d
.
GetTGGroupInfo
(
req
.
TelegramChatId
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"get telegram group info error"
)
return
}
p
:=
&
dbModel
.
Project
{
Id
:
util
.
GenFlakeID
(),
UserId
:
req
.
UserId
,
Name
:
req
.
ProjectName
,
Description
:
req
.
Description
,
TwitterAPIKey
:
req
.
TwitterAPIKey
,
TwitterAPISecret
:
req
.
TwitterAPISecret
,
TwitterAccessToken
:
req
.
TwitterAccessToken
,
TwitterAccessSecret
:
req
.
TwitterAccessSecret
,
TwitterToken
:
req
.
TwitterToken
,
TwitterHandle
:
twitterHandle
,
TwitterName
:
twitterName
,
TwitterUserId
:
twitterUserId
,
TelegramChatId
:
req
.
TelegramChatId
,
TelegramChatTitle
:
tgTitle
,
TelegramChatUsername
:
tgUsername
,
}
err
=
s
.
d
.
CreateProject
(
p
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"create project error"
)
return
}
resp
.
TelegramChatUsername
=
tgUsername
resp
.
TelegramChatTitle
=
tgTitle
resp
.
TwitterName
=
twitterName
resp
.
TwitterHandle
=
twitterHandle
resp
.
ProjectId
=
p
.
Id
return
}
func
(
s
*
Service
)
GetProjectList
(
page
,
pageSize
int
)
(
resp
*
apiModel
.
GetProjectListResponse
,
err
error
)
{
resp
=
&
apiModel
.
GetProjectListResponse
{}
list
,
count
,
err
:=
s
.
d
.
GetProjectList
(
page
,
pageSize
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"get project list error"
)
return
}
resp
.
TotalCount
=
count
resp
.
Items
=
make
([]
*
apiModel
.
GetProjectResponse
,
0
)
for
_
,
v
:=
range
list
{
resp
.
Items
=
append
(
resp
.
Items
,
&
apiModel
.
GetProjectResponse
{
ProjectId
:
v
.
Id
,
TelegramChatId
:
v
.
TelegramChatId
,
TwitterHandle
:
v
.
TwitterHandle
,
TwitterName
:
v
.
TwitterName
,
TwitterUserId
:
v
.
TwitterUserId
,
Description
:
v
.
Description
,
TelegramChatTitle
:
v
.
TelegramChatTitle
,
TelegramChatUsername
:
v
.
TelegramChatUsername
,
ProjectName
:
v
.
Name
,
})
}
return
}
func
(
s
*
Service
)
GetProject
(
pid
int
)
(
resp
*
apiModel
.
GetProjectResponse
,
err
error
)
{
p
,
err
:=
s
.
d
.
GetProject
(
pid
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"get project error"
)
return
}
if
p
==
nil
{
log
.
WithError
(
err
)
.
Error
(
"project not found"
)
return
nil
,
nil
}
return
&
apiModel
.
GetProjectResponse
{
TelegramChatId
:
p
.
TelegramChatId
,
TelegramChatUsername
:
p
.
TelegramChatUsername
,
TelegramChatTitle
:
p
.
TelegramChatTitle
,
TwitterHandle
:
p
.
TwitterHandle
,
TwitterName
:
p
.
TwitterName
,
TwitterUserId
:
p
.
TwitterUserId
,
ProjectName
:
p
.
Name
,
Description
:
p
.
Description
,
// TwitterAPIKey: p.TwitterAPIKey,
// TwitterAPISecret: p.TwitterAPISecret,
// TwitterAccessToken: p.TwitterAccessToken,
// TwitterAccessSecret: p.TwitterAccessSecret,
// TwitterToken: p.TwitterToken,
UserId
:
p
.
UserId
,
},
nil
}
service/task.go
View file @
99adfe02
package
service
package
service
import
(
import
(
"fmt"
"taskcenter/constant"
"taskcenter/dao"
apiModel
"taskcenter/model/api"
dbModel
"taskcenter/model/db"
dbModel
"taskcenter/model/db"
"taskcenter/util"
"time"
"time"
log
"github.com/sirupsen/logrus"
log
"github.com/sirupsen/logrus"
)
)
func
(
s
*
Service
)
CreateTask
(
req
*
apiModel
.
CreateTaskRequest
)
(
taskId
int
,
err
error
)
{
task
:=
&
dbModel
.
Task
{
Id
:
util
.
GenFlakeID
(),
GroupId
:
req
.
GroupId
,
Platform
:
req
.
Platform
,
Action
:
req
.
Action
,
Description
:
req
.
Description
,
Reward
:
req
.
Reward
,
Start
:
req
.
Start
,
End
:
req
.
End
,
Daily
:
req
.
Daily
,
}
config
:=
dao
.
TwitterConfig
{
APIKey
:
req
.
TwitterAPIKey
,
APISecret
:
req
.
TwitterAPISecret
,
AccessToken
:
req
.
TwitterAccessToken
,
AccessTokenSecret
:
req
.
TwitterAccessSecret
,
TwitterToken
:
req
.
TwitterToken
,
}
if
req
.
Platform
==
constant
.
TaskPlatformTwitter
{
// 获取推特信息
userId
,
userHandle
,
_
,
err
:=
s
.
d
.
GetTwitterInfo
(
config
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"get twitter info error"
)
return
0
,
err
}
task
.
TwitterHandle
=
userHandle
task
.
TwitterUserId
=
userId
var
twitterAPIAction
string
switch
req
.
Action
{
case
constant
.
TaskActionFollow
:
twitterAPIAction
=
constant
.
TwitterAPIActionFollow
task
.
Url
=
fmt
.
Sprintf
(
"https://x.com/intent/follow?screen_name=%s"
,
userHandle
)
case
constant
.
TaskActionLike
:
twitterAPIAction
=
constant
.
TwitterAPIActionLike
task
.
TweetId
=
req
.
TweetId
task
.
Url
=
fmt
.
Sprintf
(
"https://x.com/intent/like?tweet_id=%d"
,
req
.
TweetId
)
case
constant
.
TaskActionRetweet
:
twitterAPIAction
=
constant
.
TwitterAPIActionRetweet
task
.
TweetId
=
req
.
TweetId
task
.
Url
=
fmt
.
Sprintf
(
"https://x.com/intent/retweet?tweet_id=%d"
,
req
.
TweetId
)
}
var
twitterHandle
string
// 关注时传handle
if
req
.
Action
==
constant
.
TaskActionFollow
{
twitterHandle
=
task
.
TwitterHandle
}
// 推特任务中心,启动任务
err
=
s
.
d
.
DoTweetTask
(
task
.
TwitterUserId
,
task
.
TweetId
,
twitterHandle
,
twitterAPIAction
,
true
,
config
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"do tweet task error"
)
return
0
,
err
}
}
if
req
.
Platform
==
constant
.
TaskPlatformTelegram
{
_
,
username
,
err
:=
s
.
d
.
GetTGGroupInfo
(
req
.
TelegramChatId
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"get telegram group info error"
)
return
0
,
err
}
task
.
TelegramChatId
=
req
.
TelegramChatId
task
.
TelegramChatUsername
=
username
task
.
Url
=
fmt
.
Sprintf
(
"https://t.me/%s"
,
username
)
if
req
.
Action
==
constant
.
TaskActionActive
{
task
.
TelegramActiveThreshold
=
req
.
TelegramActiveThreshold
}
}
err
=
s
.
d
.
CreateTask
(
task
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"create group task error"
)
return
0
,
err
}
return
task
.
Id
,
nil
}
func
(
s
*
Service
)
CheckTask
(
taskId
int
,
userId
string
)
(
exist
,
done
bool
,
expired
bool
,
err
error
)
{
func
(
s
*
Service
)
CheckTask
(
taskId
int
,
userId
string
)
(
exist
,
done
bool
,
expired
bool
,
err
error
)
{
exist
=
true
exist
=
true
task
,
err
:=
s
.
d
.
GetTaskDetail
(
taskId
)
task
,
err
:=
s
.
d
.
GetTaskDetail
(
taskId
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment