Skip to content

Commit

Permalink
Merge pull request #52 from simple-robot/dev/public-some-message-api
Browse files Browse the repository at this point in the history
优化部分公开的内部消息解析API,并增加JVM平台对应的桥接API
  • Loading branch information
ForteScarlet authored Jun 20, 2024
2 parents 6e5e03a + 0208e66 commit 4392fc9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import love.forte.simbot.resource.toStringResource
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic

/**
* 将事件中接收到的 [OneBotMessageSegment] 解析为 [Message.Element]。

Check warning on line 38 in simbot-component-onebot-v11/simbot-component-onebot-v11-message/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/message/MessageElementResolvers.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Message'

Check warning on line 38 in simbot-component-onebot-v11/simbot-component-onebot-v11-message/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/message/MessageElementResolvers.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Element'
Expand All @@ -57,6 +58,7 @@ public fun OneBotMessageSegment.resolveToMessageElement(): Message.Element {
* @see resolveToOneBotSegment
*/
@InternalOneBotAPI
@JvmSynthetic
public suspend fun Message.resolveToOneBotSegmentList(): List<OneBotMessageSegment> {
return when (this) {
is Message.Element -> resolveToOneBotSegment()
Expand All @@ -71,6 +73,7 @@ public suspend fun Message.resolveToOneBotSegmentList(): List<OneBotMessageSegme
* 将一个 [Message.Element] 转化为用于API请求的 [OneBotMessageSegment]。

Check warning on line 73 in simbot-component-onebot-v11/simbot-component-onebot-v11-message/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/message/MessageElementResolvers.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Element'

Check warning on line 73 in simbot-component-onebot-v11/simbot-component-onebot-v11-message/src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/message/MessageElementResolvers.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unresolved reference in KDoc

Cannot resolve symbol 'Message'
*/
@InternalOneBotAPI
@JvmSynthetic
public suspend fun Message.Element.resolveToOneBotSegment(): OneBotMessageSegment? {
return when (this) {
// OB组件的 segment 类型,直接使用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
requires simbot.component.onebot.common;
requires static simbot.common.annotations;
requires transitive kotlinx.serialization.core;
requires simbot.common.suspendrunner;
requires kotlinx.coroutines.core;

exports love.forte.simbot.component.onebot.v11.message;
exports love.forte.simbot.component.onebot.v11.message.segment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,100 @@

package love.forte.simbot.component.onebot.v11.message

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import love.forte.simbot.annotations.Api4J
import love.forte.simbot.annotations.InternalSimbotAPI
import love.forte.simbot.component.onebot.common.annotations.InternalOneBotAPI
import love.forte.simbot.component.onebot.v11.message.segment.OneBotImage
import love.forte.simbot.component.onebot.v11.message.segment.OneBotMessageSegment
import love.forte.simbot.message.JvmOfflineImageValueResolver
import love.forte.simbot.message.Message
import love.forte.simbot.message.OfflineImage
import love.forte.simbot.message.OfflineImageValueResolver
import love.forte.simbot.resource.Resource
import love.forte.simbot.resource.URIResource
import love.forte.simbot.resource.toResource
import love.forte.simbot.suspendrunner.reserve.SuspendReserve
import love.forte.simbot.suspendrunner.reserve.suspendReserve
import love.forte.simbot.suspendrunner.runInAsync
import love.forte.simbot.suspendrunner.runInNoScopeBlocking
import java.io.File
import java.net.URI
import java.nio.file.Path
import java.util.concurrent.CompletableFuture
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.resume

/**
* 将 [Message] 解析为一用于API请求的 [OneBotMessageSegment] 列表。
*
* @see resolveToOneBotSegmentList
*/
@InternalOneBotAPI
@Api4J
public fun Message.resolveToOneBotSegmentListBlocking(): List<OneBotMessageSegment> =
runInNoScopeBlocking { resolveToOneBotSegmentList() }

/**
* 将 [Message] 解析为一用于API请求的 [OneBotMessageSegment] 列表。
*
* @see resolveToOneBotSegmentList
*/
@OptIn(InternalSimbotAPI::class)
@InternalOneBotAPI
@Api4J
public fun Message.resolveToOneBotSegmentListAsync(): CompletableFuture<out List<OneBotMessageSegment>> =
runInAsync { resolveToOneBotSegmentList() }

/**
* 将 [Message] 解析为一用于API请求的 [OneBotMessageSegment] 列表。
*
* @see resolveToOneBotSegmentList
*/
@OptIn(InternalSimbotAPI::class, DelicateCoroutinesApi::class)
@InternalOneBotAPI
@Api4J
public fun Message.resolveToOneBotSegmentListReserve(): SuspendReserve<List<OneBotMessageSegment>> =
suspendReserve(
GlobalScope,
EmptyCoroutineContext
) { resolveToOneBotSegmentList() }

/**
* 将一个 [Message.Element] 转化为用于API请求的 [OneBotMessageSegment]。
* @see resolveToOneBotSegment
*/
@InternalOneBotAPI
@Api4J
public fun Message.Element.resolveToOneBotSegmentBlocking(): OneBotMessageSegment? =
runInNoScopeBlocking { resolveToOneBotSegment() }

/**
* 将一个 [Message.Element] 转化为用于API请求的 [OneBotMessageSegment]。
* @see resolveToOneBotSegment
*/
@OptIn(InternalSimbotAPI::class)
@InternalOneBotAPI
@Api4J
public fun Message.Element.resolveToOneBotSegmentAsync(): CompletableFuture<out OneBotMessageSegment?> =
runInAsync { resolveToOneBotSegment() }

/**
* 将一个 [Message.Element] 转化为用于API请求的 [OneBotMessageSegment]。
* @see resolveToOneBotSegment
*/
@OptIn(DelicateCoroutinesApi::class, InternalSimbotAPI::class)
@InternalOneBotAPI
@Api4J
public fun Message.Element.resolveToOneBotSegmentReserve(): SuspendReserve<OneBotMessageSegment?> =
suspendReserve(
GlobalScope,
EmptyCoroutineContext
) { resolveToOneBotSegment() }


internal actual fun offlineImageResolver(): OfflineImageValueResolver<Continuation<OneBotMessageSegment?>> =
object : JvmOfflineImageValueResolver<Continuation<OneBotMessageSegment?>>() {
override fun resolveUnknownInternal(image: OfflineImage, context: Continuation<OneBotMessageSegment?>) {
Expand Down

0 comments on commit 4392fc9

Please sign in to comment.