From f758bf295ac7ebcaa33ea9b87aee0353b440a4d3 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 28 Oct 2022 16:27:33 +0000 Subject: [PATCH] Better title and notes formatting --- asana.go | 5 +++-- slack.go | 50 ++++++++++++++++++++++++++++++++++++++++++++------ slack2asana.go | 2 +- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/asana.go b/asana.go index f3ef681..ce8e2c7 100644 --- a/asana.go +++ b/asana.go @@ -3,8 +3,8 @@ package main import ( "bytes" "encoding/json" - "errors" "fmt" + "io" "net/http" "os" ) @@ -69,7 +69,8 @@ func (ac *AsanaClient) CreateTask(name string, notes string) error { } if resp.StatusCode != 201 { - return errors.New(resp.Status) + msg, _ := io.ReadAll(resp.Body) + return fmt.Errorf("%s: %s", resp.Status, msg) } return nil diff --git a/slack.go b/slack.go index deb6958..3600923 100644 --- a/slack.go +++ b/slack.go @@ -229,24 +229,56 @@ func (sc *SlackClient) RemoveStar(item *Item) error { func (sc *SlackClient) GetTitle(item *Item, user *User, channel *Channel) (string, error) { switch { case channel.IsIm: - return fmt.Sprintf("[%s] %s", user.Name, item.Message.Text), nil + return fmt.Sprintf("<%s> %s", user.Name, item.Message.Text), nil case channel.IsMpIm: - return fmt.Sprintf("[%s] %s", user.Name, item.Message.Text), nil + return fmt.Sprintf("<%s> %s", user.Name, item.Message.Text), nil case channel.IsChannel: - return fmt.Sprintf("[%s] %s", user.Name, item.Message.Text), nil + return fmt.Sprintf("<%s> %s", user.Name, item.Message.Text), nil default: return "", fmt.Errorf("unknown channel type: %#v", channel) } } +func (sc *SlackClient) GetTrimmedTitle(item *Item, user *User, channel *Channel) (string, error) { + title, err := sc.GetTitle(item, user, channel) + if err != nil { + return "", err + } + + parts := strings.SplitN(title, "\n", 2) + title = parts[0] + + if len(title) < 80 { + return title, nil + } + + return fmt.Sprintf("%s...", title[:77]), nil +} + func (sc *SlackClient) GetNotes(item *Item, user *User, channel *Channel) (string, error) { + title, err := sc.GetTitle(item, user, channel) + if err != nil { + return "", err + } + switch { case channel.IsIm: - return "", nil + return fmt.Sprintf( + "%s", + sc.escape(title), + ), nil case channel.IsMpIm: - return fmt.Sprintf("In %s", sc.getTaggedNamesString(channel.Purpose.Value)), nil + return fmt.Sprintf( + "%s\n\nIn %s", + sc.escape(title), + sc.escape(sc.getTaggedNamesString(channel.Purpose.Value)), + ), nil case channel.IsChannel: - return fmt.Sprintf("In #%s", channel.Name), nil + return fmt.Sprintf( + "%s\n\nIn #%s", + sc.escape(title), + sc.escape(channel.Name), + ), nil default: return "", fmt.Errorf("unknown channel type: %#v", channel) } @@ -262,6 +294,12 @@ func (sc *SlackClient) getTaggedNamesString(in string) string { return fmt.Sprintf("{%s}", strings.Join(sc.getTaggedNames(in), ",")) } +func (sc *SlackClient) escape(in string) string { + in = strings.ReplaceAll(in, "<", "<") + in = strings.ReplaceAll(in, ">", ">") + return in +} + func (sc *SlackClient) addAuth(req *http.Request) { req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", sc.token)) } diff --git a/slack2asana.go b/slack2asana.go index c249b5d..4207d34 100644 --- a/slack2asana.go +++ b/slack2asana.go @@ -48,7 +48,7 @@ func Poll(ac *AsanaClient, sc *SlackClient) error { return err } - title, err := sc.GetTitle(item, user, channel) + title, err := sc.GetTrimmedTitle(item, user, channel) if err != nil { return err }