Skip to content

Commit

Permalink
chore(message): fit message chain persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
KurenaiRyu committed Aug 12, 2024
1 parent 0ce7890 commit 73f44a7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 44 deletions.
3 changes: 3 additions & 0 deletions src/main/java/kurenai/imsyncbot/domain/QQMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class QQMessage {
@Enumerated(EnumType.STRING)
@ColumnDefault("NORMAL")
private MessageStatus status = MessageStatus.NORMAL;
@Column(name = "JSON_TXT")
@Lob
private String json;
private Boolean handled;
private LocalDateTime time;
@Version
Expand Down
41 changes: 4 additions & 37 deletions src/main/kotlin/kurenai/imsyncbot/bot/qq/QQBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class QQBot(
handleStatus()
}

private suspend fun handleStatus() {
private fun handleStatus() {
var previous: BotStatus? = null
status.onEach {
if (bot.groupConfigService.defaultTgGroup < 0) return@onEach
Expand Down Expand Up @@ -184,10 +184,8 @@ class QQBot(
try {
when (event) {
is MessageEvent -> {
val message = event.source.toEntity()
CoroutineScope(Dispatchers.IO).launch {
MessageService.save(message)
}
val message = event.toEntity()
MessageService.save(event.toEntity())
when (event) {
is FriendMessageEvent -> {
// bot.qqMessageHandler.onFriendMessage(
Expand Down Expand Up @@ -250,37 +248,6 @@ class QQBot(
}
}

suspend fun restart() {
kotlin.runCatching {
qqBot.login()
}.recoverCatching { ex ->
log.error("Re login failed, try to create a new instance...", ex)
start(true)
}.getOrThrow()
}

// suspend fun reportError(group: Group, messageChain: MessageChain, throwable: Throwable) {
// log.error(throwable.message, throwable)
// try {
//// val senderId = messageChain.source.fromId
//// val master = bot.getFriend(imSyncBot.userConfig.masterQQ)
//// master?.takeIf { it.id != 0L }?.sendMessage(
//// master.sendMessage(messageChain).quote()
//// .plus("group: ${group.name}(${group.id}), sender: ${group[senderId]?.remarkOrNameCardOrNick}(${senderId})\n\n消息发送失败: (${throwable::class.simpleName}) ${throwable.message}")
//// )
// kotlin.runCatching {
// SendMessage(
// (bot.groupConfig.qqTg[group.id] ?: bot.groupConfig.defaultTgGroup).toString(),
// messageChain.contentToString()
// ).send()
// }.onFailure {
// log.error("Report error fail.", it)
// }.getOrNull()
// } catch (e: Exception) {
// log.error("Report error fail: ${e.message}", e)
// }
// }

private suspend fun sendRemindMsg(event: GroupAwareMessageEvent) {
if (bot.userConfigService.masterUsername.isBlank()) return
val content = event.message.filterIsInstance<PlainText>().map(PlainText::content).joinToString(separator = "")
Expand All @@ -302,7 +269,7 @@ class QQBot(
}
}

fun destroy() {
fun close() {
try {
log.info("Close qq bot...")
qqBot.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class QQMessageHandler(
})
if (context.entity == null) return@also
CoroutineScope(bot.coroutineContext).launch {
MessageService.cache(context.entity, context.messageChain.source, messages)
MessageService.cache(context.entity, context.messageChain, messages)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/kurenai/imsyncbot/service/MessageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.QuoteReply
import net.mamoe.mirai.message.data.source
import net.mamoe.mirai.message.sourceMessage
import kotlin.jvm.optionals.getOrNull


Expand All @@ -40,13 +41,13 @@ object MessageService {
* @param messageChain
* @param messages
*/
suspend fun cache(entity: QQMessage?, source: MessageSource, messages: Array<TdApi.Message>? = null) =
suspend fun cache(entity: QQMessage?, chain: MessageChain, messages: Array<TdApi.Message>? = null) =
runCatching {
withIO {
val qqMsg = qqMessageRepository.save(
entity?.apply {
handled = true
} ?: source.toEntity(true)
} ?: chain.toEntity(handled = true)
)
messages?.map {
QQTg().apply {
Expand All @@ -69,7 +70,7 @@ object MessageService {
* @param message
*/
suspend fun cache(receipt: MessageReceipt<*>, message: TdApi.Message) {
cache(null, receipt.source, arrayOf(message))
cache(null, receipt.sourceMessage, arrayOf(message))
}

suspend fun findRelationByQuote(chain: MessageChain): QQTg? {
Expand Down
21 changes: 18 additions & 3 deletions src/main/kotlin/kurenai/imsyncbot/utils/BotUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import it.tdlight.jni.TdApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.await
import kotlinx.coroutines.withContext
import kurenai.imsyncbot.bot.qq.QQBot
import kurenai.imsyncbot.domain.QQMessage
import kurenai.imsyncbot.exception.BotException
import kurenai.imsyncbot.snowFlake
import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.MessageSourceBuilder
import net.mamoe.mirai.utils.toIntOrFail
import net.mamoe.mirai.message.data.source
import top.mrxiaom.overflow.Overflow
import top.mrxiaom.overflow.OverflowAPI
import top.mrxiaom.overflow.contact.RemoteBot
import java.nio.file.Files
import java.nio.file.Path
import java.time.LocalDateTime
Expand Down Expand Up @@ -193,14 +202,20 @@ object BotUtil {

/////////////////////////// message ///////////////////////////

fun MessageSource.toEntity(handled: Boolean = false): QQMessage {
val source = this
fun MessageEvent.toEntity(handled: Boolean = false): QQMessage {
return this.message.toEntity(this.bot, handled)
}

fun MessageChain.toEntity(bot: Bot? = null, handled: Boolean = false): QQMessage {
val source = this.source
val jsonTxt = Overflow.serializeMessage(bot as? RemoteBot, this)
return QQMessage().apply {
messageId = source.ids[0]
botId = source.botId
targetId = source.targetId
fromId = source.fromId
type = source.kind
json = jsonTxt
this.handled = handled
time = source.localDateTime()
}
Expand Down

0 comments on commit 73f44a7

Please sign in to comment.