From 19ad7f7550cc97c93147eb72ad36b60794c899db Mon Sep 17 00:00:00 2001 From: lauralani Date: Fri, 7 Jul 2023 09:32:01 +0200 Subject: [PATCH] set post visibility via configuration file --- app/mastodon.go | 3 ++- cmd/root.go | 4 +++- examples/config.yaml | 5 +++++ misc/misc.go | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/mastodon.go b/app/mastodon.go index 0bdb862..5723641 100644 --- a/app/mastodon.go +++ b/app/mastodon.go @@ -23,6 +23,7 @@ import ( func postQueueItemToMastodon(bundle models.QueueItem) error { urlstring := viper.GetString("mastodon.url") + "/api/v1/statuses" endpoint, _ := url.Parse(urlstring) + visibility := viper.GetString("mastodon.visibility") token := viper.GetString("mastodon.token") idemkey := uuid.New().String() @@ -42,7 +43,7 @@ func postQueueItemToMastodon(bundle models.QueueItem) error { payload := url.Values{} payload.Add("status", builder.String()) - payload.Add("visibility", "private") + payload.Add("visibility", visibility) payload.Add("language", "en") client := misc.CustomHttpClient() diff --git a/cmd/root.go b/cmd/root.go index a04dc70..0c7a45c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,6 +11,7 @@ import ( "codeberg.org/lauralani/humble-bot/constants" "codeberg.org/lauralani/humble-bot/db" "codeberg.org/lauralani/humble-bot/log" + "codeberg.org/lauralani/humble-bot/misc" log2 "log" "os" @@ -37,7 +38,7 @@ func Execute() { } func init() { - cobra.OnInitialize(initConfig, db.Initialize, log.InitializeLogger) + cobra.OnInitialize(initConfig, db.Initialize, log.InitializeLogger, misc.CheckConfig) rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $workingdir/config.yaml)") rootCmd.PersistentFlags().BoolVarP(&log.FlagDebug, "debug", "d", false, "enable debug logging") @@ -62,6 +63,7 @@ func initConfig() { } viper.SetDefault("mastodon.postinterval", "5m") + viper.SetDefault("mastodon.visibility", "public") viper.SetDefault("humblebundle.pollinterval", "30m") if err := viper.ReadInConfig(); err == nil { diff --git a/examples/config.yaml b/examples/config.yaml index 7f3c5b0..444693a 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -20,6 +20,11 @@ mastodon: # default: 5m postinterval: "5m" + # Interval in which humble-bot tries to post bundles + # allowed values: ["public", "unlisted", "private"] + # default: public + visibility: "public" + # Humble Bundle settings humblebundle: diff --git a/misc/misc.go b/misc/misc.go index 8ec25db..07da901 100644 --- a/misc/misc.go +++ b/misc/misc.go @@ -8,6 +8,8 @@ package misc import ( "github.com/microcosm-cc/bluemonday" + "github.com/rs/zerolog/log" + "github.com/spf13/viper" "html" ) @@ -15,3 +17,23 @@ func Sanitize(d string) string { p := bluemonday.StrictPolicy() return html.UnescapeString(p.Sanitize(d)) } + +func CheckConfig() { + visibility := viper.GetString("mastodon.visibility") + visibilityallowed := []string{"public", "unlisted", "private"} + + if !isValueInList(visibility, visibilityallowed) { + log.Fatal().Str("component", "config").Str("config_item", "mastodon.visibility"). + Msgf("Config value must be one of [public, unlisted, private]: %q", visibility) + } + +} + +func isValueInList(value string, list []string) bool { + for _, v := range list { + if v == value { + return true + } + } + return false +}