Skip to content

Commit

Permalink
Merge pull request #21 from KoalaSat/connect-to-inbox-relays-only
Browse files Browse the repository at this point in the history
Connect to inbox relays only
  • Loading branch information
KoalaSat authored Nov 1, 2024
2 parents cb594a1 + 16511cc commit 2716f04
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ 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 @@ -215,6 +217,7 @@ class NotificationsService : Service() {
TypedFilter(
types = COMMON_FEED_TYPES,
filter = SincePerRelayFilter(
kinds = listOf(1, 3, 4, 6, 7, 1059, 9735),
tags = mapOf("p" to listOf(hexKey)),
since = RelayPool.getAll().associate { it.url to EOSETime(latestNotification) },
),
Expand Down Expand Up @@ -305,13 +308,29 @@ class NotificationsService : Service() {
if (lastCreatedRelayAt == null || lastCreatedRelayAt < event.createdAt) {
stopSubscription()
dao.deleteRelaysByKind(event.kind)

event.tags
.filter { it.size > 1 && (it[0] == "relay" || it[0] == "r") }
.map {
val entity = RelayEntity(id = 0, url = it[1], kind = event.kind, createdAt = event.createdAt)
dao.insertRelay(entity)
entity
.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)
}
}

startSubscription()
}
}
Expand Down Expand Up @@ -433,7 +452,6 @@ class NotificationsService : Service() {
}

private fun connectRelays() {
RelayPool.unloadRelays()
val dao = AppDatabase.getDatabase(this@NotificationsService, Pokey.getInstance().getHexKey()).applicationDao()
var relays = dao.getRelays()
if (relays.isEmpty()) {
Expand All @@ -442,15 +460,17 @@ class NotificationsService : Service() {

relays.forEach {
Client.sendFilterOnlyIfDisconnected()
RelayPool.addRelay(
Relay(
it.url,
read = true,
write = false,
forceProxy = false,
activeTypes = COMMON_FEED_TYPES,
),
)
if (RelayPool.getRelays(it.url).isEmpty()) {
RelayPool.addRelay(
Relay(
it.url,
read = true,
write = false,
forceProxy = false,
activeTypes = COMMON_FEED_TYPES,
),
)
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="title_notifications">Notifications</string>
<string name="pokey_is_running_in_background">Pokey is running in background</string>
<string name="amber_not_found">Amber not installed</string>
<string name="inbox_relays_error">Your DM inbox relays are not configured</string>
<string name="relays_connection">Relays connection</string>
<string name="notifications">Notifications</string>
<string name="new_mention">New mention</string>
Expand Down

0 comments on commit 2716f04

Please sign in to comment.