From dd99d24232969434e50d9e8db680302a1240eab8 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Sat, 28 Jul 2018 10:34:26 +0200 Subject: [PATCH] feat(core): initial contact flow implementation (#40) --- core/api/p2p/event.go | 19 ++++ core/api/p2p/event.pb.go | 190 +++++++++++---------------------------- core/api/p2p/event.proto | 6 +- core/api/p2p/kind.go | 12 +++ core/cmd/berty/client.go | 1 - core/entity/contact.go | 16 ++++ core/entity/errors.go | 7 ++ core/node/errors.go | 9 ++ core/node/node.go | 37 +++++++- core/node/nodeapi.go | 126 +++++++++++++++++++++++--- core/node/p2papi.go | 3 +- core/node/p2pclient.go | 25 ++++++ core/node/sql.go | 4 +- core/sql/gorm.go | 2 + core/sql/helpers.go | 23 +++++ 15 files changed, 316 insertions(+), 164 deletions(-) create mode 100644 core/api/p2p/event.go create mode 100644 core/api/p2p/kind.go create mode 100644 core/entity/contact.go create mode 100644 core/entity/errors.go create mode 100644 core/node/errors.go create mode 100644 core/node/p2pclient.go create mode 100644 core/sql/helpers.go diff --git a/core/api/p2p/event.go b/core/api/p2p/event.go new file mode 100644 index 0000000000..db5fa1b724 --- /dev/null +++ b/core/api/p2p/event.go @@ -0,0 +1,19 @@ +package p2p + +import "time" + +func NewOutgoingEvent(sender, receiver string, kind Kind) *Event { + return &Event{ + SenderAPIVersion: Version, + CreatedAt: time.Now(), + Kind: kind, + SenderID: sender, + ReceiverID: receiver, + Direction: Event_Outgoing, + } +} + +func (e *Event) Validate() error { + // FIXME: generate validation + return nil +} diff --git a/core/api/p2p/event.pb.go b/core/api/p2p/event.pb.go index cbc6782555..cdfa20ef09 100644 --- a/core/api/p2p/event.pb.go +++ b/core/api/p2p/event.pb.go @@ -109,9 +109,8 @@ type Event struct { Kind Kind `protobuf:"varint,13,opt,name=kind,proto3,enum=berty.p2p.Kind" json:"kind,omitempty"` // Attributes is a nested protobuf message containing per-event-type additional attributes. Attributes []byte `protobuf:"bytes,14,opt,name=attributes,proto3" json:"attributes,omitempty"` - // Types that are valid to be assigned to Context: - // *Event_ConversationID - Context isEvent_Context `protobuf_oneof:"context"` + // ConversationID needs to be set if the event belongs to a conversation. + ConversationID string `protobuf:"bytes,15,opt,name=conversation_id,json=conversationId,proto3" json:"conversation_id,omitempty"` } func (m *Event) Reset() { *m = Event{} } @@ -119,25 +118,6 @@ func (m *Event) String() string { return proto.CompactTextString(m) } func (*Event) ProtoMessage() {} func (*Event) Descriptor() ([]byte, []int) { return fileDescriptorEvent, []int{0} } -type isEvent_Context interface { - isEvent_Context() - MarshalTo([]byte) (int, error) - Size() int -} - -type Event_ConversationID struct { - ConversationID string `protobuf:"bytes,15,opt,name=conversation_id,json=conversationId,proto3,oneof"` -} - -func (*Event_ConversationID) isEvent_Context() {} - -func (m *Event) GetContext() isEvent_Context { - if m != nil { - return m.Context - } - return nil -} - func (m *Event) GetID() string { if m != nil { return m.ID @@ -237,63 +217,12 @@ func (m *Event) GetAttributes() []byte { } func (m *Event) GetConversationID() string { - if x, ok := m.GetContext().(*Event_ConversationID); ok { - return x.ConversationID + if m != nil { + return m.ConversationID } return "" } -// XXX_OneofFuncs is for the internal use of the proto package. -func (*Event) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) { - return _Event_OneofMarshaler, _Event_OneofUnmarshaler, _Event_OneofSizer, []interface{}{ - (*Event_ConversationID)(nil), - } -} - -func _Event_OneofMarshaler(msg proto.Message, b *proto.Buffer) error { - m := msg.(*Event) - // context - switch x := m.Context.(type) { - case *Event_ConversationID: - _ = b.EncodeVarint(15<<3 | proto.WireBytes) - _ = b.EncodeStringBytes(x.ConversationID) - case nil: - default: - return fmt.Errorf("Event.Context has unexpected type %T", x) - } - return nil -} - -func _Event_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) { - m := msg.(*Event) - switch tag { - case 15: // context.conversation_id - if wire != proto.WireBytes { - return true, proto.ErrInternalBadWireType - } - x, err := b.DecodeStringBytes() - m.Context = &Event_ConversationID{x} - return true, err - default: - return false, nil - } -} - -func _Event_OneofSizer(msg proto.Message) (n int) { - m := msg.(*Event) - // context - switch x := m.Context.(type) { - case *Event_ConversationID: - n += proto.SizeVarint(15<<3 | proto.WireBytes) - n += proto.SizeVarint(uint64(len(x.ConversationID))) - n += len(x.ConversationID) - case nil: - default: - panic(fmt.Sprintf("proto: unexpected type %T in oneof", x)) - } - return n -} - func init() { proto.RegisterType((*Event)(nil), "berty.p2p.Event") proto.RegisterEnum("berty.p2p.Event_Direction", Event_Direction_name, Event_Direction_value) @@ -413,24 +342,15 @@ func (m *Event) MarshalTo(dAtA []byte) (int, error) { i = encodeVarintEvent(dAtA, i, uint64(len(m.Attributes))) i += copy(dAtA[i:], m.Attributes) } - if m.Context != nil { - nn7, err := m.Context.MarshalTo(dAtA[i:]) - if err != nil { - return 0, err - } - i += nn7 + if len(m.ConversationID) > 0 { + dAtA[i] = 0x7a + i++ + i = encodeVarintEvent(dAtA, i, uint64(len(m.ConversationID))) + i += copy(dAtA[i:], m.ConversationID) } return i, nil } -func (m *Event_ConversationID) MarshalTo(dAtA []byte) (int, error) { - i := 0 - dAtA[i] = 0x7a - i++ - i = encodeVarintEvent(dAtA, i, uint64(len(m.ConversationID))) - i += copy(dAtA[i:], m.ConversationID) - return i, nil -} func encodeVarintEvent(dAtA []byte, offset int, v uint64) int { for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) @@ -491,17 +411,10 @@ func (m *Event) Size() (n int) { if l > 0 { n += 1 + l + sovEvent(uint64(l)) } - if m.Context != nil { - n += m.Context.Size() - } - return n -} - -func (m *Event_ConversationID) Size() (n int) { - var l int - _ = l l = len(m.ConversationID) - n += 1 + l + sovEvent(uint64(l)) + if l > 0 { + n += 1 + l + sovEvent(uint64(l)) + } return n } @@ -960,7 +873,7 @@ func (m *Event) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Context = &Event_ConversationID{string(dAtA[iNdEx:postIndex])} + m.ConversationID = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex @@ -1091,43 +1004,42 @@ var ( func init() { proto.RegisterFile("api/p2p/event.proto", fileDescriptorEvent) } var fileDescriptorEvent = []byte{ - // 596 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x94, 0xcd, 0x6e, 0xd3, 0x4c, - 0x14, 0x86, 0xe3, 0x7c, 0x6d, 0x63, 0x9f, 0xb6, 0x69, 0x34, 0x5f, 0x84, 0xac, 0x08, 0xc5, 0x51, - 0xba, 0x89, 0x10, 0xb2, 0xa5, 0x20, 0x24, 0x7e, 0x54, 0x55, 0x4e, 0x83, 0x54, 0x8b, 0x05, 0xc8, - 0xfc, 0x2c, 0xd8, 0x44, 0x8e, 0x67, 0x30, 0xa3, 0xd4, 0x33, 0xd6, 0x64, 0x52, 0xe8, 0x5d, 0xb0, - 0xe4, 0x6a, 0x58, 0x77, 0xc9, 0x15, 0x18, 0x64, 0xee, 0x80, 0x2b, 0x40, 0x33, 0xb6, 0x83, 0x59, - 0x20, 0xca, 0xc6, 0x9a, 0x39, 0xef, 0x79, 0xde, 0x39, 0x67, 0x74, 0xc6, 0xf0, 0x7f, 0x94, 0x51, - 0x2f, 0x9b, 0x66, 0x1e, 0xb9, 0x24, 0x4c, 0xba, 0x99, 0xe0, 0x92, 0x23, 0x6b, 0x49, 0x84, 0xbc, - 0x72, 0xb3, 0x69, 0x36, 0x40, 0xb5, 0xbe, 0xa2, 0x0c, 0x97, 0xf2, 0xa0, 0x9f, 0xf0, 0x84, 0xeb, - 0xa5, 0xa7, 0x56, 0x55, 0xd4, 0x49, 0x38, 0x4f, 0x2e, 0x88, 0xa7, 0x77, 0xcb, 0xcd, 0x5b, 0x4f, - 0xd2, 0x94, 0xac, 0x65, 0x94, 0x66, 0x65, 0xc2, 0xf8, 0x73, 0x07, 0x76, 0x9f, 0xa8, 0x53, 0xd0, - 0x5d, 0x68, 0x53, 0x6c, 0x1b, 0x23, 0x63, 0x62, 0xcd, 0x6e, 0x17, 0xb9, 0xd3, 0x0e, 0xe6, 0x3f, - 0x72, 0x07, 0x25, 0x5c, 0xa4, 0x8f, 0xc6, 0x99, 0xa0, 0x69, 0x24, 0xae, 0x16, 0x2b, 0x72, 0x35, - 0x0e, 0xdb, 0x14, 0xa3, 0x53, 0xb0, 0xd6, 0x84, 0x61, 0x22, 0x16, 0x14, 0xdb, 0x6d, 0x0d, 0x8d, - 0x8b, 0xdc, 0x31, 0x5f, 0xe8, 0xe0, 0x1f, 0x51, 0xb3, 0x84, 0x02, 0x8c, 0xce, 0x00, 0x62, 0x41, - 0x22, 0x49, 0xf0, 0x22, 0x92, 0xf6, 0x7f, 0x23, 0x63, 0xb2, 0x3f, 0x1d, 0xb8, 0x65, 0xb9, 0x6e, - 0x5d, 0xae, 0xfb, 0xb2, 0x2e, 0x77, 0x66, 0x5e, 0xe7, 0x4e, 0xeb, 0xe3, 0x57, 0xc7, 0x08, 0xad, - 0x8a, 0xf3, 0xa5, 0x32, 0xd9, 0x64, 0xb8, 0x36, 0xd9, 0xf9, 0x17, 0x93, 0x8a, 0xf3, 0x25, 0x3a, - 0x05, 0xc0, 0xe4, 0x82, 0x54, 0x26, 0xbb, 0x7f, 0x35, 0xd9, 0x29, 0x0d, 0x2a, 0xc6, 0x97, 0xe8, - 0x21, 0x74, 0xd6, 0x84, 0x49, 0x45, 0xef, 0xdd, 0x90, 0xde, 0x53, 0x80, 0x2f, 0x91, 0x0f, 0xfb, - 0x82, 0xc4, 0x84, 0x5e, 0x96, 0x87, 0x77, 0x6e, 0x88, 0x43, 0x0d, 0xf9, 0x12, 0x3d, 0x06, 0x33, - 0x8a, 0x57, 0x25, 0x6f, 0xde, 0x90, 0xef, 0x68, 0xc2, 0x97, 0xe8, 0x01, 0x58, 0x98, 0x0a, 0x12, - 0x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cc, 0x53, 0xca, 0x92, 0x9e, 0xa1, 0x76, 0xcf, 0x36, - 0x32, 0xe1, 0x6a, 0xd7, 0x9e, 0x59, 0xd0, 0x89, 0x39, 0x93, 0xe4, 0x83, 0x9c, 0xdd, 0xbf, 0x2e, - 0x86, 0xc6, 0x97, 0x62, 0x68, 0x7c, 0x2b, 0x86, 0xc6, 0xa7, 0xef, 0xc3, 0xd6, 0x9b, 0xe3, 0x84, - 0xca, 0x77, 0x9b, 0xa5, 0x1b, 0xf3, 0xd4, 0xd3, 0xbd, 0x55, 0xdf, 0x98, 0x0b, 0xe2, 0x55, 0x7f, - 0x8e, 0xe5, 0x9e, 0x9e, 0x8d, 0x7b, 0x3f, 0x03, 0x00, 0x00, 0xff, 0xff, 0x71, 0xeb, 0x96, 0x50, - 0x6b, 0x04, 0x00, 0x00, + // 586 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x94, 0xdf, 0x8a, 0xd3, 0x4e, + 0x14, 0xc7, 0x37, 0xfd, 0xed, 0x6e, 0x93, 0xb3, 0xbb, 0xdd, 0x32, 0xbf, 0x22, 0xa1, 0x48, 0x53, + 0xba, 0x37, 0x45, 0x24, 0x81, 0x8a, 0xa0, 0x2e, 0xb2, 0xa4, 0x5b, 0xc1, 0xe0, 0x85, 0x12, 0xff, + 0x5c, 0x78, 0x53, 0xd2, 0xcc, 0x18, 0x87, 0x6e, 0x66, 0xc2, 0x74, 0xba, 0xd2, 0xb7, 0xf0, 0xd2, + 0xd7, 0xf0, 0x2d, 0xf6, 0xd2, 0x27, 0x88, 0x12, 0xdf, 0xc0, 0x27, 0x90, 0x99, 0x24, 0x35, 0x5e, + 0x88, 0xf5, 0x26, 0xcc, 0x9c, 0xef, 0xf9, 0x7c, 0xe7, 0x9c, 0xe1, 0x4c, 0xe0, 0xff, 0x28, 0xa3, + 0x5e, 0x36, 0xc9, 0x3c, 0x72, 0x4d,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, 0xd3, 0x98, 0x33, 0x55, 0x70, 0xa4, 0xae, 0x43, 0xbd, 0x82, 0x53, + 0x7d, 0x2e, 0x2a, 0x72, 0xa7, 0x73, 0xd9, 0x90, 0x82, 0x59, 0xd8, 0x69, 0xa6, 0x06, 0x78, 0xf4, + 0x18, 0xac, 0xed, 0x9d, 0xa2, 0x1e, 0x74, 0x5f, 0xb3, 0x25, 0xe3, 0x1f, 0xd8, 0x36, 0xd6, 0xdd, + 0x43, 0xc7, 0x60, 0x06, 0x2c, 0xe6, 0x29, 0x65, 0x49, 0xd7, 0x50, 0xbb, 0xe7, 0x6b, 0x99, 0x70, + 0xb5, 0x6b, 0x4d, 0xef, 0xdf, 0x14, 0x03, 0xe3, 0x4b, 0x31, 0x30, 0xbe, 0x15, 0x03, 0xe3, 0xd3, + 0xf7, 0xc1, 0xde, 0xdb, 0xb3, 0x84, 0xca, 0xf7, 0xeb, 0x85, 0x1b, 0xf3, 0xd4, 0xd3, 0xed, 0x54, + 0xdf, 0x98, 0x0b, 0xe2, 0x55, 0x3f, 0x8b, 0xc5, 0xa1, 0x1e, 0x87, 0x7b, 0x3f, 0x03, 0x00, 0x00, + 0xff, 0xff, 0x94, 0x91, 0x7c, 0x6b, 0x5e, 0x04, 0x00, 0x00, } diff --git a/core/api/p2p/event.proto b/core/api/p2p/event.proto index 77d4113831..10db32035c 100644 --- a/core/api/p2p/event.proto +++ b/core/api/p2p/event.proto @@ -56,10 +56,8 @@ message Event { // Attributes is a nested protobuf message containing per-event-type additional attributes. bytes attributes = 14; - oneof context { - // ConversationID needs to be set if the event belongs to a conversation. - string conversation_id = 15 [(gogoproto.customname) = "ConversationID"]; - } + // ConversationID needs to be set if the event belongs to a conversation. + string conversation_id = 15 [(gogoproto.customname) = "ConversationID"]; // // enums diff --git a/core/api/p2p/kind.go b/core/api/p2p/kind.go new file mode 100644 index 0000000000..2bc11d4e90 --- /dev/null +++ b/core/api/p2p/kind.go @@ -0,0 +1,12 @@ +package p2p + +import "github.com/gogo/protobuf/proto" + +func (e *Event) SetAttrs(attrs proto.Message) error { + raw, err := proto.Marshal(attrs) + if err != nil { + return err + } + e.Attributes = raw + return nil +} diff --git a/core/cmd/berty/client.go b/core/cmd/berty/client.go index 067d918933..a87dec68a7 100644 --- a/core/cmd/berty/client.go +++ b/core/cmd/berty/client.go @@ -46,7 +46,6 @@ func newClientCommand() *cobra.Command { } func clientUnary(opts *clientOptions) error { - fmt.Println(opts.endpoint) ctx := context.Background() zap.L().Debug("dialing node", zap.String("addr", opts.nodeAddress), zap.String("protocol", "gRPC")) diff --git a/core/entity/contact.go b/core/entity/contact.go new file mode 100644 index 0000000000..773e5302de --- /dev/null +++ b/core/entity/contact.go @@ -0,0 +1,16 @@ +package entity + +func (c *Contact) Validate() error { + if c == nil { + return ErrInvalidEntity + } + return nil +} + +func (c *Contact) Filtered() *Contact { + return &Contact{ + DisplayName: c.DisplayName, + DisplayStatus: c.DisplayStatus, + // FIXME: share sigchain + } +} diff --git a/core/entity/errors.go b/core/entity/errors.go new file mode 100644 index 0000000000..04f75a7332 --- /dev/null +++ b/core/entity/errors.go @@ -0,0 +1,7 @@ +package entity + +import "errors" + +var ( + ErrInvalidEntity = errors.New("invalid entity") +) diff --git a/core/node/errors.go b/core/node/errors.go new file mode 100644 index 0000000000..d05f05f8b6 --- /dev/null +++ b/core/node/errors.go @@ -0,0 +1,9 @@ +package node + +import "errors" + +var ( + ErrInvalidInput = errors.New("invalid input") + ErrNotImplemented = errors.New("not implemented") + ErrEntityAlreadyExists = errors.New("entity already exists") +) diff --git a/core/node/node.go b/core/node/node.go index 143055db85..841f83d4e8 100644 --- a/core/node/node.go +++ b/core/node/node.go @@ -1,15 +1,30 @@ package node -import "github.com/jinzhu/gorm" +import ( + "fmt" + + "github.com/jinzhu/gorm" + "github.com/pkg/errors" + uuid "github.com/satori/go.uuid" + + "github.com/berty/berty/core/api/p2p" + "github.com/berty/berty/core/entity" +) // Node is the top-level object of a Berty peer type Node struct { - db *gorm.DB + sql *gorm.DB + myself *entity.Contact + outgoingEvents chan *p2p.Event } // New initializes a new Node object func New(opts ...NewNodeOption) *Node { - n := &Node{} + n := &Node{ + // FIXME: fetch myself from db + myself: &entity.Contact{ID: "init"}, + outgoingEvents: make(chan *p2p.Event, 100), + } for _, opt := range opts { opt(n) @@ -19,8 +34,24 @@ func New(opts ...NewNodeOption) *Node { // Start is the node's mainloop func (n *Node) Start() error { + if err := n.Validate(); err != nil { + return errors.Wrap(err, "node is misconfigured") + } select {} } +// Validate returns an error if object is invalid +func (n *Node) Validate() error { + if n == nil || n.sql == nil { + return errors.New("missing required fields") + } + return nil +} + // NewNodeOption is a callback used to configure a Node during intiailization phase type NewNodeOption func(n *Node) + +// NewID returns a unique ID prefixed with our contact ID +func (n *Node) NewID() string { + return fmt.Sprintf("%s:%s", n.myself.ID, uuid.Must(uuid.NewV4()).String()) +} diff --git a/core/node/nodeapi.go b/core/node/nodeapi.go index 01995f9c48..c7edfcabc6 100644 --- a/core/node/nodeapi.go +++ b/core/node/nodeapi.go @@ -2,12 +2,14 @@ package node import ( "context" - "fmt" + "github.com/pkg/errors" "google.golang.org/grpc" "github.com/berty/berty/core/api/node" + "github.com/berty/berty/core/api/p2p" "github.com/berty/berty/core/entity" + "github.com/berty/berty/core/sql" ) // WithNodeGrpcServer registers the Node as a 'berty.node' protobuf server implementation @@ -23,12 +25,12 @@ func WithNodeGrpcServer(gs *grpc.Server) NewNodeOption { // EventList implements berty.node.EventList func (n *Node) EventList(*node.Void, node.Service_EventListServer) error { - return fmt.Errorf("not implemented") + return ErrNotImplemented } // EventStream implements berty.node.EventStream func (n *Node) EventStream(*node.Void, node.Service_EventStreamServer) error { - return fmt.Errorf("not implemented") + return ErrNotImplemented } // @@ -36,26 +38,124 @@ func (n *Node) EventStream(*node.Void, node.Service_EventStreamServer) error { // // ContactAcceptRequest implements berty.node.ContactAcceptRequest -func (n *Node) ContactAcceptRequest(context.Context, *entity.Contact) (*entity.Contact, error) { - return nil, fmt.Errorf("not implemented") +func (n *Node) ContactAcceptRequest(_ context.Context, input *entity.Contact) (*entity.Contact, error) { + // input validation + if err := input.Validate(); err != nil { + return nil, errors.Wrap(err, ErrInvalidInput.Error()) + } + contact, err := sql.FindContact(n.sql, input) + if err != nil { + return nil, errors.Wrap(err, "failed to get contact") + } + + // mark contact as friend + contact.Status = entity.Contact_IsFriend + if err := n.sql.Save(contact).Error; err != nil { + return nil, err + } + + // send ContactRequestAccepted event + event := n.NewContactEvent(contact, p2p.Kind_ContactRequestAccepted) + if err != nil { + return nil, err + } + if err := n.EnqueueOutgoingEvent(event); err != nil { + return nil, err + } + + // send ContactShareMe event + event = n.NewContactEvent(contact, p2p.Kind_ContactShareMe) + if err := event.SetAttrs(&p2p.ContactShareAttrs{Contact: n.myself.Filtered()}); err != nil { + return nil, err + } + if err := n.EnqueueOutgoingEvent(event); err != nil { + return nil, err + } + + return contact, nil } // ContactRequest implements berty.node.ContactRequest -func (n *Node) ContactRequest(context.Context, *node.ContactRequestInput) (*entity.Contact, error) { - return nil, fmt.Errorf("not implemented") +func (n *Node) ContactRequest(ctx context.Context, req *node.ContactRequestInput) (*entity.Contact, error) { + // input validation + if err := req.Contact.Validate(); err != nil { + return nil, errors.Wrap(err, ErrInvalidInput.Error()) + } + + // check for duplicate + _, err := sql.FindContact(n.sql, req.Contact) + if err == nil { + return nil, ErrEntityAlreadyExists + } + + // save contact in database + contact := req.Contact + contact.Status = entity.Contact_IsRequested + contact.ID = n.NewID() + if err = n.sql.Set("gorm:association_autoupdate", true).Save(contact).Error; err != nil { + return nil, errors.Wrap(err, "failed to save contact") + } + + // send request to peer + event := n.NewContactEvent(contact, p2p.Kind_ContactRequest) + if err := event.SetAttrs(&p2p.ContactRequestAttrs{ + Me: &entity.Contact{ + DisplayName: n.myself.DisplayName, + }, + IntroMessage: req.IntroMessage, + }); err != nil { + return nil, err + } + if err := n.EnqueueOutgoingEvent(event); err != nil { + return nil, err + } + + return contact, nil } // ContactUpdate implements berty.node.ContactUpdate -func (n *Node) ContactUpdate(context.Context, *entity.Contact) (*entity.Contact, error) { - return nil, fmt.Errorf("not implemented") +func (n *Node) ContactUpdate(_ context.Context, contact *entity.Contact) (*entity.Contact, error) { + // input validation + if contact == nil || contact.ID == "" { + return nil, ErrInvalidInput + } + if err := contact.Validate(); err != nil { + return nil, errors.Wrap(err, ErrInvalidInput.Error()) + } + + // FIXME: protect import fields from updatind + + return contact, n.sql.Save(contact).Error } // ContactRemove implements berty.node.ContactRemove -func (n *Node) ContactRemove(context.Context, *entity.Contact) (*entity.Contact, error) { - return nil, fmt.Errorf("not implemented") +func (n *Node) ContactRemove(_ context.Context, contact *entity.Contact) (*entity.Contact, error) { + // input validation + if contact == nil || contact.ID == "" { + return nil, ErrInvalidInput + } + + if err := contact.Validate(); err != nil { + return nil, errors.Wrap(err, ErrInvalidInput.Error()) + } + + // FIXME: should not be able to delete myself + + // remove from sql + return contact, n.sql.Delete(contact).Error } // ContactList implements berty.node.ContactList -func (n *Node) ContactList(*node.Void, node.Service_ContactListServer) error { - return fmt.Errorf("not implemented") +func (n *Node) ContactList(_ *node.Void, stream node.Service_ContactListServer) error { + var contacts []*entity.Contact + if err := n.sql.Find(&contacts).Error; err != nil { + return errors.Wrap(err, "failed to get contacts from database") + } + + for _, contact := range contacts { + if err := stream.Send(contact); err != nil { + return err + } + } + return nil } diff --git a/core/node/p2papi.go b/core/node/p2papi.go index 31d658f528..0bf5282d80 100644 --- a/core/node/p2papi.go +++ b/core/node/p2papi.go @@ -2,7 +2,6 @@ package node import ( "context" - "fmt" "google.golang.org/grpc" @@ -18,5 +17,5 @@ func WithP2PGrpcServer(gs *grpc.Server) NewNodeOption { // Handle implements berty.p2p.Handle func (n *Node) Handle(context.Context, *p2p.Event) (*p2p.Void, error) { - return nil, fmt.Errorf("not implemented") + return nil, ErrNotImplemented } diff --git a/core/node/p2pclient.go b/core/node/p2pclient.go new file mode 100644 index 0000000000..4ccc8075e1 --- /dev/null +++ b/core/node/p2pclient.go @@ -0,0 +1,25 @@ +package node + +import ( + "github.com/pkg/errors" + + "github.com/berty/berty/core/api/p2p" + "github.com/berty/berty/core/entity" +) + +func (n *Node) NewContactEvent(destination *entity.Contact, kind p2p.Kind) *p2p.Event { + event := p2p.NewOutgoingEvent(n.myself.ID, destination.ID, kind) + event.ID = n.NewID() + return event +} + +func (n *Node) EnqueueOutgoingEvent(event *p2p.Event) error { + if err := event.Validate(); err != nil { + return errors.Wrap(err, "invalid event") + } + if err := n.sql.Create(event).Error; err != nil { + return errors.Wrap(err, "failed to write event to db") + } + n.outgoingEvents <- event + return nil +} diff --git a/core/node/sql.go b/core/node/sql.go index 163ebf7e0f..1f867bdd9b 100644 --- a/core/node/sql.go +++ b/core/node/sql.go @@ -3,8 +3,8 @@ package node import "github.com/jinzhu/gorm" // WithSQL registers a gorm connection as the node database -func WithSQL(db *gorm.DB) NewNodeOption { +func WithSQL(sql *gorm.DB) NewNodeOption { return func(n *Node) { - n.db = db + n.sql = sql } } diff --git a/core/sql/gorm.go b/core/sql/gorm.go index ed1ba9d61d..0e97d632cf 100644 --- a/core/sql/gorm.go +++ b/core/sql/gorm.go @@ -6,6 +6,7 @@ import ( "github.com/go-gormigrate/gormigrate" "github.com/jinzhu/gorm" + "github.com/berty/berty/core/api/p2p" "github.com/berty/berty/core/entity" ) @@ -22,6 +23,7 @@ func Init(db *gorm.DB) (*gorm.DB, error) { ID: "1", Migrate: func(tx *gorm.DB) error { return tx.AutoMigrate( + p2p.Event{}, entity.Contact{}, ).Error }, diff --git a/core/sql/helpers.go b/core/sql/helpers.go new file mode 100644 index 0000000000..cb9d29cd50 --- /dev/null +++ b/core/sql/helpers.go @@ -0,0 +1,23 @@ +package sql + +import ( + "errors" + + "github.com/jinzhu/gorm" + + "github.com/berty/berty/core/entity" +) + +func ContactByID(db *gorm.DB, id string) (*entity.Contact, error) { + var contact entity.Contact + return &contact, db.First(&contact, "ID = ?", id).Error +} + +// FindContact tries different approaches to get a full contact based on a partial one +func FindContact(db *gorm.DB, input *entity.Contact) (*entity.Contact, error) { + if input.ID != "" { + return ContactByID(db, input.ID) + } + // FIXME: support looking-up by sigchain + return nil, errors.New("not enough information to search contact") +}