From 8b4cd3d9d2af89efdb9b0225742d70edc3483bf4 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 24 Oct 2024 10:36:17 +0300 Subject: refactor --- internal/entry/entry.go | 25 ++++++++++--------------- internal/platforms/linkedin/linkedin.go | 10 ++++++---- internal/platforms/mastodon/mastodon.go | 2 +- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/internal/entry/entry.go b/internal/entry/entry.go index 34ce5f0..a103e69 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -77,30 +77,31 @@ func New(filePath string) (Entry, error) { return e, nil } -func (e Entry) Content() (string, error) { +func (e *Entry) Content() (string, []string, error) { bytes, err := os.ReadFile(e.Path) if err != nil { - return "", err + return "", []string{}, err } - return strings.TrimSpace(string(bytes)), nil + content := strings.TrimSpace(string(bytes)) + return content, extractURLs(content), nil } -func (e Entry) ContentWithLimit(sizeLimit int) (string, error) { - content, err := e.Content() +func (e Entry) ContentWithLimit(sizeLimit int) (string, []string, error) { + content, urls, err := e.Content() if err != nil { - return "", err + return "", urls, err } if len(content) > sizeLimit { err := fmt.Errorf("entry content exceeds size limit: %d > %d: %v", len(content), sizeLimit, e) if err2 := prompt.Acknowledge("You need to shorten the content as "+err.Error(), content); err2 != nil { - return "", errors.Join(err, err2) + return "", urls, errors.Join(err, err2) } if err2 := e.Edit(); err2 != nil { - return "", errors.Join(err, err2) + return "", urls, errors.Join(err, err2) } return e.ContentWithLimit(sizeLimit) } - return content, nil + return content, urls, nil } func (e *Entry) MarkPosted() error { @@ -128,12 +129,6 @@ func (e Entry) Edit() error { return nil } -// extractURLs finds all occurrences of URLs starting with "http://" or "https://" in a given string. -func (e Entry) ExtractURLs() []string { - content, _ := e.Content() - return extractURLs(content) -} - func extractURLs(input string) []string { urlPattern := `(http://|https://|ftp://)[^\s]+` re := regexp.MustCompile(urlPattern) diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 5f222fa..b6bc3c6 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -38,11 +38,13 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) if err != nil { return err } - content, err := ent.ContentWithLimit(sizeLimit) + content, urls, err := ent.ContentWithLimit(sizeLimit) if err != nil { return err } - if err := prompt.DoYouWantThis("Do you want to post this message to Linkedin?", content); err != nil { + + question := fmt.Sprintf("Do you want to post this message to Linkedin (URLs: %v)?", urls) + if err := prompt.DoYouWantThis(question, content); err != nil { if errors.Is(err, prompt.ErrEditContent) { if err := ent.Edit(); err != nil { return err @@ -51,10 +53,10 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) } return err } - return callLinkedInAPI(ctx, personID, accessToken, content) + return callLinkedInAPI(ctx, personID, accessToken, content, urls) } -func callLinkedInAPI(ctx context.Context, personID, accessToken, content string) error { +func callLinkedInAPI(ctx context.Context, personID, accessToken, content string, urls []string) error { const url = "https://api.linkedin.com/v2/posts" post := map[string]interface{}{ diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index 3da32f4..42700e5 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -16,7 +16,7 @@ import ( ) func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { - content, err := ent.ContentWithLimit(sizeLimit) + content, _, err := ent.ContentWithLimit(sizeLimit) if err != nil { return err } -- cgit v1.2.3