refactor: rule features

This commit is contained in:
rhunk 2023-09-24 16:51:31 +02:00
parent 9e547bfe0c
commit b46139c3ad
8 changed files with 20 additions and 26 deletions

View File

@ -12,7 +12,7 @@ class UserInterfaceTweaks : ConfigContainer() {
} }
val friendFeedMenuButtons = multiple( val friendFeedMenuButtons = multiple(
"friend_feed_menu_buttons","conversation_info", *MessagingRuleType.values().toList().filter { it.listMode }.map { it.key }.toTypedArray() "friend_feed_menu_buttons","conversation_info", *MessagingRuleType.values().toList().filter { it.showInFriendMenu }.map { it.key }.toTypedArray()
).apply { ).apply {
set(mutableListOf("conversation_info", MessagingRuleType.STEALTH.key)) set(mutableListOf("conversation_info", MessagingRuleType.STEALTH.key))
} }

View File

@ -28,16 +28,17 @@ enum class SocialScope(
enum class MessagingRuleType( enum class MessagingRuleType(
val key: String, val key: String,
val listMode: Boolean val listMode: Boolean,
val showInFriendMenu: Boolean = true
) { ) {
AUTO_DOWNLOAD("auto_download", true), AUTO_DOWNLOAD("auto_download", true),
STEALTH("stealth", true), STEALTH("stealth", true),
AUTO_SAVE("auto_save", true), AUTO_SAVE("auto_save", true),
HIDE_CHAT_FEED("hide_chat_feed", false), HIDE_CHAT_FEED("hide_chat_feed", false, showInFriendMenu = false),
PIN_CONVERSATION("pin_conversation", false); PIN_CONVERSATION("pin_conversation", false, showInFriendMenu = false);
fun translateOptionKey(optionKey: String): String { fun translateOptionKey(optionKey: String): String {
return "rules.properties.${key}.options.${optionKey}" return if (listMode) "rules.properties.$key.options.$optionKey" else "rules.properties.$key.name"
} }
companion object { companion object {

View File

@ -38,7 +38,7 @@ class ProtoEditor(
private fun writeAtPath(path: IntArray, currentIndex: Int, rootReader: ProtoReader, wireToWriteCallback: WireCallback): ByteArray { private fun writeAtPath(path: IntArray, currentIndex: Int, rootReader: ProtoReader, wireToWriteCallback: WireCallback): ByteArray {
val id = path.getOrNull(currentIndex) val id = path.getOrNull(currentIndex)
val output = ProtoWriter() val output = ProtoWriter()
val wires = mutableMapOf<Int, MutableList<Wire>>() val wires = sortedMapOf<Int, MutableList<Wire>>()
rootReader.forEach { wireId, value -> rootReader.forEach { wireId, value ->
wires.putIfAbsent(wireId, mutableListOf()) wires.putIfAbsent(wireId, mutableListOf())

View File

@ -34,7 +34,7 @@ abstract class AbstractWrapper(
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) return null
val mContentType = XposedHelpers.getObjectField(instance, fieldName) as Enum<*> 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

@ -20,6 +20,8 @@ class SnapUUID(obj: Any?) : AbstractWrapper(obj) {
return uuidString return uuidString
} }
fun toBytes() = bytes
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
return other is SnapUUID && other.uuidString == uuidString return other is SnapUUID && other.uuidString == uuidString
} }

View File

@ -526,14 +526,11 @@ class MediaDownloader : MessagingRuleFeature("MediaDownloader", MessagingRuleTyp
val friendInfo: FriendInfo = context.database.getFriendInfo(message.senderId!!) ?: throw Exception("Friend not found in database") val friendInfo: FriendInfo = context.database.getFriendInfo(message.senderId!!) ?: throw Exception("Friend not found in database")
val authorName = friendInfo.usernameForSorting!! val authorName = friendInfo.usernameForSorting!!
val decodedAttachments = if (messageLogger.isMessageRemoved(message.clientConversationId!!, message.serverMessageId.toLong())) { val decodedAttachments = messageLogger.getMessageObject(message.clientConversationId!!, message.serverMessageId.toLong())?.let {
val messageObject = messageLogger.getMessageObject(message.clientConversationId!!, message.serverMessageId.toLong()) ?: throw Exception("Message not found in database") MessageDecoder.decode(it.getAsJsonObject("mMessageContent"))
MessageDecoder.decode(messageObject.getAsJsonObject("mMessageContent")) } ?: MessageDecoder.decode(
} else { protoReader = ProtoReader(message.messageContent!!)
MessageDecoder.decode( )
protoReader = ProtoReader(message.messageContent!!)
)
}
if (decodedAttachments.isEmpty()) { if (decodedAttachments.isEmpty()) {
context.shortToast(translations["no_attachments_toast"]) context.shortToast(translations["no_attachments_toast"])

View File

@ -2,8 +2,10 @@ package me.rhunk.snapenhance.manager.impl
import me.rhunk.snapenhance.ModContext import me.rhunk.snapenhance.ModContext
import me.rhunk.snapenhance.core.Logger import me.rhunk.snapenhance.core.Logger
import me.rhunk.snapenhance.features.impl.experiments.AESMessageEncryption
import me.rhunk.snapenhance.features.Feature import me.rhunk.snapenhance.features.Feature
import me.rhunk.snapenhance.features.FeatureLoadParams import me.rhunk.snapenhance.features.FeatureLoadParams
import me.rhunk.snapenhance.features.MessagingRuleFeature
import me.rhunk.snapenhance.features.impl.ConfigurationOverride import me.rhunk.snapenhance.features.impl.ConfigurationOverride
import me.rhunk.snapenhance.features.impl.Messaging import me.rhunk.snapenhance.features.impl.Messaging
import me.rhunk.snapenhance.features.impl.ScopeSync import me.rhunk.snapenhance.features.impl.ScopeSync
@ -48,6 +50,8 @@ class FeatureManager(private val context: ModContext) : Manager {
return features.find { it::class == featureClass } as? T return features.find { it::class == featureClass } as? T
} }
fun getRuleFeatures() = features.filterIsInstance<MessagingRuleFeature>()
override fun init() { override fun init() {
register( register(
ScopeSync::class, ScopeSync::class,

View File

@ -17,11 +17,7 @@ import me.rhunk.snapenhance.core.database.objects.UserConversationLink
import me.rhunk.snapenhance.core.util.snap.BitmojiSelfie import me.rhunk.snapenhance.core.util.snap.BitmojiSelfie
import me.rhunk.snapenhance.data.ContentType import me.rhunk.snapenhance.data.ContentType
import me.rhunk.snapenhance.data.FriendLinkType import me.rhunk.snapenhance.data.FriendLinkType
import me.rhunk.snapenhance.features.MessagingRuleFeature
import me.rhunk.snapenhance.features.impl.Messaging import me.rhunk.snapenhance.features.impl.Messaging
import me.rhunk.snapenhance.features.impl.downloader.MediaDownloader
import me.rhunk.snapenhance.features.impl.spying.StealthMode
import me.rhunk.snapenhance.features.impl.tweaks.AutoSave
import me.rhunk.snapenhance.ui.ViewAppearanceHelper import me.rhunk.snapenhance.ui.ViewAppearanceHelper
import me.rhunk.snapenhance.ui.applyTheme import me.rhunk.snapenhance.ui.applyTheme
import me.rhunk.snapenhance.ui.menu.AbstractMenu import me.rhunk.snapenhance.ui.menu.AbstractMenu
@ -247,13 +243,7 @@ class FriendFeedInfoMenu : AbstractMenu() {
viewConsumer(previewButton) viewConsumer(previewButton)
} }
val rules: Array<MessagingRuleFeature> = arrayOf( modContext.features.getRuleFeatures().forEach { ruleFeature ->
StealthMode::class,
AutoSave::class,
MediaDownloader::class
).map { modContext.feature(it) }.toTypedArray()
rules.forEach { ruleFeature ->
if (!friendFeedMenuOptions.contains(ruleFeature.ruleType.key)) return@forEach if (!friendFeedMenuOptions.contains(ruleFeature.ruleType.key)) return@forEach
val ruleState = ruleFeature.getRuleState() ?: return@forEach val ruleState = ruleFeature.getRuleState() ?: return@forEach