Adds support for NIP-35 torrents and their comments

This commit is contained in:
Vitor Pamplona
2024-08-21 20:31:59 -04:00
parent 83f1e523ea
commit a05ad6e686
18 changed files with 1127 additions and 5 deletions

View File

@@ -117,6 +117,21 @@ class DraftEvent(
dTag: String,
): String = ATag.assembleATag(KIND, pubKey, dTag)
fun create(
dTag: String,
originalNote: TorrentCommentEvent,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (DraftEvent) -> Unit,
) {
val tagsWithMarkers =
originalNote.tags().filter {
it.size > 3 && (it[0] == "e" || it[0] == "a") && (it[3] == "root" || it[3] == "reply")
}
create(dTag, originalNote, tagsWithMarkers, signer, createdAt, onReady)
}
fun create(
dTag: String,
originalNote: LiveActivitiesChatMessageEvent,
@@ -125,10 +140,10 @@ class DraftEvent(
onReady: (DraftEvent) -> Unit,
) {
val tags = mutableListOf<Array<String>>()
originalNote.activity()?.let { tags.add(arrayOf("a", it.toTag())) }
originalNote.replyingTo()?.let { tags.add(arrayOf("e", it)) }
originalNote.activity()?.let { tags.add(arrayOf("a", it.toTag(), "", "root")) }
originalNote.replyingTo()?.let { tags.add(arrayOf("e", it, "", "reply")) }
create(dTag, originalNote, emptyList(), signer, createdAt, onReady)
create(dTag, originalNote, tags, signer, createdAt, onReady)
}
fun create(

View File

@@ -124,6 +124,8 @@ open class Event(
override fun taggedUrls() = tags.filter { it.size > 1 && it[0] == "r" }.map { it[1] }
override fun firstTag(key: String) = tags.firstOrNull { it.size > 1 && it[0] == key }?.let { it[1] }
override fun firstTagFor(vararg key: String) = tags.firstOrNull { it.size > 1 && it[0] in key }?.let { it[1] }
override fun firstTaggedUser() = tags.firstOrNull { it.size > 1 && it[0] == "p" }?.let { it[1] }

View File

@@ -142,6 +142,8 @@ class EventFactory {
StatusEvent.KIND -> StatusEvent(id, pubKey, createdAt, tags, content, sig)
TextNoteEvent.KIND -> TextNoteEvent(id, pubKey, createdAt, tags, content, sig)
TextNoteModificationEvent.KIND -> TextNoteModificationEvent(id, pubKey, createdAt, tags, content, sig)
TorrentEvent.KIND -> TorrentEvent(id, pubKey, createdAt, tags, content, sig)
TorrentCommentEvent.KIND -> TorrentCommentEvent(id, pubKey, createdAt, tags, content, sig)
VideoHorizontalEvent.KIND -> VideoHorizontalEvent(id, pubKey, createdAt, tags, content, sig)
VideoVerticalEvent.KIND -> VideoVerticalEvent(id, pubKey, createdAt, tags, content, sig)
VideoViewEvent.KIND -> VideoViewEvent(id, pubKey, createdAt, tags, content, sig)

View File

@@ -133,6 +133,8 @@ interface EventInterface {
fun taggedUrls(): List<String>
fun firstTag(key: String): String?
fun firstTagFor(vararg key: String): String?
fun firstTaggedAddress(): ATag?

View File

@@ -0,0 +1,141 @@
/**
* 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.events
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.Nip92MediaAttachments
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@Immutable
class TorrentCommentEvent(
id: HexKey,
pubKey: HexKey,
createdAt: Long,
tags: Array<Array<String>>,
content: String,
sig: HexKey,
) : BaseTextNoteEvent(id, pubKey, createdAt, KIND, tags, content, sig) {
private fun innerTorrent() =
tags.firstOrNull { it.size > 3 && it[0] == "e" && it[3] == "root" }
?: tags.firstOrNull { it.size > 1 && it[0] == "e" }
fun torrent() = innerTorrent()?.getOrNull(1)
companion object {
const val KIND = 2004
const val ALT = "Comment for a Torrent file"
fun create(
message: String,
torrent: HexKey,
replyTos: List<String>? = null,
mentions: List<String>? = null,
addresses: List<ATag>? = null,
extraTags: List<String>? = null,
zapReceiver: List<ZapSplitSetup>? = null,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
markAsSensitive: Boolean,
replyingTo: String? = null,
directMentions: Set<HexKey> = emptySet(),
zapRaiserAmount: Long?,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
forkedFrom: Event? = null,
isDraft: Boolean,
onReady: (TorrentCommentEvent) -> Unit,
) {
val content = message
val tags = mutableListOf<Array<String>>()
replyTos?.let {
tags.addAll(
it.positionalMarkedTags(
tagName = "e",
root = torrent,
replyingTo = replyingTo,
directMentions = directMentions,
forkedFrom = forkedFrom?.id,
),
)
}
mentions?.forEach {
if (it in directMentions) {
tags.add(arrayOf("p", it, "", "mention"))
} else {
tags.add(arrayOf("p", it))
}
}
replyTos?.forEach {
if (it in directMentions) {
tags.add(arrayOf("q", it))
}
}
addresses
?.map { it.toTag() }
?.let {
tags.addAll(
it.positionalMarkedTags(
tagName = "a",
root = torrent,
replyingTo = replyingTo,
directMentions = directMentions,
forkedFrom = (forkedFrom as? AddressableEvent)?.address()?.toTag(),
),
)
}
findHashtags(message).forEach {
val lowercaseTag = it.lowercase()
tags.add(arrayOf("t", it))
if (it != lowercaseTag) {
tags.add(arrayOf("t", it.lowercase()))
}
}
extraTags?.forEach { tags.add(arrayOf("t", it)) }
zapReceiver?.forEach {
tags.add(arrayOf("zap", it.lnAddressOrPubKeyHex, it.relay ?: "", it.weight.toString()))
}
findURLs(message).forEach { tags.add(arrayOf("r", it)) }
if (markAsSensitive) {
tags.add(arrayOf("content-warning", ""))
}
zapRaiserAmount?.let { tags.add(arrayOf("zapraiser", "$it")) }
geohash?.let { tags.addAll(geohashMipMap(it)) }
nip94attachments?.let {
it.forEach {
Nip92MediaAttachments().convertFromFileHeader(it)?.let {
tags.add(it)
}
}
}
tags.add(arrayOf("alt", ALT))
if (isDraft) {
signer.assembleRumor(createdAt, KIND, tags.toTypedArray(), content, onReady)
} else {
signer.sign(createdAt, KIND, tags.toTypedArray(), content, onReady)
}
}
}
}

View File

@@ -0,0 +1,127 @@
/**
* 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.events
import android.net.Uri
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.signers.NostrSigner
import com.vitorpamplona.quartz.utils.TimeUtils
@Immutable
class TorrentEvent(
id: HexKey,
pubKey: HexKey,
createdAt: Long,
tags: Array<Array<String>>,
content: String,
sig: HexKey,
) : Event(id, pubKey, createdAt, KIND, tags, content, sig) {
fun title() = firstTag("title")
fun btih() = firstTag("btih")
fun x() = firstTag("x")
fun trackers() = tags.filter { it.size > 1 && it[0] == "tracker" }.map { it[1] }
fun files() = tags.filter { it.size > 1 && it[0] == "file" }.map { TorrentFile(it[1], it.getOrNull(2)?.toLongOrNull()) }
fun toMagnetLink(): String {
val builder = Uri.Builder()
builder
.scheme("magnet")
.appendQueryParameter("xt", "urn:btih:${btih()}")
.appendQueryParameter("dn", title())
trackers().ifEmpty { DEFAULT_TRACKERS }.forEach {
builder.appendQueryParameter("tr", it)
}
return builder.build().toString()
}
fun totalSizeBytes(): Long = tags.filter { it.size > 1 && it[0] == "file" }.sumOf { it.getOrNull(2)?.toLongOrNull() ?: 0L }
companion object {
const val KIND = 2003
const val ALT_DESCRIPTION = "A torrent file"
val DEFAULT_TRACKERS =
listOf(
"http://tracker.loadpeers.org:8080/xvRKfvAlnfuf5EfxTT5T0KIVPtbqAHnX/announce",
"udp://tracker.coppersurfer.tk:6969/announce",
"udp://tracker.openbittorrent.com:6969/announce",
"udp://open.stealth.si:80/announce",
"udp://tracker.torrent.eu.org:451/announce",
"udp://tracker.opentrackr.org:1337",
)
fun create(
title: String,
btih: String,
files: List<TorrentFile>,
description: String? = null,
x: String? = null,
trackers: List<String>? = null,
alt: String? = null,
sensitiveContent: Boolean? = null,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (TorrentEvent) -> Unit,
) {
val tags =
listOfNotNull(
arrayOf("title", title),
arrayOf("btih", btih),
x?.let { arrayOf("x", it) },
alt?.let { arrayOf("alt", it) } ?: arrayOf("alt", ALT_DESCRIPTION),
sensitiveContent?.let {
if (it) {
arrayOf("content-warning", "")
} else {
null
}
},
) +
files.map {
if (it.bytes != null) {
arrayOf(it.fileName, it.bytes.toString())
} else {
arrayOf(it.fileName)
}
} +
(
trackers?.map {
arrayOf(it)
} ?: emptyList()
)
val content = description ?: ""
signer.sign(createdAt, KIND, tags.toTypedArray(), content, onReady)
}
}
}
class TorrentFile(
val fileName: String,
val bytes: Long?,
)