-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
dbclean.go
118 lines (101 loc) · 2.97 KB
/
dbclean.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
package modules
import (
"fmt"
"strings"
"time"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/divideprojects/Alita_Robot/alita/config"
"github.com/divideprojects/Alita_Robot/alita/db"
"github.com/divideprojects/Alita_Robot/alita/utils/helpers"
"github.com/divideprojects/Alita_Robot/alita/utils/string_handling"
log "github.com/sirupsen/logrus"
)
func (moduleStruct) dbClean(b *gotgbot.Bot, ctx *ext.Context) error {
user := ctx.EffectiveSender.User
memStatus := db.GetTeamMemInfo(user.Id)
// only dev can access this
if user.Id != config.OwnerId && !memStatus.Dev {
return ext.ContinueGroups
}
msg := ctx.EffectiveMessage
_, err := msg.Reply(
b,
"What do you want to clean?",
&gotgbot.SendMessageOpts{
ParseMode: helpers.HTML,
ReplyMarkup: gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{{Text: "Chats", CallbackData: "dbclean.chats"}},
},
},
},
)
if err != nil {
log.Error(err)
return err
}
return ext.EndGroups
}
func (moduleStruct) dbCleanButtonHandler(b *gotgbot.Bot, ctx *ext.Context) error {
query := ctx.Update.CallbackQuery
user := ctx.EffectiveSender.User
msg := query.Message
memStatus := db.GetTeamMemInfo(user.Id)
// permissions check
// only dev can access this
if user.Id != config.OwnerId && !memStatus.Dev {
query.Answer(
b,
&gotgbot.AnswerCallbackQueryOpts{
Text: "This button can only be used by an admin!",
},
)
return ext.ContinueGroups
}
action := strings.Split(query.Data, ".")[1]
var finalText, progressString string
var progress int
switch action {
case "chats":
log.Infof("Chats cleanup requested by %s", user.Username)
allChats := db.GetAllChats()
finalText = "No redundant chats found!"
var chatIds, inactiveChats []int64
for k := range allChats {
chatIds = append(chatIds, k)
}
for _, chatId := range chatIds {
// updates the message when percentage gets above the progress gap we have defined
if (string_handling.FindIndexInt64(chatIds, chatId)) > progress {
progressString = fmt.Sprintf("%d completed in getting invalid chats.", progress)
msg.EditText(b, progressString, nil)
progress += 5
}
// skip chats who are marked as not IsInactive
if !db.GetChatSettings(chatId).IsInactive {
time.Sleep(250 * time.Millisecond)
_, err := b.GetChat(chatId, nil)
// only mark chat as failed if it's giving bad request or unauthorized
if err != nil {
inactiveChats = append(inactiveChats, chatId)
}
}
}
if len(inactiveChats) > 0 {
finalText = fmt.Sprintf("%d chats marked as inactive!", len(inactiveChats))
log.Infof("These chats have been marked as inactive: %v", inactiveChats)
for _, chatId := range inactiveChats {
time.Sleep(250 * time.Millisecond)
db.ToggleInactiveChat(chatId, true)
}
}
}
_, _, err := msg.EditText(b, finalText, nil)
if err != nil {
log.Error(err)
return err
}
query.Answer(b, nil)
return ext.EndGroups
}