Skip to content

Commit

Permalink
[core] Remove jvmBase: Contact, FileSupported, ExternalResource, File…
Browse files Browse the repository at this point in the history
…CacheStrategy, RemoteFile
  • Loading branch information
Him188 committed Aug 12, 2023
1 parent 42763fd commit fc62c32
Show file tree
Hide file tree
Showing 12 changed files with 525 additions and 1,008 deletions.
81 changes: 69 additions & 12 deletions mirai-core-api/src/commonMain/kotlin/contact/Contact.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
* Copyright 2019-2023 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("EXPERIMENTAL_API_USAGE", "NOTHING_TO_INLINE", "EXPERIMENTAL_OVERRIDE")
@file:OptIn(JavaFriendlyAPI::class)
@file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_OVERRIDE")
@file:JvmBlockingBridge

package net.mamoe.mirai.contact
Expand All @@ -23,16 +22,16 @@ import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.recallMessage
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
import kotlin.coroutines.cancellation.CancellationException
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic
import java.io.File
import java.io.InputStream

/**
* 联系对象, 即可以与 [Bot] 互动的对象. 包含 [用户][User], 和 [群][Group].
*/
@NotStableForInheritance
public expect interface Contact : ContactOrBot, CoroutineScope {
public interface Contact : ContactOrBot, CoroutineScope {
/**
* 这个联系对象所属 [Bot].
*/
Expand Down Expand Up @@ -67,7 +66,8 @@ public expect interface Contact : ContactOrBot, CoroutineScope {
* 发送纯文本消息
* @see sendMessage
*/
public open suspend fun sendMessage(message: String): MessageReceipt<Contact>
public suspend fun sendMessage(message: String): MessageReceipt<Contact> =
this.sendMessage(message.toPlainText())

/**
* 上传一个 [资源][ExternalResource] 作为图片以备发送.
Expand All @@ -88,25 +88,82 @@ public expect interface Contact : ContactOrBot, CoroutineScope {
*/
public suspend fun uploadImage(resource: ExternalResource): Image

@JvmBlockingBridge
public companion object {
/**
* 读取 [InputStream] 到临时文件并将其作为图片发送到指定联系人
*
* 注意:此函数不会关闭 [imageStream]
*
* @param formatName 查看 [ExternalResource.formatName]
* @throws OverFileSizeMaxException
* @see FileCacheStrategy
*/
@JvmStatic
@JvmOverloads
public suspend fun <C : Contact> C.sendImage(
imageStream: InputStream,
formatName: String? = null
): MessageReceipt<C> = imageStream.sendAsImageTo(this, formatName)

/**
* 将文件作为图片发送到指定联系人
* @param formatName 查看 [ExternalResource.formatName]
* @throws OverFileSizeMaxException
* @see FileCacheStrategy
*/
@JvmStatic
@JvmOverloads
public suspend fun <C : Contact> C.sendImage(
file: File,
formatName: String? = null
): MessageReceipt<C> = file.sendAsImageTo(this, formatName)

/**
* 将资源作为单独的图片消息发送给 [this]
*
* @see Contact.sendMessage 最终调用, 发送消息.
*/
@JvmStatic
public suspend fun <C : Contact> C.sendImage(resource: ExternalResource): MessageReceipt<C>
public suspend fun <C : Contact> C.sendImage(resource: ExternalResource): MessageReceipt<C> =
resource.sendAsImageTo(this)


/**
* 读取 [InputStream] 到临时文件并将其作为图片上传, 但不发送
*
* 注意:本函数不会关闭流
*
* @param formatName 查看 [ExternalResource.formatName]
* @throws OverFileSizeMaxException
*/
@JvmStatic
@JvmOverloads
public suspend fun Contact.uploadImage(
imageStream: InputStream,
formatName: String? = null
): Image = imageStream.uploadAsImage(this@uploadImage, formatName)

/**
* 将文件作为图片上传, 但不发送
* @param formatName 查看 [ExternalResource.formatName]
* @throws OverFileSizeMaxException
*/
@JvmStatic
@JvmOverloads
public suspend fun Contact.uploadImage(
file: File,
formatName: String? = null
): Image = file.uploadAsImage(this, formatName)

/**
* 将文件作为图片上传, 但不发送
* @throws OverFileSizeMaxException
*/
@Throws(OverFileSizeMaxException::class, CancellationException::class)
@Throws(OverFileSizeMaxException::class)
@JvmStatic
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "EXTENSION_SHADOWED_BY_MEMBER")
@kotlin.internal.LowPriorityInOverloadResolution // for better Java API
public suspend fun Contact.uploadImage(resource: ExternalResource): Image
public suspend fun Contact.uploadImage(resource: ExternalResource): Image = this.uploadImage(resource)
}
}

Expand Down
21 changes: 19 additions & 2 deletions mirai-core-api/src/commonMain/kotlin/contact/FileSupported.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
* Copyright 2019-2023 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.
Expand All @@ -11,6 +11,7 @@
package net.mamoe.mirai.contact

import net.mamoe.mirai.contact.file.RemoteFiles
import net.mamoe.mirai.utils.DeprecatedSinceMirai
import net.mamoe.mirai.utils.NotStableForInheritance

/**
Expand All @@ -23,7 +24,23 @@ import net.mamoe.mirai.utils.NotStableForInheritance
* @see RemoteFiles
*/
@NotStableForInheritance
public expect interface FileSupported : Contact {
public interface FileSupported : Contact {
/**
* 文件根目录. 可通过 [net.mamoe.mirai.utils.RemoteFile.listFiles] 获取目录下文件列表.
*
* **注意:** 已弃用, 请使用 [files].
*
* @since 2.5
*/
@Suppress("DEPRECATION_ERROR")
@Deprecated(
"Please use files instead.",
replaceWith = ReplaceWith("files.root"),
level = DeprecationLevel.ERROR
) // deprecated since 2.8.0-RC
@DeprecatedSinceMirai(warningSince = "2.8", errorSince = "2.14")
public val filesRoot: net.mamoe.mirai.utils.RemoteFile

/**
* 获取远程文件列表 (管理器).
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
* Copyright 2019-2023 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.
Expand Down
Loading

0 comments on commit fc62c32

Please sign in to comment.