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

Publish relay lists #35

Merged
merged 1 commit into from
Nov 5, 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
81 changes: 81 additions & 0 deletions app/src/main/java/com/koalasat/pokey/ui/relays/RelaysFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ import com.koalasat.pokey.R
import com.koalasat.pokey.database.AppDatabase
import com.koalasat.pokey.database.RelayEntity
import com.koalasat.pokey.databinding.FragmentRelaysBinding
import com.koalasat.pokey.models.ExternalSigner
import com.vitorpamplona.ammolite.relays.COMMON_FEED_TYPES
import com.vitorpamplona.ammolite.relays.Client
import com.vitorpamplona.ammolite.relays.Relay
import com.vitorpamplona.ammolite.relays.RelayPool
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.utils.TimeUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -82,6 +87,9 @@ class RelaysFragment : Fragment() {
binding.addPublicRelayUrl.error = getString(R.string.invalid_uri)
}
}
binding.publishPublicRelay.setOnClickListener {
publishPublicRelays()
}
binding.reloadPublicRelays.setOnClickListener {
Pokey.updateLoadingPublicRelays(true)
reconnectRelays(publicRelaysKind)
Expand Down Expand Up @@ -119,6 +127,7 @@ class RelaysFragment : Fragment() {
}
}
binding.publishPrivateRelay.setOnClickListener {
publishPrivateRelays()
}
binding.reloadPrivateRelay.setOnClickListener {
Pokey.updateLoadingPrivateRelays(true)
Expand Down Expand Up @@ -233,4 +242,76 @@ class RelaysFragment : Fragment() {
}
}
}

private fun publishPrivateRelays() {
val pubKey = Pokey.getInstance().getHexKey()
val createdAt = TimeUtils.now()
val kind = 10050
val content = ""
val tags = privateList.map { arrayOf("relay", it.url) }.toTypedArray()
val id = Event.generateId(pubKey, createdAt, kind, tags, content).toHexKey()
val event =
Event(
id = id,
pubKey = pubKey,
createdAt = createdAt,
kind = kind,
tags = tags,
content = content,
sig = "",
)
ExternalSigner.sign(event) {
val signeEvent = Event(
id = id,
pubKey = pubKey,
createdAt = createdAt,
kind = kind,
tags = tags,
content = content,
sig = it,
)
Log.d("Pokey", "Relay private list : ${signeEvent.toJson()}")
Client.send(signeEvent)
}
}

private fun publishPublicRelays() {
val pubKey = Pokey.getInstance().getHexKey()
val createdAt = TimeUtils.now()
val kind = 10002
val content = ""
val tags = publicList.map {
var tag = arrayOf("r", it.url)
if (it.read == 1 && it.write == 0) {
tag += "read"
} else if (it.read == 0 && it.write == 1) {
tag += "write"
}
tag
}.toTypedArray()
val id = Event.generateId(pubKey, createdAt, kind, tags, content).toHexKey()
val event =
Event(
id = id,
pubKey = pubKey,
createdAt = createdAt,
kind = kind,
tags = tags,
content = content,
sig = "",
)
ExternalSigner.sign(event) {
val signeEvent = Event(
id = id,
pubKey = pubKey,
createdAt = createdAt,
kind = kind,
tags = tags,
content = content,
sig = it,
)
Log.d("Pokey", "Relay public list : ${signeEvent.toJson()}")
Client.send(signeEvent)
}
}
}
3 changes: 0 additions & 3 deletions app/src/main/res/layout/fragment_relays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:enabled="false"
android:gravity="center"
android:text="@string/publish"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down Expand Up @@ -134,7 +133,6 @@
android:layout_width="match_parent"
android:layout_height="39dp"
android:layout_marginTop="25dp"
android:hint="@string/private_inbox"
android:text="@string/private_inbox"
android:textAlignment="center"
android:textSize="20sp" />
Expand All @@ -161,7 +159,6 @@
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:enabled="false"
android:gravity="center"
android:text="@string/publish"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
Loading