From eb02c28e3b18b5b76418779a41f283b47fc5f564 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 17 Mar 2020 12:57:14 +0800 Subject: [PATCH] Simplify with `fold` --- .../net/mamoe/mirai/qqandroid/QQAndroidBot.kt | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt index fabc9c806..02248167a 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt @@ -20,6 +20,8 @@ import io.ktor.client.statement.HttpResponse import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.async import kotlinx.coroutines.io.ByteReadChannel +import kotlinx.coroutines.withContext +import kotlinx.serialization.UnstableDefault import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration import kotlinx.serialization.json.int @@ -68,6 +70,7 @@ internal abstract class QQAndroidBotBase constructor( override val uin: Long get() = client.uin companion object { + @OptIn(UnstableDefault::class) val json = Json(JsonConfiguration(ignoreUnknownKeys = true, encodeDefaults = true)) } @@ -226,7 +229,7 @@ internal abstract class QQAndroidBotBase constructor( @MiraiExperimentalAPI override suspend fun _lowLevelGetAnnouncements(groupId: Long, page: Int, amount: Int): GroupAnnouncementList { val data = network.async { - HttpClient().post { + MiraiPlatformUtils.Http.post { url("https://web.qun.qq.com/cgi-bin/announce/list_announce") body = MultiPartFormDataContent(formData { append("qid", groupId) @@ -254,8 +257,8 @@ internal abstract class QQAndroidBotBase constructor( @LowLevelAPI @MiraiExperimentalAPI override suspend fun _lowLevelSendAnnouncement(groupId: Long, announcement: GroupAnnouncement): String { - val rep = network.async { - HttpClient().post { + val rep = withContext(network.coroutineContext) { + MiraiPlatformUtils.Http.post { url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice") body = MultiPartFormDataContent(formData { append("qid", groupId) @@ -282,7 +285,7 @@ internal abstract class QQAndroidBotBase constructor( } } } - val jsonObj = json.parseJson(rep.await()) + val jsonObj = json.parseJson(rep) return jsonObj.jsonObject["new_fid"]?.primitive?.content ?: throw throw IllegalStateException("Send Announcement fail group:$groupId msg:${jsonObj.jsonObject["em"]} content:${announcement.msg.text}") } @@ -290,8 +293,8 @@ internal abstract class QQAndroidBotBase constructor( @LowLevelAPI @MiraiExperimentalAPI override suspend fun _lowLevelDeleteAnnouncement(groupId: Long, fid: String) { - val rep = network.async { - HttpClient().post { + val data = withContext(network.coroutineContext) { + MiraiPlatformUtils.Http.post { url("https://web.qun.qq.com/cgi-bin/announce/del_feed") body = MultiPartFormDataContent(formData { append("qid", groupId) @@ -310,7 +313,6 @@ internal abstract class QQAndroidBotBase constructor( } } } - val data = rep.await() val jsonObj = json.parseJson(data) if (jsonObj.jsonObject["ec"]?.int ?: 1 != 0) { throw throw IllegalStateException("delete Announcement fail group:$groupId msg:${jsonObj.jsonObject["em"]} fid:$fid") @@ -361,17 +363,11 @@ internal abstract class QQAndroidBotBase constructor( } /** - * 获取 获取群公告 所需的bkn参数 + * 获取 获取群公告 所需的 bkn 参数 * */ - val bkn: Int - get() { - val str = client.wLoginSigInfo.sKey.data.encodeToString() - var magic = 5381 - for (i in str) { - magic += magic.shl(5) + i.toInt() - } - return Int.MAX_VALUE.and(magic) - } + private val bkn: Int + get() = client.wLoginSigInfo.sKey.data + .fold(5381) { acc: Int, b: Byte -> acc + acc.shl(5) + b.toInt() } } @Suppress("DEPRECATION")