From 440974f365d3b56d27f8049cb3742fc93539c48b Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 6 Jan 2018 12:37:12 -0600 Subject: [PATCH] support vote reset --- README.md | 10 +++++----- app/store/service.go | 17 ++++++++++++++--- app/store/service_test.go | 9 +++++++++ remark.rest | 2 +- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 54c4edb5..6ad39e3d 100644 --- a/README.md +++ b/README.md @@ -28,16 +28,16 @@ Remark42 is a self-hosted, lightweight, and simple (yet functional) comment engi | Command line | Environment | Default | Multi | Scope | Description | | --------------- | -------------------- | ---------------------- | ----- | ------ | ------------------------------- | -| --bolt | BOLTDB_PATH | `/tmp` | no | all | path to data directory | -| --site | SITE | `remark` | yes | server | site name(s) | | --url | REMARK_URL | `https://remark42.com` | no | all | url to remark server | +| --bolt | BOLTDB_PATH | `/tmp` | no | all | path to data directory | +| --dbg | DEBUG | `false` | no | all | debug mode | +| --dev | DEV | `false` | no | all | development mode, no auth! | +| --site | SITE | `remark` | yes | server | site name(s) | | --admin | ADMIN | | yes | server | admin(s) names (user id) | | --backup | BACKUP_PATH | `/tmp` | no | server | backups location | | --max-back | MAX_BACKUP_FILES | `10` | no | server | max backup files to keep | | --session | SESSION_STORE | `/tmp` | no | server | path to session store directory | | --store-key | STORE_KEY | `secure-store-key` | no | server | session store encryption key | -| --dbg | DEBUG | `false` | no | all | debug mode | -| --dev | DEV | `false` | no | all | development mode, no auth! | | --google-cid | REMARK_GOOGLE_CID | | no | server | Google OAuth client ID | | --google-csec | REMARK_GOOGLE_CSEC | | no | server | Google OAuth client secret | | --facebook-cid | REMARK_FACEBOOK_CID | | no | server | Facebook OAuth client ID | @@ -168,7 +168,7 @@ Sort can be `time` or `score`. Supported sort order with prefix -/+, i.e. `-time ``` - `GET /api/v1/last/{max}?site=site-id` - get up to `{max}` last comments -- `GET /api/v1/id/{id}?site=site-id` - get comment by `id` +- `GET /api/v1/id/{id}?site=site-id` - get comment by `commen id` - `GET /api/v1/comments?site=site-id&user=id` - get comment by `user id` - `GET /api/v1/count?site=site-id&url=post-url` - get comment's count for `{url}` - `GET /api/v1/list?site=site-id` - list commented posts diff --git a/app/store/service.go b/app/store/service.go index 7b90ae12..2d93eacb 100644 --- a/app/store/service.go +++ b/app/store/service.go @@ -30,12 +30,23 @@ func (s *Service) Vote(locator Locator, commentID string, userID string, val boo return comment, err } - if _, voted := comment.Votes[userID]; voted { + v, voted := comment.Votes[userID] + + if voted && v == val { return comment, errors.Errorf("user %s already voted for %s", userID, commentID) } - // update votes and score - comment.Votes[userID] = val + // reset vote if user changed to opposite + if voted && v != val { + delete(comment.Votes, userID) + } + + // add to voted map if first vote + if !voted { + comment.Votes[userID] = val + } + + // update score if val { comment.Score++ } else { diff --git a/app/store/service_test.go b/app/store/service_test.go index d6394ae9..a5672c72 100644 --- a/app/store/service_test.go +++ b/app/store/service_test.go @@ -31,6 +31,15 @@ func TestService_Vote(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 2, len(res)) assert.Equal(t, 1, res[0].Score) + + _, err = b.Vote(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", false) + assert.Nil(t, err, "vote reset") + res, err = b.Last(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, 0) + assert.Nil(t, err) + assert.Equal(t, 2, len(res)) + assert.Equal(t, 0, res[0].Score) + assert.Equal(t, map[string]bool{}, res[0].Votes) + } func TestBoltDB_Pin(t *testing.T) { diff --git a/remark.rest b/remark.rest index 5f5bb55e..198bd107 100644 --- a/remark.rest +++ b/remark.rest @@ -35,7 +35,7 @@ Content-Type: application/json } ### pin comment -PUT https://demo.remark42.com/api/v1/admin/pin/3665976683?site=remark&url=https://radio-t.com/p/2017/12/16/podcast-576/&pin=1 +PUT https://demo.remark42.com/api/v1/admin/pin/3665976683?site=remark&url=https://radio-t.com/p/2017/12/16/podcast-576/&pin=0 ### vote for comment PUT https://demo.remark42.com/api/v1/vote/3665976683?site=remark&url=https://radio-t.com/p/2017/12/16/podcast-576/&vote=1