From 1cbedb85b06b86c09f36d0b3625a210f6d54569c Mon Sep 17 00:00:00 2001 From: Umputun Date: Thu, 26 Sep 2019 23:06:20 -0500 Subject: [PATCH] fix -time sort for user comments --- backend/app/rest/api/rest_public_test.go | 4 ++++ backend/app/store/service/service.go | 3 ++- backend/app/store/service/service_test.go | 25 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index cc0c5c9b..a92f9204 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -348,6 +348,10 @@ func TestRest_FindUserComments(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 3, len(resp.Comments), "should have 3 comments") assert.Equal(t, 4, resp.Count, "should have 3 count") + + // user comment sorted with -time + assert.True(t, resp.Comments[0].Timestamp.After(resp.Comments[1].Timestamp)) + assert.True(t, resp.Comments[1].Timestamp.After(resp.Comments[2].Timestamp)) } func TestRest_UserInfo(t *testing.T) { diff --git a/backend/app/store/service/service.go b/backend/app/store/service/service.go index 0225cd8d..6fe59acb 100644 --- a/backend/app/store/service/service.go +++ b/backend/app/store/service/service.go @@ -732,7 +732,8 @@ func (s *DataStore) SetMetas(siteID string, umetas []UserMetaData, pmetas []Post // User gets comment for given userID on siteID func (s *DataStore) User(siteID, userID string, limit, skip int, user store.User) ([]store.Comment, error) { - req := engine.FindRequest{Locator: store.Locator{SiteID: siteID}, UserID: userID, Limit: limit, Skip: skip} + req := engine.FindRequest{Locator: store.Locator{SiteID: siteID}, UserID: userID, + Limit: limit, Skip: skip, Sort: "-time"} comments, err := s.Engine.Find(req) if err != nil { return comments, err diff --git a/backend/app/store/service/service_test.go b/backend/app/store/service/service_test.go index fe13745b..04ecd5e6 100644 --- a/backend/app/store/service/service_test.go +++ b/backend/app/store/service/service_test.go @@ -1060,6 +1060,31 @@ func TestService_Count(t *testing.T) { assert.Equal(t, 0, c) } +func TestService_UserComments(t *testing.T) { + defer teardown(t) + + // two comments for https://radio-t.com, no reply + b := DataStore{Engine: prepStoreEngine(t), EditDuration: 100 * time.Millisecond, + AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")} + + // add one more for user2 + comment := store.Comment{ + ID: "id-3", + Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.Local), + Text: `some text, link`, + Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"}, + User: store.User{ID: "user2", Name: "user name"}, + } + _, err := b.Create(comment) + assert.NoError(t, err) + + cc, err := b.User("radio-t", "user1", 0, 0, store.User{}) + assert.NoError(t, err) + assert.Equal(t, 2, len(cc), "two recs for user1") + assert.Equal(t, "id-2", cc[0].ID, "reverse sort") + assert.Equal(t, "id-1", cc[1].ID, "reverse sort") +} + func TestService_UserCount(t *testing.T) { defer teardown(t) // two comments for https://radio-t.com, no reply