-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.go
265 lines (235 loc) · 6.36 KB
/
methods.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"strconv"
"time"
)
// bot message type
type tMessage struct {
chatConfig *tChatConfig
ChatID int64
MsgID int
TimeStamp int
}
// method delete message from Redis and telegram
func (msg tMessage) Delete() {
// delete from telegram
delMsg := tgbotapi.DeleteMessageConfig{
MessageID: msg.MsgID,
ChatID: msg.ChatID,
}
resp, err := BOT.DeleteMessage(delMsg)
if err != nil {
switch resp.ErrorCode {
case 400:
log.Printf("Warning: %s from chat %s", resp.Description, msg.chatConfig)
default:
log.Printf("Error: %s. The message %s will be deleted later from chat %s.",
err, msg, msg.chatConfig)
return
}
}
log.Printf("The message %s has been deleted from chat %s",
msg, msg.chatConfig)
// delete from Redis
key := fmt.Sprintf("msg_%d_%d", msg.ChatID, msg.MsgID)
if err := DeleteFromDB(key); err != nil {
log.Printf("Error: message %s has not been deleted from redis: %s", msg, err)
}
}
// aging test message method
func (msg tMessage) IsOutdated() bool {
delta := int(time.Now().Unix()) - msg.TimeStamp
if delta >= msg.chatConfig.Timeout {
return true
}
return false
}
// method saving message to Redis
func (msg tMessage) Save() bool {
jsonMessage, _ := json.Marshal(msg)
key := fmt.Sprintf("msg_%d_%d", msg.ChatID, msg.MsgID)
if err := SaveToDB(key, jsonMessage); err != nil {
log.Println("Failed to save message:", err)
return false
}
return true
}
func (msg tMessage) String() string {
return strconv.Itoa(msg.MsgID)
}
// chat configuration type
type tChatConfig struct {
ChatID int64
Timeout int
ChatTitle string
Enabled bool
}
func (cnf tChatConfig) String() string {
return cnf.ChatTitle
}
// method saving configuration to Redis
func (cnf tChatConfig) Save() bool {
jsonConfig, _ := json.Marshal(cnf)
key := fmt.Sprintf("chat_%d", cnf.ChatID)
if err := SaveToDB(key, jsonConfig); err != nil {
log.Println("Failed to save message:", err)
return false
}
return true
}
// change method garbage collector timeout in configuration
func (cnf *tChatConfig) ChangeTimeout(timeout int) error {
if timeout > 0 && timeout <= SETTING.timeoutLimit {
cnf.Timeout = timeout
cnf.Save()
return nil
}
if timeout <= 0 {
return errors.New("timeout must be greater than 0")
} else if timeout >= SETTING.timeoutLimit {
maxTimeHuman, _ := time.ParseDuration(fmt.Sprintf("%ds", SETTING.timeoutLimit))
return errors.New(fmt.Sprintf("maximum timeout value: %s", maxTimeHuman))
}
log.Printf("ERROR: unknown timeout error, raw timeout: %d", timeout)
return errors.New("unknown timeout error")
}
// enable\disable saving message method
func (cnf *tChatConfig) ChangeStatus(enabled bool) bool {
cnf.Enabled = enabled
return cnf.Save()
}
// method checking enable status
func (cnf tChatConfig) IsEnabled() bool {
return cnf.Enabled
}
// delete configuration method
func (cnf tChatConfig) DeleteConfig() bool {
key := fmt.Sprintf("chat_%d", cnf.ChatID)
err := DeleteFromDB(key)
if err != nil {
log.Printf("Failed to delete chat %s configuration: %s", cnf, err)
return false
}
return true
}
// method getting all message for chat
func (cnf tChatConfig) GetAllChatMessage() []tMessage {
key := fmt.Sprintf("msg_%d_*", cnf.ChatID)
jsonMessages, err := LoadFromDB(key)
if err != nil {
log.Printf("Error occurred with loading all chat %s messages: %s", cnf, err)
return make([]tMessage, 0)
}
chatMessages := make([]tMessage, len(jsonMessages))
for n, item := range jsonMessages {
var message tMessage
if err := json.Unmarshal([]byte(item), &message); err != nil {
log.Println("Error occurred with unmarshal message:", err)
continue
}
if message.ChatID != cnf.ChatID {
log.Printf("Warning! The message %s does not belong to the chat %s. Skip!",
message, cnf)
continue
}
// add chat configuration to message
message.chatConfig = &cnf
chatMessages[n] = message
}
return chatMessages
}
// method deleting all chat messages
func (cnf tChatConfig) DeleteAllChatMessages() {
for _, message := range cnf.GetAllChatMessage() {
message.Delete()
}
}
// all chat configuration type
type Configs map[int64]*tChatConfig
// chat check method
func (c Configs) Exist(chatID int64) bool {
if _, ok := c[chatID]; ok {
return true
}
return false
}
// chat config exist and enable
func (c Configs) ExistAndEnable(chatID int64) bool {
if config, ok := c[chatID]; ok {
return config.Enabled
}
return false
}
// get all messages for all chats
func GetAllMessages(configs Configs) []tMessage {
jsonMessages, err := LoadFromDB("msg_*")
if err != nil {
log.Println("Error occurred with loading all messages:", err)
return make([]tMessage, 0)
}
allMessages := make([]tMessage, len(jsonMessages))
for n, item := range jsonMessages {
var message tMessage
if err := json.Unmarshal([]byte(item), &message); err != nil {
log.Println("Error occurred with unmarshal message:", err)
continue
}
// if message chat exist
if !configs.Exist(message.ChatID) {
log.Printf("Chat %d not found for message %s. Skip", message.ChatID, message)
continue
}
// set chat configuration in to message object
message.chatConfig = configs[message.ChatID]
allMessages[n] = message
}
return allMessages
}
// create and save new message
func NewMessage(chatID int64, msgID, timestamp int) {
newMsg := tMessage{
ChatID: chatID,
MsgID: msgID,
TimeStamp: timestamp,
}
if !newMsg.Save() {
log.Printf("Message %d from chat %d don't save", msgID, chatID)
}
}
// create and save new configuration
func NewChatConfig(chatID int64, timeout int, title string) *tChatConfig {
newConfig := tChatConfig{
ChatTitle: title,
Timeout: timeout,
Enabled: true,
ChatID: chatID,
}
if !newConfig.Save() {
log.Printf("New chat configuration fro chat %d (%s) don't save", chatID, title)
}
return &newConfig
}
// get all chat configuration
func GetChatConfigs() Configs {
chatConfigs := make(Configs)
values, err := LoadFromDB("chat_*")
if err != nil {
log.Println("Error occurred with loading chat configurations", err)
return chatConfigs
}
for _, item := range values {
var config tChatConfig
err := json.Unmarshal([]byte(item), &config)
if err != nil {
log.Println("Error occurred with unmarshal configuration:", err)
continue
}
chatConfigs[config.ChatID] = &config
}
return chatConfigs
}