FlashImage.kt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2019-2021 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/master/LICENSE
  8. */
  9. @file:Suppress("NOTHING_TO_INLINE")
  10. package net.mamoe.mirai.message.data
  11. import kotlinx.serialization.SerialName
  12. import kotlinx.serialization.Serializable
  13. import net.mamoe.mirai.message.code.CodableMessage
  14. import net.mamoe.mirai.utils.MiraiExperimentalApi
  15. import net.mamoe.mirai.utils.safeCast
  16. /**
  17. * 闪照. 闪照的内容取决于 [image] 代表的图片.
  18. *
  19. * ## 构造闪照
  20. *
  21. * 需要首先获得普通图片才能构造闪照. 详见 [Image].
  22. *
  23. * - 使用 [FlashImage.from] 将普通图片转换为闪照.
  24. * - 在 Kotlin 使用类构造器顶层函数 `FlashImage(image)`.
  25. * - 在 Kotlin 使用扩展 [Image.flash].
  26. *
  27. * ## 获得闪照代表的原图片
  28. *
  29. * 访问属性 [FlashImage.image]
  30. *
  31. * ## mirai 码支持
  32. * 格式: [mirai:flash:*[Image.imageId]*]
  33. *
  34. * @see Image 查看图片相关信息
  35. */
  36. @Serializable
  37. @SerialName(FlashImage.SERIAL_NAME)
  38. public data class FlashImage(
  39. /**
  40. * 闪照的内容图片, 即一个普通图片.
  41. */
  42. @SerialName("imageId")
  43. @Serializable(Image.AsStringSerializer::class)
  44. public val image: Image
  45. ) : MessageContent, HummerMessage, CodableMessage, ConstrainSingle {
  46. override val key: MessageKey<FlashImage> get() = Key
  47. private val stringValue: String by lazy(LazyThreadSafetyMode.NONE) { "[mirai:flash:${image.imageId}]" }
  48. @MiraiExperimentalApi
  49. override fun appendMiraiCodeTo(builder: StringBuilder) {
  50. builder.append(stringValue)
  51. }
  52. override fun serializeToMiraiCode(): String = stringValue
  53. override fun toString(): String = stringValue
  54. override fun contentToString(): String = "[闪照]"
  55. public companion object Key :
  56. AbstractPolymorphicMessageKey<HummerMessage, FlashImage>(HummerMessage, { it.safeCast() }) {
  57. public const val SERIAL_NAME: String = "FlashImage"
  58. /**
  59. * 将普通图片转换为闪照.
  60. *
  61. * @param imageId 图片 id, 详见 [Image.imageId]
  62. */
  63. @JvmStatic
  64. public fun from(imageId: String): FlashImage = FlashImage(Image(imageId))
  65. /**
  66. * 将普通图片转换为闪照.
  67. *
  68. * @see Image.flash
  69. */
  70. @JvmStatic
  71. public inline fun from(image: Image): FlashImage = FlashImage(image)
  72. }
  73. }
  74. /**
  75. * 将普通图片转换为闪照.
  76. */
  77. @JvmSynthetic
  78. public inline fun FlashImage(imageId: String): FlashImage = FlashImage.from(imageId)
  79. /**
  80. * 将普通图片转换为闪照.
  81. */
  82. @JvmSynthetic
  83. public inline fun Image.flash(): FlashImage = FlashImage(this)