From 3546b177c29d9b6d6360fdcef3046c7b3e6bfd50 Mon Sep 17 00:00:00 2001 From: Wibowo Arindrarto Date: Fri, 19 Jan 2024 06:09:47 +0100 Subject: [PATCH] feat(datastore): Add argument to filter on read status in PullFeeds --- cmd/feed_pull.go | 3 ++- internal/datastore/datastore.go | 1 + internal/datastore/sqlite_pull_feeds.go | 9 +++++---- internal/datastore/sqlite_pull_feeds_test.go | 10 +++++----- internal/server/datastore_mock_test.go | 8 ++++---- internal/server/service.go | 4 +++- internal/server/service_test.go | 8 ++++---- 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/cmd/feed_pull.go b/cmd/feed_pull.go index 81b7ff8..2a7652b 100644 --- a/cmd/feed_pull.go +++ b/cmd/feed_pull.go @@ -45,11 +45,12 @@ func newFeedPullCommand() *cobra.Command { return err } + entriesReadStatus := false var ( errs []error n int s = newPullSpinner(rawIDs) - ch = db.PullFeeds(cmd.Context(), ids) + ch = db.PullFeeds(cmd.Context(), ids, &entriesReadStatus) ) s.Start() diff --git a/internal/datastore/datastore.go b/internal/datastore/datastore.go index 4755f25..97eeaa7 100644 --- a/internal/datastore/datastore.go +++ b/internal/datastore/datastore.go @@ -52,6 +52,7 @@ type Datastore interface { PullFeeds( ctx context.Context, ids []entity.ID, + isRead *bool, ) ( results <-chan entity.PullResult, ) diff --git a/internal/datastore/sqlite_pull_feeds.go b/internal/datastore/sqlite_pull_feeds.go index 7380d7e..cd1d430 100644 --- a/internal/datastore/sqlite_pull_feeds.go +++ b/internal/datastore/sqlite_pull_feeds.go @@ -16,6 +16,7 @@ import ( func (db *SQLite) PullFeeds( ctx context.Context, ids []entity.ID, + entryReadStatus *bool, ) <-chan entity.PullResult { var ( @@ -48,7 +49,7 @@ func (db *SQLite) PullFeeds( chs := make([]<-chan entity.PullResult, len(pks)) for i, pk := range pks { - chs[i] = pullNewFeedEntries(ctx, tx, pk, db.parser) + chs[i] = pullFeedEntries(ctx, tx, pk, db.parser, entryReadStatus) } for pr := range merge(chs) { @@ -155,11 +156,12 @@ func getAllPullKeys(ctx context.Context, tx *sql.Tx) ([]pullKey, error) { return pks, nil } -func pullNewFeedEntries( +func pullFeedEntries( ctx context.Context, tx *sql.Tx, pk pullKey, parser Parser, + entryReadStatus *bool, ) chan entity.PullResult { pullTime := time.Now().UTC() @@ -186,8 +188,7 @@ func pullNewFeedEntries( return pk.err(err) } - defaultReadStatus := false - unreadEntries, err := getEntries(ctx, tx, []ID{pk.feedID}, &defaultReadStatus, nil) + unreadEntries, err := getEntries(ctx, tx, []ID{pk.feedID}, entryReadStatus, nil) if err != nil { return pk.err(err) } diff --git a/internal/datastore/sqlite_pull_feeds_test.go b/internal/datastore/sqlite_pull_feeds_test.go index ffe8e4c..f105082 100644 --- a/internal/datastore/sqlite_pull_feeds_test.go +++ b/internal/datastore/sqlite_pull_feeds_test.go @@ -30,7 +30,7 @@ func TestPullFeedsAllOkEmptyDB(t *testing.T) { ParseURLWithContext(gomock.Any(), gomock.Any()). MaxTimes(0) - c := db.PullFeeds(context.Background(), nil) + c := db.PullFeeds(context.Background(), nil, nil) a.Empty(c) } @@ -69,7 +69,7 @@ func TestPullFeedsAllOkEmptyEntries(t *testing.T) { MaxTimes(1). Return(toGFeed(t, dbFeeds[1]), nil) - c := db.PullFeeds(context.Background(), nil) + c := db.PullFeeds(context.Background(), nil, nil) got := make([]entity.PullResult, 0) for res := range c { @@ -183,7 +183,7 @@ func TestPullFeedsAllOkNoNewEntries(t *testing.T) { MaxTimes(1). Return(toGFeed(t, pulledFeeds[1]), nil) - c := db.PullFeeds(context.Background(), nil) + c := db.PullFeeds(context.Background(), nil, pointer(false)) got := make([]entity.PullResult, 0) for res := range c { @@ -324,7 +324,7 @@ func TestPullFeedsAllOkSomeNewEntries(t *testing.T) { MaxTimes(1). Return(toGFeed(t, pulledFeeds[1]), nil) - c := db.PullFeeds(context.Background(), nil) + c := db.PullFeeds(context.Background(), nil, pointer(false)) got := make([]entity.PullResult, 0) for res := range c { @@ -492,7 +492,7 @@ func TestPullFeedsSelectedOkSomeNewEntries(t *testing.T) { MaxTimes(1). Return(toGFeed(t, pulledFeed), nil) - c := db.PullFeeds(context.Background(), []ID{keys[pulledFeed.title].ID}) + c := db.PullFeeds(context.Background(), []ID{keys[pulledFeed.title].ID}, pointer(false)) got := make([]entity.PullResult, 0) for res := range c { diff --git a/internal/server/datastore_mock_test.go b/internal/server/datastore_mock_test.go index 25c79a7..52e351b 100644 --- a/internal/server/datastore_mock_test.go +++ b/internal/server/datastore_mock_test.go @@ -193,17 +193,17 @@ func (mr *MockDatastoreMockRecorder) ListFeeds(ctx any) *gomock.Call { } // PullFeeds mocks base method. -func (m *MockDatastore) PullFeeds(ctx context.Context, ids []entity.ID) <-chan entity.PullResult { +func (m *MockDatastore) PullFeeds(ctx context.Context, ids []entity.ID, isRead *bool) <-chan entity.PullResult { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "PullFeeds", ctx, ids) + ret := m.ctrl.Call(m, "PullFeeds", ctx, ids, isRead) ret0, _ := ret[0].(<-chan entity.PullResult) return ret0 } // PullFeeds indicates an expected call of PullFeeds. -func (mr *MockDatastoreMockRecorder) PullFeeds(ctx, ids any) *gomock.Call { +func (mr *MockDatastoreMockRecorder) PullFeeds(ctx, ids, isRead any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PullFeeds", reflect.TypeOf((*MockDatastore)(nil).PullFeeds), ctx, ids) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PullFeeds", reflect.TypeOf((*MockDatastore)(nil).PullFeeds), ctx, ids, isRead) } // MockeditableTable is a mock of editableTable interface. diff --git a/internal/server/service.go b/internal/server/service.go index 82d2927..1e7b156 100644 --- a/internal/server/service.go +++ b/internal/server/service.go @@ -126,7 +126,9 @@ func (svc *service) PullFeeds( ids[i] = id } - ch := svc.ds.PullFeeds(stream.Context(), ids) + // TODO: Expose isRead in proto. + isRead := false + ch := svc.ds.PullFeeds(stream.Context(), ids, &isRead) for pr := range ch { payload, err := convert(pr) diff --git a/internal/server/service_test.go b/internal/server/service_test.go index ece071c..85924f4 100644 --- a/internal/server/service_test.go +++ b/internal/server/service_test.go @@ -273,7 +273,7 @@ func TestPullFeedsAllOk(t *testing.T) { }() ds.EXPECT(). - PullFeeds(gomock.Any(), []entity.ID{}). + PullFeeds(gomock.Any(), []entity.ID{}, gomock.Any()). Return(ch) req := api.PullFeedsRequest{} @@ -354,7 +354,7 @@ func TestPullFeedsSelectedAllOk(t *testing.T) { }() ds.EXPECT(). - PullFeeds(gomock.Any(), []entity.ID{2, 3}). + PullFeeds(gomock.Any(), []entity.ID{2, 3}, gomock.Any()). Return(ch) req := api.PullFeedsRequest{FeedIds: []uint32{2, 3}} @@ -444,7 +444,7 @@ func TestPullFeedsErrSomeFeed(t *testing.T) { }() ds.EXPECT(). - PullFeeds(gomock.Any(), []entity.ID{}). + PullFeeds(gomock.Any(), []entity.ID{}, gomock.Any()). Return(ch) req := api.PullFeedsRequest{} @@ -542,7 +542,7 @@ func TestPullFeedsErrNonFeed(t *testing.T) { }() ds.EXPECT(). - PullFeeds(gomock.Any(), []entity.ID{}). + PullFeeds(gomock.Any(), []entity.ID{}, gomock.Any()). Return(ch) req := api.PullFeedsRequest{}