Adds support for NIP-29 in public messages and new DMs. NIP-54 stays in NIP-54

This commit is contained in:
Vitor Pamplona
2024-02-01 18:31:28 -05:00
parent 54155a3c30
commit e56377f8c3
18 changed files with 277 additions and 202 deletions
@@ -1325,7 +1325,7 @@ class Account(
directMentions: Set<HexKey>,
relayList: List<Relay>? = null,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
) {
if (!isWriteable()) return
@@ -1381,7 +1381,7 @@ class Account(
zapRaiserAmount: Long? = null,
relayList: List<Relay>? = null,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
) {
if (!isWriteable()) return
@@ -1428,7 +1428,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
) {
if (!isWriteable()) return
@@ -1461,7 +1461,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
nip94attachments: List<Event>? = null,
nip94attachments: List<FileHeaderEvent>? = null,
) {
if (!isWriteable()) return
@@ -1495,6 +1495,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
) {
sendPrivateMessage(
message,
@@ -1505,6 +1506,7 @@ class Account(
wantsToMarkAsSensitive,
zapRaiserAmount,
geohash,
nip94attachments,
)
}
@@ -1517,6 +1519,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
) {
if (!isWriteable()) return
@@ -1533,6 +1536,7 @@ class Account(
markAsSensitive = wantsToMarkAsSensitive,
zapRaiserAmount = zapRaiserAmount,
geohash = geohash,
nip94attachments = nip94attachments,
signer = signer,
advertiseNip18 = false,
) {
@@ -1551,6 +1555,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
nip94attachments: List<FileHeaderEvent>? = null,
) {
if (!isWriteable()) return
@@ -1567,6 +1572,7 @@ class Account(
markAsSensitive = wantsToMarkAsSensitive,
zapRaiserAmount = zapRaiserAmount,
geohash = geohash,
nip94attachments = nip94attachments,
signer = signer,
) {
broadcastPrivately(it)
@@ -34,6 +34,9 @@ import com.vitorpamplona.amethyst.ui.components.imageExtensions
import com.vitorpamplona.amethyst.ui.components.removeQueryParamsForExtensionComparison
import com.vitorpamplona.amethyst.ui.components.tagIndex
import com.vitorpamplona.amethyst.ui.components.videoExtensions
import com.vitorpamplona.quartz.encoders.Nip29
import com.vitorpamplona.quartz.encoders.Nip54
import com.vitorpamplona.quartz.events.FileHeaderEvent
import com.vitorpamplona.quartz.events.ImmutableListOfLists
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableMap
@@ -94,27 +97,33 @@ val HTTPRegex =
.toRegex(RegexOption.IGNORE_CASE)
class RichTextParser() {
fun parseMediaUrl(fullUrl: String): ZoomableUrlContent? {
fun parseMediaUrl(
fullUrl: String,
tags: ImmutableListOfLists<String>,
): ZoomableUrlContent? {
val removedParamsFromUrl = removeQueryParamsForExtensionComparison(fullUrl)
return if (imageExtensions.any { removedParamsFromUrl.endsWith(it) }) {
val frags = Nip44UrlParser().parse(fullUrl)
val frags = Nip54().parse(fullUrl)
val tags = Nip29().parse(fullUrl, tags.lists)
ZoomableUrlImage(
url = fullUrl,
description = frags["alt"],
hash = frags["x"],
blurhash = frags["blurhash"],
dim = frags["dim"],
contentWarning = frags["content-warning"],
description = frags[FileHeaderEvent.ALT] ?: tags[FileHeaderEvent.ALT],
hash = frags[FileHeaderEvent.HASH] ?: tags[FileHeaderEvent.HASH],
blurhash = frags[FileHeaderEvent.BLUR_HASH] ?: tags[FileHeaderEvent.BLUR_HASH],
dim = frags[FileHeaderEvent.DIMENSION] ?: tags[FileHeaderEvent.DIMENSION],
contentWarning = frags["content-warning"] ?: tags["content-warning"],
)
} else if (videoExtensions.any { removedParamsFromUrl.endsWith(it) }) {
val frags = Nip44UrlParser().parse(fullUrl)
val frags = Nip54().parse(fullUrl)
val tags = Nip29().parse(fullUrl, tags.lists)
ZoomableUrlVideo(
url = fullUrl,
description = frags["alt"],
hash = frags["x"],
blurhash = frags["blurhash"],
dim = frags["dim"],
contentWarning = frags["content-warning"],
description = frags[FileHeaderEvent.ALT] ?: tags[FileHeaderEvent.ALT],
hash = frags[FileHeaderEvent.HASH] ?: tags[FileHeaderEvent.HASH],
blurhash = frags[FileHeaderEvent.BLUR_HASH] ?: tags[FileHeaderEvent.BLUR_HASH],
dim = frags[FileHeaderEvent.DIMENSION] ?: tags[FileHeaderEvent.DIMENSION],
contentWarning = frags["content-warning"] ?: tags["content-warning"],
)
} else {
null
@@ -146,7 +155,7 @@ class RichTextParser() {
}
val imagesForPager =
urlSet.mapNotNull { fullUrl -> parseMediaUrl(fullUrl) }.associateBy { it.url }
urlSet.mapNotNull { fullUrl -> parseMediaUrl(fullUrl, tags) }.associateBy { it.url }
val imageList = imagesForPager.values.toList()
val emojiMap =
@@ -1,46 +0,0 @@
/**
* Copyright (c) 2023 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.amethyst.service
import java.net.URI
import java.net.URLDecoder
import kotlin.coroutines.cancellation.CancellationException
class Nip44UrlParser {
fun parse(url: String): Map<String, String> {
return try {
fragments(URI(url))
} catch (e: Exception) {
if (e is CancellationException) throw e
emptyMap()
}
}
private fun fragments(uri: URI): Map<String, String> {
if (uri.rawFragment == null) return emptyMap()
return uri.rawFragment.split('&').associate { keyValuePair ->
val parts = keyValuePair.split('=')
val name = parts.firstOrNull() ?: ""
val value = parts.getOrNull(1)?.let { URLDecoder.decode(it, "UTF-8") } ?: ""
Pair(name, value)
}
}
}
@@ -71,7 +71,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.launch
import java.net.URLEncoder
enum class UserSuggestionAnchor {
MAIN_MESSAGE,
@@ -89,6 +88,7 @@ open class NewPostViewModel() : ViewModel() {
var pTags by mutableStateOf<List<User>?>(null)
var eTags by mutableStateOf<List<Note>?>(null)
var imetaTags = mutableStateListOf<Array<String>>()
var nip94attachments by mutableStateOf<List<FileHeaderEvent>>(emptyList())
var nip95attachments by
@@ -266,44 +266,46 @@ open class NewPostViewModel() : ViewModel() {
val urls = findURLs(tagger.message)
val usedAttachments = nip94attachments.filter { it.urls().intersect(urls.toSet()).isNotEmpty() }
usedAttachments.forEach { account?.sendHeader(it, relayList, {}) }
// Doesn't send as nip94 yet because we don't know if it makes sense.
// usedAttachments.forEach { account?.sendHeader(it, relayList, {}) }
if (originalNote?.channelHex() != null) {
if (originalNote is AddressableEvent && originalNote?.address() != null) {
account?.sendLiveMessage(
tagger.message,
originalNote?.address()!!,
tagger.eTags,
tagger.pTags,
zapReceiver,
wantsToMarkAsSensitive,
localZapRaiserAmount,
geoHash,
message = tagger.message,
toChannel = originalNote?.address()!!,
replyTo = tagger.eTags,
mentions = tagger.pTags,
zapReceiver = zapReceiver,
wantsToMarkAsSensitive = wantsToMarkAsSensitive,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash,
nip94attachments = usedAttachments,
)
} else {
account?.sendChannelMessage(
tagger.message,
tagger.channelHex!!,
tagger.eTags,
tagger.pTags,
zapReceiver,
wantsToMarkAsSensitive,
localZapRaiserAmount,
geoHash,
message = tagger.message,
toChannel = tagger.channelHex!!,
replyTo = tagger.eTags,
mentions = tagger.pTags,
zapReceiver = zapReceiver,
wantsToMarkAsSensitive = wantsToMarkAsSensitive,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash,
nip94attachments = usedAttachments,
)
}
} else if (originalNote?.event is PrivateDmEvent) {
account?.sendPrivateMessage(
tagger.message,
originalNote!!.author!!,
originalNote!!,
tagger.pTags,
zapReceiver,
wantsToMarkAsSensitive,
localZapRaiserAmount,
geoHash,
message = tagger.message,
toUser = originalNote!!.author!!,
replyingTo = originalNote!!,
mentions = tagger.pTags,
zapReceiver = zapReceiver,
wantsToMarkAsSensitive = wantsToMarkAsSensitive,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash,
nip94attachments = usedAttachments,
)
} else if (originalNote?.event is ChatMessageEvent) {
val receivers =
@@ -324,6 +326,7 @@ open class NewPostViewModel() : ViewModel() {
zapReceiver = zapReceiver,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash,
nip94attachments = usedAttachments,
)
} else if (!dmUsers.isNullOrEmpty()) {
if (nip24 || dmUsers.size > 1) {
@@ -337,6 +340,7 @@ open class NewPostViewModel() : ViewModel() {
zapReceiver = zapReceiver,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash,
nip94attachments = usedAttachments,
)
} else {
account?.sendPrivateMessage(
@@ -348,6 +352,7 @@ open class NewPostViewModel() : ViewModel() {
zapReceiver = zapReceiver,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash,
nip94attachments = usedAttachments,
)
}
} else {
@@ -461,21 +466,12 @@ open class NewPostViewModel() : ViewModel() {
onProgress = {},
)
if (!isPrivate) {
createNIP94Record(
uploadingResult = result,
localContentType = contentType,
alt = alt,
sensitiveContent = sensitiveContent,
)
} else {
noNIP94(
uploadingResult = result,
localContentType = contentType,
alt = alt,
sensitiveContent = sensitiveContent,
)
}
createNIP94Record(
uploadingResult = result,
localContentType = contentType,
alt = alt,
sensitiveContent = sensitiveContent,
)
} catch (e: Exception) {
if (e is CancellationException) throw e
Log.e(
@@ -508,6 +504,7 @@ open class NewPostViewModel() : ViewModel() {
urlPreview = null
isUploadingImage = false
pTags = null
imetaTags.clear()
wantsDirectMessage = false
@@ -703,21 +700,6 @@ open class NewPostViewModel() : ViewModel() {
contentToAddUrl == null
}
fun includePollHashtagInMessage(
include: Boolean,
hashtag: String,
) {
if (include) {
updateMessage(TextFieldValue(message.text + " $hashtag"))
} else {
updateMessage(
TextFieldValue(
message.text.replace(" $hashtag", "").replace(hashtag, ""),
),
)
}
}
suspend fun createNIP94Record(
uploadingResult: Nip96Uploader.PartialEvent,
localContentType: String?,
@@ -751,25 +733,13 @@ open class NewPostViewModel() : ViewModel() {
mimeType = remoteMimeType ?: localContentType,
dimPrecomputed = dim,
onReady = { header: FileHeader ->
account?.createHeader(imageUrl, magnet, header, alt, sensitiveContent, originalHash) {
event,
->
account?.createHeader(imageUrl, magnet, header, alt, sensitiveContent, originalHash) { event ->
isUploadingImage = false
nip94attachments = nip94attachments + event
val contentWarning = if (sensitiveContent) "" else null
message =
TextFieldValue(
message.text +
"\n" +
addInlineMetadataAsNIP54(
imageUrl,
header.dim,
header.mimeType,
alt,
header.blurHash,
header.hash,
contentWarning,
),
message.text + "\n" + imageUrl,
)
urlPreview = findUrlInMessage()
}
@@ -781,84 +751,6 @@ open class NewPostViewModel() : ViewModel() {
)
}
suspend fun noNIP94(
uploadingResult: Nip96Uploader.PartialEvent,
localContentType: String?,
alt: String?,
sensitiveContent: Boolean,
) {
// Images don't seem to be ready immediately after upload
val imageUrl = uploadingResult.tags?.firstOrNull { it.size > 1 && it[0] == "url" }?.get(1)
val remoteMimeType =
uploadingResult.tags?.firstOrNull { it.size > 1 && it[0] == "m" }?.get(1)?.ifBlank { null }
val dim =
uploadingResult.tags?.firstOrNull { it.size > 1 && it[0] == "dim" }?.get(1)?.ifBlank { null }
if (imageUrl.isNullOrBlank()) {
Log.e("ImageDownload", "Couldn't download image from server")
cancel()
isUploadingImage = false
viewModelScope.launch { imageUploadingError.emit("Server failed to return a url") }
return
}
FileHeader.prepare(
fileUrl = imageUrl,
mimeType = remoteMimeType ?: localContentType,
dimPrecomputed = dim,
onReady = { header: FileHeader ->
isUploadingImage = false
val contentWarning = if (sensitiveContent) "" else null
message =
TextFieldValue(
message.text +
"\n" +
addInlineMetadataAsNIP54(
imageUrl,
header.dim,
header.mimeType,
alt,
header.blurHash,
header.hash,
contentWarning,
),
)
urlPreview = findUrlInMessage()
},
onError = {
isUploadingImage = false
viewModelScope.launch { imageUploadingError.emit("Failed to upload the image / video") }
},
)
}
fun addInlineMetadataAsNIP54(
imageUrl: String,
dim: String?,
m: String?,
alt: String?,
blurHash: String?,
x: String?,
sensitiveContent: String?,
): String {
val extension =
listOfNotNull(
m?.ifBlank { null }?.let { "m=${URLEncoder.encode(it, "utf-8")}" },
dim?.ifBlank { null }?.let { "dim=${URLEncoder.encode(it, "utf-8")}" },
alt?.ifBlank { null }?.let { "alt=${URLEncoder.encode(it, "utf-8")}" },
blurHash?.ifBlank { null }?.let { "blurhash=${URLEncoder.encode(it, "utf-8")}" },
x?.ifBlank { null }?.let { "x=${URLEncoder.encode(it, "utf-8")}" },
sensitiveContent?.let { "content-warning=${URLEncoder.encode(it, "utf-8")}" },
)
.joinToString("&")
return if (imageUrl.contains("#")) {
"$imageUrl&$extension"
} else {
"$imageUrl#$extension"
}
}
fun createNIP95Record(
bytes: ByteArray,
mimeType: String?,
@@ -103,6 +103,7 @@ import com.vitorpamplona.amethyst.ui.theme.markdownStyle
import com.vitorpamplona.amethyst.ui.theme.replyModifier
import com.vitorpamplona.amethyst.ui.uriToRoute
import com.vitorpamplona.quartz.encoders.Nip19
import com.vitorpamplona.quartz.events.EmptyTagList
import com.vitorpamplona.quartz.events.ImmutableListOfLists
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -184,7 +185,7 @@ private fun RenderRegular(
accountViewModel: AccountViewModel,
nav: (String) -> Unit,
) {
val state by remember(content) { mutableStateOf(CachedRichTextParser.parseText(content, tags)) }
val state by remember(content, tags) { mutableStateOf(CachedRichTextParser.parseText(content, tags)) }
val currentTextStyle = LocalTextStyle.current
val currentTextColor = LocalContentColor.current
@@ -416,8 +417,8 @@ private fun RenderContentAsMarkdown(
onMediaCompose = { title, destination ->
ZoomableContentView(
content =
remember(destination) {
RichTextParser().parseMediaUrl(destination) ?: ZoomableUrlImage(url = destination)
remember(destination, tags) {
RichTextParser().parseMediaUrl(destination, tags ?: EmptyTagList) ?: ZoomableUrlImage(url = destination)
},
roundedCorner = true,
accountViewModel = accountViewModel,
@@ -78,6 +78,7 @@ import com.vitorpamplona.amethyst.ui.theme.ChatPaddingInnerQuoteModifier
import com.vitorpamplona.amethyst.ui.theme.ChatPaddingModifier
import com.vitorpamplona.amethyst.ui.theme.DoubleHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.Font12SP
import com.vitorpamplona.amethyst.ui.theme.HalfTopPadding
import com.vitorpamplona.amethyst.ui.theme.ReactionRowHeightChat
import com.vitorpamplona.amethyst.ui.theme.Size10dp
import com.vitorpamplona.amethyst.ui.theme.Size15Modifier
@@ -619,19 +620,18 @@ private fun RenderRegularTextNote(
accountViewModel: AccountViewModel,
nav: (String) -> Unit,
) {
val tags = remember(note.event) { note.event?.tags()?.toImmutableListOfLists() ?: EmptyTagList }
val modifier = remember { Modifier.padding(top = 5.dp) }
LoadDecryptedContentOrNull(note = note, accountViewModel = accountViewModel) { eventContent ->
if (eventContent != null) {
SensitivityWarning(
note = note,
accountViewModel = accountViewModel,
) {
val tags = remember(note.event) { note.event?.tags()?.toImmutableListOfLists() ?: EmptyTagList }
TranslatableRichTextViewer(
content = eventContent!!,
content = eventContent,
canPreview = canPreview,
modifier = modifier,
modifier = HalfTopPadding,
tags = tags,
backgroundColor = backgroundBubbleColor,
accountViewModel = accountViewModel,
@@ -642,8 +642,8 @@ private fun RenderRegularTextNote(
TranslatableRichTextViewer(
content = stringResource(id = R.string.could_not_decrypt_the_message),
canPreview = true,
modifier = modifier,
tags = tags,
modifier = HalfTopPadding,
tags = EmptyTagList,
backgroundColor = backgroundBubbleColor,
accountViewModel = accountViewModel,
nav = nav,
@@ -156,6 +156,7 @@ import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.events.EmptyTagList
import com.vitorpamplona.quartz.events.LiveActivitiesEvent.Companion.STATUS_LIVE
import com.vitorpamplona.quartz.events.Participant
import com.vitorpamplona.quartz.events.findURLs
import com.vitorpamplona.quartz.events.toImmutableListOfLists
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
@@ -300,6 +301,10 @@ fun ChannelScreen(
dao = accountViewModel,
)
tagger.run()
val urls = findURLs(tagger.message)
val usedAttachments = newPostModel.nip94attachments.filter { it.urls().intersect(urls.toSet()).isNotEmpty() }
if (channel is PublicChatChannel) {
accountViewModel.account.sendChannelMessage(
message = tagger.message,
@@ -307,6 +312,7 @@ fun ChannelScreen(
replyTo = tagger.eTags,
mentions = tagger.pTags,
wantsToMarkAsSensitive = false,
nip94attachments = usedAttachments,
)
} else if (channel is LiveActivitiesChannel) {
accountViewModel.account.sendLiveMessage(
@@ -315,6 +321,7 @@ fun ChannelScreen(
replyTo = tagger.eTags,
mentions = tagger.pTags,
wantsToMarkAsSensitive = false,
nip94attachments = usedAttachments,
)
}
newPostModel.message = TextFieldValue("")
@@ -115,6 +115,7 @@ import com.vitorpamplona.amethyst.ui.theme.StdPadding
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.events.ChatMessageEvent
import com.vitorpamplona.quartz.events.ChatroomKey
import com.vitorpamplona.quartz.events.findURLs
import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.Dispatchers
@@ -324,6 +325,9 @@ fun ChatroomScreen(
// LAST ROW
PrivateMessageEditFieldRow(newPostModel, isPrivate = true, accountViewModel) {
scope.launch(Dispatchers.IO) {
val urls = findURLs(newPostModel.message.text)
val usedAttachments = newPostModel.nip94attachments.filter { it.urls().intersect(urls.toSet()).isNotEmpty() }
if (newPostModel.nip24 || room.users.size > 1 || replyTo.value?.event is ChatMessageEvent) {
accountViewModel.account.sendNIP24PrivateMessage(
message = newPostModel.message.text,
@@ -331,6 +335,7 @@ fun ChatroomScreen(
replyingTo = replyTo.value,
mentions = null,
wantsToMarkAsSensitive = false,
nip94attachments = usedAttachments,
)
} else {
accountViewModel.account.sendPrivateMessage(
@@ -339,6 +344,7 @@ fun ChatroomScreen(
replyingTo = replyTo.value,
mentions = null,
wantsToMarkAsSensitive = false,
nip94attachments = usedAttachments,
)
}