mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-06-13 05:37:48 +02:00
mod database exp
This commit is contained in:
@ -9,6 +9,7 @@ import me.rhunk.snapenhance.bridge.wrapper.LocaleWrapper
|
||||
import me.rhunk.snapenhance.bridge.wrapper.MappingsWrapper
|
||||
import me.rhunk.snapenhance.core.config.ModConfig
|
||||
import me.rhunk.snapenhance.download.DownloadTaskManager
|
||||
import me.rhunk.snapenhance.messaging.ModDatabase
|
||||
import me.rhunk.snapenhance.ui.manager.data.InstallationSummary
|
||||
import me.rhunk.snapenhance.ui.manager.data.ModMappingsInfo
|
||||
import me.rhunk.snapenhance.ui.manager.data.SnapchatAppInfo
|
||||
@ -30,6 +31,7 @@ class RemoteSideContext(
|
||||
val translation = LocaleWrapper()
|
||||
val mappings = MappingsWrapper()
|
||||
val downloadTaskManager = DownloadTaskManager()
|
||||
val modDatabase = ModDatabase(this)
|
||||
|
||||
init {
|
||||
runCatching {
|
||||
@ -41,6 +43,7 @@ class RemoteSideContext(
|
||||
init(androidContext)
|
||||
}
|
||||
downloadTaskManager.init(androidContext)
|
||||
modDatabase.init()
|
||||
}.onFailure {
|
||||
Logger.error("Failed to initialize RemoteSideContext", it)
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
package me.rhunk.snapenhance.friends
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import me.rhunk.snapenhance.RemoteSideContext
|
||||
import me.rhunk.snapenhance.util.SQLiteDatabaseHelper
|
||||
|
||||
class FriendDatabase(
|
||||
private val context: RemoteSideContext,
|
||||
) {
|
||||
private lateinit var database: SQLiteDatabase
|
||||
|
||||
fun init() {
|
||||
database = context.androidContext.openOrCreateDatabase("friends.db", 0, null)
|
||||
SQLiteDatabaseHelper.createTablesFromSchema(database, mapOf(
|
||||
"friends" to listOf(
|
||||
"userId VARCHAR PRIMARY KEY",
|
||||
"displayName VARCHAR",
|
||||
"mutable_username VARCHAR",
|
||||
"bitmojiId VARCHAR",
|
||||
"selfieId VARCHAR"
|
||||
),
|
||||
"rules" to listOf(
|
||||
"userId VARCHAR PRIMARY KEY",
|
||||
"enabled BOOLEAN",
|
||||
"mode VARCHAR",
|
||||
"type VARCHAR"
|
||||
),
|
||||
"streaks" to listOf(
|
||||
"userId VARCHAR PRIMARY KEY",
|
||||
"notify BOOLEAN",
|
||||
"expirationTimestamp BIGINT",
|
||||
"count INTEGER"
|
||||
),
|
||||
"analytics_config" to listOf(
|
||||
"userId VARCHAR PRIMARY KEY",
|
||||
"modes VARCHAR"
|
||||
),
|
||||
"analytics" to listOf(
|
||||
"hash VARCHAR PRIMARY KEY",
|
||||
"userId VARCHAR",
|
||||
"conversationId VARCHAR",
|
||||
"timestamp BIGINT",
|
||||
"eventName VARCHAR",
|
||||
"eventData VARCHAR"
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package me.rhunk.snapenhance.messaging
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import me.rhunk.snapenhance.RemoteSideContext
|
||||
import me.rhunk.snapenhance.core.messaging.FriendStreaks
|
||||
import me.rhunk.snapenhance.core.messaging.MessagingRule
|
||||
import me.rhunk.snapenhance.core.messaging.Mode
|
||||
import me.rhunk.snapenhance.core.messaging.ObjectType
|
||||
import me.rhunk.snapenhance.database.objects.FriendInfo
|
||||
import me.rhunk.snapenhance.util.SQLiteDatabaseHelper
|
||||
import me.rhunk.snapenhance.util.getInteger
|
||||
import me.rhunk.snapenhance.util.getLongOrNull
|
||||
import me.rhunk.snapenhance.util.getStringOrNull
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
|
||||
class ModDatabase(
|
||||
private val context: RemoteSideContext,
|
||||
) {
|
||||
private val executor = Executors.newSingleThreadExecutor()
|
||||
private lateinit var database: SQLiteDatabase
|
||||
|
||||
fun init() {
|
||||
database = context.androidContext.openOrCreateDatabase("main.db", 0, null)
|
||||
SQLiteDatabaseHelper.createTablesFromSchema(database, mapOf(
|
||||
"friends" to listOf(
|
||||
"userId VARCHAR PRIMARY KEY",
|
||||
"displayName VARCHAR",
|
||||
"mutable_username VARCHAR",
|
||||
"bitmojiId VARCHAR",
|
||||
"selfieId VARCHAR"
|
||||
),
|
||||
"rules" to listOf(
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT",
|
||||
"objectType VARCHAR",
|
||||
"targetUuid VARCHAR",
|
||||
"enabled BOOLEAN",
|
||||
"mode VARCHAR",
|
||||
"subject VARCHAR"
|
||||
),
|
||||
"streaks" to listOf(
|
||||
"userId VARCHAR PRIMARY KEY",
|
||||
"notify BOOLEAN",
|
||||
"expirationTimestamp BIGINT",
|
||||
"count INTEGER"
|
||||
),
|
||||
"analytics_config" to listOf(
|
||||
"userId VARCHAR PRIMARY KEY",
|
||||
"modes VARCHAR"
|
||||
),
|
||||
"analytics" to listOf(
|
||||
"hash VARCHAR PRIMARY KEY",
|
||||
"userId VARCHAR",
|
||||
"conversationId VARCHAR",
|
||||
"timestamp BIGINT",
|
||||
"eventName VARCHAR",
|
||||
"eventData VARCHAR"
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
fun syncFriends(friends: List<FriendInfo>) {
|
||||
executor.execute {
|
||||
try {
|
||||
friends.forEach { friend ->
|
||||
database.execSQL("INSERT OR REPLACE INTO friends VALUES (?, ?, ?, ?, ?)", arrayOf(
|
||||
friend.userId,
|
||||
friend.displayName,
|
||||
friend.username,
|
||||
friend.bitmojiAvatarId,
|
||||
friend.bitmojiSelfieId
|
||||
))
|
||||
//sync streaks
|
||||
if (friend.streakLength > 0) {
|
||||
database.execSQL("INSERT OR REPLACE INTO streaks (userId, expirationTimestamp, count) VALUES (?, ?, ?)", arrayOf(
|
||||
friend.userId,
|
||||
friend.streakExpirationTimestamp,
|
||||
friend.streakLength
|
||||
))
|
||||
} else {
|
||||
database.execSQL("DELETE FROM streaks WHERE userId = ?", arrayOf(friend.userId))
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getRulesFromId(type: ObjectType, targetUuid: String): List<MessagingRule> {
|
||||
return database.rawQuery("SELECT * FROM rules WHERE objectType = ? AND targetUuid = ?", arrayOf(type.name, targetUuid)).use { cursor ->
|
||||
val rules = mutableListOf<MessagingRule>()
|
||||
while (cursor.moveToNext()) {
|
||||
rules.add(MessagingRule(
|
||||
id = cursor.getInteger("id"),
|
||||
objectType = ObjectType.valueOf(cursor.getStringOrNull("objectType")!!),
|
||||
targetUuid = cursor.getStringOrNull("targetUuid")!!,
|
||||
enabled = cursor.getInteger("enabled") == 1,
|
||||
mode = Mode.valueOf(cursor.getStringOrNull("mode")!!),
|
||||
subject = cursor.getStringOrNull("subject")!!
|
||||
))
|
||||
}
|
||||
rules
|
||||
}
|
||||
}
|
||||
|
||||
fun getFriendStreaks(userId: String): FriendStreaks? {
|
||||
return database.rawQuery("SELECT * FROM streaks WHERE userId = ?", arrayOf(userId)).use { cursor ->
|
||||
if (!cursor.moveToFirst()) return@use null
|
||||
FriendStreaks(
|
||||
userId = cursor.getStringOrNull("userId")!!,
|
||||
notify = cursor.getInteger("notify") == 1,
|
||||
expirationTimestamp = cursor.getLongOrNull("expirationTimestamp") ?: 0L,
|
||||
count = cursor.getInteger("count")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package me.rhunk.snapenhance.core.friends
|
||||
|
||||
enum class FriendFeature(
|
||||
val value: String,
|
||||
) {
|
||||
DOWNLOAD("download"),
|
||||
STEALTH("stealth"),
|
||||
AUTO_SAVE("auto_save");
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package me.rhunk.snapenhance.core.messaging
|
||||
|
||||
enum class EnumConversationFeature(
|
||||
val value: String,
|
||||
val objectType: ObjectType,
|
||||
) {
|
||||
DOWNLOAD("download", ObjectType.USER),
|
||||
STEALTH("stealth", ObjectType.CONVERSATION),
|
||||
AUTO_SAVE("auto_save", ObjectType.CONVERSATION);
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package me.rhunk.snapenhance.core.messaging
|
||||
|
||||
|
||||
enum class Mode {
|
||||
BLACKLIST,
|
||||
WHITELIST
|
||||
}
|
||||
|
||||
enum class ObjectType {
|
||||
USER,
|
||||
CONVERSATION
|
||||
}
|
||||
|
||||
data class FriendStreaks(
|
||||
val userId: String,
|
||||
val notify: Boolean,
|
||||
val expirationTimestamp: Long,
|
||||
val count: Int
|
||||
)
|
||||
|
||||
data class MessagingRule(
|
||||
val id: Int,
|
||||
val objectType: ObjectType,
|
||||
val targetUuid: String,
|
||||
val enabled: Boolean,
|
||||
val mode: Mode?,
|
||||
val subject: String
|
||||
)
|
Reference in New Issue
Block a user