forked from mamoe/mirai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlashImage.kt
102 lines (89 loc) · 3.09 KB
/
FlashImage.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
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("NOTHING_TO_INLINE")
package net.mamoe.mirai.message.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
/**
* 闪照. 闪照的内容取决于 [image] 代表的图片.
*
* ## 构造闪照
*
* 需要首先获得普通图片才能构造闪照. 详见 [Image].
*
* - 使用 [FlashImage.from] 将普通图片转换为闪照.
* - 在 Kotlin 使用类构造器顶层函数 `FlashImage(image)`.
* - 在 Kotlin 使用扩展 [Image.flash].
*
* ## 获得闪照代表的原图片
*
* 访问属性 [FlashImage.image]
*
* ## mirai 码支持
* 格式: [mirai:flash:*[Image.imageId]*]
*
* @see Image 查看图片相关信息
*/
@Serializable
@SerialName(FlashImage.SERIAL_NAME)
public data class FlashImage(
/**
* 闪照的内容图片, 即一个普通图片.
*/
@SerialName("imageId")
@Serializable(Image.AsStringSerializer::class)
public val image: Image
) : MessageContent, HummerMessage, CodableMessage, ConstrainSingle {
override val key: MessageKey<FlashImage> get() = Key
private val stringValue: String by lazy(LazyThreadSafetyMode.NONE) { "[mirai:flash:${image.imageId}]" }
@MiraiExperimentalApi
override fun appendMiraiCodeTo(builder: StringBuilder) {
builder.append(stringValue)
}
override fun serializeToMiraiCode(): String = stringValue
override fun toString(): String = stringValue
override fun contentToString(): String = "[闪照]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitFlashImage(this, data)
}
public companion object Key :
AbstractPolymorphicMessageKey<HummerMessage, FlashImage>(HummerMessage, { it.safeCast() }) {
public const val SERIAL_NAME: String = "FlashImage"
/**
* 将普通图片转换为闪照.
*
* @param imageId 图片 id, 详见 [Image.imageId]
*/
@JvmStatic
public fun from(imageId: String): FlashImage = FlashImage(Image(imageId))
/**
* 将普通图片转换为闪照.
*
* @see Image.flash
*/
@JvmStatic
public inline fun from(image: Image): FlashImage = FlashImage(image)
}
}
/**
* 将普通图片转换为闪照.
*/
@JvmSynthetic
public inline fun FlashImage(imageId: String): FlashImage = FlashImage.from(imageId)
/**
* 将普通图片转换为闪照.
*/
@JvmSynthetic
public inline fun Image.flash(): FlashImage = FlashImage(this)