/
store.go
271 lines (231 loc) · 5.8 KB
/
store.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package telegram
import (
"fmt"
"github.com/google/uuid"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"time"
)
func NewMongoStore(mongo *mgo.Database) Store {
return &mongoStore{mongo}
}
/*
State is the current user state when flowing through a multi step process.
*/
type State struct {
Userid int
ChatId int64
State string
Field []string
}
type botRoom struct {
ChatId int64 `json:"id" bson:"_id,omitempty"`
Uuid uint32
RoomName string `json:"room_name"`
}
type authRequest struct {
ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
MessageID int
ChatId int64
Name string
Requested time.Time
ApprovedByName string
ApprovedById int
ApprovedTime time.Time
Used bool
}
type Store interface {
SetState(user int, chat int64, state string, field []string) error
getState(user int, chat int64) State
addBot(chat int64, name string) (id uint32, err error)
GetRoomKey(chat uint32) (roomid int64, err error)
GetUUID(chat int64, name string) (uuid uint32, err error)
newAuthRequest(msgid int, chat int64, name string) (string, error)
approveAuthRequest(id int, chat int64, approvedByName string, approvedById int) error
useToken(id string) (chat int64, err error)
saveMessageCorrelation(chat int64, message int, correlationId string) error
getCorrelationId(chat int64, message int) (string, error)
SaveReply(chat int64, message string, correlationId string) error
GetReplies(chat int64) ([]Replies, error)
AcknowledgeReply(id bson.ObjectId) error
}
type mongoStore struct {
mongo *mgo.Database
}
func (s mongoStore) SaveReply(chat int64, message string, correlationId string) error {
r := &Replies{
CorrelationId: correlationId,
ChatId: chat,
Message: message,
}
return s.mongo.C("replies").Insert(&r)
}
func (s mongoStore) GetReplies(chat int64) (r []Replies, err error) {
err = s.mongo.C("replies").Find(bson.M{"chatId": chat}).All(&r)
return
}
func (s mongoStore) AcknowledgeReply(id bson.ObjectId) error {
return s.mongo.C("replies").RemoveId(id)
}
func (s mongoStore) useToken(id string) (chat int64, err error) {
c := s.mongo.C("authrequests")
q := c.FindId(bson.ObjectIdHex(id))
i, err := q.Count()
if err != nil {
return
}
if i == 0 {
err = fmt.Errorf("unabkle to find approval request for id %v", id)
return
}
v := authRequest{}
q.One(&v)
if v.Used {
err = fmt.Errorf("approval token %v has already been used", id)
return
}
if v.ApprovedById == 0 {
err = fmt.Errorf("approval request %v has not been approved yet", id)
return
}
v.Used = true
err = c.UpdateId(v.ID, v)
if err != nil {
return
}
return v.ChatId, nil
}
func (s mongoStore) newAuthRequest(msgid int, chat int64, name string) (string, error) {
c := s.mongo.C("authrequests")
v := &authRequest{
ID: bson.NewObjectId(),
MessageID: msgid,
ChatId: chat,
Name: name,
Requested: time.Now(),
}
err := c.Insert(&v)
if err != nil {
return "", err
}
return v.ID.Hex(), nil
}
func (s mongoStore) approveAuthRequest(id int, chat int64, approvedByName string, approvedById int) error {
c := s.mongo.C("authrequests")
q := c.Find(bson.M{"messageid": id, "chatid": chat})
i, err := q.Count()
if err != nil {
return err
}
if i == 0 {
return fmt.Errorf("unabkle to find approval request for id %v", id)
}
v := authRequest{}
q.One(&v)
v.ApprovedById = approvedById
v.ApprovedByName = approvedByName
v.ApprovedTime = time.Now()
return c.UpdateId(v.ID, v)
}
func (s mongoStore) SetState(user int, chat int64, state string, field []string) error {
ss := State{Userid: user, State: state, Field: field, ChatId: chat}
c := s.mongo.C("userstate")
c.RemoveAll(bson.M{"userid": user, "chatid": chat})
return c.Insert(&ss)
}
func (s mongoStore) getState(user int, chat int64) State {
c := s.mongo.C("userstate")
var state State
c.Find(bson.M{"userid": user, "chatid": chat}).One(&state)
return state
}
func (s mongoStore) addBot(chat int64, room string) (id uint32, err error) {
c := s.mongo.C("botroom")
q := c.FindId(chat)
n, err := q.Count()
if err != nil {
return
}
if n > 0 {
g := botRoom{}
q.One(&g)
id = g.Uuid
return
}
g := botRoom{ChatId: chat, Uuid: uuid.New().ID(), RoomName: room}
err = c.Insert(&g)
id = g.Uuid
return
}
func (s mongoStore) GetRoomKey(chat uint32) (roomid int64, err error) {
c := s.mongo.C("botroom")
q := c.Find(bson.M{"uuid": chat})
n, err := q.Count()
if err != nil {
return
}
if n > 0 {
g := botRoom{}
q.One(&g)
roomid = g.ChatId
return
}
err = fmt.Errorf("no room found for id %v", chat)
return
}
func (s mongoStore) GetUUID(chat int64, room string) (uuid uint32, err error) {
c := s.mongo.C("botroom")
q := c.FindId(chat)
n, err := q.Count()
if err != nil {
return
}
if n > 0 {
g := botRoom{}
q.One(&g)
uuid = g.Uuid
if room != "" {
g.RoomName = room
c.UpdateId(chat, g)
}
return
}
err = fmt.Errorf("no UUID could be found for chat group %v", chat)
return
}
func (s mongoStore) saveMessageCorrelation(chat int64, message int, correlationId string) error {
id := fmt.Sprintf("%v_%v", chat, message)
r := &record{
ID: id,
CorrelationId: correlationId,
}
return s.mongo.C("reply_correlation").Insert(&r)
}
func (s mongoStore) getCorrelationId(chat int64, message int) (string, error) {
id := fmt.Sprintf("%v_%v", chat, message)
q := s.mongo.C("reply_correlation").FindId(id)
i, err := q.Count()
if err != nil {
return "", err
}
if i > 0 {
var r record
err = q.One(&r)
if err != nil {
return "", err
}
return r.CorrelationId, nil
}
return "", nil
}
type record struct {
ID string `bson:"_id,omitempty"`
CorrelationId string
}
//swagger:model
type Replies struct {
ID bson.ObjectId `bson:"_id,omitempty"`
CorrelationId string
ChatId int64 `bson:"chatId"`
Message string
}