set post visibility via configuration file

This commit is contained in:
Adora Laura Kalb 2023-07-07 09:32:01 +02:00
parent 612b484daa
commit 19ad7f7550
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056
4 changed files with 32 additions and 2 deletions

View file

@ -23,6 +23,7 @@ import (
func postQueueItemToMastodon(bundle models.QueueItem) error { func postQueueItemToMastodon(bundle models.QueueItem) error {
urlstring := viper.GetString("mastodon.url") + "/api/v1/statuses" urlstring := viper.GetString("mastodon.url") + "/api/v1/statuses"
endpoint, _ := url.Parse(urlstring) endpoint, _ := url.Parse(urlstring)
visibility := viper.GetString("mastodon.visibility")
token := viper.GetString("mastodon.token") token := viper.GetString("mastodon.token")
idemkey := uuid.New().String() idemkey := uuid.New().String()
@ -42,7 +43,7 @@ func postQueueItemToMastodon(bundle models.QueueItem) error {
payload := url.Values{} payload := url.Values{}
payload.Add("status", builder.String()) payload.Add("status", builder.String())
payload.Add("visibility", "private") payload.Add("visibility", visibility)
payload.Add("language", "en") payload.Add("language", "en")
client := misc.CustomHttpClient() client := misc.CustomHttpClient()

View file

@ -11,6 +11,7 @@ import (
"codeberg.org/lauralani/humble-bot/constants" "codeberg.org/lauralani/humble-bot/constants"
"codeberg.org/lauralani/humble-bot/db" "codeberg.org/lauralani/humble-bot/db"
"codeberg.org/lauralani/humble-bot/log" "codeberg.org/lauralani/humble-bot/log"
"codeberg.org/lauralani/humble-bot/misc"
log2 "log" log2 "log"
"os" "os"
@ -37,7 +38,7 @@ func Execute() {
} }
func init() { 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().StringVarP(&cfgFile, "config", "c", "", "config file (default is $workingdir/config.yaml)")
rootCmd.PersistentFlags().BoolVarP(&log.FlagDebug, "debug", "d", false, "enable debug logging") 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.postinterval", "5m")
viper.SetDefault("mastodon.visibility", "public")
viper.SetDefault("humblebundle.pollinterval", "30m") viper.SetDefault("humblebundle.pollinterval", "30m")
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err == nil {

View file

@ -20,6 +20,11 @@ mastodon:
# default: 5m # default: 5m
postinterval: "5m" postinterval: "5m"
# Interval in which humble-bot tries to post bundles
# allowed values: ["public", "unlisted", "private"]
# default: public
visibility: "public"
# Humble Bundle settings # Humble Bundle settings
humblebundle: humblebundle:

View file

@ -8,6 +8,8 @@ package misc
import ( import (
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"html" "html"
) )
@ -15,3 +17,23 @@ func Sanitize(d string) string {
p := bluemonday.StrictPolicy() p := bluemonday.StrictPolicy()
return html.UnescapeString(p.Sanitize(d)) 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
}