mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-05 19:08:23 +00:00
Refactor: Move logging to a separate database
This commit moves all logging functionality into a new, separate `LogDatabase`. Key changes include: * Creating a `LogDatabase` and `LogDao` to handle all log entries. * Removing the `LogEntity` and related queries from the main `AppDatabase` and `ApplicationDao`. * Introducing a new `MIGRATION_14_15` to drop the old `amber_log` table from `AppDatabase`. * Updating all call sites to use `Amber.instance.getLogDatabase(npub).logDao()` instead of the old `applicationDao` for logging operations.
This commit is contained in:
@@ -0,0 +1,308 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 15,
|
||||
"identityHash": "3690c8720be33c75a319014268cf1afe",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "application",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` TEXT NOT NULL, `name` TEXT NOT NULL, `relays` TEXT NOT NULL, `url` TEXT NOT NULL, `icon` TEXT NOT NULL, `description` TEXT NOT NULL, `pubKey` TEXT NOT NULL, `isConnected` INTEGER NOT NULL, `secret` TEXT NOT NULL, `useSecret` INTEGER NOT NULL, `signPolicy` INTEGER NOT NULL, `closeApplication` INTEGER NOT NULL, `deleteAfter` INTEGER NOT NULL, `lastUsed` INTEGER NOT NULL, PRIMARY KEY(`key`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "key",
|
||||
"columnName": "key",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "relays",
|
||||
"columnName": "relays",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "url",
|
||||
"columnName": "url",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "icon",
|
||||
"columnName": "icon",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "description",
|
||||
"columnName": "description",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "pubKey",
|
||||
"columnName": "pubKey",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "isConnected",
|
||||
"columnName": "isConnected",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "secret",
|
||||
"columnName": "secret",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "useSecret",
|
||||
"columnName": "useSecret",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "signPolicy",
|
||||
"columnName": "signPolicy",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "closeApplication",
|
||||
"columnName": "closeApplication",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "deleteAfter",
|
||||
"columnName": "deleteAfter",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "lastUsed",
|
||||
"columnName": "lastUsed",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"key"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_key",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"key"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_key` ON `${TABLE_NAME}` (`key`)"
|
||||
},
|
||||
{
|
||||
"name": "index_name",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"name"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_name` ON `${TABLE_NAME}` (`name`)"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tableName": "applicationPermission",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER, `pkKey` TEXT NOT NULL, `type` TEXT NOT NULL, `kind` INTEGER, `acceptable` INTEGER NOT NULL, `rememberType` INTEGER NOT NULL, `acceptUntil` INTEGER NOT NULL, `rejectUntil` INTEGER NOT NULL, PRIMARY KEY(`id`), FOREIGN KEY(`pkKey`) REFERENCES `application`(`key`) ON UPDATE NO ACTION ON DELETE CASCADE )",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER"
|
||||
},
|
||||
{
|
||||
"fieldPath": "pkKey",
|
||||
"columnName": "pkKey",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "type",
|
||||
"columnName": "type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "kind",
|
||||
"columnName": "kind",
|
||||
"affinity": "INTEGER"
|
||||
},
|
||||
{
|
||||
"fieldPath": "acceptable",
|
||||
"columnName": "acceptable",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "rememberType",
|
||||
"columnName": "rememberType",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "acceptUntil",
|
||||
"columnName": "acceptUntil",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "rejectUntil",
|
||||
"columnName": "rejectUntil",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "permissions_by_pk_key",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"pkKey"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `permissions_by_pk_key` ON `${TABLE_NAME}` (`pkKey`)"
|
||||
},
|
||||
{
|
||||
"name": "permissions_unique",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"pkKey",
|
||||
"type",
|
||||
"kind"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `permissions_unique` ON `${TABLE_NAME}` (`pkKey`, `type`, `kind`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": [
|
||||
{
|
||||
"table": "application",
|
||||
"onDelete": "CASCADE",
|
||||
"onUpdate": "NO ACTION",
|
||||
"columns": [
|
||||
"pkKey"
|
||||
],
|
||||
"referencedColumns": [
|
||||
"key"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tableName": "history2",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `pkKey` TEXT NOT NULL, `type` TEXT NOT NULL, `kind` INTEGER, `time` INTEGER NOT NULL, `accepted` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "pkKey",
|
||||
"columnName": "pkKey",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "type",
|
||||
"columnName": "type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "kind",
|
||||
"columnName": "kind",
|
||||
"affinity": "INTEGER"
|
||||
},
|
||||
{
|
||||
"fieldPath": "time",
|
||||
"columnName": "time",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "accepted",
|
||||
"columnName": "accepted",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "history_by_pk_key2",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"pkKey"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `history_by_pk_key2` ON `${TABLE_NAME}` (`pkKey`)"
|
||||
},
|
||||
{
|
||||
"name": "history_by_id2",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `history_by_id2` ON `${TABLE_NAME}` (`id`)"
|
||||
},
|
||||
{
|
||||
"name": "history_by_time",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"time"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `history_by_time` ON `${TABLE_NAME}` (`time`)"
|
||||
},
|
||||
{
|
||||
"name": "history_by_key_and_time",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"pkKey",
|
||||
"time"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `history_by_key_and_time` ON `${TABLE_NAME}` (`pkKey`, `time`)"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3690c8720be33c75a319014268cf1afe')"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 1,
|
||||
"identityHash": "3d70a45c6ee30a3f8c9d79b6d3a55cf0",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "amber_log",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `url` TEXT NOT NULL, `type` TEXT NOT NULL, `message` TEXT NOT NULL, `time` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "url",
|
||||
"columnName": "url",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "type",
|
||||
"columnName": "type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "message",
|
||||
"columnName": "message",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "time",
|
||||
"columnName": "time",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3d70a45c6ee30a3f8c9d79b6d3a55cf0')"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import com.greenart7c3.nostrsigner.database.AppDatabase
|
||||
import com.greenart7c3.nostrsigner.database.LogDatabase
|
||||
import com.greenart7c3.nostrsigner.models.Account
|
||||
import com.greenart7c3.nostrsigner.models.AmberSettings
|
||||
import com.greenart7c3.nostrsigner.models.FeedbackType
|
||||
@@ -115,6 +116,7 @@ class Amber : Application(), LifecycleObserver {
|
||||
val profileSubscription = ProfileSubscription(client, this, applicationIOScope)
|
||||
|
||||
private var databases = ConcurrentHashMap<String, AppDatabase>()
|
||||
private var logDatabases = ConcurrentHashMap<String, LogDatabase>()
|
||||
|
||||
val isOnMobileDataState = mutableStateOf(false)
|
||||
val isOnWifiDataState = mutableStateOf(false)
|
||||
@@ -373,6 +375,13 @@ class Amber : Application(), LifecycleObserver {
|
||||
return databases[npub]!!
|
||||
}
|
||||
|
||||
fun getLogDatabase(npub: String): LogDatabase {
|
||||
if (!logDatabases.containsKey(npub)) {
|
||||
logDatabases[npub] = LogDatabase.getDatabase(this, npub)
|
||||
}
|
||||
return logDatabases[npub]!!
|
||||
}
|
||||
|
||||
fun getSavedRelays(): Set<NormalizedRelayUrl> {
|
||||
val savedRelays = mutableSetOf<NormalizedRelayUrl>()
|
||||
LocalPreferences.allSavedAccounts(this).forEach { accountInfo ->
|
||||
|
||||
@@ -277,6 +277,7 @@ class SignerProvider : ContentProvider() {
|
||||
val pubkey = projection[1]
|
||||
val account = LocalPreferences.loadFromEncryptedStorageSync(context!!, npub) ?: return null
|
||||
val database = Amber.instance.getDatabase(account.npub)
|
||||
val logDatabase = Amber.instance.getLogDatabase(account.npub)
|
||||
var permission =
|
||||
database
|
||||
.applicationDao()
|
||||
@@ -351,7 +352,7 @@ class SignerProvider : ContentProvider() {
|
||||
) ?: "Could not decrypt the message"
|
||||
} catch (e: Exception) {
|
||||
scope.launch {
|
||||
database.applicationDao().insertLog(
|
||||
logDatabase.logDao().insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
packageName,
|
||||
@@ -460,8 +461,8 @@ class SignerProvider : ContentProvider() {
|
||||
} catch (e: Exception) {
|
||||
scope.launch {
|
||||
LocalPreferences.allSavedAccounts(context!!).forEach { accInfo ->
|
||||
val database = Amber.instance.getDatabase(accInfo.npub)
|
||||
database.applicationDao().insertLog(
|
||||
val database = Amber.instance.getLogDatabase(accInfo.npub)
|
||||
database.logDao().insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
packageName,
|
||||
|
||||
@@ -128,14 +128,23 @@ val MIGRATION_13_14 = object : Migration(13, 14) {
|
||||
}
|
||||
}
|
||||
|
||||
val MIGRATION_14_15 = object : Migration(14, 15) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
try {
|
||||
db.execSQL("DROP TABLE amber_log")
|
||||
} catch (e: Exception) {
|
||||
Log.e(Amber.TAG, "No amber_log table", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
ApplicationEntity::class,
|
||||
ApplicationPermissionsEntity::class,
|
||||
LogEntity::class,
|
||||
HistoryEntity2::class,
|
||||
],
|
||||
version = 14,
|
||||
version = 15,
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
@@ -171,6 +180,7 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
.addMigrations(MIGRATION_11_12)
|
||||
.addMigrations(MIGRATION_12_13)
|
||||
.addMigrations(MIGRATION_13_14)
|
||||
.addMigrations(MIGRATION_14_15)
|
||||
.build()
|
||||
instance.openHelper.writableDatabase.execSQL("VACUUM")
|
||||
|
||||
|
||||
@@ -158,20 +158,6 @@ interface ApplicationDao {
|
||||
@Query("DELETE FROM history2 where pkKey = :pk")
|
||||
suspend fun deleteHistory(pk: String)
|
||||
|
||||
@Insert
|
||||
@Transaction
|
||||
suspend fun insertLog(logEntity: LogEntity)
|
||||
|
||||
@Query("SELECT * FROM amber_log ORDER BY time DESC")
|
||||
fun getLogs(): Flow<List<LogEntity>>
|
||||
|
||||
@Query("SELECT * FROM amber_log where url = :url ORDER BY time DESC")
|
||||
fun getLogsByUrl(url: String): Flow<List<LogEntity>>
|
||||
|
||||
@Query("DELETE FROM amber_log")
|
||||
@Transaction
|
||||
suspend fun clearLogs()
|
||||
|
||||
@Delete
|
||||
@Transaction
|
||||
suspend fun deletePermission(permission: ApplicationPermissionsEntity)
|
||||
@@ -188,18 +174,6 @@ interface ApplicationDao {
|
||||
@Transaction
|
||||
suspend fun deleteHistory(historyEntity: HistoryEntity2)
|
||||
|
||||
@Query("SELECT COUNT(*) FROM amber_log WHERE time < :time")
|
||||
@Transaction
|
||||
suspend fun countOldLog(time: Long): Long
|
||||
|
||||
@Query("SELECT * FROM amber_log WHERE time < :time LIMIT 100")
|
||||
@Transaction
|
||||
suspend fun getOldLog(time: Long): List<LogEntity>
|
||||
|
||||
@Delete
|
||||
@Transaction
|
||||
suspend fun deleteLog(logEntity: LogEntity)
|
||||
|
||||
@Query("DELETE FROM application WHERE deleteAfter < :time AND deleteAfter > 0")
|
||||
@Transaction
|
||||
suspend fun deleteOldApplications(time: Long): Int
|
||||
|
||||
@@ -93,8 +93,3 @@ class Converters {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class ApplicationWithLatestHistory(
|
||||
@Embedded val application: ApplicationEntity,
|
||||
val latestTime: Long?,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.greenart7c3.nostrsigner.database
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface LogDao {
|
||||
@Insert
|
||||
@Transaction
|
||||
suspend fun insertLog(logEntity: LogEntity)
|
||||
|
||||
@Query("SELECT * FROM amber_log ORDER BY time DESC")
|
||||
fun getLogs(): Flow<List<LogEntity>>
|
||||
|
||||
@Query("SELECT * FROM amber_log where url = :url ORDER BY time DESC")
|
||||
fun getLogsByUrl(url: String): Flow<List<LogEntity>>
|
||||
|
||||
@Query("DELETE FROM amber_log")
|
||||
@Transaction
|
||||
suspend fun clearLogs()
|
||||
|
||||
@Query("SELECT COUNT(*) FROM amber_log WHERE time < :time")
|
||||
@Transaction
|
||||
suspend fun countOldLog(time: Long): Long
|
||||
|
||||
@Query("SELECT * FROM amber_log WHERE time < :time LIMIT 100")
|
||||
@Transaction
|
||||
suspend fun getOldLog(time: Long): List<LogEntity>
|
||||
|
||||
@Delete
|
||||
@Transaction
|
||||
suspend fun deleteLog(logEntity: LogEntity)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.greenart7c3.nostrsigner.database
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
LogEntity::class,
|
||||
],
|
||||
version = 1,
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class LogDatabase : RoomDatabase() {
|
||||
abstract fun logDao(): LogDao
|
||||
|
||||
companion object {
|
||||
fun getDatabase(
|
||||
context: Context,
|
||||
npub: String,
|
||||
): LogDatabase {
|
||||
return synchronized(this) {
|
||||
val executor = Executors.newCachedThreadPool()
|
||||
val transactionExecutor = Executors.newCachedThreadPool()
|
||||
|
||||
val instance =
|
||||
Room.databaseBuilder(
|
||||
context,
|
||||
LogDatabase::class.java,
|
||||
"log_db_$npub",
|
||||
)
|
||||
.setQueryExecutor(executor)
|
||||
.setTransactionExecutor(transactionExecutor)
|
||||
.build()
|
||||
instance.openHelper.writableDatabase.execSQL("VACUUM")
|
||||
|
||||
instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ class NostrClientLoggerListener(
|
||||
override fun onAuth(relay: IRelayClient, challenge: String) {
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
@@ -66,7 +66,7 @@ class NostrClientLoggerListener(
|
||||
override fun onBeforeSend(relay: IRelayClient, event: Event) {
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
@@ -82,7 +82,7 @@ class NostrClientLoggerListener(
|
||||
override fun onSend(relay: IRelayClient, msg: String, success: Boolean) {
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
@@ -105,7 +105,7 @@ class NostrClientLoggerListener(
|
||||
override fun onSendResponse(relay: IRelayClient, eventId: String, success: Boolean, message: String) {
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
@@ -132,7 +132,7 @@ class NostrClientLoggerListener(
|
||||
if (error.message?.trim()?.equals("Relay sent notice:") == true) return
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
@@ -157,7 +157,7 @@ class NostrClientLoggerListener(
|
||||
override fun onEvent(relay: IRelayClient, subId: String, event: Event, arrivalTime: Long, afterEOSE: Boolean) {
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
@@ -175,7 +175,7 @@ class NostrClientLoggerListener(
|
||||
override fun onNotify(relay: IRelayClient, description: String) {
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
@@ -191,7 +191,7 @@ class NostrClientLoggerListener(
|
||||
override fun onRelayStateChange(relay: IRelayClient, type: RelayState) {
|
||||
scope.launch {
|
||||
LocalPreferences.currentAccount(context)?.let { account ->
|
||||
Amber.instance.getDatabase(account).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url.url,
|
||||
|
||||
@@ -65,7 +65,7 @@ object BunkerRequestUtils {
|
||||
if (relays.isEmpty()) {
|
||||
onDone(true)
|
||||
onLoading(false)
|
||||
Amber.instance.getDatabase(account.npub).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account.npub).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = bunkerRequest.localKey,
|
||||
@@ -89,7 +89,7 @@ object BunkerRequestUtils {
|
||||
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
relays.forEach { relay ->
|
||||
Amber.instance.getDatabase(account.npub).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account.npub).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = relay.url,
|
||||
@@ -124,7 +124,7 @@ object BunkerRequestUtils {
|
||||
onDone(false)
|
||||
onLoading(false)
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
Amber.instance.getDatabase(account.npub).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account.npub).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = bunkerRequest.localKey,
|
||||
|
||||
@@ -33,18 +33,19 @@ class ClearLogsWorker(appContext: Context, workerParams: WorkerParameters) :
|
||||
}
|
||||
}
|
||||
|
||||
val countLog = database.applicationDao().countOldLog(oneWeek)
|
||||
val logDatabase = Amber.instance.getLogDatabase(it.npub)
|
||||
val countLog = logDatabase.logDao().countOldLog(oneWeek)
|
||||
Log.d(Amber.TAG, "Deleting $countLog old log entries from ${com.greenart7c3.nostrsigner.models.TimeUtils.formatLongToCustomDateTimeWithSeconds(oneWeek)}")
|
||||
if (countLog > 0) {
|
||||
var logs = database.applicationDao().getOldLog(oneWeek)
|
||||
var logs = logDatabase.logDao().getOldLog(oneWeek)
|
||||
var count = 0
|
||||
while (logs.isNotEmpty()) {
|
||||
count++
|
||||
logs.forEach { history ->
|
||||
Log.d(Amber.TAG, "Deleting log entry ${com.greenart7c3.nostrsigner.models.TimeUtils.formatLongToCustomDateTimeWithSeconds(history.time)}")
|
||||
database.applicationDao().deleteLog(history)
|
||||
logDatabase.logDao().deleteLog(history)
|
||||
}
|
||||
logs = database.applicationDao().getOldLog(oneWeek)
|
||||
logs = logDatabase.logDao().getOldLog(oneWeek)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
|
||||
@@ -129,15 +129,17 @@ class ConnectivityService : Service() {
|
||||
LocalPreferences.allSavedAccounts(Amber.instance).forEach { accountInfo ->
|
||||
val now = System.currentTimeMillis() / 1000
|
||||
val deleted = Amber.instance.getDatabase(accountInfo.npub).applicationDao().deleteOldApplications(now)
|
||||
Amber.instance.getDatabase(accountInfo.npub).applicationDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = "",
|
||||
type = "deleteApplications",
|
||||
message = "Deleted $deleted expired applications",
|
||||
time = System.currentTimeMillis(),
|
||||
),
|
||||
)
|
||||
if (deleted > 0) {
|
||||
Amber.instance.getLogDatabase(accountInfo.npub).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = "",
|
||||
type = "deleteApplications",
|
||||
message = "Deleted $deleted expired applications",
|
||||
time = System.currentTimeMillis(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
|
||||
val accounts = LocalPreferences.allSavedAccounts(applicationContext)
|
||||
accounts.forEach {
|
||||
LocalPreferences.loadFromEncryptedStorage(applicationContext, it.npub)?.let { acc ->
|
||||
val dao = Amber.instance.getDatabase(acc.npub).applicationDao()
|
||||
val dao = Amber.instance.getLogDatabase(acc.npub).logDao()
|
||||
dao.insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
@@ -114,7 +114,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
|
||||
) {
|
||||
if (event.content.isEmpty()) return
|
||||
|
||||
val dao = Amber.instance.getDatabase(acc.npub).applicationDao()
|
||||
val dao = Amber.instance.getLogDatabase(acc.npub).logDao()
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
dao.insertLog(
|
||||
LogEntity(
|
||||
@@ -149,12 +149,13 @@ class EventNotificationConsumer(private val applicationContext: Context) {
|
||||
val responseRelay = listOf(relay)
|
||||
val database = Amber.instance.getDatabase(acc.npub)
|
||||
val dao = database.applicationDao()
|
||||
val logDao = Amber.instance.getLogDatabase(acc.npub).logDao()
|
||||
|
||||
val notification = Amber.instance.notificationCache[event.id]
|
||||
if (notification != null) return
|
||||
Amber.instance.notificationCache.put(event.id, event.createdAt)
|
||||
|
||||
dao.insertLog(
|
||||
logDao.insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
"nostrsigner",
|
||||
@@ -194,7 +195,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
|
||||
val type = BunkerRequestUtils.getTypeFromBunker(bunkerRequest)
|
||||
if (type == SignerType.INVALID) {
|
||||
Log.d(Amber.TAG, "Invalid request method ${bunkerRequest.method}")
|
||||
dao.insertLog(
|
||||
logDao.insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
"nostrsigner",
|
||||
|
||||
@@ -176,8 +176,8 @@ object IntentUtils {
|
||||
) ?: "Could not decrypt the message"
|
||||
} catch (e: Exception) {
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
val database = Amber.instance.getDatabase(account.npub)
|
||||
database.applicationDao().insertLog(
|
||||
val database = Amber.instance.getLogDatabase(account.npub)
|
||||
database.logDao().insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
packageName ?: "",
|
||||
@@ -359,8 +359,8 @@ object IntentUtils {
|
||||
) ?: "Could not decrypt the message"
|
||||
} catch (e: Exception) {
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
val database = Amber.instance.getDatabase(account.npub)
|
||||
database.applicationDao().insertLog(
|
||||
val database = Amber.instance.getLogDatabase(account.npub)
|
||||
database.logDao().insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
packageName ?: "",
|
||||
@@ -508,7 +508,7 @@ object IntentUtils {
|
||||
} catch (e: Exception) {
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
LocalPreferences.allSavedAccounts(Amber.instance).forEach {
|
||||
Amber.instance.getDatabase(it.npub).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(it.npub).logDao().insertLog(
|
||||
LogEntity(
|
||||
id = 0,
|
||||
url = "IntentUtils",
|
||||
|
||||
@@ -131,7 +131,7 @@ object NostrConnectUtils {
|
||||
} catch (e: Exception) {
|
||||
Log.e(Amber.TAG, e.message, e)
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
Amber.instance.getDatabase(account.npub).applicationDao().insertLog(
|
||||
Amber.instance.getLogDatabase(account.npub).logDao().insertLog(
|
||||
LogEntity(
|
||||
0,
|
||||
"nostrconnect",
|
||||
|
||||
@@ -36,7 +36,7 @@ fun LogsScreen(
|
||||
account: Account,
|
||||
) {
|
||||
val scope = rememberCoroutineScope()
|
||||
val logsFlow = Amber.instance.getDatabase(account.npub).applicationDao().getLogs()
|
||||
val logsFlow = Amber.instance.getLogDatabase(account.npub).logDao().getLogs()
|
||||
val logs = logsFlow.collectAsStateWithLifecycle(initialValue = emptyList())
|
||||
val state = rememberLazyListState()
|
||||
|
||||
@@ -51,7 +51,7 @@ fun LogsScreen(
|
||||
modifier = Modifier.padding(bottom = 8.dp),
|
||||
onClick = {
|
||||
scope.launch(Dispatchers.IO) {
|
||||
Amber.instance.getDatabase(account.npub).applicationDao().clearLogs()
|
||||
Amber.instance.getLogDatabase(account.npub).logDao().clearLogs()
|
||||
}
|
||||
},
|
||||
text = stringResource(R.string.clear_logs),
|
||||
|
||||
@@ -290,18 +290,19 @@ fun SettingsScreen(
|
||||
}
|
||||
}
|
||||
|
||||
val countLog = database.applicationDao().countOldLog(oneWeek)
|
||||
val logDatabase = Amber.instance.getLogDatabase(it.npub)
|
||||
val countLog = logDatabase.logDao().countOldLog(oneWeek)
|
||||
if (countLog > 0) {
|
||||
status = context.getString(R.string.deleting_old_log_entries, countLog)
|
||||
var logs = database.applicationDao().getOldLog(oneWeek)
|
||||
var logs = logDatabase.logDao().getOldLog(oneWeek)
|
||||
var count = 0
|
||||
while (logs.isNotEmpty()) {
|
||||
count++
|
||||
status = context.getString(R.string.deleting_old_log_entries_2, 100 * count, countLog)
|
||||
logs.forEach { history ->
|
||||
database.applicationDao().deleteLog(history)
|
||||
logDatabase.logDao().deleteLog(history)
|
||||
}
|
||||
logs = database.applicationDao().getOldLog(oneWeek)
|
||||
logs = logDatabase.logDao().getOldLog(oneWeek)
|
||||
}
|
||||
}
|
||||
val dbFile = context.getDatabasePath("amber_db_${account.npub}")
|
||||
|
||||
@@ -451,7 +451,7 @@ fun RelayLogScreen(
|
||||
val context = LocalContext.current
|
||||
|
||||
val flows = LocalPreferences.allSavedAccounts(context).map {
|
||||
Amber.instance.getDatabase(it.npub).applicationDao().getLogsByUrl(url)
|
||||
Amber.instance.getLogDatabase(it.npub).logDao().getLogsByUrl(url)
|
||||
}.merge()
|
||||
|
||||
val logs = flows.collectAsStateWithLifecycle(initialValue = emptyList())
|
||||
|
||||
Reference in New Issue
Block a user