Skip to content
Open
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
10 changes: 8 additions & 2 deletions app/src/main/kotlin/com/wisp/app/nostr/Blossom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ object Blossom {
if (tag.size >= 2 && tag[0] == "server") {
val url = tag[1].toHttpUrlOrNull()
url?.toString()
}
else {
} else {
null
}
}.distinct()
}

/**
* Builds BUD-03 server tags from a non-empty list of unique already-valid server URLs.
* URL validation is expected to happen when URLs enter repository/viewmodel state.
*
* @throws IllegalArgumentException if [urls] is empty.
*/
fun buildServerListTags(urls: List<String>): List<List<String>> {
require(urls.isNotEmpty()) { "Blossom server list must contain at least one server" }
return urls.map { listOf("server", it) }
}

Expand Down
26 changes: 26 additions & 0 deletions app/src/test/kotlin/com/wisp/app/nostr/BlossomTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,30 @@ class BlossomTest {
result
)
}

@Test
fun `buildServerListTags throws IllegalArgumentException when urls are empty`() {
val emptyUrls = emptyList<String>()
assertFailsWith<IllegalArgumentException> { Blossom.buildServerListTags(emptyUrls) }
}

@Test
fun `buildServerListTags builds server tags preserving order`() {
val valid1 = "http://example.com"
val valid2 = "https://example.com/"
val singleServerTags = Blossom.buildServerListTags(listOf(valid1))
assertEquals(
listOf(
listOf("server", valid1)
), singleServerTags
)

val manyServerTags = Blossom.buildServerListTags(listOf(valid2, valid1))
assertEquals(
listOf(
listOf("server", valid2),
listOf("server", valid1)
), manyServerTags
)
}
}