mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-24 15:00:38 +08:00
Simplify with fold
This commit is contained in:
parent
ee45ad636b
commit
eb02c28e3b
@ -20,6 +20,8 @@ import io.ktor.client.statement.HttpResponse
|
|||||||
import kotlinx.coroutines.CoroutineName
|
import kotlinx.coroutines.CoroutineName
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.io.ByteReadChannel
|
import kotlinx.coroutines.io.ByteReadChannel
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlinx.serialization.UnstableDefault
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.JsonConfiguration
|
import kotlinx.serialization.json.JsonConfiguration
|
||||||
import kotlinx.serialization.json.int
|
import kotlinx.serialization.json.int
|
||||||
@ -68,6 +70,7 @@ internal abstract class QQAndroidBotBase constructor(
|
|||||||
override val uin: Long get() = client.uin
|
override val uin: Long get() = client.uin
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@OptIn(UnstableDefault::class)
|
||||||
val json = Json(JsonConfiguration(ignoreUnknownKeys = true, encodeDefaults = true))
|
val json = Json(JsonConfiguration(ignoreUnknownKeys = true, encodeDefaults = true))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,7 +229,7 @@ internal abstract class QQAndroidBotBase constructor(
|
|||||||
@MiraiExperimentalAPI
|
@MiraiExperimentalAPI
|
||||||
override suspend fun _lowLevelGetAnnouncements(groupId: Long, page: Int, amount: Int): GroupAnnouncementList {
|
override suspend fun _lowLevelGetAnnouncements(groupId: Long, page: Int, amount: Int): GroupAnnouncementList {
|
||||||
val data = network.async {
|
val data = network.async {
|
||||||
HttpClient().post<String> {
|
MiraiPlatformUtils.Http.post<String> {
|
||||||
url("https://web.qun.qq.com/cgi-bin/announce/list_announce")
|
url("https://web.qun.qq.com/cgi-bin/announce/list_announce")
|
||||||
body = MultiPartFormDataContent(formData {
|
body = MultiPartFormDataContent(formData {
|
||||||
append("qid", groupId)
|
append("qid", groupId)
|
||||||
@ -254,8 +257,8 @@ internal abstract class QQAndroidBotBase constructor(
|
|||||||
@LowLevelAPI
|
@LowLevelAPI
|
||||||
@MiraiExperimentalAPI
|
@MiraiExperimentalAPI
|
||||||
override suspend fun _lowLevelSendAnnouncement(groupId: Long, announcement: GroupAnnouncement): String {
|
override suspend fun _lowLevelSendAnnouncement(groupId: Long, announcement: GroupAnnouncement): String {
|
||||||
val rep = network.async {
|
val rep = withContext(network.coroutineContext) {
|
||||||
HttpClient().post<String> {
|
MiraiPlatformUtils.Http.post<String> {
|
||||||
url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice")
|
url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice")
|
||||||
body = MultiPartFormDataContent(formData {
|
body = MultiPartFormDataContent(formData {
|
||||||
append("qid", groupId)
|
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
|
return jsonObj.jsonObject["new_fid"]?.primitive?.content
|
||||||
?: throw throw IllegalStateException("Send Announcement fail group:$groupId msg:${jsonObj.jsonObject["em"]} content:${announcement.msg.text}")
|
?: 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
|
@LowLevelAPI
|
||||||
@MiraiExperimentalAPI
|
@MiraiExperimentalAPI
|
||||||
override suspend fun _lowLevelDeleteAnnouncement(groupId: Long, fid: String) {
|
override suspend fun _lowLevelDeleteAnnouncement(groupId: Long, fid: String) {
|
||||||
val rep = network.async {
|
val data = withContext(network.coroutineContext) {
|
||||||
HttpClient().post<String> {
|
MiraiPlatformUtils.Http.post<String> {
|
||||||
url("https://web.qun.qq.com/cgi-bin/announce/del_feed")
|
url("https://web.qun.qq.com/cgi-bin/announce/del_feed")
|
||||||
body = MultiPartFormDataContent(formData {
|
body = MultiPartFormDataContent(formData {
|
||||||
append("qid", groupId)
|
append("qid", groupId)
|
||||||
@ -310,7 +313,6 @@ internal abstract class QQAndroidBotBase constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val data = rep.await()
|
|
||||||
val jsonObj = json.parseJson(data)
|
val jsonObj = json.parseJson(data)
|
||||||
if (jsonObj.jsonObject["ec"]?.int ?: 1 != 0) {
|
if (jsonObj.jsonObject["ec"]?.int ?: 1 != 0) {
|
||||||
throw throw IllegalStateException("delete Announcement fail group:$groupId msg:${jsonObj.jsonObject["em"]} fid:$fid")
|
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
|
private val bkn: Int
|
||||||
get() {
|
get() = client.wLoginSigInfo.sKey.data
|
||||||
val str = client.wLoginSigInfo.sKey.data.encodeToString()
|
.fold(5381) { acc: Int, b: Byte -> acc + acc.shl(5) + b.toInt() }
|
||||||
var magic = 5381
|
|
||||||
for (i in str) {
|
|
||||||
magic += magic.shl(5) + i.toInt()
|
|
||||||
}
|
|
||||||
return Int.MAX_VALUE.and(magic)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
|
Loading…
Reference in New Issue
Block a user