Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ac8d01

Browse files
committed
refactor: Move common code away from activitypub package
The following packages have been moved from the activitypub package so that they may be reused by non AativityPub code: - errors - wmlogger - lifecycle - amqp - mempubsub - redeliver closes #460 Signed-off-by: Bob Stasyszyn <[email protected]>
1 parent d402e1c commit 0ac8d01

34 files changed

Lines changed: 173 additions & 148 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ test/bdd/fixtures/keys
3232
test/bdd/fixtures/data
3333

3434
/coverage.txt
35+
/cmd/orb-cli/ipfskeygencmd/k1.key

cmd/orb-server/startcmd/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import (
5858
"github.com/trustbloc/orb/pkg/activitypub/httpsig"
5959
aphandler "github.com/trustbloc/orb/pkg/activitypub/resthandler"
6060
apservice "github.com/trustbloc/orb/pkg/activitypub/service"
61-
"github.com/trustbloc/orb/pkg/activitypub/service/amqp"
6261
"github.com/trustbloc/orb/pkg/activitypub/service/monitoring"
6362
apspi "github.com/trustbloc/orb/pkg/activitypub/service/spi"
6463
"github.com/trustbloc/orb/pkg/activitypub/service/vct"
@@ -89,6 +88,7 @@ import (
8988
"github.com/trustbloc/orb/pkg/httpserver/auth"
9089
"github.com/trustbloc/orb/pkg/observer"
9190
"github.com/trustbloc/orb/pkg/protocolversion/factoryregistry"
91+
"github.com/trustbloc/orb/pkg/pubsub/amqp"
9292
"github.com/trustbloc/orb/pkg/resolver/document"
9393
casstore "github.com/trustbloc/orb/pkg/store/cas"
9494
didanchorstore "github.com/trustbloc/orb/pkg/store/didanchor"

pkg/activitypub/service/activityhandler/activityhandler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919

2020
"github.com/trustbloc/orb/pkg/activitypub/client"
2121
"github.com/trustbloc/orb/pkg/activitypub/client/transport"
22-
aperrors "github.com/trustbloc/orb/pkg/activitypub/errors"
23-
"github.com/trustbloc/orb/pkg/activitypub/service/lifecycle"
2422
service "github.com/trustbloc/orb/pkg/activitypub/service/spi"
2523
store "github.com/trustbloc/orb/pkg/activitypub/store/spi"
2624
"github.com/trustbloc/orb/pkg/activitypub/vocab"
25+
orberrors "github.com/trustbloc/orb/pkg/errors"
26+
"github.com/trustbloc/orb/pkg/lifecycle"
2727
)
2828

2929
var logger = log.New("activitypub_service")
@@ -138,7 +138,7 @@ func (h *handler) handleUndoActivity(undo *vocab.ActivityType) error {
138138
return e
139139
}
140140

141-
return aperrors.NewTransient(e)
141+
return orberrors.NewTransient(e)
142142
}
143143

