Skip to content

Commit

Permalink
[core] Remove jvmBase target (#2772)
Browse files Browse the repository at this point in the history
* [core] Remove jvmBase: Contact, FileSupported, ExternalResource, FileCacheStrategy, RemoteFile

* [core] Remove jvmBase: AbsoluteFolder

* [core] Remove jvmBase: RoamingMessages

* [core] Remove jvmBase: EventChannel

* [core] Remove jvmBase: message

* [core] Remove jvmBase: logging

* [core] Remove jvmBase: Streamable

* [core] Remove jvmBase: AbstractBotConfiguration

* [core] Remove jvmBase: test and cleanup module
  • Loading branch information
Him188 authored Aug 14, 2023
1 parent 42763fd commit 5ef291f
Show file tree
Hide file tree
Showing 57 changed files with 1,691 additions and 3,437 deletions.
12 changes: 4 additions & 8 deletions mirai-core-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ kotlin {
implementation(project(":mirai-console-compiler-annotations"))
implementation(`kotlinx-serialization-protobuf`)
implementation(`kotlinx-atomicfu`)
implementation(`jetbrains-annotations`)

// runtime from mirai-core-utils
relocateCompileOnly(`ktor-io_relocated`)

implementation(`kotlin-jvm-blocking-bridge`)
implementation(`kotlin-dynamic-delegation`)

implementation(`log4j-api`)
compileOnly(`slf4j-api`)
}
}

Expand All @@ -61,14 +65,6 @@ kotlin {
}
}

findByName("jvmBaseMain")?.apply {
dependencies {
implementation(`jetbrains-annotations`)
implementation(`log4j-api`)
compileOnly(`slf4j-api`)
}
}

