mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-05 23:50:08 +08:00
Improve Announcement logging
This commit is contained in:
parent
6145b3ef47
commit
51d354c5e9
@ -218,4 +218,10 @@ public inline fun Throwable.findCauseOrSelf(maxDepth: Int = 20, filter: (Throwab
|
||||
|
||||
public fun String.capitalize(): String {
|
||||
return replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.ROOT) else it.toString() }
|
||||
}
|
||||
|
||||
public fun String.truncated(length: Int, truncated: String = "..."): String {
|
||||
return if (this.length > length) {
|
||||
this.take(10) + truncated
|
||||
} else this
|
||||
}
|
@ -21,7 +21,7 @@ import net.mamoe.mirai.contact.announcement.OnlineAnnouncement
|
||||
*/
|
||||
//@SerialName(OnlineAnnouncementImpl.SERIAL_NAME)
|
||||
//@Serializable(OnlineAnnouncementImpl.Serializer::class)
|
||||
internal class OnlineAnnouncementImpl(
|
||||
internal data class OnlineAnnouncementImpl(
|
||||
override val group: Group,
|
||||
override val senderId: Long,
|
||||
override val sender: NormalMember?,
|
||||
|
@ -34,6 +34,9 @@ import net.mamoe.mirai.internal.contact.announcement.AnnouncementProtocol.getRaw
|
||||
import net.mamoe.mirai.internal.contact.announcement.AnnouncementProtocol.sendGroupAnnouncement
|
||||
import net.mamoe.mirai.internal.contact.announcement.AnnouncementProtocol.toAnnouncement
|
||||
import net.mamoe.mirai.internal.contact.announcement.AnnouncementProtocol.toGroupAnnouncement
|
||||
import net.mamoe.mirai.internal.network.highway.ChannelKind
|
||||
import net.mamoe.mirai.internal.network.highway.ResourceKind
|
||||
import net.mamoe.mirai.internal.network.highway.tryServersUpload
|
||||
import net.mamoe.mirai.internal.network.psKey
|
||||
import net.mamoe.mirai.internal.network.sKey
|
||||
import net.mamoe.mirai.internal.utils.io.writeResource
|
||||
@ -96,7 +99,24 @@ internal class AnnouncementsImpl(
|
||||
return bot.getGroupAnnouncement(group.id, fid).toAnnouncement(group)
|
||||
}
|
||||
|
||||
|
||||
private fun Announcement.describe(): String =
|
||||
"'${content.truncated(10)}' ${parameters.describe()}"
|
||||
|
||||
private fun AnnouncementParameters.describe(): String {
|
||||
return mutableListOf<String>().apply {
|
||||
if (image != null) add("with image")
|
||||
if (sendToNewMember) add("sendToNewMember")
|
||||
if (pinned) add("pinned")
|
||||
if (showEditCard) add("showEditCard")
|
||||
if (popup) add("popup")
|
||||
if (needConfirm) add("needConfirm")
|
||||
}.joinToString()
|
||||
}
|
||||
|
||||
override suspend fun publish(announcement: Announcement): OnlineAnnouncement = announcement.run {
|
||||
val id = announcement.hashCode()
|
||||
logger.verbose { "Publishing announcement #$id: ${announcement.describe()}" }
|
||||
val bot = group.bot
|
||||
group.checkBotPermission(MemberPermission.ADMINISTRATOR) { "Only administrator have permission to send group announcement" }
|
||||
val image = parameters.image
|
||||
@ -112,14 +132,28 @@ internal class AnnouncementsImpl(
|
||||
isAllRead = false,
|
||||
readMemberNumber = 0,
|
||||
publishTime = currentTimeSeconds()
|
||||
)
|
||||
).also {
|
||||
logger.verbose { "Publishing announcement #$id: success." }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun uploadImage(resource: ExternalResource): AnnouncementImage {
|
||||
return AnnouncementProtocol.uploadGroupAnnouncementImage(bot, resource)
|
||||
return tryServersUpload(
|
||||
bot,
|
||||
serversStub,
|
||||
resource.size,
|
||||
ResourceKind.ANNOUNCEMENT_IMAGE,
|
||||
ChannelKind.HTTP
|
||||
) { _, _ ->
|
||||
// use common logging
|
||||
|
||||
AnnouncementProtocol.uploadGroupAnnouncementImage(bot, resource)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val serversStub = listOf("web.qun.qq.com" to 80)
|
||||
|
||||
internal object AnnouncementProtocol {
|
||||
@Serializable
|
||||
data class UploadImageResp(
|
||||
@ -148,10 +182,12 @@ internal object AnnouncementProtocol {
|
||||
writeResource(resource)
|
||||
}
|
||||
})
|
||||
cookie("p_uin", "o${bot.id}")
|
||||
cookie("uin", "o$id")
|
||||
cookie("p_uin", "o$id")
|
||||
cookie("skey", sKey)
|
||||
cookie("p_skey", psKey("qun.qq.com"))
|
||||
}.loadSafelyAs(UploadImageResp.serializer()).checked()
|
||||
return resp.id.loadSafelyAs(GroupAnnouncementImage.serializer()).checked().toPublic()
|
||||
return resp.id.replace(""", "\"").loadSafelyAs(GroupAnnouncementImage.serializer()).checked().toPublic()
|
||||
}
|
||||
|
||||
@Serializable
|
||||
|
@ -1,10 +1,10 @@
|
||||
/*
|
||||
* Copyright 2019-2021 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.
|
||||
* 此源代码的使用受 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/master/LICENSE
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.internal.network.highway
|
||||
@ -129,6 +129,8 @@ internal enum class ResourceKind(
|
||||
|
||||
LONG_MESSAGE("long message"),
|
||||
FORWARD_MESSAGE("forward message"),
|
||||
|
||||
ANNOUNCEMENT_IMAGE("announcement image"),
|
||||
;
|
||||
|
||||
override fun toString(): String = display
|
||||
|
Loading…
Reference in New Issue
Block a user