144144
if activity.Actor().String() != undo.Actor().String() {
@@ -195,7 +195,7 @@ func (h *handler) resolveActor(iri *url.URL) (*vocab.ActorType, error) {
195195
}
196196

197197
if !errors.Is(err, store.ErrNotFound) {
198-
return nil, aperrors.NewTransient(err)
198+
return nil, orberrors.NewTransient(err)
199199
}
200200

201201
// The actor isn't in our local store. Retrieve the actor from the remote server.

pkg/activitypub/service/activityhandler/activityhandler_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/trustbloc/edge-core/pkg/log"
2020

2121
"github.com/trustbloc/orb/pkg/activitypub/client"
22-
aperrors "github.com/trustbloc/orb/pkg/activitypub/errors"
2322
apmocks "github.com/trustbloc/orb/pkg/activitypub/mocks"
2423
"github.com/trustbloc/orb/pkg/activitypub/service/mocks"
2524
"github.com/trustbloc/orb/pkg/activitypub/service/spi"
@@ -28,7 +27,9 @@ import (
2827
store "github.com/trustbloc/orb/pkg/activitypub/store/spi"
2928
"github.com/trustbloc/orb/pkg/activitypub/store/storeutil"
3029
"github.com/trustbloc/orb/pkg/activitypub/vocab"
30+
orberrors "github.com/trustbloc/orb/pkg/errors"
3131
"github.com/trustbloc/orb/pkg/internal/testutil"
32+
"github.com/trustbloc/orb/pkg/lifecycle"
3233
)
3334

3435
const cid = "bafkrwihwsnuregfeqh263vgdathcprnbvatyat6h6mu7ipjhhodcdbyhoy"
@@ -48,15 +49,15 @@ func TestNewInbox(t *testing.T) {
4849
h := NewInbox(cfg, &mocks.ActivityStore{}, &mocks.Outbox{}, &apmocks.HTTPTransport{})
4950
require.NotNil(t, h)
5051

51-
require.Equal(t, spi.StateNotStarted, h.State())
52+
require.Equal(t, lifecycle.StateNotStarted, h.State())
5253

5354
h.Start()
5455

55-
require.Equal(t, spi.StateStarted, h.State())
56+
require.Equal(t, lifecycle.StateStarted, h.State())
5657

5758
h.Stop()
5859

59-
require.Equal(t, spi.StateStopped, h.State())
60+
require.Equal(t, lifecycle.StateStopped, h.State())
6061
}
6162

6263
func TestNewOutbox(t *testing.T) {
@@ -68,15 +69,15 @@ func TestNewOutbox(t *testing.T) {
6869
h := NewOutbox(cfg, &mocks.ActivityStore{}, &apmocks.HTTPTransport{})
6970
require.NotNil(t, h)
7071

71-
require.Equal(t, spi.StateNotStarted, h.State())
72+
require.Equal(t, lifecycle.StateNotStarted, h.State())
7273

7374
h.Start()
7475

75-
require.Equal(t, spi.StateStarted, h.State())
76+
require.Equal(t, lifecycle.StateStarted, h.State())
7677

7778
h.Stop()
7879

79-
require.Equal(t, spi.StateStopped, h.State())
80+
require.Equal(t, lifecycle.StateStopped, h.State())
8081
}
8182

8283
func TestNoOpProofHandler_HandleProof(t *testing.T) {
@@ -1977,10 +1978,10 @@ func TestHandler_HandleUndoFollowActivity(t *testing.T) {
19771978
)
19781979

19791980
err := inboxHandler.HandleActivity(undo)
1980-
require.True(t, aperrors.IsTransient(err))
1981+
require.True(t, orberrors.IsTransient(err))
19811982

19821983
_, err = inboxHandler.resolveActor(service1IRI)
1983-
require.True(t, aperrors.IsTransient(err))
1984+
require.True(t, orberrors.IsTransient(err))
19841985

19851986
obj, err := vocab.NewObjectWithDocument(vocab.MustUnmarshalToDoc([]byte(anchorCredential1)))
19861987
if err != nil {
@@ -1994,10 +1995,10 @@ func TestHandler_HandleUndoFollowActivity(t *testing.T) {
19941995
)
19951996

19961997
err = inboxHandler.announceAnchorCredential(create)
1997-
require.True(t, aperrors.IsTransient(err))
1998+
require.True(t, orberrors.IsTransient(err))
19981999

19992000
err = inboxHandler.announceAnchorCredentialRef(create)
2000-
require.True(t, aperrors.IsTransient(err))
2001+
require.True(t, orberrors.IsTransient(err))
20012002
})
20022003

20032004
t.Run("Inbox Undo Follow", func(t *testing.T) {

pkg/activitypub/service/activityhandler/inboxhandler.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"net/url"
1414
"time"
1515

16-
aperrors "github.com/trustbloc/orb/pkg/activitypub/errors"
1716
"github.com/trustbloc/orb/pkg/activitypub/resthandler"
1817
service "github.com/trustbloc/orb/pkg/activitypub/service/spi"
1918
store "github.com/trustbloc/orb/pkg/activitypub/store/spi"
2019
"github.com/trustbloc/orb/pkg/activitypub/store/storeutil"
2120
"github.com/trustbloc/orb/pkg/activitypub/vocab"
21+
orberrors "github.com/trustbloc/orb/pkg/errors"
2222
)
2323

2424
var errDuplicateAnchorCredential = errors.New("anchor credential already handled")
@@ -203,7 +203,7 @@ func (h *Inbox) validateActivity(activity *vocab.ActivityType) error {
203203

204204
func (h *Inbox) acceptActor(activity *vocab.ActivityType, actor *vocab.ActorType, refType store.ReferenceType) error {
205205
if err := h.store.AddReference(refType, h.ServiceIRI, actor.ID().URL()); err != nil {
206-
return aperrors.NewTransient(fmt.Errorf("unable to store reference: %w", err))
206+
return orberrors.NewTransient(fmt.Errorf("unable to store reference: %w", err))
207207
}
208208

209209
if err := h.store.PutActor(actor); err != nil {
@@ -263,7 +263,7 @@ func (h *Inbox) handleAccept(accept *vocab.ActivityType, refType store.Reference
263263

264264
err = h.store.AddReference(refType, h.ServiceIRI, accept.Actor())
265265
if err != nil {
266-
return aperrors.NewTransient(fmt.Errorf("handle accept '%s' activity %s: %w", refType, accept.ID(), err))
266+
return orberrors.NewTransient(fmt.Errorf("handle accept '%s' activity %s: %w", refType, accept.ID(), err))
267267
}
268268

269269
return nil
@@ -322,7 +322,7 @@ func (h *Inbox) postAccept(activity *vocab.ActivityType, toIRI *url.URL) error {
322322
logger.Debugf("[%s] Publishing 'Accept' activity to %s", h.ServiceName, toIRI)
323323

324324
if _, err := h.outbox.Post(acceptActivity); err != nil {
325-
return aperrors.NewTransient(fmt.Errorf("unable to reply with 'Accept' to %s: %w", toIRI, err))
325+
return orberrors.NewTransient(fmt.Errorf("unable to reply with 'Accept' to %s: %w", toIRI, err))
326326
}
327327

328328
return nil
@@ -337,7 +337,7 @@ func (h *Inbox) postReject(activity *vocab.ActivityType, toIRI *url.URL) error {
337337
logger.Debugf("[%s] Publishing 'Reject' activity to %s", h.ServiceName, toIRI)
338338

339339
if _, err := h.outbox.Post(reject); err != nil {
340-
return aperrors.NewTransient(fmt.Errorf("unable to reply with 'Accept' to %s: %w", toIRI, err))
340+
return orberrors.NewTransient(fmt.Errorf("unable to reply with 'Accept' to %s: %w", toIRI, err))
341341
}
342342

343343
return nil
@@ -351,7 +351,7 @@ func (h *Inbox) hasReference(objectIRI, refIRI *url.URL, refType store.Reference
351351
),
352352
)
353353
if err != nil {
354-
return false, aperrors.NewTransient(fmt.Errorf("query references: %w", err))
354+
return false, orberrors.NewTransient(fmt.Errorf("query references: %w", err))
355355
}
356356

357357
defer func() {
@@ -367,7 +367,7 @@ func (h *Inbox) hasReference(objectIRI, refIRI *url.URL, refType store.Reference
367367
return false, nil
368368
}
369369

370-
return false, aperrors.NewTransient(fmt.Errorf("get next reference: %w", err))
370+
return false, orberrors.NewTransient(fmt.Errorf("get next reference: %w", err))
371371
}
372372

373373
return true, nil
@@ -442,13 +442,13 @@ func (h *Inbox) handleOfferActivity(offer *vocab.ActivityType) error {
442442

443443
activityID, err := h.outbox.Post(like)
444444
if err != nil {
445-
return aperrors.NewTransient(fmt.Errorf("unable to reply with 'Like' to %s for offer [%s]: %w",
445+
return orberrors.NewTransient(fmt.Errorf("unable to reply with 'Like' to %s for offer [%s]: %w",
446446
offer.Actor(), offer.ID(), err))
447447
}
448448

449449
err = h.store.AddReference(store.Liked, h.ServiceIRI, activityID)
450450
if err != nil {
451-
return aperrors.NewTransient(fmt.Errorf("unable to store 'Like' activity for offer [%s]: %w", offer.ID(), err))
451+
return orberrors.NewTransient(fmt.Errorf("unable to store 'Like' activity for offer [%s]: %w", offer.ID(), err))
452452
}
453453

454454
h.notify(offer)
@@ -476,7 +476,7 @@ func (h *Inbox) handleLikeActivity(like *vocab.ActivityType) error {
476476

477477
err = h.store.AddReference(store.Like, h.ServiceIRI, like.ID().URL())
478478
if err != nil {
479-
return aperrors.NewTransient(fmt.Errorf("unable to store 'Like' activity [%s]: %w", like.ID(), err))
479+
return orberrors.NewTransient(fmt.Errorf("unable to store 'Like' activity [%s]: %w", like.ID(), err))
480480
}
481481

482482
h.notify(like)
@@ -493,7 +493,7 @@ func (h *Inbox) handleAnchorCredential(target *vocab.ObjectProperty, obj *vocab.
493493

494494
ok, err := h.hasReference(targetIRI, h.ServiceIRI, store.AnchorCredential)
495495
if err != nil {
496-
return aperrors.NewTransient(fmt.Errorf("has anchor credential [%s]: %w", targetIRI, err))
496+
return orberrors.NewTransient(fmt.Errorf("has anchor credential [%s]: %w", targetIRI, err))
497497
}
498498

499499
if ok {
@@ -514,7 +514,7 @@ func (h *Inbox) handleAnchorCredential(target *vocab.ObjectProperty, obj *vocab.
514514

515515
err = h.store.AddReference(store.AnchorCredential, targetIRI, h.ServiceIRI)
516516
if err != nil {
517-
return aperrors.NewTransient(fmt.Errorf("store anchor credential reference: %w", err))
517+
return orberrors.NewTransient(fmt.Errorf("store anchor credential reference: %w", err))
518518
}
519519

520520
return nil
@@ -569,7 +569,7 @@ func (h *Inbox) announceAnchorCredential(create *vocab.ActivityType) error {
569569

570570
activityID, err := h.outbox.Post(announce)
571571
if err != nil {
572-
return aperrors.NewTransient(err)
572+
return orberrors.NewTransient(err)
573573
}
574574

575575
logger.Debugf("[%s] Adding 'Announce' %s to shares of %s", h.ServiceIRI, announce.ID(), ref.ID())
@@ -606,7 +606,7 @@ func (h *Inbox) announceAnchorCredentialRef(create *vocab.ActivityType) error {
606606

607607
activityID, err := h.outbox.Post(announce)
608608
if err != nil {
609-
return aperrors.NewTransient(err)
609+
return orberrors.NewTransient(err)
610610
}
611611

612612
anchorCredID := ref.Target().Object().ID()
@@ -703,7 +703,7 @@ func (h *Inbox) undoAddReference(activity *vocab.ActivityType, refType store.Ref
703703

704704
err := h.store.DeleteReference(refType, h.ServiceIRI, actorIRI)
705705
if err != nil {
706-
return aperrors.NewTransient(fmt.Errorf("unable to delete %s from %s's collection of %s",
706+
return orberrors.NewTransient(fmt.Errorf("unable to delete %s from %s's collection of %s",
707707
actorIRI, h.ServiceIRI, refType))
708708
}
709709

@@ -720,7 +720,7 @@ func (h *Inbox) ensureActivityInOutbox(activity *vocab.ActivityType) error {
720720
return fmt.Errorf("get activity from outbox: %w", err)
721721
}
722722

723-
return aperrors.NewTransient(fmt.Errorf("get activity from outbox: %w", err))
723+
return orberrors.NewTransient(fmt.Errorf("get activity from outbox: %w", err))
724724
}
725725

726726
// Ensure the activity in the outbox is the same as the given activity.

pkg/activitypub/service/activityhandler/outboxhandler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ package activityhandler
99
import (
1010
"fmt"
1111

12-
"github.com/trustbloc/orb/pkg/activitypub/errors"
1312
store "github.com/trustbloc/orb/pkg/activitypub/store/spi"
1413
"github.com/trustbloc/orb/pkg/activitypub/vocab"
14+
orberrors "github.com/trustbloc/orb/pkg/errors"
1515
)
1616

1717
// Outbox handles activities posted to the outbox.
@@ -72,7 +72,7 @@ func (h *handler) handleCreateActivity(create *vocab.ActivityType) error {
7272

7373
err := h.store.AddReference(store.AnchorCredential, target.Object().ID().URL(), h.ServiceIRI)
7474
if err != nil {
75-
return errors.NewTransient(fmt.Errorf("store anchor credential reference: %w", err))
75+
return orberrors.NewTransient(fmt.Errorf("store anchor credential reference: %w", err))
7676
}
7777

7878
return nil
@@ -89,7 +89,8 @@ func (h *Outbox) undoAddReference(activity *vocab.ActivityType, refType store.Re
8989
}
9090

9191
if err := h.store.DeleteReference(refType, h.ServiceIRI, iri); err != nil {
92-
return errors.NewTransient(fmt.Errorf("unable to delete %s from %s's collection of %s", iri, h.ServiceIRI, refType))
92+
return orberrors.NewTransient(fmt.Errorf("unable to delete %s from %s's collection of %s",
93+
iri, h.ServiceIRI, refType))
9394
}
9495

9596
logger.Debugf("[%s] %s (if found) was successfully deleted from %s's collection of %s",

pkg/activitypub/service/inbox/httpsubscriber/httpsubscriber.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/trustbloc/edge-core/pkg/log"
1919
"github.com/trustbloc/sidetree-core-go/pkg/restapi/common"
2020

21-
"github.com/trustbloc/orb/pkg/activitypub/service/lifecycle"
21+
"github.com/trustbloc/orb/pkg/lifecycle"
2222
)
2323

2424
var logger = log.New("activitypub_service")

pkg/activitypub/service/inbox/httpsubscriber/httpsubscriber_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"github.com/stretchr/testify/require"
2121

2222
"github.com/trustbloc/orb/pkg/activitypub/service/mocks"
23-
"github.com/trustbloc/orb/pkg/activitypub/service/spi"
2423
"github.com/trustbloc/orb/pkg/internal/testutil"
24+
"github.com/trustbloc/orb/pkg/lifecycle"
2525
)
2626

2727
const (
@@ -33,14 +33,14 @@ func TestNew(t *testing.T) {
3333
s := New(&Config{ServiceEndpoint: endpoint}, &mocks.SignatureVerifier{})
3434
require.NotNil(t, s)
3535

36-
require.Equal(t, spi.StateStarted, s.State())
36+
require.Equal(t, lifecycle.StateStarted, s.State())
3737
require.Equal(t, http.MethodPost, s.Method())
3838
require.Equal(t, endpoint, s.Path())
3939
require.NotNil(t, endpoint, s.Handler())
4040

4141
require.NoError(t, s.Close())
4242

43-
require.Equal(t, spi.StateStopped, s.State())
43+
require.Equal(t, lifecycle.StateStopped, s.State())
4444
}
4545

4646
func TestSubscriber_HandleAck(t *testing.T) {

pkg/activitypub/service/inbox/inbox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import (
2020
"github.com/trustbloc/edge-core/pkg/log"
2121
"github.com/trustbloc/sidetree-core-go/pkg/restapi/common"
2222

23-
aperrors "github.com/trustbloc/orb/pkg/activitypub/errors"
2423
"github.com/trustbloc/orb/pkg/activitypub/service/inbox/httpsubscriber"
25-
"github.com/trustbloc/orb/pkg/activitypub/service/lifecycle"
2624
service "github.com/trustbloc/orb/pkg/activitypub/service/spi"
27-
"github.com/trustbloc/orb/pkg/activitypub/service/wmlogger"
2825
store "github.com/trustbloc/orb/pkg/activitypub/store/spi"
2926
"github.com/trustbloc/orb/pkg/activitypub/vocab"
27+
orberrors "github.com/trustbloc/orb/pkg/errors"
28+
"github.com/trustbloc/orb/pkg/lifecycle"
29+
"github.com/trustbloc/orb/pkg/pubsub/wmlogger"
3030
)
3131

3232
var logger = log.New("activitypub_service")
@@ -196,7 +196,7 @@ func (h *Inbox) handle(msg *message.Message) {
196196
}
197197

198198
if e := h.activityHandler.HandleActivity(activity); e != nil {
199-
if aperrors.IsTransient(e) {
199+
if orberrors.IsTransient(e) {
200200
logger.Warnf("[%s] Transient error handling message [%s]: %s", h.ServiceEndpoint, msg.UUID, e)
201201

202202
// Nack the message so that it may be retried (potentially on a different server).

0 commit comments

Comments
 (0)