Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rich member card #1386

Draft
wants to merge 18 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public final class net/mamoe/mirai/contact/ContactKt {
}

public final class net/mamoe/mirai/contact/ContactList : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public final field delegate Ljava/util/concurrent/ConcurrentLinkedQueue;
public final field delegate Ljava/util/Collection;
public synthetic fun add (Ljava/lang/Object;)Z
public fun add (Lnet/mamoe/mirai/contact/Contact;)Z
public fun addAll (Ljava/util/Collection;)Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public final class net/mamoe/mirai/contact/ContactKt {
}

public final class net/mamoe/mirai/contact/ContactList : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public final field delegate Ljava/util/concurrent/ConcurrentLinkedQueue;
public final field delegate Ljava/util/Collection;
public synthetic fun add (Ljava/lang/Object;)Z
public fun add (Lnet/mamoe/mirai/contact/Contact;)Z
public fun addAll (Ljava/util/Collection;)Z
Expand Down
6 changes: 2 additions & 4 deletions mirai-core-api/src/commonMain/kotlin/contact/ContactList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import java.util.concurrent.ConcurrentLinkedQueue
* @see ContactList.asSequence
*/
@Suppress("unused")
public class ContactList<C : Contact>
@MiraiInternalApi public constructor(@JvmField @MiraiInternalApi public val delegate: ConcurrentLinkedQueue<C>) :
public class ContactList<out C : Contact>
@MiraiInternalApi public constructor(@JvmField @MiraiInternalApi public val delegate: MutableCollection<@UnsafeVariance C>) :
Collection<C> by delegate {
@MiraiInternalApi
public constructor(collection: Collection<C>) : this(ConcurrentLinkedQueue(collection))

@MiraiInternalApi
public constructor() : this(ConcurrentLinkedQueue())
Expand Down
30 changes: 15 additions & 15 deletions mirai-core-api/src/commonMain/kotlin/event/events/friend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@ import java.util.concurrent.atomic.AtomicBoolean
/**
* 好友昵称改变事件. 目前仅支持解析 (来自 PC 端的修改).
*/
public data class FriendRemarkChangeEvent internal constructor(
public data class FriendRemarkChangeEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
public val oldRemark: String,
public val newRemark: String
public val newRemark: String,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 成功添加了一个新好友的事件
*/
public data class FriendAddEvent @MiraiInternalApi constructor(
public data class FriendAddEvent @MiraiInternalApi public constructor(
/**
* 新好友. 已经添加到 [Bot.friends]
*/
public override val friend: Friend
public override val friend: Friend,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 好友已被删除或主动删除的事件.
*/
public data class FriendDeleteEvent internal constructor(
public override val friend: Friend
public data class FriendDeleteEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 一个账号请求添加机器人为好友的事件
*/
@Suppress("DEPRECATION")
public data class NewFriendRequestEvent internal constructor(
public data class NewFriendRequestEvent @MiraiInternalApi public constructor(
public override val bot: Bot,
/**
* 事件唯一识别号
Expand All @@ -76,7 +76,7 @@ public data class NewFriendRequestEvent internal constructor(
/**
* 群名片或好友昵称
*/
public val fromNick: String
public val fromNick: String,
) : BotEvent, Packet, AbstractEvent(), FriendInfoChangeEvent {
@JvmField
internal val responded: AtomicBoolean = AtomicBoolean(false)
Expand All @@ -97,25 +97,25 @@ public data class NewFriendRequestEvent internal constructor(
/**
* [Friend] 头像被修改. 在此事件广播前就已经修改完毕.
*/
public data class FriendAvatarChangedEvent internal constructor(
public override val friend: Friend
public data class FriendAvatarChangedEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
) : FriendEvent, Packet, AbstractEvent()

/**
* [Friend] 昵称改变事件, 在此事件广播时好友已经完成改名
* @see BotNickChangedEvent
*/
public data class FriendNickChangedEvent internal constructor(
public data class FriendNickChangedEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
public val from: String,
public val to: String
public val to: String,
) : FriendEvent, Packet, AbstractEvent(), FriendInfoChangeEvent

/**
* 好友输入状态改变的事件,当开始输入文字、退出聊天窗口或清空输入框时会触发此事件
*/
public data class FriendInputStatusChangedEvent internal constructor(
public data class FriendInputStatusChangedEvent @MiraiInternalApi public constructor(
public override val friend: Friend,
public val inputting: Boolean
public val inputting: Boolean,

) : FriendEvent, Packet, AbstractEvent()
) : FriendEvent, Packet, AbstractEvent()
16 changes: 7 additions & 9 deletions mirai-core-api/src/commonMain/kotlin/event/events/stranger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.Stranger
import net.mamoe.mirai.event.AbstractEvent
import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.utils.MiraiInternalApi

/**
* 新增陌生人的事件
*
*/
public data class StrangerAddEvent internal constructor(
public data class StrangerAddEvent @MiraiInternalApi public constructor(
/**
* 新的陌生人. 已经添加到 [Bot.strangers]
*/
public override val stranger: Stranger
public override val stranger: Stranger,
) : StrangerEvent, Packet, AbstractEvent()


Expand All @@ -32,19 +33,16 @@ public data class StrangerAddEvent internal constructor(
*
*/
public sealed class StrangerRelationChangeEvent(
public override val stranger: Stranger
public override val stranger: Stranger,
) : StrangerEvent, Packet, AbstractEvent() {
/**
* 主动删除陌生人或陌生人被删除的事件
*
* 除主动删除外,此事件为惰性广播,无法确保实时性
* 目前被动删除仅会在陌生人二次添加时才会进行广播
* 主动删除陌生人或陌生人被删除的事件, 不一定能接收到被动删除的事件
*/
public class Deleted(
/**
* 被删除的陌生人
*/
stranger: Stranger
stranger: Stranger,
) : StrangerRelationChangeEvent(stranger)

/**
Expand All @@ -62,7 +60,7 @@ public sealed class StrangerRelationChangeEvent(
*
* 已经添加到Bot的好友列表中
*/
public val friend: Friend
public val friend: Friend,
) : StrangerRelationChangeEvent(stranger)

}
3 changes: 2 additions & 1 deletion mirai-core-utils/src/commonMain/kotlin/Numbers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

package net.mamoe.mirai.utils

public fun Int.toLongUnsigned(): Long = this.toLong().and(0xFFFF_FFFF)
public fun Int.toLongUnsigned(): Long = this.toLong().and(0xFFFF_FFFF)
public fun Short.toIntUnsigned(): Int = this.toUShort().toInt()
153 changes: 153 additions & 0 deletions mirai-core-utils/src/commonMain/kotlin/ResultExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/


@file:JvmMultifileClass
@file:JvmName("MiraiUtils")

package net.mamoe.mirai.utils

import kotlin.reflect.KClass


@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun <R, T : R> Result<T>.recoverCatchingSuppressed(transform: (exception: Throwable) -> R): Result<R> {
return when (val exception = exceptionOrNull()) {
null -> this
else -> {
try {
Result.success(transform(exception))
} catch (e: Throwable) {
e.addSuppressed(exception)
Result.failure(e)
}
}
}
}

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun <R> retryCatching(
n: Int,
except: KClass<out Throwable>? = null,
block: (count: Int, lastException: Throwable?) -> R,
): Result<R> {
require(n >= 0) {
"param n for retryCatching must not be negative"
}
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block(it, exception))
} catch (e: Throwable) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressed(e)
exception = e
}
}
return Result.failure(exception!!)
}

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun <R> retryCatchingExceptions(
n: Int,
except: KClass<out Exception>? = null,
block: (count: Int, lastException: Throwable?) -> R,
): Result<R> {
require(n >= 0) {
"param n for retryCatching must not be negative"
}
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block(it, exception))
} catch (e: Exception) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressed(e)
exception = e
}
}
return Result.failure(exception!!)
}


@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
public inline fun <R> retryCatching(
n: Int,
except: KClass<out Throwable>? = null,
block: () -> R,
): Result<R> {
require(n >= 0) {
"param n for retryCatching must not be negative"
}
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block())
} catch (e: Throwable) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressed(e)
exception = e
}
}
return Result.failure(exception!!)
}

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
public inline fun <R> retryCatchingExceptions(
n: Int,
except: KClass<out Exception>? = null,
block: () -> R,
): Result<R> {
require(n >= 0) {
"param n for retryCatching must not be negative"
}
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block())
} catch (e: Exception) {
if (except?.isInstance(e) == true) {
return Result.failure(e)
}
exception?.addSuppressed(e)
exception = e
}
}
return Result.failure(exception!!)
}

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
public inline fun <R> runCatchingExceptions(block: () -> R): Result<R> {
return try {
Result.success(block())
} catch (e: Exception) {
Result.failure(e)
}
}

public inline fun <R> Result<R>.mapFailure(
block: (Throwable) -> Throwable,
): Result<R> = onFailure {
return Result.failure(block(it))
}
Loading