From cb5fc79dbe522acb4edae3e214254f114cb23fa0 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 26 Jan 2019 01:38:56 -0600 Subject: [PATCH] add site-level positve score support #260 --- backend/app/cmd/server.go | 4 +++- backend/app/rest/api/rest_public.go | 2 ++ backend/app/rest/api/rest_public_test.go | 1 + backend/app/store/service/service.go | 9 +++++++-- backend/app/store/service/service_test.go | 15 +++++++++++++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 0b10896c..861ef789 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -12,7 +12,7 @@ import ( "syscall" "time" - "github.com/coreos/bbolt" + bolt "github.com/coreos/bbolt" log "github.com/go-pkgz/lgr" auth_cache "github.com/patrickmn/go-cache" "github.com/pkg/errors" @@ -53,6 +53,7 @@ type ServerCommand struct { MaxVotes int `long:"max-votes" env:"MAX_VOTES" default:"-1" description:"maximum number of votes per comment"` LowScore int `long:"low-score" env:"LOW_SCORE" default:"-5" description:"low score threshold"` CriticalScore int `long:"critical-score" env:"CRITICAL_SCORE" default:"-10" description:"critical score threshold"` + PositiveScore bool `long:"positive-score" env:"POSITIVE_SCORE" description:"enable positive score only"` ReadOnlyAge int `long:"read-age" env:"READONLY_AGE" default:"0" description:"read-only age of comments, days"` EditDuration time.Duration `long:"edit-time" env:"EDIT_TIME" default:"5m" description:"edit window"` Port int `long:"port" env:"REMARK_PORT" default:"8080" description:"port"` @@ -217,6 +218,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) { AdminStore: adminStore, MaxCommentSize: s.MaxCommentSize, MaxVotes: s.MaxVotes, + PositiveScore: s.PositiveScore, TitleExtractor: service.NewTitleExtractor(http.Client{Timeout: time.Second * 5}), RestrictedWordsMatcher: service.NewRestrictedWordsMatcher(service.StaticRestrictedWordsLister{Words: s.RestrictedWords}), } diff --git a/backend/app/rest/api/rest_public.go b/backend/app/rest/api/rest_public.go index cbfa6c74..84b52e6a 100644 --- a/backend/app/rest/api/rest_public.go +++ b/backend/app/rest/api/rest_public.go @@ -224,6 +224,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { Auth []string `json:"auth_providers"` LowScore int `json:"low_score"` CriticalScore int `json:"critical_score"` + PositiveScore bool `json:"positive_score"` ReadOnlyAge int `json:"readonly_age"` } @@ -235,6 +236,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { AdminEmail: s.DataService.AdminStore.Email(siteID), LowScore: s.ScoreThresholds.Low, CriticalScore: s.ScoreThresholds.Critical, + PositiveScore: s.DataService.PositiveScore, ReadOnlyAge: s.ReadOnlyAge, } diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index 67624ee4..4285b384 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -392,6 +392,7 @@ func TestRest_Config(t *testing.T) { assert.Equal(t, 4000., j["max_comment_size"]) assert.Equal(t, -5., j["low_score"]) assert.Equal(t, -10., j["critical_score"]) + assert.False(t, j["positive_score"].(bool)) assert.Equal(t, 10., j["readonly_age"]) t.Logf("%+v", j) } diff --git a/backend/app/store/service/service.go b/backend/app/store/service/service.go index ac7ca6fb..ccfdae20 100644 --- a/backend/app/store/service/service.go +++ b/backend/app/store/service/service.go @@ -7,8 +7,8 @@ import ( log "github.com/go-pkgz/lgr" "github.com/google/uuid" - "github.com/hashicorp/go-multierror" - "github.com/patrickmn/go-cache" + multierror "github.com/hashicorp/go-multierror" + cache "github.com/patrickmn/go-cache" "github.com/pkg/errors" "github.com/umputun/remark/backend/app/store" @@ -23,6 +23,7 @@ type DataStore struct { AdminStore admin.Store MaxCommentSize int MaxVotes int + PositiveScore bool TitleExtractor *TitleExtractor RestrictedWordsMatcher *RestrictedWordsMatcher @@ -154,6 +155,10 @@ func (s *DataStore) Vote(locator store.Locator, commentID string, userID string, return comment, errors.Errorf("maximum number of votes exceeded for comment %s", commentID) } + if s.PositiveScore && comment.Score <= 0 { + return comment, errors.Errorf("minimal score reached for comment %s", commentID) + } + // reset vote if user changed to opposite if voted && v != val { delete(comment.Votes, userID) diff --git a/backend/app/store/service/service_test.go b/backend/app/store/service/service_test.go index ee5454c1..3e568a59 100644 --- a/backend/app/store/service/service_test.go +++ b/backend/app/store/service/service_test.go @@ -317,6 +317,21 @@ func TestService_VoteConcurrent(t *testing.T) { assert.Equal(t, 100, len(res[0].Votes), "should have 1000 votes") } +func TestService_VotePositive(t *testing.T) { + defer os.Remove(testDb) + b := DataStore{Interface: prepStoreEngine(t), AdminStore: admin.NewStaticKeyStore("secret 123"), + MaxVotes: -1, PositiveScore: true} + + _, err := b.Vote(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "id-1", "user2", false) + assert.EqualError(t, err, "minimal score reached for comment id-1") + + b = DataStore{Interface: prepStoreEngine(t), AdminStore: admin.NewStaticKeyStore("secret 123"), + MaxVotes: -1, PositiveScore: false} + c, err := b.Vote(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "id-1", "user2", false) + assert.Nil(t, err, "minimal score ignored") + assert.Equal(t, -1, c.Score) +} + func TestService_Pin(t *testing.T) { defer os.Remove(testDb) b := DataStore{Interface: prepStoreEngine(t), AdminStore: admin.NewStaticKeyStore("secret 123")}