forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_contactrequest.go
135 lines (103 loc) · 5.06 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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"
"berty.tech/berty/v2/go/pkg/tyber"
)
// ContactRequestReference retrieves the necessary information to create a contact link
func (s *service) ContactRequestReference(ctx 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, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Disabling contact requests")
defer func() { endSection(err, "") }()
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, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Enabling contact requests")
defer func() { endSection(err, "") }()
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, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Resetting contact requests reference")
defer func() { endSection(err, "") }()
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, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Sending contact request")
defer func() { endSection(err, "") }()
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, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Accepting contact request")
defer func() { endSection(err, "") }()
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.groupDatastore.PutForContactPK(ctx, pk, s.deviceKeystore); 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, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Discarding contact request")
defer func() { endSection(err, "") }()
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
}