Skip to content

Commit

Permalink
[core]修复从可确定图片类型的字符串组成的mirai code构造的图片type为UNKNOW或与期望类型不一致
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamusuta0320 committed Jul 30, 2023
1 parent 5867bd4 commit e08bccd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mirai-core-api/src/commonMain/kotlin/message/data/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ public interface Image : Message, MessageContent, CodableMessage {
* @see Image.imageType
*/
public var type: ImageType = ImageType.UNKNOWN
get() = if(field == ImageType.UNKNOWN) ImageType.match(imageId.split(".").last()) else field
/**
* @see Image.width
*/
Expand All @@ -324,6 +323,7 @@ public interface Image : Message, MessageContent, CodableMessage {


public fun build(): Image {
if(type == ImageType.UNKNOWN) type = ImageType.match(imageId.split(".").last())
@OptIn(MiraiInternalApi::class)
return InternalImageProtocol.instance.createImage(
imageId = imageId,
Expand Down
56 changes: 34 additions & 22 deletions mirai-core/src/commonTest/kotlin/message/ImageBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, type)
assertEquals(ImageType.UNKNOWN, type)
assertEquals(false, isEmoji)
}

Expand All @@ -43,7 +43,7 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, type)
assertEquals(ImageType.UNKNOWN, type)
assertEquals(false, isEmoji)
}

Expand All @@ -52,7 +52,7 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, type)
assertEquals(ImageType.UNKNOWN, type)
assertEquals(false, isEmoji)
}

Expand All @@ -67,25 +67,7 @@ internal class ImageBuilderTest : AbstractTest() {
}

@Test
fun legacyMethods() {
// just make sure they work

Mirai.createImage(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image.fromId(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
fun imageType() {
Image(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
Expand Down Expand Up @@ -126,6 +108,36 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(ImageType.UNKNOWN, imageType)
assertEquals(false, isEmoji)
}
}

@Test
fun legacyMethods() {
// just make sure they work

Mirai.createImage(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image.fromId(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID) {
width = 1
height = 2
Expand Down

0 comments on commit e08bccd

Please sign in to comment.