Better title and notes formatting
This commit is contained in:
5
asana.go
5
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
|
||||
|
||||
50
slack.go
50
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(
|
||||
"<body>%s</body>",
|
||||
sc.escape(title),
|
||||
), nil
|
||||
case channel.IsMpIm:
|
||||
return fmt.Sprintf("<body>In %s</body>", sc.getTaggedNamesString(channel.Purpose.Value)), nil
|
||||
return fmt.Sprintf(
|
||||
"<body>%s\n\nIn %s</body>",
|
||||
sc.escape(title),
|
||||
sc.escape(sc.getTaggedNamesString(channel.Purpose.Value)),
|
||||
), nil
|
||||
case channel.IsChannel:
|
||||
return fmt.Sprintf("<body>In #%s</body>", channel.Name), nil
|
||||
return fmt.Sprintf(
|
||||
"<body>%s\n\nIn #%s</body>",
|
||||
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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user