Adds the ability to see and reply to Git Issues and Patches.

This commit is contained in:
Vitor Pamplona
2024-03-01 18:09:15 -05:00
parent 786802b708
commit 81cc985e3b
7 changed files with 228 additions and 38 deletions
@@ -41,7 +41,7 @@ class GitIssueEvent(
private fun repositoryHex() = innerRepository()?.getOrNull(1)
fun rootIssueOrPath() = tags.lastOrNull { it.size > 3 && it[0] == "e" && it[3] == "root" }?.get(1)
fun rootIssueOrPatch() = tags.lastOrNull { it.size > 3 && it[0] == "e" && it[3] == "root" }?.get(1)
fun repository() =
innerRepository()?.let {
@@ -23,6 +23,7 @@ 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
@@ -75,5 +76,87 @@ class GitReplyEvent(
signer.sign(createdAt, KIND, tags.toTypedArray(), content, onReady)
}
fun create(
msg: String,
replyTos: List<String>? = null,
mentions: List<String>? = null,
addresses: List<ATag>? = null,
extraTags: List<String>? = null,
zapReceiver: List<ZapSplitSetup>? = null,
markAsSensitive: Boolean = false,
zapRaiserAmount: Long? = null,
replyingTo: String? = null,
root: String? = null,
directMentions: Set<HexKey> = emptySet(),
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
forkedFrom: Event? = null,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (GitReplyEvent) -> Unit,
) {
val tags = mutableListOf<Array<String>>()
replyTos?.let {
tags.addAll(
it.positionalMarkedTags(
tagName = "e",
root = root,
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 = root,
replyingTo = replyingTo,
directMentions = directMentions,
forkedFrom = (forkedFrom as? AddressableEvent)?.address()?.toTag(),
),
)
}
findHashtags(msg).forEach {
tags.add(arrayOf("t", it))
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(msg).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", "a git issue reply"))
signer.sign(createdAt, KIND, tags.toTypedArray(), msg, onReady)
}
}
}
@@ -123,45 +123,45 @@ class TextNoteEvent(
signer.sign(createdAt, KIND, tags.toTypedArray(), msg, onReady)
}
/**
* Returns a list of NIP-10 marked tags that are also ordered at best effort to support the
* deprecated method of positional tags to maximize backwards compatibility with clients that
* support replies but have not been updated to understand tag markers.
*
* https://github.com/nostr-protocol/nips/blob/master/10.md
*
* The tag to the root of the reply chain goes first. The tag to the reply event being responded
* to goes last. The order for any other tag does not matter, so keep the relative order.
*/
private fun List<String>.positionalMarkedTags(
tagName: String,
root: String?,
replyingTo: String?,
directMentions: Set<HexKey>,
forkedFrom: String?,
) = sortedWith { o1, o2 ->
when {
o1 == o2 -> 0
o1 == root -> -1 // root goes first
o2 == root -> 1 // root goes first
o1 == replyingTo -> 1 // reply event being responded to goes last
o2 == replyingTo -> -1 // reply event being responded to goes last
else -> 0 // keep the relative order for any other tag
}
}
.map {
when (it) {
root -> arrayOf(tagName, it, "", "root")
replyingTo -> arrayOf(tagName, it, "", "reply")
forkedFrom -> arrayOf(tagName, it, "", "fork")
in directMentions -> arrayOf(tagName, it, "", "mention")
else -> arrayOf(tagName, it)
}
}
}
}
fun findURLs(text: String): List<String> {
return UrlDetector(text, UrlDetectorOptions.Default).detect().map { it.originalUrl }
}
/**
* Returns a list of NIP-10 marked tags that are also ordered at best effort to support the
* deprecated method of positional tags to maximize backwards compatibility with clients that
* support replies but have not been updated to understand tag markers.
*
* https://github.com/nostr-protocol/nips/blob/master/10.md
*
* The tag to the root of the reply chain goes first. The tag to the reply event being responded
* to goes last. The order for any other tag does not matter, so keep the relative order.
*/
fun List<String>.positionalMarkedTags(
tagName: String,
root: String?,
replyingTo: String?,
directMentions: Set<HexKey>,
forkedFrom: String?,
) = sortedWith { o1, o2 ->
when {
o1 == o2 -> 0
o1 == root -> -1 // root goes first
o2 == root -> 1 // root goes first
o1 == replyingTo -> 1 // reply event being responded to goes last
o2 == replyingTo -> -1 // reply event being responded to goes last
else -> 0 // keep the relative order for any other tag
}
}
.map {
when (it) {
root -> arrayOf(tagName, it, "", "root")
replyingTo -> arrayOf(tagName, it, "", "reply")
forkedFrom -> arrayOf(tagName, it, "", "fork")
in directMentions -> arrayOf(tagName, it, "", "mention")
else -> arrayOf(tagName, it)
}
}