Skip to content

Commit

Permalink
chore: Added a sort order parameter to activities handler
Browse files Browse the repository at this point in the history
Paging with descending sort order currently does not work with MongoDB (issue trustbloc#893) so sorting of activities is temporarily (maybe permanently) set to ascending.

Signed-off-by: Bob Stasyszyn <[email protected]>
  • Loading branch information
bstasyszyn committed Nov 26, 2021
1 parent e274a57 commit d20e54d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 66 deletions.
12 changes: 6 additions & 6 deletions cmd/orb-server/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,15 +865,15 @@ func startOrbServices(parameters *orbParameters) error {
aphandler.NewPublicKeys(apEndpointCfg, apStore, publicKey),
aphandler.NewFollowers(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewFollowing(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewOutbox(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewInbox(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewOutbox(apEndpointCfg, apStore, apSigVerifier, activitypubspi.SortAscending),
aphandler.NewInbox(apEndpointCfg, apStore, apSigVerifier, activitypubspi.SortAscending),
aphandler.NewWitnesses(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewWitnessing(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewLiked(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewLikes(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewShares(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewLiked(apEndpointCfg, apStore, apSigVerifier, activitypubspi.SortAscending),
aphandler.NewLikes(apEndpointCfg, apStore, apSigVerifier, activitypubspi.SortAscending),
aphandler.NewShares(apEndpointCfg, apStore, apSigVerifier, activitypubspi.SortAscending),
aphandler.NewPostOutbox(apEndpointCfg, activityPubService.Outbox(), apStore, apSigVerifier),
aphandler.NewActivity(apEndpointCfg, apStore, apSigVerifier),
aphandler.NewActivity(apEndpointCfg, apStore, apSigVerifier, activitypubspi.SortAscending),
webcas.New(apEndpointCfg, apStore, apSigVerifier, coreCASClient),
auth.NewHandlerWrapper(authCfg, policyhandler.New(configStore)),
auth.NewHandlerWrapper(authCfg, nodeinfo.NewHandler(nodeinfo.V2_0, nodeInfoService, nodeInfoLogger)),
Expand Down
34 changes: 17 additions & 17 deletions pkg/activitypub/resthandler/activityhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,51 @@ import (
)

// NewActivity returns a new 'activities/{id}' REST handler that retrieves a single activity by ID.
func NewActivity(cfg *Config, activityStore spi.Store, verifier signatureVerifier) *Activity {
func NewActivity(cfg *Config, activityStore spi.Store, verifier signatureVerifier, sortOrder spi.SortOrder) *Activity {
h := &Activity{}

h.handler = newHandler(ActivitiesPath, cfg, activityStore, h.handle, verifier)
h.handler = newHandler(ActivitiesPath, cfg, activityStore, h.handle, verifier, sortOrder)

return h
}

// NewOutbox returns a new 'outbox' REST handler that retrieves a service's outbox.
func NewOutbox(cfg *Config, activityStore spi.Store, verifier signatureVerifier) *ReadOutbox {
func NewOutbox(cfg *Config, activityStore spi.Store, verifier signatureVerifier, sortOrder spi.SortOrder) *ReadOutbox {
h := &ReadOutbox{
Activities: &Activities{
getID: getID("outbox"),
getObjectIRI: getObjectIRI(cfg.ObjectIRI),
},
}

h.Activities.handler = newHandler(OutboxPath, cfg, activityStore, h.handleOutbox, verifier)
h.Activities.handler = newHandler(OutboxPath, cfg, activityStore, h.handleOutbox, verifier, sortOrder)

return h
}

// NewInbox returns a new 'inbox' REST handler that retrieves a service's inbox.
func NewInbox(cfg *Config, activityStore spi.Store, verifier signatureVerifier) *Activities {
func NewInbox(cfg *Config, activityStore spi.Store, verifier signatureVerifier, sortOrder spi.SortOrder) *Activities {
return NewActivities(InboxPath, spi.Inbox, cfg, activityStore,
getObjectIRI(cfg.ObjectIRI), getID("inbox"), verifier)
getObjectIRI(cfg.ObjectIRI), getID("inbox"), verifier, sortOrder)
}

// NewShares returns a new 'shares' REST handler that retrieves an object's 'Announce' activities.
func NewShares(cfg *Config, activityStore spi.Store, verifier signatureVerifier) *Activities {
func NewShares(cfg *Config, activityStore spi.Store, verifier signatureVerifier, sortOrder spi.SortOrder) *Activities {
return NewActivities(SharesPath, spi.Share, cfg, activityStore,
getObjectIRIFromIDParam, getIDFromParam(cfg.ObjectIRI, SharesPath), verifier)
getObjectIRIFromIDParam, getIDFromParam(cfg.ObjectIRI, SharesPath), verifier, sortOrder)
}

// NewLikes returns a new 'likes' REST handler that retrieves an object's 'Like' activities.
func NewLikes(cfg *Config, activityStore spi.Store, verifier signatureVerifier) *Activities {
func NewLikes(cfg *Config, activityStore spi.Store, verifier signatureVerifier, sortOrder spi.SortOrder) *Activities {
return NewActivities(LikesPath, spi.Like, cfg, activityStore,
getObjectIRIFromIDParam, getIDFromParam(cfg.ObjectIRI, LikesPath), verifier)
getObjectIRIFromIDParam, getIDFromParam(cfg.ObjectIRI, LikesPath), verifier, sortOrder)
}

// NewLiked returns a new 'liked' REST handler that retrieves a service's 'Like' activities, i.e. the Like
// activities that were posted by the given service.
func NewLiked(cfg *Config, activityStore spi.Store, verifier signatureVerifier) *Activities {
func NewLiked(cfg *Config, activityStore spi.Store, verifier signatureVerifier, sortOrder spi.SortOrder) *Activities {
return NewActivities(LikedPath, spi.Liked, cfg, activityStore,
getObjectIRI(cfg.ObjectIRI), getID("liked"), verifier)
getObjectIRI(cfg.ObjectIRI), getID("liked"), verifier, sortOrder)
}

type getIDFunc func(objectIRI *url.URL, req *http.Request) (*url.URL, error)
Expand All @@ -81,14 +81,14 @@ type Activities struct {

// NewActivities returns a new activities REST handler.
func NewActivities(path string, refType spi.ReferenceType, cfg *Config, activityStore spi.Store,
getObjectIRI getObjectIRIFunc, getID getIDFunc, verifier signatureVerifier) *Activities {
getObjectIRI getObjectIRIFunc, getID getIDFunc, verifier signatureVerifier, sortOrder spi.SortOrder) *Activities {
h := &Activities{
refType: refType,
getID: getID,
getObjectIRI: getObjectIRI,
}

h.handler = newHandler(path, cfg, activityStore, h.handle, verifier)
h.handler = newHandler(path, cfg, activityStore, h.handle, verifier, sortOrder)

return h
}
Expand Down Expand Up @@ -169,12 +169,12 @@ func (h *Activities) handleActivitiesPage(rw http.ResponseWriter, req *http.Requ
page, err = h.getPage(objectIRI, id, refType,
spi.WithPageSize(h.PageSize),
spi.WithPageNum(pageNum),
spi.WithSortOrder(spi.SortDescending),
spi.WithSortOrder(h.sortOrder),
)
} else {
page, err = h.getPage(objectIRI, id, refType,
spi.WithPageSize(h.PageSize),
spi.WithSortOrder(spi.SortDescending),
spi.WithSortOrder(h.sortOrder),
)
}

Expand Down Expand Up @@ -228,7 +228,7 @@ func (h *Activities) getActivities(objectIRI, id *url.URL,
return nil, fmt.Errorf("failed to get total items from reference query: %w", err)
}

lastURL, err := h.getPageURL(id, getLastPageNum(totalItems, h.PageSize, spi.SortDescending))
lastURL, err := h.getPageURL(id, getLastPageNum(totalItems, h.PageSize, h.sortOrder))
if err != nil {
return nil, err
}
Expand Down
58 changes: 29 additions & 29 deletions pkg/activitypub/resthandler/activityhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestNewOutbox(t *testing.T) {
PageSize: 4,
}

h := NewOutbox(cfg, memstore.New(""), &mocks.SignatureVerifier{})
h := NewOutbox(cfg, memstore.New(""), &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)
require.Equal(t, "/services/orb/outbox", h.Path())
require.Equal(t, http.MethodGet, h.Method())
Expand All @@ -66,7 +66,7 @@ func TestNewInbox(t *testing.T) {
PageSize: 4,
}

h := NewInbox(cfg, memstore.New(""), &mocks.SignatureVerifier{})
h := NewInbox(cfg, memstore.New(""), &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)
require.Equal(t, "/services/orb/inbox", h.Path())
require.Equal(t, http.MethodGet, h.Method())
Expand All @@ -91,7 +91,7 @@ func TestNewShares(t *testing.T) {
ObjectIRI: serviceIRI,
}

h := NewShares(cfg, memstore.New(""), &mocks.SignatureVerifier{})
h := NewShares(cfg, memstore.New(""), &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)
require.Equal(t, "/services/orb/shares", h.Path())
require.Equal(t, http.MethodGet, h.Method())
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestNewLikes(t *testing.T) {
ObjectIRI: serviceIRI,
}

h := NewLikes(cfg, memstore.New(""), &mocks.SignatureVerifier{})
h := NewLikes(cfg, memstore.New(""), &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)
require.Equal(t, "/services/orb/likes", h.Path())
require.Equal(t, http.MethodGet, h.Method())
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestActivities_Handler(t *testing.T) {
verifier.VerifyRequestReturns(true, service2IRI, nil)

t.Run("Success", func(t *testing.T) {
h := NewInbox(cfg, activityStore, verifier)
h := NewInbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -229,7 +229,7 @@ func TestActivities_Handler(t *testing.T) {
s := &mocks.ActivityStore{}
s.QueryReferencesReturns(nil, errExpected)

h := NewInbox(cfg, s, verifier)
h := NewInbox(cfg, s, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -243,7 +243,7 @@ func TestActivities_Handler(t *testing.T) {
})

t.Run("Marshal error", func(t *testing.T) {
h := NewInbox(cfg, activityStore, verifier)
h := NewInbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

errExpected := fmt.Errorf("injected marshal error")
Expand All @@ -263,7 +263,7 @@ func TestActivities_Handler(t *testing.T) {
})

t.Run("GetObjectIRI error", func(t *testing.T) {
h := NewInbox(cfg, activityStore, verifier)
h := NewInbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

t.Run("Internal error", func(t *testing.T) {
Expand Down Expand Up @@ -302,7 +302,7 @@ func TestActivities_Handler(t *testing.T) {
})

t.Run("GetID error", func(t *testing.T) {
h := NewInbox(cfg, activityStore, verifier)
h := NewInbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

errExpected := fmt.Errorf("injected error")
Expand All @@ -327,7 +327,7 @@ func TestActivities_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(false, nil, errExpected)

h := NewInbox(cfg, activityStore, verifier)
h := NewInbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -344,7 +344,7 @@ func TestActivities_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(false, nil, nil)

h := NewInbox(cfg, activityStore, verifier)
h := NewInbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand Down Expand Up @@ -404,7 +404,7 @@ func TestActivities_PageHandler(t *testing.T) {
PageSize: 4,
}

h := NewOutbox(cfg, s, verifier)
h := NewOutbox(cfg, s, verifier, spi.SortDescending)
require.NotNil(t, h)

restorePaging := setPaging(h.handler, "true", "0")
Expand All @@ -426,7 +426,7 @@ func TestActivities_PageHandler(t *testing.T) {
PageSize: 4,
}

h := NewOutbox(cfg, activityStore, verifier)
h := NewOutbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

restorePaging := setPaging(h.handler, "true", "0")
Expand Down Expand Up @@ -493,7 +493,7 @@ func TestReadOutbox_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(true, service2IRI, nil)

h := NewOutbox(cfg, activityStore, verifier)
h := NewOutbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -517,7 +517,7 @@ func TestReadOutbox_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(false, nil, nil)

h := NewOutbox(cfg, activityStore, verifier)
h := NewOutbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -541,7 +541,7 @@ func TestReadOutbox_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(false, nil, errors.New("injected auth error"))

h := NewOutbox(cfg, activityStore, verifier)
h := NewOutbox(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand Down Expand Up @@ -583,7 +583,7 @@ func TestShares_Handler(t *testing.T) {
verifier.VerifyRequestReturns(true, srvcIRI, nil)

t.Run("Success", func(t *testing.T) {
h := NewShares(cfg, activityStore, verifier)
h := NewShares(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

restore := setIDParam(id)
Expand Down Expand Up @@ -635,7 +635,7 @@ func TestShares_PageHandler(t *testing.T) {
verifier.VerifyRequestReturns(true, srvcIRI, nil)

t.Run("First page -> Success", func(t *testing.T) {
h := NewShares(cfg, activityStore, verifier)
h := NewShares(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

restorePaging := setPaging(h.handler, "true", "")
Expand All @@ -662,7 +662,7 @@ func TestShares_PageHandler(t *testing.T) {
})

t.Run("By page -> Success", func(t *testing.T) {
h := NewShares(cfg, activityStore, verifier)
h := NewShares(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

restorePaging := setPaging(h.handler, "true", "1")
Expand Down Expand Up @@ -710,7 +710,7 @@ func TestLiked_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(true, serviceIRI, nil)

h := NewLiked(cfg, activityStore, verifier)
h := NewLiked(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

t.Run("Main page -> Success", func(t *testing.T) {
Expand All @@ -723,7 +723,7 @@ func TestLiked_Handler(t *testing.T) {
}

func TestNewActivity(t *testing.T) {
h := NewActivity(&Config{BasePath: basePath}, memstore.New(""), &mocks.SignatureVerifier{})
h := NewActivity(&Config{BasePath: basePath}, memstore.New(""), &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)
require.Equal(t, basePath+ActivitiesPath, h.Path())
require.Equal(t, http.MethodGet, h.Method())
Expand Down Expand Up @@ -751,7 +751,7 @@ func TestActivity_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(true, nil, nil)

h := NewActivity(cfg, activityStore, verifier)
h := NewActivity(cfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -775,7 +775,7 @@ func TestActivity_Handler(t *testing.T) {
})

t.Run("No activity ID -> BadRequest", func(t *testing.T) {
h := NewActivity(cfg, activityStore, &mocks.SignatureVerifier{})
h := NewActivity(cfg, activityStore, &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -789,7 +789,7 @@ func TestActivity_Handler(t *testing.T) {
})

t.Run("Activity ID not found -> NotFound", func(t *testing.T) {
h := NewActivity(cfg, activityStore, &mocks.SignatureVerifier{})
h := NewActivity(cfg, activityStore, &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -809,7 +809,7 @@ func TestActivity_Handler(t *testing.T) {
as := &mocks.ActivityStore{}
as.GetActivityReturns(nil, errors.New("injected store error"))

h := NewActivity(cfg, as, &mocks.SignatureVerifier{})
h := NewActivity(cfg, as, &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand All @@ -826,7 +826,7 @@ func TestActivity_Handler(t *testing.T) {
})

t.Run("Marshal error", func(t *testing.T) {
h := NewActivity(cfg, activityStore, &mocks.SignatureVerifier{})
h := NewActivity(cfg, activityStore, &mocks.SignatureVerifier{}, spi.SortDescending)
require.NotNil(t, h)

errExpected := fmt.Errorf("injected marshal error")
Expand Down Expand Up @@ -869,7 +869,7 @@ func TestActivity_Handler(t *testing.T) {
},
}

h := NewActivity(cnfg, activityStore, verifier)
h := NewActivity(cnfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

t.Run("Non-public activity -> unauthorized", func(t *testing.T) {
Expand Down Expand Up @@ -913,7 +913,7 @@ func TestActivity_Handler(t *testing.T) {
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(false, nil, errExpected)

h := NewActivity(cnfg, activityStore, verifier)
h := NewActivity(cnfg, activityStore, verifier, spi.SortDescending)
require.NotNil(t, h)

rw := httptest.NewRecorder()
Expand Down Expand Up @@ -979,7 +979,7 @@ func handleActivitiesRequest(t *testing.T, serviceIRI *url.URL, as spi.Store, pa
verifier := &mocks.SignatureVerifier{}
verifier.VerifyRequestReturns(true, serviceIRI, nil)

h := NewInbox(cfg, as, verifier)
h := NewInbox(cfg, as, verifier, spi.SortDescending)
require.NotNil(t, h)

restorePaging := setPaging(h.handler, page, pageNum)
Expand Down
Loading

0 comments on commit d20e54d

Please sign in to comment.