diff --git a/app/schemas/com.koalasat.pokey.database.AppDatabase/6.json b/app/schemas/com.koalasat.pokey.database.AppDatabase/6.json new file mode 100644 index 0000000..d82fa3e --- /dev/null +++ b/app/schemas/com.koalasat.pokey.database.AppDatabase/6.json @@ -0,0 +1,116 @@ +{ + "formatVersion": 1, + "database": { + "version": 6, + "identityHash": "90a758055ce890febc96cb7454446261", + "entities": [ + { + "tableName": "notification", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `eventId` TEXT NOT NULL, `time` INTEGER NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "eventId", + "columnName": "eventId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "time", + "columnName": "time", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "notification_by_eventId", + "unique": false, + "columnNames": [ + "eventId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `notification_by_eventId` ON `${TABLE_NAME}` (`eventId`)" + } + ], + "foreignKeys": [] + }, + { + "tableName": "relay", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `url` TEXT NOT NULL, `kind` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `read` INTEGER NOT NULL, `write` INTEGER NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "url", + "columnName": "url", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "kind", + "columnName": "kind", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "read", + "columnName": "read", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "write", + "columnName": "write", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "relay_by_url", + "unique": false, + "columnNames": [ + "url" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `relay_by_url` ON `${TABLE_NAME}` (`url`)" + } + ], + "foreignKeys": [] + } + ], + "views": [], + "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, '90a758055ce890febc96cb7454446261')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/com/koalasat/pokey/database/AppDatabase.kt b/app/src/main/java/com/koalasat/pokey/database/AppDatabase.kt index 7f001df..848403e 100644 --- a/app/src/main/java/com/koalasat/pokey/database/AppDatabase.kt +++ b/app/src/main/java/com/koalasat/pokey/database/AppDatabase.kt @@ -6,15 +6,25 @@ import androidx.room.Room import androidx.room.RoomDatabase import androidx.room.TypeConverter import androidx.room.TypeConverters +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase import com.vitorpamplona.ammolite.relays.COMMON_FEED_TYPES import com.vitorpamplona.ammolite.relays.RelaySetupInfo +val MIGRATION_5_6 = + object : Migration(5, 6) { + override fun migrate(db: SupportSQLiteDatabase) { + db.execSQL("ALTER TABLE `relay` ADD COLUMN `read` BOOLEAN NOT NULL DEFAULT TRUE") + db.execSQL("ALTER TABLE `relay` ADD COLUMN `write` BOOLEAN NOT NULL DEFAULT TRUE") + } + } + @Database( entities = [ NotificationEntity::class, RelayEntity::class, ], - version = 5, + version = 6, ) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { @@ -32,6 +42,7 @@ abstract class AppDatabase : RoomDatabase() { AppDatabase::class.java, "pokey_db_$pubKey", ) + .addMigrations(MIGRATION_5_6) .build() instance } diff --git a/app/src/main/java/com/koalasat/pokey/database/ApplicationDao.kt b/app/src/main/java/com/koalasat/pokey/database/ApplicationDao.kt index 3329eb8..52171b0 100644 --- a/app/src/main/java/com/koalasat/pokey/database/ApplicationDao.kt +++ b/app/src/main/java/com/koalasat/pokey/database/ApplicationDao.kt @@ -16,8 +16,8 @@ interface ApplicationDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertNotification(notificationEntity: NotificationEntity): Long? - @Query("SELECT * FROM relay") - fun getRelays(): List + @Query("SELECT * FROM relay WHERE read = 1") + fun getReadRelays(): List @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertRelay(notificationEntity: RelayEntity): Long? diff --git a/app/src/main/java/com/koalasat/pokey/database/RelayEntity.kt b/app/src/main/java/com/koalasat/pokey/database/RelayEntity.kt index 7c63132..ab125c7 100644 --- a/app/src/main/java/com/koalasat/pokey/database/RelayEntity.kt +++ b/app/src/main/java/com/koalasat/pokey/database/RelayEntity.kt @@ -19,4 +19,6 @@ data class RelayEntity( val url: String, val kind: Int, val createdAt: Long, + val read: Boolean, + val write: Boolean, ) diff --git a/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt b/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt index 0f73e0a..3eb510f 100644 --- a/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt +++ b/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt @@ -43,8 +43,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import org.json.JSONException -import org.json.JSONObject class NotificationsService : Service() { private var channelRelaysId = "RelaysConnections" @@ -313,22 +311,13 @@ class NotificationsService : Service() { .filter { it.size > 1 && (it[0] == "relay" || it[0] == "r") } .forEach { var read = true - if (event.kind == 10002) { - try { - val relaysConfig = JSONObject(event.content) - val config = relaysConfig.getJSONObject(it[1]) - read = config.getBoolean("read") - } catch (_: JSONException) { - Log.d( - "Pokey", - "Public inbox relays not configured", - ) - } - } - if (read) { - val entity = RelayEntity(id = 0, url = it[1], kind = event.kind, createdAt = event.createdAt) - dao.insertRelay(entity) + var write = true + if (event.kind == 10002 && it.size > 2) { + read = it[2] == "read" + write = it[2] == "write" } + val entity = RelayEntity(id = 0, url = it[1], kind = event.kind, createdAt = event.createdAt, read = read, write = write) + dao.insertRelay(entity) } startSubscription() @@ -453,9 +442,9 @@ class NotificationsService : Service() { private fun connectRelays() { val dao = AppDatabase.getDatabase(this@NotificationsService, Pokey.getInstance().getHexKey()).applicationDao() - var relays = dao.getRelays() + var relays = dao.getReadRelays() if (relays.isEmpty()) { - relays = defaultRelayUrls.map { RelayEntity(id = 0, url = it, kind = 0, createdAt = 0) } + relays = defaultRelayUrls.map { RelayEntity(id = 0, url = it, kind = 0, createdAt = 0, read = true, write = true) } } relays.forEach {