support controversy in Find for comments stored without it #274
This commit is contained in:
@@ -180,17 +180,7 @@ func (s *DataStore) Vote(locator store.Locator, commentID string, userID string,
|
||||
comment.Score--
|
||||
}
|
||||
|
||||
upsAndDowns := func() (ups, downs int) {
|
||||
for _, v := range comment.Votes {
|
||||
if v {
|
||||
ups++
|
||||
continue
|
||||
}
|
||||
downs++
|
||||
}
|
||||
return ups, downs
|
||||
}
|
||||
comment.Controversy = s.controversy(upsAndDowns())
|
||||
comment.Controversy = s.controversy(s.upsAndDowns(comment))
|
||||
|
||||
return comment, s.Put(locator, comment)
|
||||
}
|
||||
@@ -427,6 +417,33 @@ func (s *DataStore) SetMetas(siteID string, umetas []UserMetaData, pmetas []Post
|
||||
return errs.ErrorOrNil()
|
||||
}
|
||||
|
||||
func (s *DataStore) Find(locator store.Locator, sort string) ([]store.Comment, error) {
|
||||
comments, err := s.Interface.Find(locator, sort)
|
||||
if err != nil {
|
||||
return comments, err
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
}
|
||||
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) upsAndDowns(c store.Comment) (ups, downs int) {
|
||||
for _, v := range c.Votes {
|
||||
if v {
|
||||
ups++
|
||||
continue
|
||||
}
|
||||
downs++
|
||||
}
|
||||
return ups, downs
|
||||
}
|
||||
|
||||
// getsScopedLocks pull lock from the map if found or create a new one
|
||||
func (s *DataStore) getsScopedLocks(id string) (lock sync.Locker) {
|
||||
s.scopedLocks.Do(func() { s.scopedLocks.locks = map[string]sync.Locker{} })
|
||||
|
||||
@@ -652,6 +652,40 @@ func TestService_HasReplies(t *testing.T) {
|
||||
assert.True(t, b.HasReplies(comment))
|
||||
}
|
||||
|
||||
func TestService_Find(t *testing.T) {
|
||||
defer os.Remove(testDb)
|
||||
|
||||
// two comments for https://radio-t.com, no reply
|
||||
b := DataStore{Interface: prepStoreEngine(t), EditDuration: 100 * time.Millisecond,
|
||||
AdminStore: admin.NewStaticStore("secret 123", []string{"user2"}, "user@email.com")}
|
||||
|
||||
res, err := b.Find(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "time")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, len(res))
|
||||
|
||||
// add one more for https://radio-t.com/2
|
||||
comment := store.Comment{
|
||||
ID: "123456",
|
||||
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
||||
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.Local),
|
||||
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
||||
User: store.User{ID: "user1", Name: "user name"},
|
||||
Score: 1,
|
||||
Votes: map[string]bool{"id-1": true, "id-2": true, "123456": false},
|
||||
}
|
||||
_, err = b.Interface.Create(comment) // create directly with engine, doesn't set Controversy
|
||||
assert.Nil(t, err)
|
||||
|
||||
// make sure Controversy altered
|
||||
res, err = b.Find(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "-controversy")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 3, len(res))
|
||||
assert.Equal(t, "123456", res[0].ID)
|
||||
assert.InDelta(t, 1.73, res[0].Controversy, 0.01)
|
||||
assert.Equal(t, "id-1", res[1].ID)
|
||||
assert.InDelta(t, 0, res[1].Controversy, 0.01)
|
||||
}
|
||||
|
||||
// makes new boltdb, put two records
|
||||
func prepStoreEngine(t *testing.T) engine.Interface {
|
||||
os.Remove(testDb)
|
||||
|
||||
Reference in New Issue
Block a user