Skip to content

Commit

Permalink
Changes for v1.0-beta-07 (#23)
Browse files Browse the repository at this point in the history
* Finish migration of Android source set.

* Configure the default OkHttp engine with ping interval support.

* Expose function for making a single filter request.

* Use varargs for the different Filter builder functions.

* Add Kind 1111(comment) to list of kinds. Update README to show new API usage.

* Remove Ktor-OkHttp engine ping config, until we find a reliable one.

* NostrService refactor: Remove questionable and useless code. Move Eose and Relay error counts inside requestWithResult. Improve message handling. Break should be called when we receive an EOSE. Add stopService() API.

* Add custom Ktor HTTP client support. Update and make adjustments to README.

* Re-enable Maven publish plugin.

* Add config for Jitpack CI builds.

* Tests: Migrate to new APi definitions.

* Disable publish at the module level.

* Rename project to Ballast. Refactor accordingly.

* Move to vanniktech publish plugin. Add a publish release workflow.

* Enable Jitpack support and automate release publication.

* Rename Tag properties to align with meaning in protocol specs.
  • Loading branch information
KotlinGeekDev authored Feb 12, 2025
1 parent 8282ca1 commit 2ad6068
Show file tree
Hide file tree
Showing 39 changed files with 301 additions and 164 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

name: Publish
on:
release:
types: [released, prereleased]
jobs:
publish:
name: Release build and publish
runs-on: macOS-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 17
- name: Publish to MavenCentral
run: ./gradlew publishAndReleaseToMavenCentral --no-configuration-cache
env:
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
ORG_GRADLE_PROJECT_signingInMemoryKeyId: ${{ secrets.SIGNING_KEY_ID }}
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }}
ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.GPG_KEY_CONTENTS }}
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ val clientService = NostrService(
//Without a custom pool(using the default pool)
val service = NostrService()
```
You can also use a custom HTTP client for the `NostrService`, as long as it has Websocket support.
```kotlin
val myHttpClient = httpClient {
// custom configs(or not)
}
val service = NostrService(customClient = myHttpClient)
```
The current limitation is that the HTTP client needs to be Ktor-compatible, that is, you create a
custom Ktor engine that uses you client underneath.

Note that if you need to do anything custom, such as using read-only relays,
you will need to setup the list of relays, then use them in the relay pool:
```kotlin
Expand All @@ -107,15 +117,27 @@ val customPool = RelayPool(relays = customRelays)
In order to make a subscription request, you need to construct a `RequestMessage`.
And to do that, you need to pass in a subscriptionId(just a string), and a `NostrFilter`:
```kotlin
val postsByFiatjafFilter = NostrFilter.newFilter()
.kinds(listOf(EventKind.TEXT_NOTE.kind)) // <-- Looking for posts. Other kinds can be added
.authors(listOf("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d")) // <-- The profiles for which we want to find the posts(as indicated by .kinds() above)
val postsCommentsByFiatjafFilter = NostrFilter.newFilter()
.kinds(EventKind.TEXT_NOTE.kind, EventKind.COMMENT.kind) // <-- Looking for posts and comments. Other kinds can be added
.authors("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d") // <-- The profiles for which we want to find the posts and comments(as indicated by .kinds() above)
.limit(20) // <-- Setting a limit is important, to avoid issues with relays
.build()

val myRequest = RequestMessage(
subscriptionId = "fiatjaf_posts",
filters = listOf(postsByFiatjafFilter)
subscriptionId = "fiatjaf_posts_and_comments",
filters = listOf(postsCommentsByFiatjafFilter)
)
```

If you want to make a request with a single filter, you can do so with `RequestMessage.singleFilterRequest`.
Using the same example above, we can rewrite it as:

```kotlin
val postsCommentsByFiatjafFilter = ... //Same as above

val myRequest = RequestMessage.singleFilterRequest( // <- singleFilterRequest is called here.
subscriptionId = "fiatjaf_posts_and_comments",
filter = postsByFiatjafFilter
)
```
Now, you can use the `NostrService` to make the request, either using `request()` or `requestWithResult()`.
Expand Down Expand Up @@ -145,9 +167,11 @@ during its execution. The function however displays the errors using standard ou
//Using requestWithResult() ---
// - Assuming a suspending context:
val events = clientService.requestWithResult(myRequest)
events.forEach { println(it.content) }
// OR, following with the request() example above:
appScope.launch {
val events = clientService.requestWithResult(myRequest)
val events = clientService.requestWithResult(myRequest)
events.forEach { println(it.content) }
}
```
## License
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions kostr-core/build.gradle.kts → ballast-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ kotlin {
}
}

androidInstrumentedTest.configure {
//dependsOn(commonJvmTest)
}

val androidUnitTest by getting {
dependsOn(commonJvmTest)
}
androidUnitTest.dependencies {

}


val baseJvmTest by getting {
Expand Down Expand Up @@ -200,7 +201,7 @@ kotlin {
}

android {
namespace = "ktnostr.android"
namespace = "io.github.kotlingeekdev.ballast.android"
compileSdk = 34
defaultConfig {
minSdk = 21
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.android
package ballast.android

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
Expand All @@ -15,8 +15,10 @@ import org.junit.runner.RunWith
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// val someProfile = AndroidClient()
// someProfile.testProfile()
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("kt.nostr.kostr_android.test", appContext.packageName)
assertEquals("ktnostr.android.test", appContext.packageName)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.android
package ballast.android

import org.junit.Assert.assertEquals
import org.junit.Test
Expand All @@ -11,6 +11,7 @@ import org.junit.Test
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
// println("Decoded profile: ${someProfile.toString()}")
assertEquals(4, 2 + 2)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.crypto
package ballast.crypto

import dev.whyoleg.cryptography.CryptographyProvider
import dev.whyoleg.cryptography.providers.apple.Apple
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.net
package ballast.net

import io.ktor.client.*
import io.ktor.client.engine.darwin.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.crypto
package ballast.crypto

import dev.whyoleg.cryptography.CryptographyProvider
import dev.whyoleg.cryptography.providers.jdk.JDK
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.net
package ballast.net

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr
package ballast

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.crypto
package ballast.crypto

import dev.whyoleg.cryptography.CryptographyProvider
import dev.whyoleg.cryptography.random.CryptographyRandom
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@file:JvmName("CryptoUtils")

package ktnostr.crypto
package ballast.crypto

import dev.whyoleg.cryptography.algorithms.SHA256
import fr.acinq.secp256k1.Hex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktnostr.net
package ballast.net

import io.ktor.client.*

Expand Down
Loading

0 comments on commit 2ad6068

Please sign in to comment.