From 61e07bf9457e6eb0b99b43aaa29a1b838a3b2501 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Sat, 22 May 2021 21:50:34 -0700 Subject: [PATCH] API for setting a list of blocked usernames. For #782 --- controllers/admin/config.go | 12 +++++++++++ controllers/admin/serverConfig.go | 36 ++++++++++++++++--------------- core/data/config.go | 18 ++++++++++++++++ 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/controllers/admin/config.go b/controllers/admin/config.go index a57fc5cb1..c6461abfb 100644 --- a/controllers/admin/config.go +++ b/controllers/admin/config.go @@ -539,7 +539,19 @@ func SetCustomStyles(w http.ResponseWriter, r *http.Request) { data.SetCustomStyles(customStyles.Value.(string)) controllers.WriteSimpleResponse(w, true, "custom styles updated") +} +// SetUsernameBlocklist will set the list of usernames we do not allow to use. +func SetUsernameBlocklist(w http.ResponseWriter, r *http.Request) { + usernames, success := getValueFromRequest(w, r) + if !success { + controllers.WriteSimpleResponse(w, false, "unable to update custom styles") + return + } + + data.SetUsernameBlocklist(usernames.Value.(string)) + + controllers.WriteSimpleResponse(w, true, "blocklist updated") } func requirePOST(w http.ResponseWriter, r *http.Request) bool { diff --git a/controllers/admin/serverConfig.go b/controllers/admin/serverConfig.go index 9ac37d458..51fdb0812 100644 --- a/controllers/admin/serverConfig.go +++ b/controllers/admin/serverConfig.go @@ -56,10 +56,11 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) { Enabled: data.GetDirectoryEnabled(), InstanceURL: data.GetServerURL(), }, - S3: data.GetS3Config(), - ExternalActions: data.GetExternalActions(), - SupportedCodecs: transcoder.GetCodecs(ffmpeg), - VideoCodec: data.GetVideoCodec(), + S3: data.GetS3Config(), + ExternalActions: data.GetExternalActions(), + SupportedCodecs: transcoder.GetCodecs(ffmpeg), + VideoCodec: data.GetVideoCodec(), + UsernameBlocklist: data.GetUsernameBlocklist(), } w.Header().Set("Content-Type", "application/json") @@ -70,19 +71,20 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) { } type serverConfigAdminResponse struct { - InstanceDetails webConfigResponse `json:"instanceDetails"` - FFmpegPath string `json:"ffmpegPath"` - StreamKey string `json:"streamKey"` - WebServerPort int `json:"webServerPort"` - RTMPServerPort int `json:"rtmpServerPort"` - S3 models.S3 `json:"s3"` - VideoSettings videoSettings `json:"videoSettings"` - LatencyLevel int `json:"latencyLevel"` - YP yp `json:"yp"` - ChatDisabled bool `json:"chatDisabled"` - ExternalActions []models.ExternalAction `json:"externalActions"` - SupportedCodecs []string `json:"supportedCodecs"` - VideoCodec string `json:"videoCodec"` + InstanceDetails webConfigResponse `json:"instanceDetails"` + FFmpegPath string `json:"ffmpegPath"` + StreamKey string `json:"streamKey"` + WebServerPort int `json:"webServerPort"` + RTMPServerPort int `json:"rtmpServerPort"` + S3 models.S3 `json:"s3"` + VideoSettings videoSettings `json:"videoSettings"` + LatencyLevel int `json:"latencyLevel"` + YP yp `json:"yp"` + ChatDisabled bool `json:"chatDisabled"` + ExternalActions []models.ExternalAction `json:"externalActions"` + SupportedCodecs []string `json:"supportedCodecs"` + VideoCodec string `json:"videoCodec"` + UsernameBlocklist string `json:"usernameBlocklist"` } type videoSettings struct { diff --git a/core/data/config.go b/core/data/config.go index c00af9da6..161c65e97 100644 --- a/core/data/config.go +++ b/core/data/config.go @@ -40,6 +40,7 @@ const chatDisabledKey = "chat_disabled" const externalActionsKey = "external_actions" const customStylesKey = "custom_styles" const videoCodecKey = "video_codec" +const blockedUsernamesKey = "blocked_usernames" // GetExtraPageBodyContent will return the user-supplied body content. func GetExtraPageBodyContent() string { @@ -552,3 +553,20 @@ func FindHighestVideoQualityIndex(qualities []models.StreamOutputVariant) int { return indexedQualities[0].index } + +// GetUsernameBlocklist will return the blocked usernames as a comma seperated string. +func GetUsernameBlocklist() string { + usernameString, err := _datastore.GetString(blockedUsernamesKey) + + if err != nil { + log.Traceln(blockedUsernamesKey, err) + return "" + } + + return usernameString +} + +// SetUsernameBlocklist set the username blocklist as a comma seperated string. +func SetUsernameBlocklist(usernames string) error { + return _datastore.SetString(serverMetadataTagsKey, usernames) +}