-
Notifications
You must be signed in to change notification settings - Fork 0
/
Identity.go
60 lines (50 loc) · 1.21 KB
/
Identity.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
package core
import (
"errors"
"github.com/hood-chat/core/entity"
rp "github.com/hood-chat/core/repo"
st "github.com/hood-chat/core/store"
"github.com/libp2p/go-libp2p/core/peer"
)
var _ IdentityAPI = (*identityAPI)(nil)
type IdentityRepo = rp.IRepo[entity.Identity]
type identityAPI struct {
repo rp.IRepo[entity.Identity]
identity *entity.Identity
}
func NewIdentityAPI(store *st.Store) IdentityAPI {
repo := rp.NewIdentityRepo(store)
identity, err := repo.Get()
if err != nil {
return &identityAPI{repo, nil}
}
return &identityAPI{repo, &identity}
}
func (i *identityAPI) IsLogin() bool {
return i.identity != nil
}
func (i *identityAPI) SignUp(name string) (*entity.Identity, error) {
rIdentity := i.repo
iden, err := entity.CreateIdentity(name)
if err != nil {
return nil, err
}
err = rIdentity.Put(iden)
i.identity = &iden
if err != nil {
return nil, err
}
return &iden, nil
}
func (i *identityAPI) Get() (entity.Identity, error) {
if i.identity==nil {
return entity.Identity{}, errors.New("Not Login")
}
return *i.identity, nil
}
func (i *identityAPI) PeerID() (peer.ID, error) {
if i.identity==nil {
return peer.ID(""), errors.New("Not Login")
}
return i.identity.PeerID()
}