feat(message_logger): quoted messages support

This commit is contained in:
rhunk 2024-01-17 17:37:24 +01:00
parent ec0fd2cf08
commit a95c75a0b6
5 changed files with 25 additions and 11 deletions

View File

@ -158,4 +158,13 @@ enum class MixerStoryType(
return entries.firstOrNull { it.index == index } ?: UNKNOWN return entries.firstOrNull { it.index == index } ?: UNKNOWN
} }
} }
}
enum class QuotedMessageContentStatus {
UNKNOWN,
AVAILABLE,
DELETED,
JOINEDAFTERORIGINALMESSAGESENT,
UNAVAILABLE,
STORYMEDIADELETEDBYPOSTER
} }

View File

@ -27,9 +27,9 @@ class EventBus(
} }
} }
inline fun <T : Event> subscribe(event: KClass<T>, priority: Int? = null, crossinline listener: (T) -> Unit) = subscribe(event, { true }, priority, listener) fun <T : Event> subscribe(event: KClass<T>, priority: Int? = null, listener: (T) -> Unit) = subscribe(event, { true }, priority, listener)
inline fun <T : Event> subscribe(event: KClass<T>, crossinline filter: (T) -> Boolean, priority: Int? = null, crossinline listener: (T) -> Unit): () -> Unit { fun <T : Event> subscribe(event: KClass<T>, filter: (T) -> Boolean, priority: Int? = null, listener: (T) -> Unit): () -> Unit {
val obj = object : IListener<T> { val obj = object : IListener<T> {
override fun handle(event: T) { override fun handle(event: T) {
if (!filter(event)) return if (!filter(event)) return

View File

@ -9,6 +9,7 @@ import com.google.gson.JsonObject
import com.google.gson.JsonParser import com.google.gson.JsonParser
import me.rhunk.snapenhance.common.data.ContentType import me.rhunk.snapenhance.common.data.ContentType
import me.rhunk.snapenhance.common.data.MessageState import me.rhunk.snapenhance.common.data.MessageState
import me.rhunk.snapenhance.common.data.QuotedMessageContentStatus
import me.rhunk.snapenhance.common.util.ktx.longHashCode import me.rhunk.snapenhance.common.util.ktx.longHashCode
import me.rhunk.snapenhance.common.util.protobuf.ProtoReader import me.rhunk.snapenhance.common.util.protobuf.ProtoReader
import me.rhunk.snapenhance.core.event.events.impl.BindViewEvent import me.rhunk.snapenhance.core.event.events.impl.BindViewEvent
@ -38,7 +39,7 @@ class MessageLogger : Feature("MessageLogger",
private val threadPool = Executors.newFixedThreadPool(10) private val threadPool = Executors.newFixedThreadPool(10)
private val cachedIdLinks = mutableMapOf<Long, Long>() // client id -> server id private val cachedIdLinks = EvictingMap<Long, Long>(500) // client id -> server id
private val fetchedMessages = mutableListOf<Long>() // list of unique message ids private val fetchedMessages = mutableListOf<Long>() // list of unique message ids
private val deletedMessageCache = EvictingMap<Long, JsonObject>(200) // unique message id -> message json object private val deletedMessageCache = EvictingMap<Long, JsonObject>(200) // unique message id -> message json object
@ -112,8 +113,11 @@ class MessageLogger : Feature("MessageLogger",
val uniqueMessageIdentifier = computeMessageIdentifier(conversationId, event.message.orderKey!!) val uniqueMessageIdentifier = computeMessageIdentifier(conversationId, event.message.orderKey!!)
val messageContentType = event.message.messageContent!!.contentType val messageContentType = event.message.messageContent!!.contentType
val isMessageDeleted = messageContentType == ContentType.STATUS || event.message.messageContent!!.quotedMessage?.status?.let {
it == QuotedMessageContentStatus.DELETED || it == QuotedMessageContentStatus.STORYMEDIADELETEDBYPOSTER
} == true
if (messageContentType != ContentType.STATUS) { if (!isMessageDeleted) {
if (messageFilter.isNotEmpty() && !messageFilter.contains(messageContentType?.name)) return@subscribe if (messageFilter.isNotEmpty() && !messageFilter.contains(messageContentType?.name)) return@subscribe
if (fetchedMessages.contains(uniqueMessageIdentifier)) return@subscribe if (fetchedMessages.contains(uniqueMessageIdentifier)) return@subscribe
fetchedMessages.add(uniqueMessageIdentifier) fetchedMessages.add(uniqueMessageIdentifier)
@ -136,18 +140,16 @@ class MessageLogger : Feature("MessageLogger",
} }
} ?: return@subscribe } ?: return@subscribe
val messageJsonObject = deletedMessageObject.asJsonObject
//if the message is a snap make it playable //if the message is a snap make it playable
if (messageJsonObject["mMessageContent"]?.asJsonObject?.get("mContentType")?.asString == "SNAP") { if (deletedMessageObject["mMessageContent"]?.asJsonObject?.get("mContentType")?.asString == "SNAP") {
messageJsonObject["mMetadata"].asJsonObject.addProperty("mPlayableSnapState", "PLAYABLE") deletedMessageObject["mMetadata"].asJsonObject.addProperty("mPlayableSnapState", "PLAYABLE")
} }
//serialize all properties of messageJsonObject and put mMessageContent & mMetadata in the message object //serialize all properties of messageJsonObject and put mMessageContent & mMetadata in the message object
messageInstance.javaClass.declaredFields.forEach { field -> messageInstance::class.java.declaredFields.forEach { field ->
if (field.name != "mMessageContent" && field.name != "mMetadata") return@forEach if (field.name != "mMessageContent" && field.name != "mMetadata") return@forEach
field.isAccessible = true field.isAccessible = true
messageJsonObject[field.name]?.let { fieldValue -> deletedMessageObject[field.name]?.let { fieldValue ->
field.set(messageInstance, context.gson.fromJson(fieldValue, field.type)) field.set(messageInstance, context.gson.fromJson(fieldValue, field.type))
} }
} }

View File

@ -57,7 +57,7 @@ abstract class AbstractWrapper(
protected fun <T> field(fieldName: String, mapper: ((Any?) -> T?)? = null) = FieldAccessor(fieldName, mapper) protected fun <T> field(fieldName: String, mapper: ((Any?) -> T?)? = null) = FieldAccessor(fieldName, mapper)
fun <T : Enum<*>> getEnumValue(fieldName: String, defaultValue: T?): T? { fun <T : Enum<*>> getEnumValue(fieldName: String, defaultValue: T?): T? {
if (defaultValue == null) return null if (defaultValue == null || instance == null) return null
val mContentType = XposedHelpers.getObjectField(instance, fieldName) as? Enum<*> ?: return null val mContentType = XposedHelpers.getObjectField(instance, fieldName) as? Enum<*> ?: return null
return java.lang.Enum.valueOf(defaultValue::class.java, mContentType.name) return java.lang.Enum.valueOf(defaultValue::class.java, mContentType.name)
} }

View File

@ -1,5 +1,6 @@
package me.rhunk.snapenhance.core.wrapper.impl package me.rhunk.snapenhance.core.wrapper.impl
import me.rhunk.snapenhance.common.data.QuotedMessageContentStatus
import me.rhunk.snapenhance.core.wrapper.AbstractWrapper import me.rhunk.snapenhance.core.wrapper.AbstractWrapper
import org.mozilla.javascript.annotations.JSGetter import org.mozilla.javascript.annotations.JSGetter
import org.mozilla.javascript.annotations.JSSetter import org.mozilla.javascript.annotations.JSSetter
@ -7,4 +8,6 @@ import org.mozilla.javascript.annotations.JSSetter
class QuotedMessage(obj: Any?) : AbstractWrapper(obj) { class QuotedMessage(obj: Any?) : AbstractWrapper(obj) {
@get:JSGetter @set:JSSetter @get:JSGetter @set:JSSetter
var content by field("mContent") { QuotedMessageContent(it) } var content by field("mContent") { QuotedMessageContent(it) }
@get:JSGetter
val status by enum("mStatus", QuotedMessageContentStatus.UNKNOWN)
} }