Simplify with fold

This commit is contained in:
Him188 2020-03-17 12:57:14 +08:00
parent ee45ad636b
commit eb02c28e3b

View File

@ -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<String> {
MiraiPlatformUtils.Http.post<String> {
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<String> {
val rep = withContext(network.coroutineContext) {
MiraiPlatformUtils.Http.post<String> {
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<String> {
val data = withContext(network.coroutineContext) {
MiraiPlatformUtils.Http.post<String> {
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")