-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.go
231 lines (204 loc) · 5.11 KB
/
chat.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package core
import (
"errors"
"time"
"github.com/google/uuid"
"github.com/hood-chat/core/entity"
"github.com/hood-chat/core/protocol/invite"
rp "github.com/hood-chat/core/repo"
st "github.com/hood-chat/core/store"
)
var _ ChatAPI = (*Chat)(nil)
type ChatRepo = rp.IRepo[entity.ChatInfo]
type MessageRepo = rp.IRepo[entity.Message]
type Chat struct {
chRepo ChatRepo
mRepo MessageRepo
book ContactBookAPI
pms DirectService
gps PubSubService
Identity IdentityAPI
}
func NewChatAPI(store *st.Store, b ContactBookAPI, p DirectService, g PubSubService,i IdentityAPI) ChatAPI {
ch := rp.NewChatRepo(store)
m := rp.NewMessageRepo(store)
return &Chat{ch, m, b, p, g,i}
}
func (c *Chat) ChatInfo(id entity.ID) (entity.ChatInfo, error) {
rChat := c.chRepo
ci := entity.ChatInfo{}
chat, err := rChat.GetByID(id)
if err != nil {
return ci, err
}
return chat, nil
}
func (c *Chat) ChatInfos(skip int, limit int) ([]entity.ChatInfo, error) {
rChat := c.chRepo
opt := rp.NewOption(skip, limit)
return rChat.GetAll(opt)
}
func (c *Chat) New(opt NewChatOpt) (entity.ChatInfo, error) {
me, err := c.Identity.Get()
if err != nil {
return entity.ChatInfo{}, err
}
switch opt.Type {
case entity.Private:
chat := entity.NewPrivateChat(opt.Members[0], *me.ToContact())
err = c.chRepo.Add(chat)
return chat, err
case entity.Group:
members := append(opt.Members, *me.ToContact())
chat := entity.NewGroupChat(opt.Name, members, []entity.Contact{*me.ToContact()})
err := c.chRepo.Add(chat)
c.gps.Join(chat.ID, members)
return chat, err
default:
return entity.ChatInfo{}, errors.New("type not supported")
}
}
func (c *Chat) Messages(chatID entity.ID, skip int, limit int) ([]entity.Message, error) {
opt := rp.NewOption(skip, limit)
opt.AddFilter("ChatID", string(chatID))
return c.mRepo.GetAll(opt)
}
func (c *Chat) Message(ID entity.ID) (entity.Message, error) {
return c.mRepo.GetByID(ID)
}
func (c *Chat) Find(opt SearchChatOpt) ([]entity.ChatInfo, error) {
if opt.Type == entity.Private {
contactID := opt.Members[0]
con, err := c.book.Get(contactID)
if err != nil {
return nil, err
}
me, err := c.Identity.Get()
if err != nil {
return nil, err
}
chatID := entity.NewPrivateChat(con, *me.ToContact())
if err != nil {
return nil, err
}
chat, err := c.chRepo.GetByID(chatID.ID)
return []entity.ChatInfo{chat}, err
}
return nil, errors.New("type not supported")
}
func (c *Chat) Send(chatID entity.ID, content string) (*entity.Message, error) {
me, err := c.Identity.Get()
if err != nil {
return nil, err
}
rchat := c.chRepo
chat, err := rchat.GetByID(chatID)
if err != nil {
log.Errorf("Can not get chat %s", err.Error())
return nil, err
}
msg := entity.Message{
ID: entity.ID(uuid.New().String()),
ChatID: chatID,
CreatedAt: time.Now().UTC().Unix(),
Text: content,
Status: entity.Pending,
Author: *me.ToContact(),
ChatType: chat.Type,
}
rmsg := c.mRepo
err = rmsg.Add(msg)
if err != nil {
log.Errorf("Can not add message %s", err.Error())
return nil, err
}
switch chat.Type {
case entity.Private:
for _, to := range chat.Members {
if to.ID != msg.Author.ID {
log.Debugf("outbox message")
n,_ := entity.NewMessageEnvelop(to, msg)
c.pms.Send(n)
log.Debugf("outboxed message")
}
}
return &msg, nil
case entity.Group:
c.gps.Send(entity.PubSubEnvelop{Topic: chatID.String(), Message: msg})
return &msg, nil
default:
return nil, errors.New("Chat type not supported")
}
}
func (c Chat) received(msg entity.Message) error {
_, err := c.ChatInfo(msg.ChatID)
if err != nil {
log.Errorf("can not find chat %s", err.Error())
opt := NewChatOpt{
msg.Author.Name,
[]entity.Contact{msg.Author},
entity.Private,
}
_, err = c.New(opt)
if err != nil {
log.Errorf("fail to handle new message %s", err.Error())
return err
}
}
rmsg := c.mRepo
err = rmsg.Add(msg)
if err != nil {
log.Errorf("Can not add message %s , %d", err.Error(), msg)
return err
}
log.Debugf("new message %s ", msg)
return nil
}
func (c *Chat) Seen(chatID entity.ID) error {
opt := rp.NewOption(0, 0)
opt.AddFilter("ChatID", string(chatID))
opt.AddFilter("Status", []entity.Status{entity.Received})
unread, err := c.mRepo.GetAll(opt)
if err != nil {
return err
}
for _, msg := range unread {
msg.Status = entity.Seen
err = c.mRepo.Put(msg)
if err != nil {
return err
}
}
return nil
}
func (c *Chat) Join(ci entity.ChatInfo) error {
err := c.chRepo.Add(ci)
if err != nil {
return err
}
c.gps.Join(ci.ID, ci.Admins)
return nil
}
func (c *Chat) Invite(chatID entity.ID, cons []entity.Contact) error {
chat, err := c.chRepo.GetByID(chatID)
if err != nil {
return err
}
for _, con := range cons {
c.pms.Send(&entity.Envelop{To: con,Message: chat,ID: chat.ID.String(),CreatedAt: time.Now().Unix(),Protocol: invite.ID})
}
return nil
}
func (c *Chat) updateMessageStatus(msgID entity.ID, status entity.Status) error {
rmsg := c.mRepo
msg, err := rmsg.GetByID(msgID)
if err != nil {
return err
}
msg.Status = status
err = rmsg.Put(msg)
if err != nil {
return err
}
return nil
}