afterEvaluate {
findByName("androidUnitTest")?.apply {
dependencies {
Expand Down
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,19 +1,24 @@
/*
* 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:JvmBlockingBridge

package net.mamoe.mirai.contact.file

import kotlinx.coroutines.flow.Flow
import me.him188.kotlin.jvm.blocking.bridge.JvmBlockingBridge
import net.mamoe.mirai.contact.PermissionDeniedException
import net.mamoe.mirai.utils.ExternalResource
import net.mamoe.mirai.utils.JavaFriendlyAPI
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.ProgressionCallback
import java.util.stream.Stream

/**
* 绝对目录标识. 精确表示一个远程目录. 不会受同名文件或目录的影响.
Expand All @@ -23,8 +28,9 @@ import net.mamoe.mirai.utils.ProgressionCallback
* @see AbsoluteFile
* @see AbsoluteFileFolder
*/
@Suppress("SEALED_INHERITOR_IN_DIFFERENT_MODULE")
@NotStableForInheritance
public expect interface AbsoluteFolder : AbsoluteFileFolder {
public interface AbsoluteFolder : AbsoluteFileFolder {
/**
* 当前快照中文件数量, 当有文件更新时(上传/删除文件) 该属性不会更新.
*
Expand All @@ -37,7 +43,7 @@ public expect interface AbsoluteFolder : AbsoluteFileFolder {
/**
* 当该目录为空时返回 `true`.
*/
public open fun isEmpty(): Boolean
public fun isEmpty(): Boolean = contentsCount == 0

/**
* 返回更新了文件或目录信息 ([lastModifiedTime] 等) 的, 指向相同文件的 [AbsoluteFileFolder].
Expand All @@ -58,18 +64,42 @@ public expect interface AbsoluteFolder : AbsoluteFileFolder {
*/
public suspend fun folders(): Flow<AbsoluteFolder>

/**
* 获取该目录下所有子目录列表.
*
* 实现细节: 为了适合 Java 调用, 实现类似为阻塞式的 [folders], 因此不建议在 Kotlin 使用. 在 Kotlin 请使用 [folders].
*/
@JavaFriendlyAPI
public suspend fun foldersStream(): Stream<AbsoluteFolder>


/**
* 获取该目录下所有文件列表.
*/
public suspend fun files(): Flow<AbsoluteFile>

/**
* 获取该目录下所有文件列表.
*
* 实现细节: 为了适合 Java 调用, 实现类似为阻塞式的 [files], 因此不建议在 Kotlin 使用. 在 Kotlin 请使用 [files].
*/
@JavaFriendlyAPI
public suspend fun filesStream(): Stream<AbsoluteFile>


/**
* 获取该目录下所有文件和子目录列表.
*/
public suspend fun children(): Flow<AbsoluteFileFolder>

/**
* 获取该目录下所有文件和子目录列表.
*
* 实现细节: 为了适合 Java 调用, 实现类似为阻塞式的 [children], 因此不建议在 Kotlin 使用. 在 Kotlin 请使用 [children].
*/
@JavaFriendlyAPI
public suspend fun childrenStream(): Stream<AbsoluteFileFolder>

///////////////////////////////////////////////////////////////////////////
// resolve and upload
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -101,6 +131,8 @@ public expect interface AbsoluteFolder : AbsoluteFileFolder {
/**
* 精确获取 [AbsoluteFile.id] 为 [id] 的文件. 在目标文件不存在时返回 `null`. 当 [deep] 为 `true` 时还会深入子目录查找.
*/
@Suppress("OVERLOADS_INTERFACE", "ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") // Keep JVM ABI
@JvmOverloads
public suspend fun resolveFileById(
id: String,
deep: Boolean = false
Expand All @@ -113,13 +145,33 @@ public expect interface AbsoluteFolder : AbsoluteFileFolder {
path: String
): Flow<AbsoluteFile>

/**
* 根据路径获取指向的所有路径为 [path] 的文件列表. 同时支持相对路径和绝对路径. 支持获取子目录内的文件.
*
* 实现细节: 为了适合 Java 调用, 实现类似为阻塞式的 [resolveFiles], 因此不建议在 Kotlin 使用. 在 Kotlin 请使用 [resolveFiles].
*/
@JavaFriendlyAPI
public suspend fun resolveFilesStream(
path: String
): Stream<AbsoluteFile>

/**
* 根据路径获取指向的所有路径为 [path] 的文件和目录列表. 同时支持相对路径和绝对路径. 支持获取子目录内的文件和目录.
*/
public suspend fun resolveAll(
path: String
): Flow<AbsoluteFileFolder>

/**
* 根据路径获取指向的所有路径为 [path] 的文件和目录列表. 同时支持相对路径和绝对路径. 支持获取子目录内的文件和目录.
*
* 实现细节: 为了适合 Java 调用, 实现类似为阻塞式的 [resolveAll], 因此不建议在 Kotlin 使用. 在 Kotlin 请使用 [resolveAll].
*/
@JavaFriendlyAPI
public suspend fun resolveAllStream(
path: String
): Stream<AbsoluteFileFolder>

/**
* 上传一个文件到该目录, 返回上传成功的文件标识.
*
Expand All @@ -137,6 +189,8 @@ public expect interface AbsoluteFolder : AbsoluteFileFolder {
*
* @throws PermissionDeniedException 当无管理员权限时抛出 (若群仅允许管理员上传)
*/
@Suppress("OVERLOADS_INTERFACE", "ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") // Keep JVM ABI
@JvmOverloads
public suspend fun uploadNewFile(
filepath: String,
content: ExternalResource,
Expand All @@ -148,7 +202,6 @@ public expect interface AbsoluteFolder : AbsoluteFileFolder {
* 根目录 folder ID.
* @see id
*/
@Suppress("CONST_VAL_WITHOUT_INITIALIZER") // compiler bug
public const val ROOT_FOLDER_ID: String
public const val ROOT_FOLDER_ID: String = "/"
}
}
Loading

0 comments on commit 5ef291f

Please sign in to comment.