Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constructors to all commands and queries #210

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions service/adapters/ebt/ebt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ebt

import (
"context"
"github.com/planetary-social/scuttlego/logging"

"github.com/planetary-social/scuttlego/service/app/queries"
"github.com/planetary-social/scuttlego/service/domain/feeds/message"
Expand All @@ -15,20 +16,36 @@ type CreateHistoryStreamHandler interface {

type CreateHistoryStreamHandlerAdapter struct {
handler CreateHistoryStreamHandler
logger logging.Logger
}

func NewCreateHistoryStreamHandlerAdapter(handler CreateHistoryStreamHandler) *CreateHistoryStreamHandlerAdapter {
return &CreateHistoryStreamHandlerAdapter{handler: handler}
func NewCreateHistoryStreamHandlerAdapter(
handler CreateHistoryStreamHandler,
logger logging.Logger,
) *CreateHistoryStreamHandlerAdapter {
return &CreateHistoryStreamHandlerAdapter{
handler: handler,
logger: logger.New("create_history_stream_handler_adapter"),
}
}

func (h CreateHistoryStreamHandlerAdapter) Handle(ctx context.Context, id refs.Feed, seq *message.Sequence, messageWriter ebt.MessageWriter) {
query := queries.CreateHistoryStream{
Id: id,
Seq: seq,
Limit: nil,
Live: true,
Old: true,
ResponseWriter: NewMessageWriterAdapter(messageWriter),
func (h CreateHistoryStreamHandlerAdapter) Handle(
ctx context.Context,
id refs.Feed,
seq *message.Sequence,
messageWriter ebt.MessageWriter,
) {
query, err := queries.NewCreateHistoryStream(
id,
seq,
nil,
true,
true,
NewMessageWriterAdapter(messageWriter),
)
if err != nil {
h.logger.WithError(err).Error("error creating query")
return
}

h.handler.Handle(ctx, query)
Expand Down
24 changes: 22 additions & 2 deletions service/app/commands/handler_accept_new_peer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package commands

import (
"github.com/boreq/errors"
"github.com/planetary-social/scuttlego/service/domain/transport"
)

type AcceptNewPeer struct {
Peer transport.Peer
peer transport.Peer
}

func NewAcceptNewPeer(peer transport.Peer) (AcceptNewPeer, error) {
if peer.IsZero() {
return AcceptNewPeer{}, errors.New("zero value of peer")
}
return AcceptNewPeer{peer: peer}, nil
}

func (a AcceptNewPeer) Peer() transport.Peer {
return a.peer
}

func (a AcceptNewPeer) IsZero() bool {
return a.peer.IsZero()
}

type AcceptNewPeerHandler struct {
Expand All @@ -21,6 +37,10 @@ func NewAcceptNewPeerHandler(
}

func (h *AcceptNewPeerHandler) Handle(cmd AcceptNewPeer) error {
h.peerHandler.HandleNewPeer(cmd.Peer)
if cmd.IsZero() {
return errors.New("zero value of cmd")
}

h.peerHandler.HandleNewPeer(cmd.Peer())
return nil
}
23 changes: 21 additions & 2 deletions service/app/commands/handler_add_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"io"

"github.com/boreq/errors"
"github.com/planetary-social/scuttlego/service/domain/refs"
)

Expand All @@ -11,7 +12,22 @@ type BlobCreator interface {
}

type CreateBlob struct {
Reader io.Reader
reader io.Reader
}

func NewCreateBlob(reader io.Reader) (CreateBlob, error) {
if reader == nil {
return CreateBlob{}, errors.New("zero value of reader")
}
return CreateBlob{reader: reader}, nil
}

func (c CreateBlob) Reader() io.Reader {
return c.reader
}

func (c CreateBlob) IsZero() bool {
return c == CreateBlob{}
}

type CreateBlobHandler struct {
Expand All @@ -27,5 +43,8 @@ func NewCreateBlobHandler(
}

func (h *CreateBlobHandler) Handle(cmd CreateBlob) (refs.Blob, error) {
return h.creator.Create(cmd.Reader)
if cmd.IsZero() {
return refs.Blob{}, errors.New("zero value of cmd")
}
return h.creator.Create(cmd.Reader())
}
8 changes: 6 additions & 2 deletions service/app/commands/handler_add_to_ban_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func NewAddToBanList(hash bans.Hash) (AddToBanList, error) {
return AddToBanList{hash: hash}, nil
}

func (c AddToBanList) Hash() bans.Hash {
return c.hash
}

func (c AddToBanList) IsZero() bool {
return c.hash.IsZero()
}
Expand All @@ -41,10 +45,10 @@ func (h *AddToBanListHandler) Handle(cmd AddToBanList) error {
}

if err := h.transaction.Transact(func(adapters Adapters) error {
if err := adapters.BanList.Add(cmd.hash); err != nil {
if err := adapters.BanList.Add(cmd.Hash()); err != nil {
return errors.Wrap(err, "could not add the hash to the ban list")
}
return h.tryToRemoveTheBannedThing(adapters, cmd.hash)
return h.tryToRemoveTheBannedThing(adapters, cmd.Hash())
}); err != nil {
return errors.Wrap(err, "transaction failed")
}
Expand Down
48 changes: 37 additions & 11 deletions service/app/commands/handler_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,40 @@ import (
"github.com/planetary-social/scuttlego/service/domain/network"
)

// Connect tries to initiate the connection to the specified node. This is most likely useful when you want to
// explicitly stimulate the program to talk to a specific node. Normally connections are initiated and managed
// automatically. Executing this command doesn't necessarily mean that a new connection will be established, for
// example the underlying implementation may decide not to do this if the connection with the specified identity
// already exists.
// Connect tries to initiate the connection to the specified node. This is most
// likely useful when you want to explicitly stimulate the program to talk to a
// specific node. Normally connections are initiated and managed automatically.
// Executing this command doesn't necessarily mean that a new connection will be
// established, for example the underlying implementation may decide not to do
// this if the connection with the specified identity already exists.
type Connect struct {
// Remote is the identity of the remote node.
Remote identity.Public
// remote is the identity of the remote node.
remote identity.Public

// Address is the address of the remote node.
Address network.Address
// address is the address of the remote node.
address network.Address
}

func NewConnect(remote identity.Public, address network.Address) (Connect, error) {
if remote.IsZero() {
return Connect{}, errors.New("zero value of remote")
}
if address.IsZero() {
return Connect{}, errors.New("zero value of address")
}
return Connect{remote: remote, address: address}, nil
}

func (c Connect) Remote() identity.Public {
return c.remote
}

func (c Connect) Address() network.Address {
return c.address
}

func (c Connect) IsZero() bool {
return c.remote.IsZero()
}

type ConnectHandler struct {
Expand All @@ -36,9 +59,12 @@ func NewConnectHandler(
}

func (h *ConnectHandler) Handle(cmd Connect) error {
if err := h.peerManager.Connect(cmd.Remote, cmd.Address); err != nil {
return errors.Wrap(err, "error initiating the connection")
if cmd.IsZero() {
return errors.New("zero value of cmd")
}

if err := h.peerManager.Connect(cmd.Remote(), cmd.Address()); err != nil {
return errors.Wrap(err, "connect error")
}
return nil
}
5 changes: 1 addition & 4 deletions service/app/commands/handler_create_wants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ type BlobReplicationManager interface {
HandleIncomingCreateWantsRequest(ctx context.Context) (<-chan messages.BlobWithSizeOrWantDistance, error)
}

type CreateWants struct {
}

type CreateWantsHandler struct {
manager BlobReplicationManager
}
Expand All @@ -23,6 +20,6 @@ func NewCreateWantsHandler(manager BlobReplicationManager) *CreateWantsHandler {
}
}

func (h *CreateWantsHandler) Handle(ctx context.Context, cmd CreateWants) (<-chan messages.BlobWithSizeOrWantDistance, error) {
func (h *CreateWantsHandler) Handle(ctx context.Context) (<-chan messages.BlobWithSizeOrWantDistance, error) {
return h.manager.HandleIncomingCreateWantsRequest(ctx)
}
21 changes: 19 additions & 2 deletions service/app/commands/handler_download_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ package commands
import (
"time"

"github.com/boreq/errors"
"github.com/planetary-social/scuttlego/service/domain/refs"
)

const temporaryBlobWantListDuration = 1 * time.Hour

type DownloadBlob struct {
Id refs.Blob
id refs.Blob
}

func NewDownloadBlob(id refs.Blob) *DownloadBlob {
return &DownloadBlob{id: id}
}

func (d DownloadBlob) Id() refs.Blob {
return d.id
}

func (d DownloadBlob) IsZero() bool {
return d.id.IsZero()
}

type DownloadBlobHandler struct {
Expand All @@ -28,8 +41,12 @@ func NewDownloadBlobHandler(
}

func (h *DownloadBlobHandler) Handle(cmd DownloadBlob) error {
if cmd.IsZero() {
return errors.New("zero value of cmd")
}

until := h.currentTimeProvider.Get().Add(temporaryBlobWantListDuration)
return h.transaction.Transact(func(adapters Adapters) error {
return adapters.BlobWantList.Add(cmd.Id, until)
return adapters.BlobWantList.Add(cmd.Id(), until)
})
}
27 changes: 23 additions & 4 deletions service/app/commands/handler_follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@ import (
)

type Follow struct {
Target refs.Identity
target refs.Identity
}

func NewFollow(target refs.Identity) (Follow, error) {
if target.IsZero() {
return Follow{}, errors.New("zero value of target")
}
return Follow{target: target}, nil
}

func (f Follow) Target() refs.Identity {
return f.target
}

func (f Follow) IsZero() bool {
return f.target.IsZero()
}

type FollowHandler struct {
Expand All @@ -38,17 +53,21 @@ func NewFollowHandler(
}

func (h *FollowHandler) Handle(cmd Follow) error {
if cmd.IsZero() {
return errors.New("zero value of cmd")
}

contactActions, err := known.NewContactActions([]known.ContactAction{known.ContactActionFollow})
if err != nil {
return errors.Wrap(err, "failed to create contact actions")
}

contact, err := known.NewContact(cmd.Target, contactActions)
contact, err := known.NewContact(cmd.Target(), contactActions)
if err != nil {
return errors.Wrap(err, "failed to create a contact message")
}

content, err := h.marshaler.Marshal(contact)
rawContent, err := h.marshaler.Marshal(contact)
if err != nil {
return errors.Wrap(err, "failed to create message content")
}
Expand All @@ -60,7 +79,7 @@ func (h *FollowHandler) Handle(cmd Follow) error {

return h.transaction.Transact(func(adapters Adapters) error {
return adapters.Feed.UpdateFeed(myRef.MainFeed(), func(feed *feeds.Feed) error {
if _, err := feed.CreateMessage(content, time.Now(), h.local); err != nil {
if _, err := feed.CreateMessage(rawContent, time.Now(), h.local); err != nil {
return errors.Wrap(err, "failed to create a message")
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func NewDeleteGoSSBRepositoryInOldFormat(
}, nil
}

func (cmd DeleteGoSSBRepositoryInOldFormat) Directory() string {
return cmd.directory
}

func (cmd DeleteGoSSBRepositoryInOldFormat) IsZero() bool {
return cmd == DeleteGoSSBRepositoryInOldFormat{}
}
Expand All @@ -47,13 +51,13 @@ func (h MigrationHandlerDeleteGoSSBRepositoryInOldFormat) Handle(ctx context.Con
return errors.New("zero value of command")
}

canRead, err := h.canReadData(ctx, cmd.directory)
canRead, err := h.canReadData(ctx, cmd.Directory())
if err != nil {
return errors.Wrap(err, "error checking if data can be read")
}

if !canRead {
if err := os.RemoveAll(cmd.directory); err != nil {
if err := os.RemoveAll(cmd.Directory()); err != nil {
return errors.Wrap(err, "error removing the directory")
}
}
Expand Down
Loading