forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_contactrequest.go
116 lines (90 loc) · 4.21 KB
/
api_contactrequest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package bertyprotocol
import (
"context"
"github.com/libp2p/go-libp2p-core/crypto"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
// ContactRequestReference retrieves the necessary information to create a contact link
func (s *service) ContactRequestReference(context.Context, *protocoltypes.ContactRequestReference_Request) (*protocoltypes.ContactRequestReference_Reply, error) {
enabled, shareableContact := s.accountGroup.MetadataStore().GetIncomingContactRequestsStatus()
rdvSeed := []byte(nil)
if shareableContact != nil {
rdvSeed = shareableContact.PublicRendezvousSeed
}
return &protocoltypes.ContactRequestReference_Reply{
PublicRendezvousSeed: rdvSeed,
Enabled: enabled,
}, nil
}
// ContactRequestDisable disables incoming contact requests
func (s *service) ContactRequestDisable(ctx context.Context, _ *protocoltypes.ContactRequestDisable_Request) (*protocoltypes.ContactRequestDisable_Reply, error) {
if _, err := s.accountGroup.MetadataStore().ContactRequestDisable(ctx); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.ContactRequestDisable_Reply{}, nil
}
// ContactRequestEnable enables incoming contact requests
func (s *service) ContactRequestEnable(ctx context.Context, _ *protocoltypes.ContactRequestEnable_Request) (*protocoltypes.ContactRequestEnable_Reply, error) {
if _, err := s.accountGroup.MetadataStore().ContactRequestEnable(ctx); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
_, shareableContact := s.accountGroup.MetadataStore().GetIncomingContactRequestsStatus()
rdvSeed := []byte(nil)
if shareableContact != nil {
rdvSeed = shareableContact.PublicRendezvousSeed
}
return &protocoltypes.ContactRequestEnable_Reply{
PublicRendezvousSeed: rdvSeed,
}, nil
}
// ContactRequestResetReference generates a new contact request reference
func (s *service) ContactRequestResetReference(ctx context.Context, _ *protocoltypes.ContactRequestResetReference_Request) (*protocoltypes.ContactRequestResetReference_Reply, error) {
if _, err := s.accountGroup.MetadataStore().ContactRequestReferenceReset(ctx); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
_, shareableContact := s.accountGroup.MetadataStore().GetIncomingContactRequestsStatus()
rdvSeed := []byte(nil)
if shareableContact != nil {
rdvSeed = shareableContact.PublicRendezvousSeed
}
return &protocoltypes.ContactRequestResetReference_Reply{
PublicRendezvousSeed: rdvSeed,
}, nil
}
// ContactRequestSend enqueues a new contact request to be sent
func (s *service) ContactRequestSend(ctx context.Context, req *protocoltypes.ContactRequestSend_Request) (*protocoltypes.ContactRequestSend_Reply, error) {
shareableContact := req.Contact
if shareableContact == nil {
return nil, errcode.ErrInvalidInput
}
if _, err := s.accountGroup.MetadataStore().ContactRequestOutgoingEnqueue(ctx, shareableContact, req.OwnMetadata); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.ContactRequestSend_Reply{}, nil
}
// ContactRequestAccept accepts a contact request
func (s *service) ContactRequestAccept(ctx context.Context, req *protocoltypes.ContactRequestAccept_Request) (*protocoltypes.ContactRequestAccept_Reply, error) {
pk, err := crypto.UnmarshalEd25519PublicKey(req.ContactPK)
if err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
if _, err := s.accountGroup.MetadataStore().ContactRequestIncomingAccept(ctx, pk); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
if err = s.indexGroups(); err != nil {
return nil, err
}
return &protocoltypes.ContactRequestAccept_Reply{}, nil
}
// ContactRequestDiscard ignores a contact request without informing the request sender
func (s *service) ContactRequestDiscard(ctx context.Context, req *protocoltypes.ContactRequestDiscard_Request) (*protocoltypes.ContactRequestDiscard_Reply, error) {
pk, err := crypto.UnmarshalEd25519PublicKey(req.ContactPK)
if err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
if _, err := s.accountGroup.MetadataStore().ContactRequestIncomingDiscard(ctx, pk); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.ContactRequestDiscard_Reply{}, nil
}