Skip to content

Commit 07d3e82

Browse files
authored
Merge pull request #878 from simple-robot/add-richMediaMessage
增加接口 RichMediaMessage 用以描述一个富媒体消息元素
2 parents 2acd92f + 7de9e0a commit 07d3e82

File tree

4 files changed

+132
-23
lines changed

4 files changed

+132
-23
lines changed

simbot-api/api/simbot-api.api

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,13 @@ public final class love/forte/simbot/message/AtAll : love/forte/simbot/message/M
18301830
public fun toString ()Ljava/lang/String;
18311831
}
18321832

1833+
public abstract interface class love/forte/simbot/message/BinaryDataAwareMessage {
1834+
public abstract synthetic fun binaryData (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
1835+
public fun getBinaryData ()[B
1836+
public fun getBinaryDataAsync ()Ljava/util/concurrent/CompletableFuture;
1837+
public fun getBinaryDataReserve ()Llove/forte/simbot/suspendrunner/reserve/SuspendReserve;
1838+
}
1839+
18331840
public final class love/forte/simbot/message/Emoji : love/forte/simbot/message/EmoticonMessage, love/forte/simbot/message/StandardMessage {
18341841
public static final field Companion Llove/forte/simbot/message/Emoji$Companion;
18351842
public fun <init> (Llove/forte/simbot/common/id/ID;)V
@@ -1891,7 +1898,7 @@ public abstract interface class love/forte/simbot/message/IDAwareImage : love/fo
18911898
public abstract fun getId ()Llove/forte/simbot/common/id/ID;
18921899
}
18931900

1894-
public abstract interface class love/forte/simbot/message/Image : love/forte/simbot/message/StandardMessage {
1901+
public abstract interface class love/forte/simbot/message/Image : love/forte/simbot/message/RichMediaMessage, love/forte/simbot/message/StandardMessage {
18951902
}
18961903

18971904
public abstract interface class love/forte/simbot/message/JvmOfflineImageResolver : love/forte/simbot/message/OfflineImageResolver {
@@ -2248,6 +2255,9 @@ public abstract interface class love/forte/simbot/message/RemoteUrlAwareImage :
22482255
public abstract synthetic fun url (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
22492256
}
22502257

2258+
public abstract interface class love/forte/simbot/message/RichMediaMessage : love/forte/simbot/message/StandardMessage {
2259+
}
2260+
22512261
public final class love/forte/simbot/message/SimpleOfflineResourceImage : love/forte/simbot/message/OfflineResourceImage {
22522262
public static final field Companion Llove/forte/simbot/message/SimpleOfflineResourceImage$Companion;
22532263
public fun <init> (Llove/forte/simbot/resource/Resource;)V
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
@file:JvmName("StandardMessages")
25+
@file:JvmMultifileClass
26+
27+
package love.forte.simbot.message
28+
29+
import love.forte.simbot.suspendrunner.STP
30+
import kotlin.jvm.JvmMultifileClass
31+
import kotlin.jvm.JvmName
32+
33+
34+
/**
35+
* 表示为一个可以得知 URL 地址的消息元素,
36+
* 例如 [UrlAwareImage]。
37+
* 应当由某个 [Message.Element] 的子类型实现。
38+
*
39+
* [UrlAwareMessage] 主要用于那些从服务端返回的消息元素使用,
40+
* 而不是本地构建的消息元素。
41+
*
42+
* @since 4.3.0
43+
*/
44+
public interface UrlAwareMessage {
45+
/**
46+
* 获取到链接字符串。
47+
* 如果链接信息包含在响应数据中,则会立即返回,
48+
* 否则会挂起并查询链接信息(例如通过网络接口查询)。
49+
* 如果需要查询,其内部不会缓存结果,因此每次调用 [url]
50+
* 均会产生挂起与查询行为。
51+
*
52+
* @throws IllegalStateException 如果当前状态无法查询信息,
53+
* 比如由于消息元素的序列化导致某些认证信息丢失。
54+
* @throws RuntimeException 在获取过程中可能产生的任何异常,
55+
* 比如网络请求问题、权限问题等。
56+
* JVM中的受检异常应当被包装为非受检异常。具体其他可能的异常请参考具体实现说明。
57+
*/
58+
@STP
59+
public suspend fun url(): String
60+
}
61+
62+
/**
63+
* 表示一个可以获取到其二进制数据的消息元素,
64+
* 例如某种图片消息或文件消息。
65+
* 应当由某个 [Message.Element] 的子类型实现。
66+
*
67+
* [BinaryDataAwareMessage] 主要用于那些从服务端返回的消息元素使用,
68+
* 而不是本地构建的消息元素。
69+
*
70+
* 注意:如果文件很大,则操作可能会比较耗时。
71+
*
72+
* @since 4.3.0
73+
*/
74+
public interface BinaryDataAwareMessage {
75+
/**
76+
* 获取到二进制数据。当需要进行网络请求才可得到内容时,会挂起。
77+
*
78+
* @throws IllegalStateException 如果当前状态无法读取数据,
79+
* 比如由于消息元素的序列化导致某些认证信息丢失。
80+
* @throws RuntimeException 在获取过程中可能产生的任何异常,
81+
* 比如网络请求问题、权限问题等。
82+
* JVM中的受检异常应当被包装为非受检异常。具体其他可能的异常请参考具体实现说明。
83+
*/
84+
@STP
85+
public suspend fun binaryData(): ByteArray
86+
}

simbot-api/src/commonMain/kotlin/love/forte/simbot/message/StandardMessages.kt

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import love.forte.simbot.message.Text.Companion.of
3737
import love.forte.simbot.resource.ByteArrayResource
3838
import love.forte.simbot.resource.Resource
3939
import love.forte.simbot.resource.ResourceBase64Serializer
40-
import love.forte.simbot.suspendrunner.STP
4140
import kotlin.io.encoding.ExperimentalEncodingApi
4241
import kotlin.js.JsName
4342
import kotlin.jvm.*
@@ -57,6 +56,7 @@ import kotlin.jvm.*
5756
*/
5857
public sealed interface StandardMessage : Message.Element
5958

59+
6060
//region Text
6161

6262
/**
@@ -223,23 +223,50 @@ public data object AtAll : MentionMessage
223223

224224
//endregion
225225

226-
//region Description
226+
//region RichMedia
227+
228+
/**
229+
* 一个可以表示富媒体的消息元素类型,
230+
* 即一个与二进制数据(例如文件、音频等)相关的非文字消息元素。
231+
*
232+
* 可以是本地或远程的类型,例如常见的 [Image]。
233+
*
234+
* 在不同的平台中,富媒体的表现方式或实现方式千变万化,
235+
* 它们的类型很可能并非标准消息类型中提供的已知类型。
236+
* 对于实现者,在实现 [RichMediaMessage] 类型的基础上,
237+
* 应当尽可能支持一些具有功能描述的标记性接口:
238+
* - [UrlAwareMessage]
239+
* - [BinaryDataAwareMessage]
240+
* 或它们的衍生类型,来表示你的实现类型具有哪些功能。
241+
*
242+
* @since 4.3.0
243+
*/
244+
public interface RichMediaMessage : StandardMessage
245+
227246

228247
/**
229-
* 一个图片消息元素类型。
248+
* 一个图片消息元素类型,
249+
* 最常见的 [RichMediaMessage] 类型之一。
230250
*
231251
* 图片消息可能被分为 [离线图片][OfflineImage]
232-
* 和 [远端图片][RemoteImage]。
252+
* 和 [远端图片][RemoteImage],也可能是由组件实现的独立特殊类型
233253
*
234254
* 在不同的平台中,图片的表现方式或实现方式千变万化,
235255
* 它们的类型很可能并非标准消息类型中提供的已知类型。
236-
* 对于实现者,应当尽可能支持 [UrlAwareImage]
237-
* 来表示一个能够得到 URL 信息的图片。
256+
* 对于实现者,在实现 [Image] 类型的基础上,
257+
* 应当尽可能支持一些具有功能描述的标记性接口:
258+
* - [UrlAwareMessage]
259+
* - [BinaryDataAwareMessage]
260+
* 或它们的衍生类型:
261+
* - [UrlAwareImage]
262+
* 等等,来表示你的实现类型具有哪些功能。
238263
*
264+
*
265+
* @see RichMediaMessage
239266
* @see OfflineImage
240267
* @see RemoteImage
241268
*/
242-
public interface Image : StandardMessage
269+
public interface Image : StandardMessage, RichMediaMessage
243270

244271
/**
245272
* 一个可以感知到 [ID] 信息的 [Image]。
@@ -453,17 +480,3 @@ public data class Emoji(public val id: ID) : StandardMessage, EmoticonMessage
453480
public data class Face(public val id: ID) : StandardMessage, EmoticonMessage
454481
//endregion
455482

456-
457-
/**
458-
* 表示为一个可以得知 URL 地址的消息元素,
459-
* 例如 [UrlAwareImage]。
460-
*
461-
* @since 4.3.0
462-
*/
463-
@STP
464-
public interface UrlAwareMessage {
465-
/**
466-
* 获取到链接字符串。
467-
*/
468-
public suspend fun url(): String
469-
}

website

Submodule website updated 59 files

0 commit comments

Comments
 (0)