-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessenger.go
152 lines (135 loc) · 3.05 KB
/
messenger.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package core
import (
"github.com/hood-chat/core/entity"
"github.com/hood-chat/core/event"
"github.com/hood-chat/core/store"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
)
var log = logging.Logger("msgr-core")
var _ MessengerAPI = (*Messenger)(nil)
type Messenger struct {
Host host.Host
store *store.Store
identity IdentityAPI
book ContactBookAPI
pms DirectService
// gps PubSubService
chat ChatAPI
hb Builder
opt Option
bus Bus
connector Connector
}
func NewMessengerAPI(path string, opt Option, hb Builder) MessengerAPI {
if hb == nil {
hb = DefaultRoutedHost{}
}
msgr := Messenger{
bus: eventbus.NewBus(),
hb: hb,
opt: opt,
}
err := checkWritable(path)
if err != nil {
panic("path is not writable ")
}
s, err := store.NewStore(path + "/store")
if err != nil {
panic(err)
}
msgr.store = s
msgr.book = NewContactBook(s)
identity := NewIdentityAPI(s)
msgr.identity = identity
if !identity.IsLogin() {
return &msgr
}
msgr.Start()
return &msgr
}
func (m *Messenger) Start() {
id, err := m.identity.Get()
if err != nil {
panic(err)
}
m.opt.SetIdentity(&id)
h, err := m.hb.Create(m.opt)
if err != nil {
panic(err)
}
m.Host = h
m.connector = NewConnector(h)
gpCh := make(chan string)
m.pms = NewDirectMessaging(h, m.bus, m.connector,make(chan*Envelop))
// m.gps = NewGPService(context.Background(),h,m.IdentityAPI(),m.bus,gpCh,m.connector)
m.chat = NewChatAPI(m.store, m.book, m.pms, m.identity)
chats,err := m.chat.ChatInfos(0,0)
if err != nil {
panic("cant load groups chats")
}
for _, c := range chats {
if c.Type == entity.Group {
gpCh <- c.ID.String()
}
}
msgSub, err := m.bus.Subscribe(new(event.MessageEventObj))
if err != nil {
panic(err)
}
go func() {
defer msgSub.Close()
for e := range msgSub.Out() {
evt := e.(event.MessageEventObj)
switch msg := e.(event.MessageEventObj).GetPayload().(type) {
case entity.ID:
if evt.GetAction() == entity.Sent || evt.GetAction() == entity.Failed {
m.chat.updateMessageStatus(msg, evt.GetAction())
}
case entity.Message:
m.messageHandler(msg)
}
}
}()
chatSub, err := m.bus.Subscribe(new(event.ChatEventObj))
if err != nil {
panic(err)
}
go func() {
defer chatSub.Close()
for e := range chatSub.Out() {
evt := e.(event.ChatEventObj)
switch evt.GetAction() {
case event.InviteReceived:
m.ChatAPI().Join(evt.GetPayload().(entity.ChatInfo))
case event.InviteSent:
// change Chat info so it keep track of chat request's
break;
}
}
}()
}
func (m *Messenger) messageHandler(msg entity.Message) {
err := m.chat.received(msg)
if err != nil {
return
}
}
func (m *Messenger) ContactBookAPI() ContactBookAPI {
return m.book
}
func (m *Messenger) IdentityAPI() IdentityAPI {
return m.identity
}
func (m *Messenger) ChatAPI() ChatAPI {
return m.chat
}
func (m *Messenger) EventBus() Bus {
return m.bus
}
func (m *Messenger) Stop() {
m.store.Close()
m.pms.Stop()
m.Host.Close()
}