From dfe6d644aec8558e8adc07e18d8c484d0f26884e Mon Sep 17 00:00:00 2001 From: Umputun Date: Wed, 16 May 2018 21:52:05 -0500 Subject: [PATCH] clear import aliases --- app/rest/api/import_test.go | 4 +- app/rest/api/middleware.go | 1 + app/rest/api/rest.go | 2 +- app/rest/api/rest_test.go | 2 +- app/rest/api/rss.go | 1 + app/rest/auth/auth_test.go | 1 + app/store/engine/engine.go | 25 +++++----- app/store/service/service.go | 22 ++++----- app/store/service/service_test.go | 80 +++++++++++++++---------------- 9 files changed, 71 insertions(+), 67 deletions(-) diff --git a/app/rest/api/import_test.go b/app/rest/api/import_test.go index 9050a5dc..96600595 100644 --- a/app/rest/api/import_test.go +++ b/app/rest/api/import_test.go @@ -14,10 +14,10 @@ import ( "github.com/coreos/bbolt" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/umputun/remark/app/store/engine" - "github.com/umputun/remark/app/store/service" "github.com/umputun/remark/app/migrator" + "github.com/umputun/remark/app/store/engine" + "github.com/umputun/remark/app/store/service" ) func TestImport(t *testing.T) { diff --git a/app/rest/api/middleware.go b/app/rest/api/middleware.go index ed60660d..9d19420b 100644 --- a/app/rest/api/middleware.go +++ b/app/rest/api/middleware.go @@ -14,6 +14,7 @@ import ( "time" "github.com/go-chi/chi/middleware" + "github.com/umputun/remark/app/rest" ) diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index 50477b1d..f92b8bf8 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -19,13 +19,13 @@ import ( "github.com/go-chi/render" "github.com/gorilla/context" "github.com/pkg/errors" - "github.com/umputun/remark/app/store/service" "gopkg.in/russross/blackfriday.v2" "github.com/umputun/remark/app/migrator" "github.com/umputun/remark/app/rest" "github.com/umputun/remark/app/rest/auth" "github.com/umputun/remark/app/store" + "github.com/umputun/remark/app/store/service" ) // Rest is a rest access server diff --git a/app/rest/api/rest_test.go b/app/rest/api/rest_test.go index 7c1de080..7bd02333 100644 --- a/app/rest/api/rest_test.go +++ b/app/rest/api/rest_test.go @@ -17,13 +17,13 @@ import ( "github.com/gorilla/sessions" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/umputun/remark/app/store/service" "github.com/umputun/remark/app/migrator" "github.com/umputun/remark/app/rest/auth" "github.com/umputun/remark/app/rest/avatar" "github.com/umputun/remark/app/store" "github.com/umputun/remark/app/store/engine" + "github.com/umputun/remark/app/store/service" ) var testDb = "/tmp/test-remark.db" diff --git a/app/rest/api/rss.go b/app/rest/api/rss.go index 3b417f57..23091806 100644 --- a/app/rest/api/rss.go +++ b/app/rest/api/rss.go @@ -8,6 +8,7 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/render" "github.com/gorilla/feeds" + "github.com/umputun/remark/app/rest" "github.com/umputun/remark/app/store" ) diff --git a/app/rest/auth/auth_test.go b/app/rest/auth/auth_test.go index 51a6e527..17680f65 100644 --- a/app/rest/auth/auth_test.go +++ b/app/rest/auth/auth_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/go-chi/chi" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/app/store/engine/engine.go b/app/store/engine/engine.go index b2714957..4a520268 100644 --- a/app/store/engine/engine.go +++ b/app/store/engine/engine.go @@ -6,7 +6,7 @@ import ( "sort" "strings" - R "github.com/umputun/remark/app/store" + "github.com/umputun/remark/app/store" ) //go:generate sh -c "mockery -inpkg -name Interface -print > file.tmp && mv file.tmp engine_mock.go" @@ -19,26 +19,27 @@ type Interface interface { // Accessor defines all usual access ops avail for regular user type Accessor interface { - Create(comment R.Comment) (commentID string, err error) // create new comment, avoid dups by id - Get(locator R.Locator, commentID string) (comment R.Comment, err error) // get comment by id - Put(locator R.Locator, comment R.Comment) error // update comment, mutable parts only - Find(locator R.Locator, sort string) ([]R.Comment, error) // find comments for locator - Last(siteID string, limit int) ([]R.Comment, error) // last comments for given site, sorted by time - User(siteID string, userID string, limit int) ([]R.Comment, int, error) // comments by user, sorted by time - Count(locator R.Locator) (int, error) // number of comments for the post - List(siteID string, limit int, skip int) ([]R.PostInfo, error) // list of commented posts + Create(comment store.Comment) (commentID string, err error) // create new comment, avoid dups by id + Get(locator store.Locator, commentID string) (store.Comment, error) // get comment by id + Put(locator store.Locator, comment store.Comment) error // update comment, mutable parts only + Find(locator store.Locator, sort string) ([]store.Comment, error) // find comments for locator + Last(siteID string, limit int) ([]store.Comment, error) // last comments for given site, sorted by time + User(siteID string, userID string, limit int) ([]store.Comment, int, error) // comments by user, sorted by time + Count(locator store.Locator) (int, error) // number of comments for the post + List(siteID string, limit int, skip int) ([]store.PostInfo, error) // list of commented posts } // Admin defines all store ops avail for admin only type Admin interface { - Delete(locator R.Locator, commentID string) error // delete comment by id + Delete(locator store.Locator, commentID string) error // delete comment by id DeleteAll(siteID string) error // delete all data from site SetBlock(siteID string, userID string, status bool) error // block or unblock user IsBlocked(siteID string, userID string) bool // check if user blocked - Blocked(siteID string) ([]R.BlockedUser, error) // get list of blocked users + Blocked(siteID string) ([]store.BlockedUser, error) // get list of blocked users } -func sortComments(comments []R.Comment, sortFld string) []R.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": diff --git a/app/store/service/service.go b/app/store/service/service.go index 97e862e0..134c94af 100644 --- a/app/store/service/service.go +++ b/app/store/service/service.go @@ -6,7 +6,7 @@ import ( "github.com/google/uuid" "github.com/pkg/errors" - R "github.com/umputun/remark/app/store" + "github.com/umputun/remark/app/store" "github.com/umputun/remark/app/store/engine" ) @@ -21,7 +21,7 @@ type DataStore struct { const defaultCommentMaxSize = 2000 // Create prepares comment and forward to Interface.Create -func (s *DataStore) Create(comment R.Comment) (commentID string, err error) { +func (s *DataStore) Create(comment store.Comment) (commentID string, err error) { // fill ID and time if empty if comment.ID == "" { comment.ID = uuid.New().String() @@ -41,7 +41,7 @@ func (s *DataStore) Create(comment R.Comment) (commentID string, err error) { } // SetPin pin/un-pin comment as special -func (s *DataStore) SetPin(locator R.Locator, commentID string, status bool) error { +func (s *DataStore) SetPin(locator store.Locator, commentID string, status bool) error { comment, err := s.Get(locator, commentID) if err != nil { return err @@ -51,7 +51,7 @@ func (s *DataStore) SetPin(locator R.Locator, commentID string, status bool) err } // Vote for comment by id and locator -func (s *DataStore) Vote(locator R.Locator, commentID string, userID string, val bool) (comment R.Comment, err error) { +func (s *DataStore) Vote(locator store.Locator, commentID string, userID string, val bool) (comment store.Comment, err error) { comment, err = s.Get(locator, commentID) if err != nil { @@ -99,7 +99,7 @@ type EditRequest struct { } // EditComment to edit text and update Edit info -func (s *DataStore) EditComment(locator R.Locator, commentID string, req EditRequest) (comment R.Comment, err error) { +func (s *DataStore) EditComment(locator store.Locator, commentID string, req EditRequest) (comment store.Comment, err error) { comment, err = s.Get(locator, commentID) if err != nil { return comment, err @@ -116,7 +116,7 @@ func (s *DataStore) EditComment(locator R.Locator, commentID string, req EditReq comment.Text = req.Text comment.Orig = req.Orig - comment.Edit = &R.Edit{ + comment.Edit = &store.Edit{ Timestamp: time.Now(), Summary: req.Summary, } @@ -127,18 +127,18 @@ func (s *DataStore) EditComment(locator R.Locator, commentID string, req EditReq } // Counts returns postID+count list for given comments -func (s *DataStore) Counts(siteID string, postIDs []string) ([]R.PostInfo, error) { - res := []R.PostInfo{} +func (s *DataStore) Counts(siteID string, postIDs []string) ([]store.PostInfo, error) { + res := []store.PostInfo{} for _, p := range postIDs { - if c, err := s.Count(R.Locator{SiteID: siteID, URL: p}); err == nil { - res = append(res, R.PostInfo{URL: p, Count: c}) + if c, err := s.Count(store.Locator{SiteID: siteID, URL: p}); err == nil { + res = append(res, store.PostInfo{URL: p, Count: c}) } } return res, nil } // ValidateComment checks if comment size below max and user fields set -func (s *DataStore) ValidateComment(c *R.Comment) error { +func (s *DataStore) ValidateComment(c *store.Comment) error { maxSize := s.MaxCommentSize if s.MaxCommentSize <= 0 { maxSize = defaultCommentMaxSize diff --git a/app/store/service/service_test.go b/app/store/service/service_test.go index bf0f2fad..66809dfa 100644 --- a/app/store/service/service_test.go +++ b/app/store/service/service_test.go @@ -11,7 +11,7 @@ import ( "github.com/pkg/errors" "github.com/stretchr/testify/assert" - R "github.com/umputun/remark/app/store" + "github.com/umputun/remark/app/store" "github.com/umputun/remark/app/store/engine" ) @@ -20,16 +20,16 @@ var testDb = "/tmp/test-remark.db" func TestService_CreateFromEmpty(t *testing.T) { defer os.Remove(testDb) b := DataStore{Interface: prepStoreEngine(t), Secret: "secret 123"} - comment := R.Comment{ + comment := store.Comment{ Text: "text", - User: R.User{IP: "192.168.1.1", ID: "user", Name: "name"}, - Locator: R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, + User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"}, + Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, } id, err := b.Create(comment) assert.NoError(t, err) assert.True(t, id != "", id) - res, err := b.Get(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, id) + res, err := b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, id) assert.NoError(t, err) t.Logf("%+v", res) assert.Equal(t, "text", res.Text) @@ -43,18 +43,18 @@ func TestService_CreateFromEmpty(t *testing.T) { func TestService_CreateFromPartial(t *testing.T) { defer os.Remove(testDb) b := DataStore{Interface: prepStoreEngine(t), Secret: "secret 123"} - comment := R.Comment{ + comment := store.Comment{ Text: "text", Timestamp: time.Date(2018, 3, 25, 16, 34, 33, 0, time.UTC), Votes: map[string]bool{"u1": true, "u2": false}, - User: R.User{IP: "192.168.1.1", ID: "user", Name: "name"}, - Locator: R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, + User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"}, + Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, } id, err := b.Create(comment) assert.NoError(t, err) assert.True(t, id != "", id) - res, err := b.Get(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, id) + res, err := b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, id) assert.NoError(t, err) t.Logf("%+v", res) assert.Equal(t, "text", res.Text) @@ -69,10 +69,10 @@ func TestService_Vote(t *testing.T) { defer os.Remove(testDb) b := DataStore{Interface: prepStoreEngine(t)} - comment := R.Comment{ + comment := store.Comment{ Text: "text", - User: R.User{IP: "192.168.1.1", ID: "user", Name: "name"}, - Locator: R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, + User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"}, + Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, } _, err := b.Create(comment) assert.NoError(t, err) @@ -84,15 +84,15 @@ func TestService_Vote(t *testing.T) { assert.Equal(t, 0, res[0].Score) assert.Equal(t, map[string]bool{}, res[0].Votes, "no votes initially") - c, err := b.Vote(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", true) + c, err := b.Vote(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", true) assert.Nil(t, err) assert.Equal(t, 1, c.Score) assert.Equal(t, map[string]bool{"user1": true}, c.Votes, "user voted +") - c, err = b.Vote(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user", true) + c, err = b.Vote(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user", true) assert.NotNil(t, err, "self-voting not allowed") - _, err = b.Vote(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", true) + _, err = b.Vote(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", true) assert.NotNil(t, err, "double-voting rejected") assert.True(t, strings.HasPrefix(err.Error(), "user user1 already voted")) @@ -101,7 +101,7 @@ func TestService_Vote(t *testing.T) { assert.Equal(t, 3, len(res)) assert.Equal(t, 1, res[0].Score) - _, err = b.Vote(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", false) + _, err = b.Vote(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "user1", false) assert.Nil(t, err, "vote reset") res, err = b.Last("radio-t", 0) assert.Nil(t, err) @@ -120,16 +120,16 @@ func TestService_Pin(t *testing.T) { assert.Equal(t, 2, len(res)) assert.Equal(t, false, res[0].Pin) - err = b.SetPin(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, true) + err = b.SetPin(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, true) assert.Nil(t, err) - c, err := b.Get(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID) + c, err := b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID) assert.Nil(t, err) assert.Equal(t, true, c.Pin) - err = b.SetPin(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, false) + err = b.SetPin(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, false) assert.Nil(t, err) - c, err = b.Get(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID) + c, err = b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID) assert.Nil(t, err) assert.Equal(t, false, c.Pin) } @@ -144,19 +144,19 @@ func TestService_EditComment(t *testing.T) { assert.Equal(t, 2, len(res)) assert.Nil(t, res[0].Edit) - comment, err := b.EditComment(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, + comment, err := b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit"}) assert.Nil(t, err) assert.Equal(t, "my edit", comment.Edit.Summary) assert.Equal(t, "xxx", comment.Text) assert.Equal(t, "yyy", comment.Orig) - c, err := b.Get(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID) + c, err := b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID) assert.Nil(t, err) assert.Equal(t, "my edit", c.Edit.Summary) assert.Equal(t, "xxx", c.Text) - _, err = b.EditComment(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, + _, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit"}) assert.NotNil(t, err, "allow edit once") } @@ -173,7 +173,7 @@ func TestService_EditCommentDurationFailed(t *testing.T) { time.Sleep(time.Second) - _, err = b.EditComment(R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, + _, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit"}) assert.NotNil(t, err) } @@ -184,13 +184,13 @@ func TestService_ValidateComment(t *testing.T) { longText := fmt.Sprintf("%4000s", "X") tbl := []struct { - inp R.Comment + inp store.Comment err error }{ - {inp: R.Comment{}, err: errors.New("empty comment text")}, - {inp: R.Comment{Orig: "something blah", User: R.User{ID: "myid", Name: "name"}}, err: nil}, - {inp: R.Comment{Orig: "something blah", User: R.User{ID: "myid"}}, err: errors.New("empty user info")}, - {inp: R.Comment{Orig: longText, User: R.User{ID: "myid", Name: "name"}}, err: errors.New("comment text exceeded max allowed size 2000 (4000)")}, + {inp: store.Comment{}, err: errors.New("empty comment text")}, + {inp: store.Comment{Orig: "something blah", User: store.User{ID: "myid", Name: "name"}}, err: nil}, + {inp: store.Comment{Orig: "something blah", User: store.User{ID: "myid"}}, err: errors.New("empty user info")}, + {inp: store.Comment{Orig: longText, User: store.User{ID: "myid", Name: "name"}}, err: errors.New("comment text exceeded max allowed size 2000 (4000)")}, } for n, tt := range tbl { @@ -208,12 +208,12 @@ func TestService_Counts(t *testing.T) { b := prepStoreEngine(t) // two comments for https://radio-t.com // add one more for https://radio-t.com/2 - comment := R.Comment{ + comment := store.Comment{ ID: "123456", Text: `some text, link`, Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.Local), - Locator: R.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"}, - User: R.User{ID: "user1", Name: "user name"}, + Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"}, + User: store.User{ID: "user1", Name: "user name"}, } _, err := b.Create(comment) assert.Nil(t, err) @@ -221,11 +221,11 @@ func TestService_Counts(t *testing.T) { svc := DataStore{Interface: b} res, err := svc.Counts("radio-t", []string{"https://radio-t.com/2"}) assert.Nil(t, err) - assert.Equal(t, []R.PostInfo{{URL: "https://radio-t.com/2", Count: 1}}, res) + assert.Equal(t, []store.PostInfo{{URL: "https://radio-t.com/2", Count: 1}}, res) res, err = svc.Counts("radio-t", []string{"https://radio-t.com", "https://radio-t.com/2", "blah"}) assert.Nil(t, err) - assert.Equal(t, []R.PostInfo{ + assert.Equal(t, []store.PostInfo{ {URL: "https://radio-t.com", Count: 2}, {URL: "https://radio-t.com/2", Count: 1}, {URL: "blah", Count: 0}, @@ -240,22 +240,22 @@ func prepStoreEngine(t *testing.T) engine.Interface { assert.Nil(t, err) b := boltStore - comment := R.Comment{ + comment := store.Comment{ ID: "id-1", Text: `some text, link`, Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.Local), - Locator: R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, - User: R.User{ID: "user1", Name: "user name"}, + Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, + User: store.User{ID: "user1", Name: "user name"}, } _, err = b.Create(comment) assert.Nil(t, err) - comment = R.Comment{ + comment = store.Comment{ ID: "id-2", Text: "some text2", Timestamp: time.Date(2017, 12, 20, 15, 18, 23, 0, time.Local), - Locator: R.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, - User: R.User{ID: "user1", Name: "user name"}, + Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, + User: store.User{ID: "user1", Name: "user name"}, } _, err = b.Create(comment) assert.Nil(t, err)