From 1f9aeddb57fd068faf44e9d4dc630d179d01bf50 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 15 Jan 2025 09:17:32 -0500 Subject: [PATCH] Adds support for NIP-48 to Quartz --- .../quartz/nip01Core/core/TagArrayBuilder.kt | 31 ++++++++++ .../quartz/nip48ProxyTags/EventExt.kt | 29 +++++++++ .../quartz/nip48ProxyTags/ProxyTag.kt | 60 +++++++++++++++++++ .../nip48ProxyTags/TagArrayBuilderExt.kt | 33 ++++++++++ .../quartz/nip48ProxyTags/TagArrayExt.kt | 32 ++++++++++ 5 files changed, 185 insertions(+) create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/EventExt.kt create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayBuilderExt.kt create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayExt.kt diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt new file mode 100644 index 000000000..9827b415b --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip01Core.core + +class TagArrayBuilder { + val tagList = mutableListOf>() + + fun add(tag: Array) { + tagList.add(tag) + } + + fun build() = tagList.toTypedArray() +} diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/EventExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/EventExt.kt new file mode 100644 index 000000000..8357a15d0 --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/EventExt.kt @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip48ProxyTags + +import com.vitorpamplona.quartz.nip01Core.core.Event + +fun Event.taggedProxies() = tags.taggedProxies() + +fun Event.firstTaggedProxy() = tags.firstTaggedProxy() + +fun Event.hasAnyTaggedProxy() = tags.hasAnyTaggedProxy() diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt new file mode 100644 index 000000000..b11c0e4f2 --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip48ProxyTags + +import androidx.compose.runtime.Immutable +import com.vitorpamplona.quartz.utils.bytesUsedInMemory +import com.vitorpamplona.quartz.utils.pointerSizeInBytes + +@Immutable +data class ProxyTag( + val id: String, + val protocol: String, +) { + fun countMemory(): Long = 2 * pointerSizeInBytes + id.bytesUsedInMemory() + protocol.bytesUsedInMemory() + + fun toTagArray() = assemble(id, protocol) + + companion object { + const val TAG_NAME = "proxy" + + @JvmStatic + fun parse(tags: Array): ProxyTag { + require(tags[0] == TAG_NAME) + return ProxyTag(tags[1], tags[2]) + } + + @JvmStatic + fun assemble( + id: String, + protocol: String, + ) = arrayOf(TAG_NAME, id, protocol) + } + + enum class Protocol( + val code: String, + ) { + ACT_PUB("activitypub"), + AT("atproto"), + RSS("rss"), + WEB("web"), + } +} diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayBuilderExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayBuilderExt.kt new file mode 100644 index 000000000..21e12bf74 --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayBuilderExt.kt @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip48ProxyTags + +import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder + +fun TagArrayBuilder.proxy( + id: String, + pt: ProxyTag.Protocol, +) = add(ProxyTag.assemble(id, pt.code)) + +fun TagArrayBuilder.proxy( + id: String, + pt: String, +) = add(ProxyTag.assemble(id, pt)) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayExt.kt new file mode 100644 index 000000000..6b74a8c9d --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip48ProxyTags/TagArrayExt.kt @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip48ProxyTags + +import com.vitorpamplona.quartz.nip01Core.core.TagArray +import com.vitorpamplona.quartz.nip01Core.core.firstTagValue +import com.vitorpamplona.quartz.nip01Core.core.hasTagWithContent +import com.vitorpamplona.quartz.nip01Core.core.mapValues + +fun TagArray.taggedProxies() = this.mapValues(ProxyTag.TAG_NAME) + +fun TagArray.firstTaggedProxy() = this.firstTagValue(ProxyTag.TAG_NAME) + +fun TagArray.hasAnyTaggedProxy() = this.hasTagWithContent(ProxyTag.TAG_NAME)