-
Notifications
You must be signed in to change notification settings - Fork 36
/
PmInstance.kt
139 lines (80 loc) · 3.53 KB
/
PmInstance.kt
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
package io.nekohasekai.pm.database
import cn.hutool.core.date.DateUtil
import cn.hutool.core.date.SystemClock
import io.nekohasekai.ktlib.core.defaultLog
import io.nekohasekai.ktlib.core.receive
import io.nekohasekai.ktlib.td.core.TdException
import io.nekohasekai.ktlib.td.core.TdHandler
import io.nekohasekai.ktlib.td.core.extensions.displayName
import io.nekohasekai.ktlib.td.core.i18n.LocaleController
import io.nekohasekai.ktlib.td.core.i18n.localeFor
import io.nekohasekai.ktlib.td.core.raw.getChat
import io.nekohasekai.ktlib.td.core.raw.getMessageOrNull
import io.nekohasekai.pm.instance.messagesForCurrentBot
import io.nekohasekai.pm.launcher
import org.jetbrains.exposed.sql.*
interface PmInstance {
val admin: Long
val integration: BotIntegration?
val settings: BotSetting?
val blocks: UserBlocks.Cache
}
val PmInstance.L by receive<PmInstance, LocaleController> { (this as TdHandler).launcher.localeFor(admin) }
fun TdHandler.saveMessage(type: Int, chatId: Long, messageId: Long, targetId: Long? = null) {
database.write {
MessageRecords.insert {
it[MessageRecords.messageId] = messageId
it[MessageRecords.type] = type
it[MessageRecords.chatId] = chatId
if (targetId != null) it[MessageRecords.targetId] = targetId
it[createAt] = (SystemClock.now() / 100L).toInt()
it[botId] = me.id
}
}
}
suspend fun TdHandler.gc(instance: PmInstance) {
instance.blocks.gc()
var deleted = 0
val integration = instance.integration
val query = MessageRecords.select { messagesForCurrentBot and (MessageRecords.createAt less (DateUtil.yesterday().time / 100)) }
for (row in database { query.iterator() }) {
when (row[MessageRecords.type]) {
MessageRecords.MESSAGE_TYPE_INPUT_OTHER,
MessageRecords.MESSAGE_TYPE_INPUT_FORWARDED,
MessageRecords.MESSAGE_TYPE_OUTPUT_MESSAGE -> {
val messageId = row[MessageRecords.messageId]
try {
if (integration?.paused == false) {
getChat(integration.integration)
if (getMessageOrNull(integration.integration, messageId) != null) continue
}
getChat(instance.admin)
if (getMessageOrNull(instance.admin, messageId) != null) continue
} catch (ignored: TdException) {
}
deleted++
database.write {
MessageRecords.deleteWhere { messagesForCurrentBot and (MessageRecords.messageId eq messageId) }
}
}
}
}
for (row in database { query.iterator() }) {
when (row[MessageRecords.type]) {
MessageRecords.MESSAGE_TYPE_INPUT_MESSAGE,
MessageRecords.MESSAGE_TYPE_OUTPUT_FORWARDED -> {
val messageId = row[MessageRecords.messageId]
if (database { MessageRecords.select { messagesForCurrentBot and (MessageRecords.targetId eq messageId) }.firstOrNull() } != null) {
if (getMessageOrNull(row[MessageRecords.chatId], messageId) != null) continue
}
deleted++
database.write {
MessageRecords.deleteWhere { messagesForCurrentBot and (MessageRecords.messageId eq messageId) }
}
}
}
}
if (deleted > 0) {
defaultLog.info("[${me.displayName}] 回收了 $deleted 条 无效消息.")
}
}