From f2c9bbacbc1d066f7f86275e9c911e6e64cfe42e Mon Sep 17 00:00:00 2001 From: Daniele Tricoli Date: Wed, 2 Mar 2022 01:34:11 +0100 Subject: [PATCH] Make toot visibility configurable --- cmd/run.go | 31 ++++++++++++++++++++++++------- cmd/run_test.go | 18 ++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 946edec..8a17bca 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -4,7 +4,9 @@ import ( "context" "log" "os" + "sort" "strconv" + "strings" "github.com/cking/go-mastodon" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" @@ -12,10 +14,11 @@ import ( ) const ( - DEBUG = "DEBUG" - TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN" - MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS" - MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN" + DEBUG = "DEBUG" + TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN" + MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS" + MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN" + MASTODON_TOOT_VISIBILITY = "MASTODON_TOOT_VISIBILITY" ) // runCmd represents the run command @@ -52,9 +55,8 @@ the specified Mastodon account.`, log.Println(update.Message) status, err := c.PostStatus(context.Background(), &mastodon.Toot{ - Status: update.Message.Text, - // TODO: make users able to set visibility - Visibility: "unlisted", + Status: update.Message.Text, + Visibility: parseMastodonVisibility(os.Getenv(MASTODON_TOOT_VISIBILITY)), }) if err != nil { @@ -79,3 +81,18 @@ func parseBoolOrFalse(s string) bool { return r } + +// Check the specified Mastodon visibility and return it if valid or return +// unlisted if it's not valid. +// The specified string will be cheched case unsensitive. +func parseMastodonVisibility(s string) string { + s = strings.ToLower(s) + // Keep sorted since we search inside. + visibilities := []string{"direct", "private", "public", "unlisted"} + r := sort.SearchStrings(visibilities, s) + if r < len(visibilities) && visibilities[r] == s { + return s + } + + return "unlisted" +} diff --git a/cmd/run_test.go b/cmd/run_test.go index 54788d8..980d6c6 100644 --- a/cmd/run_test.go +++ b/cmd/run_test.go @@ -16,3 +16,21 @@ func TestParseBoolOrFalse(t *testing.T) { assert.Equal(t, parseBoolOrFalse("FALSE"), false) assert.Equal(t, parseBoolOrFalse("false"), false) } + +func TestParseMastodonVisibility(t *testing.T) { + + assert.Equal(t, parseMastodonVisibility("public"), "public") + assert.Equal(t, parseMastodonVisibility("direct"), "direct") + assert.Equal(t, parseMastodonVisibility("unlisted"), "unlisted") + assert.Equal(t, parseMastodonVisibility("private"), "private") + + assert.Equal(t, parseMastodonVisibility("Public"), "public") + assert.Equal(t, parseMastodonVisibility("diRect"), "direct") + assert.Equal(t, parseMastodonVisibility("unlisTED"), "unlisted") + assert.Equal(t, parseMastodonVisibility("PRIVATE"), "private") + + assert.Equal(t, parseMastodonVisibility("True"), "unlisted") + assert.Equal(t, parseMastodonVisibility("eriol"), "unlisted") + assert.Equal(t, parseMastodonVisibility(""), "unlisted") + assert.Equal(t, parseMastodonVisibility(" "), "unlisted") +}