Rename ticktickutil, add interface for di
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package ticktick
|
||||
package ticktickutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -15,59 +15,71 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
authState string
|
||||
)
|
||||
|
||||
const (
|
||||
DateTimeLayout = "2006-01-02T15:04:05-0700"
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color,omitempty"`
|
||||
SortOrder int64 `json:"sortOrder,omitempty"`
|
||||
Closed bool `json:"closed,omitempty"`
|
||||
GroupId string `json:"groupId,omitempty"`
|
||||
ViewMode string `json:"viewMode,omitempty"`
|
||||
Permission string `json:"permission,omitempty"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
}
|
||||
type (
|
||||
TicktickUtil interface {
|
||||
HandleAuthCode(w http.ResponseWriter, r *http.Request)
|
||||
GetTasks(projectId string) []Task
|
||||
HasDuplicateTask(projectId string, taskTitile string) bool
|
||||
CreateTask(task Task) error
|
||||
}
|
||||
|
||||
type Column struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ProjectId string `json:"projectId"`
|
||||
SortOrder int64 `json:"sortOrder,omitempty"`
|
||||
}
|
||||
TicktickUtilImpl struct {
|
||||
authState string
|
||||
}
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
Id string `json:"id"`
|
||||
ProjectId string `json:"projectId"`
|
||||
Title string `json:"title"`
|
||||
IsAllDay bool `json:"isAllDay,omitempty"`
|
||||
CompletedTime string `json:"completedTime,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Desc string `json:"desc,omitempty"`
|
||||
DueDate string `json:"dueDate,omitempty"`
|
||||
Items []interface{} `json:"items,omitempty"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
Reminders []string `json:"reminders,omitempty"`
|
||||
RepeatFlag string `json:"repeatFlag,omitempty"`
|
||||
SortOrder int64 `json:"sortOrder,omitempty"`
|
||||
StartDate string `json:"startDate,omitempty"`
|
||||
Status int32 `json:"status,omitempty"`
|
||||
TimeZone string `json:"timeZone,omitempty"`
|
||||
}
|
||||
type (
|
||||
Project struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color,omitempty"`
|
||||
SortOrder int64 `json:"sortOrder,omitempty"`
|
||||
Closed bool `json:"closed,omitempty"`
|
||||
GroupId string `json:"groupId,omitempty"`
|
||||
ViewMode string `json:"viewMode,omitempty"`
|
||||
Permission string `json:"permission,omitempty"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectData struct {
|
||||
Project Project `json:"project"`
|
||||
Tasks []Task `json:"tasks"`
|
||||
Columns []Column `json:"columns,omitempty"`
|
||||
}
|
||||
Column struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ProjectId string `json:"projectId"`
|
||||
SortOrder int64 `json:"sortOrder,omitempty"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
Task struct {
|
||||
Id string `json:"id"`
|
||||
ProjectId string `json:"projectId"`
|
||||
Title string `json:"title"`
|
||||
IsAllDay bool `json:"isAllDay,omitempty"`
|
||||
CompletedTime string `json:"completedTime,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Desc string `json:"desc,omitempty"`
|
||||
DueDate string `json:"dueDate,omitempty"`
|
||||
Items []interface{} `json:"items,omitempty"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
Reminders []string `json:"reminders,omitempty"`
|
||||
RepeatFlag string `json:"repeatFlag,omitempty"`
|
||||
SortOrder int64 `json:"sortOrder,omitempty"`
|
||||
StartDate string `json:"startDate,omitempty"`
|
||||
Status int32 `json:"status,omitempty"`
|
||||
TimeZone string `json:"timeZone,omitempty"`
|
||||
}
|
||||
|
||||
ProjectData struct {
|
||||
Project Project `json:"project"`
|
||||
Tasks []Task `json:"tasks"`
|
||||
Columns []Column `json:"columns,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
func Init() *TicktickUtilImpl {
|
||||
ticktickUtilImpl := &TicktickUtilImpl{}
|
||||
if !viper.InConfig("ticktick.clientId") {
|
||||
slog.Error("TickTick clientId not found in config file, exiting..")
|
||||
os.Exit(1)
|
||||
@@ -83,14 +95,15 @@ func Init() {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
beginAuth()
|
||||
ticktickUtilImpl.beginAuth()
|
||||
}
|
||||
return ticktickUtilImpl
|
||||
}
|
||||
|
||||
func HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||
func (t *TicktickUtilImpl) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||
state := r.URL.Query().Get("state")
|
||||
code := r.URL.Query().Get("code")
|
||||
if state != authState {
|
||||
if state != t.authState {
|
||||
slog.Warn(fmt.Sprintln("HandleAuthCode Invalid state", state))
|
||||
http.Error(w, "Invalid state", http.StatusBadRequest)
|
||||
return
|
||||
@@ -148,7 +161,7 @@ func HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Authorization successful"))
|
||||
}
|
||||
|
||||
func GetTasks(projectId string) []Task {
|
||||
func (t *TicktickUtilImpl) GetTasks(projectId string) []Task {
|
||||
getTaskUrl := fmt.Sprintf("https://api.ticktick.com/open/v1/project/%s/data", projectId)
|
||||
token := viper.GetString("ticktick.token")
|
||||
req, err := http.NewRequest("GET", getTaskUrl, nil)
|
||||
@@ -184,8 +197,8 @@ func GetTasks(projectId string) []Task {
|
||||
return projectData.Tasks
|
||||
}
|
||||
|
||||
func HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||
tasks := GetTasks(projectId)
|
||||
func (t *TicktickUtilImpl) HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||
tasks := t.GetTasks(projectId)
|
||||
for _, task := range tasks {
|
||||
if task.Title == taskTitile {
|
||||
return true
|
||||
@@ -194,8 +207,8 @@ func HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func CreateTask(task Task) error {
|
||||
if HasDuplicateTask(task.ProjectId, task.Title) {
|
||||
func (t *TicktickUtilImpl) CreateTask(task Task) error {
|
||||
if t.HasDuplicateTask(task.ProjectId, task.Title) {
|
||||
return nil
|
||||
}
|
||||
token := viper.GetString("ticktick.token")
|
||||
@@ -259,7 +272,7 @@ func getProjects() ([]Project, error) {
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func beginAuth() {
|
||||
func (t *TicktickUtilImpl) beginAuth() {
|
||||
if !viper.InConfig("ticktick.redirectUri") {
|
||||
slog.Error("TickTick redirectUri not found in config file, exiting..")
|
||||
os.Exit(1)
|
||||
@@ -272,12 +285,12 @@ func beginAuth() {
|
||||
slog.Error(fmt.Sprintln("Error generating auth state", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
authState = hex.EncodeToString(authStateBytes)
|
||||
t.authState = hex.EncodeToString(authStateBytes)
|
||||
params := url.Values{}
|
||||
params.Add("client_id", viper.GetString("ticktick.clientId"))
|
||||
params.Add("response_type", "code")
|
||||
params.Add("redirect_uri", viper.GetString("ticktick.redirectUri"))
|
||||
params.Add("state", authState)
|
||||
params.Add("state", t.authState)
|
||||
params.Add("scope", "tasks:read tasks:write")
|
||||
authUrl.RawQuery = params.Encode()
|
||||
slog.Info(fmt.Sprintln("Please visit the following URL to authorize TickTick:", authUrl.String()))
|
||||
Reference in New Issue
Block a user