fix sorting after altering find results

This commit is contained in:
Umputun
2019-02-11 16:15:41 -06:00
parent 3ba38dd4c7
commit 643f255832
5 changed files with 28 additions and 18 deletions
+1 -1
View File
@@ -163,7 +163,7 @@ func (b *BoltDB) Find(locator store.Locator, sortFld string) (comments []store.C
})
})
comments = sortComments(comments, sortFld)
comments = SortComments(comments, sortFld)
return comments, err
}
+2 -2
View File
@@ -62,8 +62,8 @@ const (
userLimit = 500
)
// sortComments is for engines can't sort data internally
func sortComments(comments []store.Comment, sortFld string) []store.Comment {
// SortComments is for engines can't sort data internally
func SortComments(comments []store.Comment, sortFld string) []store.Comment {
sort.Slice(comments, func(i, j int) bool {
switch sortFld {
case "+time", "-time", "time", "+active", "-active", "active":
+6 -6
View File
@@ -17,37 +17,37 @@ func TestEngine_sortComments(t *testing.T) {
{ID: "4", Score: 6, Controversy: 1, Timestamp: time.Date(2018, 2, 5, 10, 4, 0, 0, time.Local)},
}
sortComments(cc, "+time")
SortComments(cc, "+time")
assert.Equal(t, "1", cc[0].ID)
assert.Equal(t, "2", cc[1].ID)
assert.Equal(t, "3", cc[2].ID)
assert.Equal(t, "4", cc[3].ID)
sortComments(cc, "-time")
SortComments(cc, "-time")
assert.Equal(t, "4", cc[0].ID)
assert.Equal(t, "3", cc[1].ID)
assert.Equal(t, "2", cc[2].ID)
assert.Equal(t, "1", cc[3].ID)
sortComments(cc, "score")
SortComments(cc, "score")
assert.Equal(t, "2", cc[0].ID)
assert.Equal(t, "1", cc[1].ID)
assert.Equal(t, "3", cc[2].ID)
assert.Equal(t, "4", cc[3].ID)
sortComments(cc, "-score")
SortComments(cc, "-score")
assert.Equal(t, "3", cc[0].ID)
assert.Equal(t, "4", cc[1].ID)
assert.Equal(t, "1", cc[2].ID)
assert.Equal(t, "2", cc[3].ID)
sortComments(cc, "controversy")
SortComments(cc, "controversy")
assert.Equal(t, "1", cc[0].ID)
assert.Equal(t, "4", cc[1].ID)
assert.Equal(t, "2", cc[2].ID)
assert.Equal(t, "3", cc[3].ID)
sortComments(cc, "-controversy")
SortComments(cc, "-controversy")
assert.Equal(t, "3", cc[0].ID)
assert.Equal(t, "2", cc[1].ID)
assert.Equal(t, "1", cc[2].ID)
+10
View File
@@ -3,6 +3,7 @@ package service
import (
"math"
"sort"
"strings"
"sync"
"time"
@@ -424,13 +425,22 @@ func (s *DataStore) Find(locator store.Locator, sort string) ([]store.Comment, e
return comments, err
}
changedSort := false
// set votes controversy for comments added prior to #274
for i, c := range comments {
if c.Controversy == 0 && len(c.Votes) > 0 {
comments[i].Controversy = s.controversy(s.upsAndDowns(c))
if !changedSort && strings.Contains(sort, "controversy") { // trigger sort change
changedSort = true
}
}
}
// resort commits if altered
if changedSort {
comments = engine.SortComments(comments, sort)
}
return comments, nil
}
+9 -9
View File
@@ -1,9 +1,9 @@
### find request with tree
GET {{host}}/api/v1/find?site={{site}}&sort=-active&format=tree&url={{url}}
GET {{host}}/api/v1/find?site={{site}}&sort=-controversy&format=tree&url={{url}}
### find request with plain
GET {{host}}/api/v1/find?site={{site}}&sort=-time&format=plain&url={{url}}
GET {{host}}/api/v1/find?site={{site}}&sort=-controversy&format=plain&url={{url}}
### last 50 comments
GET {{host}}/api/v1/last/50?site={{site}}
@@ -41,7 +41,7 @@ Content-Type: application/json
}
### update comment
PUT {{host}}/api/v1/comment/7b88d7a91353ab206cb63cdca18fb26bcb30205b?site=remark&url=https://radio-t.com/blah1
PUT {{host}}/api/v1/comment/7b88d7a91353ab206cb63cdca18fb26bcb30205b?site={{site}}&url=https://radio-t.com/blah1
Content-Type: application/json
{
@@ -50,22 +50,22 @@ Content-Type: application/json
}
### pin comment
PUT {{host}}/api/v1/admin/pin/3665976683?site=remark&url={{url}}&pin=1
PUT {{host}}/api/v1/admin/pin/3665976683?site={{site}}&url={{url}}&pin=1
### vote for comment
PUT {{host}}/api/v1/vote/3665976683?site=remark&url={{url}}&vote=1
PUT {{host}}/api/v1/vote/73e346f4-d57d-41a8-8803-6671aa187d8e?site={{site}}&url={{url}}&vote=1
### get user info
GET {{host}}/api/v1/user
### get comment by id
GET {{host}}/api/v1/id/3665976683?site=remark&url={{url}}
GET {{host}}/api/v1/id/73e346f4-d57d-41a8-8803-6671aa187d8e?site={{site}}&url={{url}}
### get comment by id 2
GET {{host}}/api/v1/id/a2ddb8d2f65008ee1a1e3af8df0f26beb042309c?site=remark&url=https://radio-t.com/blah1
GET {{host}}/api/v1/id/a2ddb8d2f65008ee1a1e3af8df0f26beb042309c?site={{site}}&url=https://radio-t.com/blah1
### get comment by user id
GET {{host}}/api/v1/comments?site={{site}}&user=github_f1fda731dd18fbb388c943599fcae5a213315add&limit=5
GET {{host}}/api/v1/comments?site={{site}}&user={{user}}&limit=5
### get comment by user id2
GET {{host}}/api/v1/comments?site={{site}}&user=github_0a4349d868946d7841424c9bdd4415629df771e6
@@ -84,7 +84,7 @@ Content-Type: application/json
]
### list commented posts
GET {{host}}/api/v1/list?site={{site}}&limit=10&skip=5
GET {{host}}/api/v1/list?site={{site}}&limit=10&skip=0
### block user
PUT {{host}}/api/v1/admin/user/disqus_grigorybakunov?site={{site}}&block=1