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

Connect to inbox relays only #21

Merged
merged 1 commit into from
Nov 1, 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
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
Loading