Skip to content

Commit

Permalink
add site-level positve score support #260
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 26, 2019
1 parent 440beb3 commit cb5fc79
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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}),
}
Expand Down
2 changes: 2 additions & 0 deletions backend/app/rest/api/rest_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions backend/app/rest/api/rest_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 7 additions & 2 deletions backend/app/store/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,6 +23,7 @@ type DataStore struct {
AdminStore admin.Store
MaxCommentSize int
MaxVotes int
PositiveScore bool
TitleExtractor *TitleExtractor
RestrictedWordsMatcher *RestrictedWordsMatcher

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions backend/app/store/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Expand Down

0 comments on commit cb5fc79

Please sign in to comment.