Updates NIP-56 to the newest tag structure for Quartz
This commit is contained in:
@@ -25,14 +25,15 @@ import com.vitorpamplona.quartz.nip01Core.core.AddressableEvent
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
|
||||
import com.vitorpamplona.quartz.nip31Alts.AltTag
|
||||
import com.vitorpamplona.quartz.nip31Alts.alt
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.DefaultReportTag
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.ReportedAddressTag
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.ReportedAuthorTag
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.ReportedEventTag
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
|
||||
@Immutable data class ReportedKey(
|
||||
val key: String,
|
||||
val reportType: ReportEvent.ReportType,
|
||||
)
|
||||
|
||||
// NIP 56 event.
|
||||
@Immutable
|
||||
class ReportEvent(
|
||||
@@ -43,48 +44,35 @@ class ReportEvent(
|
||||
content: String,
|
||||
sig: HexKey,
|
||||
) : Event(id, pubKey, createdAt, KIND, tags, content, sig) {
|
||||
@Transient
|
||||
private var defaultType: ReportType? = null
|
||||
|
||||
private fun defaultReportTypes() = tags.mapNotNull(DefaultReportTag::parse)
|
||||
|
||||
private fun defaultReportType(): ReportType {
|
||||
defaultType?.let { return it }
|
||||
|
||||
// Works with old and new structures for report.
|
||||
var reportType =
|
||||
tags
|
||||
.filter { it.firstOrNull() == "report" }
|
||||
.mapNotNull { it.getOrNull(1) }
|
||||
.map { ReportType.valueOf(it.uppercase()) }
|
||||
.firstOrNull()
|
||||
var reportType = defaultReportTypes().firstOrNull()
|
||||
if (reportType == null) {
|
||||
reportType =
|
||||
tags.mapNotNull { it.getOrNull(2) }.map { ReportType.valueOf(it.uppercase()) }.firstOrNull()
|
||||
reportType = tags.mapNotNull { it.getOrNull(2) }.map { ReportType.parseOrNull(it, emptyArray()) }.firstOrNull()
|
||||
}
|
||||
if (reportType == null) {
|
||||
reportType = ReportType.SPAM
|
||||
}
|
||||
defaultType = reportType
|
||||
return reportType
|
||||
}
|
||||
|
||||
fun reportedPost() =
|
||||
tags
|
||||
.filter { it.size > 1 && it[0] == "e" }
|
||||
.map {
|
||||
ReportedKey(
|
||||
it[1],
|
||||
it.getOrNull(2)?.uppercase()?.let { it1 -> ReportType.valueOf(it1) }
|
||||
?: defaultReportType(),
|
||||
)
|
||||
}
|
||||
fun reportedPost() = tags.mapNotNull { ReportedEventTag.parse(it, defaultReportType()) }
|
||||
|
||||
fun reportedAuthor() =
|
||||
tags
|
||||
.filter { it.size > 1 && it[0] == "p" }
|
||||
.map {
|
||||
ReportedKey(
|
||||
it[1],
|
||||
it.getOrNull(2)?.uppercase()?.let { it1 -> ReportType.valueOf(it1) }
|
||||
?: defaultReportType(),
|
||||
)
|
||||
}
|
||||
fun reportedAddresses() = tags.mapNotNull { ReportedAddressTag.parse(it, defaultReportType()) }
|
||||
|
||||
fun reportedAuthor() = tags.mapNotNull { ReportedAuthorTag.parse(it, defaultReportType()) }
|
||||
|
||||
companion object {
|
||||
const val KIND = 1984
|
||||
const val ALT_PREFIX = "Report for "
|
||||
|
||||
fun create(
|
||||
reportedPost: Event,
|
||||
@@ -108,32 +96,27 @@ class ReportEvent(
|
||||
signer.sign(createdAt, KIND, tags, content, onReady)
|
||||
}
|
||||
|
||||
fun create(
|
||||
reportedUser: String,
|
||||
fun build(
|
||||
reportedPost: Event,
|
||||
type: ReportType,
|
||||
signer: NostrSigner,
|
||||
createdAt: Long = TimeUtils.now(),
|
||||
onReady: (ReportEvent) -> Unit,
|
||||
) {
|
||||
val content = ""
|
||||
) = eventTemplate(KIND, "", createdAt) {
|
||||
alt(ALT_PREFIX + type.code)
|
||||
event(reportedPost.id, type)
|
||||
user(reportedPost.pubKey, type)
|
||||
|
||||
val reportAuthorTag = arrayOf("p", reportedUser, type.name.lowercase())
|
||||
val alt = AltTag.assemble("Report for ${type.name}")
|
||||
if (reportedPost is AddressableEvent) {
|
||||
address(reportedPost.address(), type)
|
||||
}
|
||||
}
|
||||
|
||||
val tags: Array<Array<String>> = arrayOf(reportAuthorTag, alt)
|
||||
signer.sign(createdAt, KIND, tags, content, onReady)
|
||||
fun build(
|
||||
reportedUser: HexKey,
|
||||
type: ReportType,
|
||||
createdAt: Long = TimeUtils.now(),
|
||||
) = eventTemplate(KIND, "", createdAt) {
|
||||
alt(ALT_PREFIX + type.code)
|
||||
user(reportedUser, type)
|
||||
}
|
||||
}
|
||||
|
||||
enum class ReportType {
|
||||
EXPLICIT, // Not used anymore.
|
||||
ILLEGAL,
|
||||
SPAM,
|
||||
IMPERSONATION,
|
||||
NUDITY,
|
||||
PROFANITY,
|
||||
MALWARE,
|
||||
MOD,
|
||||
OTHER,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 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.nip56Reports
|
||||
|
||||
import android.util.Log
|
||||
|
||||
enum class ReportType(
|
||||
val code: String,
|
||||
) {
|
||||
EXPLICIT("explicit"), // Not used anymore.
|
||||
ILLEGAL("illegal"),
|
||||
SPAM("spam"),
|
||||
IMPERSONATION("impersonation"),
|
||||
NUDITY("nudity"),
|
||||
PROFANITY("profanity"),
|
||||
MALWARE("malware"),
|
||||
MOD("mod"),
|
||||
OTHER("other"),
|
||||
;
|
||||
|
||||
companion object {
|
||||
fun parseOrNull(
|
||||
code: String,
|
||||
tag: Array<String>,
|
||||
): ReportType? =
|
||||
when (code) {
|
||||
EXPLICIT.code -> EXPLICIT
|
||||
ILLEGAL.code -> ILLEGAL
|
||||
SPAM.code -> SPAM
|
||||
IMPERSONATION.code -> IMPERSONATION
|
||||
NUDITY.code -> NUDITY
|
||||
PROFANITY.code -> PROFANITY
|
||||
MALWARE.code -> MALWARE
|
||||
MOD.code -> MOD
|
||||
"MOD" -> MOD
|
||||
OTHER.code -> OTHER
|
||||
else -> {
|
||||
Log.w("ReportedEventTag", "Report type not supported: $code ${tag.joinToString(", ")}")
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 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.nip56Reports
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
|
||||
import com.vitorpamplona.quartz.nip01Core.tags.addressables.Address
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.HashSha256Tag
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.ReportedAddressTag
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.ReportedAuthorTag
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.ReportedEventTag
|
||||
import com.vitorpamplona.quartz.nip56Reports.tags.ServerTag
|
||||
|
||||
fun TagArrayBuilder<ReportEvent>.event(
|
||||
eventId: HexKey,
|
||||
reportType: ReportType,
|
||||
) = addUnique(ReportedEventTag.assemble(eventId, reportType))
|
||||
|
||||
fun TagArrayBuilder<ReportEvent>.address(
|
||||
address: Address,
|
||||
reportType: ReportType,
|
||||
) = addUnique(ReportedAddressTag.assemble(address, reportType))
|
||||
|
||||
fun TagArrayBuilder<ReportEvent>.user(
|
||||
pubkey: HexKey,
|
||||
reportType: ReportType,
|
||||
) = addUnique(ReportedAuthorTag.assemble(pubkey, reportType))
|
||||
|
||||
fun TagArrayBuilder<ReportEvent>.hash(
|
||||
x: String,
|
||||
reportType: ReportType,
|
||||
) = addUnique(HashSha256Tag.assemble(x, reportType))
|
||||
|
||||
fun TagArrayBuilder<ReportEvent>.server(url: String) = addUnique(ServerTag.assemble(url))
|
||||
+5
-5
@@ -18,10 +18,10 @@
|
||||
* 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.nip70ProtectedEvts
|
||||
package com.vitorpamplona.quartz.nip56Reports.tags
|
||||
|
||||
class ProtectedTagSerializer {
|
||||
companion object {
|
||||
fun toTagArray(reason: String = "") = arrayOf("-", reason)
|
||||
}
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportType
|
||||
|
||||
interface BaseReportTag {
|
||||
val type: ReportType?
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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.nip56Reports.tags
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportType
|
||||
import com.vitorpamplona.quartz.utils.arrayOfNotNull
|
||||
import com.vitorpamplona.quartz.utils.ensure
|
||||
|
||||
@Immutable
|
||||
class DefaultReportTag {
|
||||
companion object {
|
||||
const val TAG_NAME = "report"
|
||||
|
||||
@JvmStatic
|
||||
fun parse(tag: Array<String>): ReportType? {
|
||||
ensure(tag.has(1)) { return null }
|
||||
ensure(tag[0] == TAG_NAME) { return null }
|
||||
ensure(tag[1].isNotEmpty()) { return null }
|
||||
|
||||
return ReportType.parseOrNull(tag[1], tag)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun assemble(type: ReportType? = null) = arrayOfNotNull(TAG_NAME, type?.code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* 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.nip56Reports.tags
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportType
|
||||
import com.vitorpamplona.quartz.utils.arrayOfNotNull
|
||||
import com.vitorpamplona.quartz.utils.ensure
|
||||
|
||||
class HashSha256Tag(
|
||||
val hash: HexKey,
|
||||
override val type: ReportType? = null,
|
||||
) : BaseReportTag {
|
||||
companion object {
|
||||
const val TAG_NAME = "x"
|
||||
|
||||
@JvmStatic
|
||||
fun parse(
|
||||
tag: Array<String>,
|
||||
defaultReportType: ReportType? = null,
|
||||
): HashSha256Tag? {
|
||||
ensure(tag.has(1)) { return null }
|
||||
ensure(tag[0] == TAG_NAME) { return null }
|
||||
ensure(tag[1].length == 64) { return null }
|
||||
|
||||
val type = tag.getOrNull(2)?.let { ReportType.parseOrNull(it, tag) } ?: defaultReportType
|
||||
return HashSha256Tag(tag[1], type)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun assemble(
|
||||
hash: String,
|
||||
type: ReportType? = null,
|
||||
) = arrayOfNotNull(TAG_NAME, hash, type?.code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* 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.nip56Reports.tags
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||
import com.vitorpamplona.quartz.nip01Core.tags.addressables.Address
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportType
|
||||
import com.vitorpamplona.quartz.utils.arrayOfNotNull
|
||||
import com.vitorpamplona.quartz.utils.ensure
|
||||
|
||||
@Immutable
|
||||
class ReportedAddressTag(
|
||||
val address: Address,
|
||||
override val type: ReportType? = null,
|
||||
) : BaseReportTag {
|
||||
fun toTagArray() = assemble(address, type)
|
||||
|
||||
companion object {
|
||||
const val TAG_NAME = "a"
|
||||
|
||||
@JvmStatic
|
||||
fun parse(
|
||||
tag: Array<String>,
|
||||
defaultReportType: ReportType? = null,
|
||||
): ReportedAddressTag? {
|
||||
ensure(tag.has(1)) { return null }
|
||||
ensure(tag[0] == TAG_NAME) { return null }
|
||||
ensure(tag[1].isNotEmpty()) { return null }
|
||||
|
||||
val address = Address.parse(tag[1])
|
||||
|
||||
ensure(address != null) { return null }
|
||||
|
||||
val type =
|
||||
if (tag.size == 2) {
|
||||
defaultReportType
|
||||
} else if (tag.size == 3) {
|
||||
ReportType.parseOrNull(tag[2], tag) ?: defaultReportType
|
||||
} else {
|
||||
ReportType.parseOrNull(tag[3], tag) ?: defaultReportType
|
||||
}
|
||||
|
||||
return ReportedAddressTag(address, type)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun assemble(
|
||||
address: Address,
|
||||
type: ReportType? = null,
|
||||
) = arrayOfNotNull(TAG_NAME, address.toValue(), type?.code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 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.nip56Reports.tags
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportType
|
||||
import com.vitorpamplona.quartz.utils.arrayOfNotNull
|
||||
import com.vitorpamplona.quartz.utils.ensure
|
||||
|
||||
@Immutable
|
||||
class ReportedAuthorTag(
|
||||
val pubkey: HexKey,
|
||||
override val type: ReportType? = null,
|
||||
) : BaseReportTag {
|
||||
fun toTagArray() = assemble(pubkey, type)
|
||||
|
||||
companion object {
|
||||
const val TAG_NAME = "p"
|
||||
|
||||
@JvmStatic
|
||||
fun parse(
|
||||
tag: Array<String>,
|
||||
defaultReportType: ReportType? = null,
|
||||
): ReportedAuthorTag? {
|
||||
ensure(tag.has(1)) { return null }
|
||||
ensure(tag[0] == TAG_NAME) { return null }
|
||||
ensure(tag[1].length == 64) { return null }
|
||||
|
||||
val type =
|
||||
if (tag.size == 2) {
|
||||
defaultReportType
|
||||
} else if (tag.size == 3) {
|
||||
ReportType.parseOrNull(tag[2], tag) ?: defaultReportType
|
||||
} else {
|
||||
ReportType.parseOrNull(tag[3], tag) ?: defaultReportType
|
||||
}
|
||||
|
||||
return ReportedAuthorTag(tag[1], type)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun assemble(
|
||||
pubkey: HexKey,
|
||||
type: ReportType? = null,
|
||||
) = arrayOfNotNull(TAG_NAME, pubkey, type?.code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 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.nip56Reports.tags
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportType
|
||||
import com.vitorpamplona.quartz.utils.arrayOfNotNull
|
||||
import com.vitorpamplona.quartz.utils.ensure
|
||||
|
||||
@Immutable
|
||||
class ReportedEventTag(
|
||||
val eventId: HexKey,
|
||||
override val type: ReportType? = null,
|
||||
) : BaseReportTag {
|
||||
fun toTagArray() = assemble(eventId, type)
|
||||
|
||||
companion object {
|
||||
const val TAG_NAME = "e"
|
||||
|
||||
@JvmStatic
|
||||
fun parse(
|
||||
tag: Array<String>,
|
||||
defaultReportType: ReportType? = null,
|
||||
): ReportedEventTag? {
|
||||
ensure(tag.has(1)) { return null }
|
||||
ensure(tag[0] == TAG_NAME) { return null }
|
||||
ensure(tag[1].length == 64) { return null }
|
||||
|
||||
val type =
|
||||
if (tag.size == 2) {
|
||||
defaultReportType
|
||||
} else if (tag.size == 3) {
|
||||
ReportType.parseOrNull(tag[2], tag) ?: defaultReportType
|
||||
} else {
|
||||
ReportType.parseOrNull(tag[3], tag) ?: defaultReportType
|
||||
}
|
||||
|
||||
return ReportedEventTag(tag[1], type)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun assemble(
|
||||
eventId: HexKey,
|
||||
type: ReportType? = null,
|
||||
) = arrayOfNotNull(TAG_NAME, eventId, type?.code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* 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.nip56Reports.tags
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.has
|
||||
import com.vitorpamplona.quartz.utils.ensure
|
||||
|
||||
class ServerTag {
|
||||
companion object {
|
||||
const val TAG_NAME = "server"
|
||||
|
||||
@JvmStatic
|
||||
fun parse(tag: Array<String>): String? {
|
||||
ensure(tag.has(1)) { return null }
|
||||
ensure(tag[0] == TAG_NAME) { return null }
|
||||
return tag[1]
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun assemble(url: String) = arrayOf(TAG_NAME, url)
|
||||
|
||||
@JvmStatic
|
||||
fun assemble(urls: List<String>) = urls.map { assemble(it) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user