Make toot visibility configurable
parent
8d2d78c8ec
commit
f2c9bbacbc
31
cmd/run.go
31
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"
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue