diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-17 10:38:40 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-17 10:38:40 +0300 |
| commit | 563ea5dbaaa77e59939812d6825d54cf829db8eb (patch) | |
| tree | 1518d99868e86357f4ae81c0946f209adad4d8c1 /internal | |
| parent | a66b2114ee9fb9078d6f3c12566d7178e0662903 (diff) | |
implement sizeLimit per platform
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/config/args.go | 4 | ||||
| -rw-r--r-- | internal/entry/entry.go | 12 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 14 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 4 | ||||
| -rw-r--r-- | internal/queue/queue.go | 2 | ||||
| -rw-r--r-- | internal/queue/sharetags.go | 4 | ||||
| -rw-r--r-- | internal/queue/sharetags_test.go | 8 | ||||
| -rw-r--r-- | internal/run.go | 10 |
8 files changed, 36 insertions, 22 deletions
diff --git a/internal/config/args.go b/internal/config/args.go index 3bfa78e..47712f9 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -12,7 +12,7 @@ var validPlatforms = []string{"mastodon", "linkedin"} type Args struct { GosDir string DryRun bool - Platforms []string + Platforms map[string]int // Platform name and post size limits Target int Lookback time.Duration SecretsConfigPath string @@ -21,7 +21,7 @@ type Args struct { } func (a Args) Validate() error { - for _, platform := range a.Platforms { + for platform := range a.Platforms { if !slices.Contains(validPlatforms, strings.ToLower(platform)) { return fmt.Errorf("Platform %s not supported", platform) } diff --git a/internal/entry/entry.go b/internal/entry/entry.go index 5a9c0fb..e1d9696 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -80,6 +80,18 @@ func (e Entry) Content() (string, error) { return string(bytes), err } +func (e Entry) ContentWithLimit(sizeLimit int) (string, error) { + content, err := e.Content() + if err != nil { + return "", err + } + if len(content) > sizeLimit { + return "", fmt.Errorf("entry content exceeds size limit: %d > %d: %v", + len(content), sizeLimit, e) + } + return content, nil +} + func (e *Entry) MarkPosted() error { if e.State != Queued { return errors.New("entry is not queued") diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 401d5a3..5f68edf 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -19,18 +19,18 @@ import ( var errUnauthorized = errors.New("unauthorized access, refresh or create token?") // TODO: Also implemebt a Text Platform output, which then laster can be -// processed by Gemtexter as a page -func Post(ctx context.Context, args config.Args, ent entry.Entry) error { - err := post(ctx, args, ent) +// processed by Gemtexter as a page. Or not? +func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { + err := post(ctx, args, sizeLimit, ent) if errors.Is(err, errUnauthorized) { log.Println(err, "=> trying to refresh LinkedIn access token") args.Secrets.LinkedInAccessToken = "" // Reset the token - return post(ctx, args, ent) + return post(ctx, args, sizeLimit, ent) } return err } -func post(ctx context.Context, args config.Args, ent entry.Entry) error { +func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { if args.DryRun { log.Println("Not posting", ent, "to LinkedIn as dry-run enabled") return nil @@ -39,9 +39,9 @@ func post(ctx context.Context, args config.Args, ent entry.Entry) error { if err != err { return err } - content, err := ent.Content() + content, err := ent.ContentWithLimit(sizeLimit) if err != nil { - return nil + return err } return callLinkedInAPI(personID, accessToken, content) } diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index 7cdb513..ce9a765 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -13,8 +13,8 @@ import ( "codeberg.org/snonux/gos/internal/prompt" ) -func Post(ctx context.Context, args config.Args, ent entry.Entry) error { - content, err := ent.Content() +func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { + content, err := ent.ContentWithLimit(sizeLimit) if err != nil { return err } diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 73d5789..ddd3136 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -65,7 +65,7 @@ func queuePlatforms(args config.Args) error { } for filePath := range ch { - for _, platform := range args.Platforms { + for platform := range args.Platforms { if newShareTags(args, filePath).IsExcluded(platform) { log.Println("Not queueing entry", filePath, "to platform", platform, "as it is excluded") continue diff --git a/internal/queue/sharetags.go b/internal/queue/sharetags.go index 53d6306..b8088c0 100644 --- a/internal/queue/sharetags.go +++ b/internal/queue/sharetags.go @@ -32,7 +32,9 @@ func newShareTags(args config.Args, filePath string) shareTags { if len(s.includes) == 0 && len(s.excludes) == 0 { // If nothing found, include all of them - s.includes = args.Platforms + for platform := range args.Platforms { + s.includes = append(s.includes, platform) + } } return s diff --git a/internal/queue/sharetags_test.go b/internal/queue/sharetags_test.go index b2dbeec..2a2a6aa 100644 --- a/internal/queue/sharetags_test.go +++ b/internal/queue/sharetags_test.go @@ -10,10 +10,10 @@ import ( func TestShareTagsPositive(t *testing.T) { t.Parallel() - args := config.Args{Platforms: []string{"mastodon", "linkedin"}} + args := config.Args{Platforms: map[string]int{"mastodon": 100, "linkedin": 100}} testTable := map[string]shareTags{ "./foo/bar.without.tags.txt": { - includes: args.Platforms, // No tags: default platforms + includes: []string{"mastodon", "linkedin"}, }, "./foo/bar.share:linkedin.txt": { includes: []string{"linkedin"}, @@ -46,7 +46,7 @@ func TestShareTagsPositive(t *testing.T) { func TestShareTagsNegative(t *testing.T) { t.Parallel() - args := config.Args{Platforms: []string{"mastodon", "linkedin"}} + args := config.Args{Platforms: map[string]int{"mastodon": 100, "linkedin": 100}} testTable := map[string]shareTags{ "./foo/bar.without.tags.txt": { includes: []string{"linkedin"}, @@ -101,7 +101,7 @@ func TestShareTagsIsIncluded(t *testing.T) { } } } - args := config.Args{Platforms: []string{"mastodon", "linkedin"}} + args := config.Args{Platforms: map[string]int{"mastodon": 100, "linkedin": 100}} filePath := "foo/bar/baz.txt" t.Run(filePath, func(t *testing.T) { diff --git a/internal/run.go b/internal/run.go index acf0525..ed70511 100644 --- a/internal/run.go +++ b/internal/run.go @@ -20,8 +20,8 @@ func Run(ctx context.Context, args config.Args) error { return err } - for _, platform := range args.Platforms { - if err := runPlatform(ctx, args, platform); err != nil { + for platform, sizeLimit := range args.Platforms { + if err := runPlatform(ctx, args, platform, sizeLimit); err != nil { if errors.Is(err, prompt.ErrAborted) { log.Println("Aborted posting to", platform) continue @@ -33,7 +33,7 @@ func Run(ctx context.Context, args config.Args) error { return nil } -func runPlatform(ctx context.Context, args config.Args, platform string) error { +func runPlatform(ctx context.Context, args config.Args, platform string, sizeLimit int) error { ent, err := schedule.Run(args, platform) switch { case errors.Is(err, schedule.ErrNothingToSchedule): @@ -47,7 +47,7 @@ func runPlatform(ctx context.Context, args config.Args, platform string) error { } log.Println("Scheduling", ent) - var postCB func(context.Context, config.Args, entry.Entry) error + var postCB func(context.Context, config.Args, int, entry.Entry) error switch strings.ToLower(platform) { case "mastodon": postCB = mastodon.Post @@ -57,7 +57,7 @@ func runPlatform(ctx context.Context, args config.Args, platform string) error { log.Fatal("Platform", platform, "(not yet) implemented") } - if err := postCB(ctx, args, ent); err != nil { + if err := postCB(ctx, args, sizeLimit, ent); err != nil { return err } |
