diff --git a/module/story/stories.go b/module/story/stories.go index 92f4d21..c36f054 100644 --- a/module/story/stories.go +++ b/module/story/stories.go @@ -8,6 +8,7 @@ import ( "git.drinkme.beer/yinghe/log" + "git.drinkme.beer/yinghe/asana/module/users" "git.drinkme.beer/yinghe/asana/util" ) @@ -55,14 +56,21 @@ func (r *Request) SetPAToken(token string) { } type Response struct { - ID string `json:"gid"` - ResourceType string `json:"resource_type"` - Type string `json:"type"` - Text string `json:"text"` + ID string `json:"gid"` + ResourceType string `json:"resource_type"` + ResourceSubtype string `json:"resource_subtype"` + Type string `json:"type"` + Text string `json:"text"` // IsPinned Conditional // Whether the story should be pinned on the resource. - IsPinned bool `json:"is_pinned"` - StickerName string `json:"sticker_name,omitempty"` + IsPinned bool `json:"is_pinned"` + StickerName string `json:"sticker_name,omitempty"` + CreatedAt string `json:"created_at"` + CreatedBy users.User `json:"created_by"` +} + +func (r *Response) IsComplete() bool { + return "marked_complete" == r.ResourceSubtype } func (r Request) validate() error { @@ -125,3 +133,31 @@ func (r Request) Create() (resp *Response, err error) { log.Infof("[%s]new comment ID:%s", r.taskID, resp.ID) return } + +func (r Request) Get() ([]Response, error) { + var ( + resp struct { + Data []Response `json:"data"` + } + ) + headers := make(map[string]string) + headers["Authorization"] = r.paToken + headers["Content-Type"] = util.ContentType + + client := util.NewHttpClient(util.AsanaHost, fmt.Sprintf(URI, r.taskID), util.HttpGetMethod, nil) + client.Headers = headers + + err := client.Request() + if nil != err || http.StatusOK != client.HTTPStatus { + client.Print() + log.Errorf("failed") + } + + err = json.Unmarshal(client.Body, &resp) + if nil != err { + log.Errorf("invalid response") + return nil, fmt.Errorf("invalid response") + } + + return resp.Data, nil +} diff --git a/module/task/task.go b/module/task/task.go index 8813e03..41867b9 100644 --- a/module/task/task.go +++ b/module/task/task.go @@ -67,7 +67,7 @@ func (r Request) Update(taskID string) error { return err } if http.StatusOK != c.HTTPStatus { - log.Infof("response: %s", string(c.Body)) + c.Print() log.Errorf("unexpected response") err = errors.New("unexpected response") return err @@ -86,7 +86,7 @@ func (r Request) Create() (resp *Response, err error) { log.Infof("response status: %d", client.HTTPStatus) if http.StatusCreated != client.HTTPStatus { - log.Infof("response: %s", string(client.Body)) + client.Print() log.Errorf("unexpected response") err = errors.New("unexpected response") return @@ -94,7 +94,7 @@ func (r Request) Create() (resp *Response, err error) { err = json.Unmarshal(client.Body, &respData) if nil != err { - log.Infof("response: %s", string(client.Body)) + client.Print() log.Errorf("illegal task result: %s", err.Error()) return } @@ -105,64 +105,11 @@ func (r Request) Create() (resp *Response, err error) { return } -//func (r Request) Create() (resp *Response, err error) { -// var ( -// reqData Data -// respData struct { -// Response Response `json:"data"` -// } -// ) -// -// resp = new(Response) -// -// headers := make(map[string]string) -// headers["Authorization"] = r.paToken -// headers["Content-Type"] = util.ContentType -// -// if "" == r.ResourceSubtype { -// r.ResourceSubtype = ResourceSubtype -// } -// -// reqData.Request = r -// -// buf, err := json.Marshal(&reqData) -// if nil != err { -// log.Errorf("failed to generate task request: %s", err.Error()) -// return -// } -// -// log.Infof("request: %s", string(buf)) -// -// client := util.NewHttpClient(util.AsanaHost, URI, util.HttpPostMethod, buf) -// client.Headers = headers -// -// err = client.Request() -// if nil != err { -// log.Errorf("failed to create new Asana task: %s", err.Error()) -// return -// } -// log.Infof("response status: %d", client.HTTPStatus) -// if http.StatusCreated != client.HTTPStatus { -// log.Errorf("unexpected response") -// err = errors.New("unexpected response") -// return -// } -// -// err = json.Unmarshal(client.Body, &respData) -// if nil != err { -// log.Errorf("illegal task result: %s", err.Error()) -// return -// } -// resp = &respData.Response -// log.Infof("[%s]new task ID:%s", r.TicketID, resp.ID) -// return -//} - func (r Request) call(uri, httpMethod string) (*util.Client, error) { - var ( err error reqData Data + client = new(util.Client) ) headers := make(map[string]string) @@ -178,18 +125,18 @@ func (r Request) call(uri, httpMethod string) (*util.Client, error) { buf, err := json.Marshal(&reqData) if nil != err { log.Errorf("failed to generate task request: %s", err.Error()) - return nil, err + return client, err } log.Infof("request: %s", string(buf)) - client := util.NewHttpClient(util.AsanaHost, uri, httpMethod, buf) + client = util.NewHttpClient(util.AsanaHost, uri, httpMethod, buf) client.Headers = headers err = client.Request() if nil != err { log.Errorf("failed to create new Asana task: %s", err.Error()) - return nil, err + return client, err } return client, nil diff --git a/text/rich_text.go b/text/rich_text.go index 5cc7aaf..e733ac2 100644 --- a/text/rich_text.go +++ b/text/rich_text.go @@ -1,6 +1,11 @@ package text -import "fmt" +import ( + "fmt" + "html" + "regexp" + "strings" +) const ( DataMention = `` @@ -14,3 +19,28 @@ const ( func Mention(gid string) string { return fmt.Sprintf(DataMention, gid) } + +func DeleteHtmlTags(src string) string { + var ( + line int + v, des string + ) + if "" == src || 0 == len(src) { + return "" + } + reg := regexp.MustCompile(`<.+?>`) + str := reg.ReplaceAllString(src, " ") + s := strings.Split(str, "\n") + for _, v = range s { + if 0 == len(strings.TrimSpace(v)) { + continue + } + des = des + fmt.Sprintf("\n%s", v) + line++ + } + if 0 == line { + return "" + } + + return html.UnescapeString(des) +} diff --git a/util/http.go b/util/http.go index 76f838f..869281e 100644 --- a/util/http.go +++ b/util/http.go @@ -1,7 +1,6 @@ package util import ( - "bufio" "bytes" "context" "errors" @@ -80,13 +79,21 @@ type Client struct { HTTPStatus int Body []byte // Indicates both Request Body & Response Body TraceId interface{} - Buffer *bufio.Reader + //Buffer *bufio.Reader } func (c *Client) AddHeader(k, v string) { c.Headers[k] = v } +func (c *Client) Print() { + if nil != c.Body { + log.Infof(string(c.Body)) + } else { + log.Infof("nil") + } +} + type Bytes []byte func (self Bytes) BuildRequest() []byte {