From 2caebcfecffad6d472db7b11c2082b43d6d5f270 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 12 Mar 2018 11:20:56 -0400 Subject: [PATCH] Support blacklist for terms that prevent a public post This adds a BLACKLIST_TERMS configuration value that contains a comma-separated list of terms that indicate spam posts and will prevent a link from being showcased on the Browse page, tweeted out, etc. --- README.md | 1 + config.go | 11 +++++++++++ construction.go | 2 +- filter.go | 6 ++++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 filter.go diff --git a/README.md b/README.md index 96f16ea..286e994 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ DB_USER=dbuser DB_PASSWORD=pass DB_DB=htmlhouse PRIVATE_KEY=keys/dev PUBLIC_KEY= | `PREVIEWS_HOST` | Fully-qualified URL (without trailing slash) of screenshot server | None. | | `ADMIN_PASS` | Password to perform admin functions via API | `uhoh` | | `BROWSE_ITEMS` | Number of items to show on Browse page | 10 | +| `BLACKLIST_TERMS` | Comma-separated list of terms to prevent a post from being made public | None. | | `TWITTER_KEY` | Twitter consumer key | `notreal` | | `TWITTER_SECRET` | Twitter consumer secret | `notreal` | | `TWITTER_TOKEN` | Twitter access token of the posting Twitter account | `notreal` | diff --git a/config.go b/config.go index 41e64b5..cbd6943 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,8 @@ package htmlhouse import ( "github.com/danryan/env" + "regexp" + "strings" ) type config struct { @@ -23,6 +25,9 @@ type config struct { AdminPass string `env:"key=ADMIN_PASS default=uhoh"` BrowseItems int `env:"key=BROWSE_ITEMS default=10"` + BlacklistTerms string `env:"key=BLACKLIST_TERMS"` + BlacklistReg *regexp.Regexp + // Twitter configuration TwitterConsumerKey string `env:"key=TWITTER_KEY default=notreal"` TwitterConsumerSecret string `env:"key=TWITTER_SECRET default=notreal"` @@ -36,5 +41,11 @@ func newConfig() (*config, error) { return cfg, err } + // Process anything + termsReg := `(?i)\b` + cfg.BlacklistTerms + `\b` + termsReg = strings.Replace(termsReg, ",", `\b|\b`, -1) + cfg.BlacklistReg = regexp.MustCompile(termsReg) + + // Return result return cfg, nil } diff --git a/construction.go b/construction.go index 3f8165c..018b449 100644 --- a/construction.go +++ b/construction.go @@ -39,7 +39,7 @@ func createHouse(app *app, w http.ResponseWriter, r *http.Request) error { resUser := newSessionInfo(houseID) - if public { + if public && passesPublicFilter(app, html) { go addPublicAccess(app, houseID, html) } diff --git a/filter.go b/filter.go new file mode 100644 index 0000000..703f7e4 --- /dev/null +++ b/filter.go @@ -0,0 +1,6 @@ +package htmlhouse + +func passesPublicFilter(app *app, html string) bool { + spam := app.cfg.BlacklistReg.MatchString(html) + return !spam +}