Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db columns for relays #22

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions app/schemas/com.koalasat.pokey.database.AppDatabase/6.json
Original file line number Diff line number Diff line change
@@ -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')"
]
}
}
13 changes: 12 additions & 1 deletion app/src/main/java/com/koalasat/pokey/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -32,6 +42,7 @@ abstract class AppDatabase : RoomDatabase() {
AppDatabase::class.java,
"pokey_db_$pubKey",
)
.addMigrations(MIGRATION_5_6)
.build()
instance
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ interface ApplicationDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertNotification(notificationEntity: NotificationEntity): Long?

@Query("SELECT * FROM relay")
fun getRelays(): List<RelayEntity>
@Query("SELECT * FROM relay WHERE read = 1")
fun getReadRelays(): List<RelayEntity>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertRelay(notificationEntity: RelayEntity): Long?
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/koalasat/pokey/database/RelayEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ data class RelayEntity(
val url: String,
val kind: Int,
val createdAt: Long,
val read: Boolean,
val write: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
Loading