Skip to content

Commit

Permalink
Fixes content sensitivity for NIP-54 images.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Jan 29, 2024
1 parent d88eb01 commit 6638def
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class RichTextParser() {
hash = frags["x"],
blurhash = frags["blurhash"],
dim = frags["dim"],
contentWarning = frags["content-warning"],
)
} else if (videoExtensions.any { removedParamsFromUrl.endsWith(it) }) {
val frags = Nip44UrlParser().parse(fullUrl)
Expand All @@ -112,6 +113,7 @@ class RichTextParser() {
hash = frags["x"],
blurhash = frags["blurhash"],
dim = frags["dim"],
contentWarning = frags["content-warning"],
)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ fun ImageVideoPost(
modifier = Modifier.fillMaxWidth(),
) {
SettingSwitchItem(
modifier = Modifier.fillMaxWidth().padding(8.dp),
checked = postViewModel.sensitiveContent,
onCheckedChange = { postViewModel.sensitiveContent = it },
title = R.string.add_sensitive_content_label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,7 @@ fun ImageVideoDescription(
modifier = Modifier.fillMaxWidth(),
) {
SettingSwitchItem(
modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp),
checked = sensitiveContent,
onCheckedChange = { sensitiveContent = it },
title = R.string.add_sensitive_content_label,
Expand Down Expand Up @@ -1764,7 +1765,7 @@ fun ImageVideoDescription(

@Composable
fun SettingSwitchItem(
modifier: Modifier = Modifier,
modifier: Modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
title: Int,
Expand All @@ -1774,8 +1775,6 @@ fun SettingSwitchItem(
Row(
modifier =
modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp)
.toggleable(
value = checked,
enabled = enabled,
Expand All @@ -1786,7 +1785,7 @@ fun SettingSwitchItem(
) {
Column(
modifier = Modifier.weight(1.0f),
verticalArrangement = Arrangement.spacedBy(3.dp),
verticalArrangement = Arrangement.spacedBy(Size5dp),
) {
Text(
text = stringResource(id = title),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ open class NewPostViewModel() : ViewModel() {
}

val urls = findURLs(tagger.message)
val usedAttachments = nip94attachments.filter { it.urls().intersect(urls).isNotEmpty() }
val usedAttachments = nip94attachments.filter { it.urls().intersect(urls.toSet()).isNotEmpty() }
usedAttachments.forEach { account?.sendHeader(it, relayList, {}) }

if (originalNote?.channelHex() != null) {
Expand Down Expand Up @@ -846,9 +846,7 @@ open class NewPostViewModel() : ViewModel() {
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
?.ifBlank { null }
?.let { "content-warning=${URLEncoder.encode(it, "utf-8")}" },
sensitiveContent?.let { "content-warning=${URLEncoder.encode(it, "utf-8")}" },
)
.joinToString("&")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,17 @@ fun SensitivityWarning(
accountViewModel: AccountViewModel,
content: @Composable () -> Unit,
) {
val hasSensitiveContent = remember(event) { event.isSensitive() ?: false }
val hasSensitiveContent = remember(event) { event.isSensitive() }

SensitivityWarning(hasSensitiveContent, accountViewModel, content)
}

@Composable
fun SensitivityWarning(
hasSensitiveContent: Boolean,
accountViewModel: AccountViewModel,
content: @Composable () -> Unit,
) {
if (hasSensitiveContent) {
SensitivityWarning(accountViewModel, content)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class ZoomableUrlImage(
val blurhash: String? = null,
dim: String? = null,
uri: String? = null,
val contentWarning: String? = null,
) : ZoomableUrlContent(url, description, hash, dim, uri)

@Immutable
Expand All @@ -173,6 +174,7 @@ class ZoomableUrlVideo(
val artworkUri: String? = null,
val authorName: String? = null,
val blurhash: String? = null,
val contentWarning: String? = null,
) : ZoomableUrlContent(url, description, hash, dim, uri)

@Immutable
Expand Down Expand Up @@ -265,20 +267,24 @@ fun ZoomableContentView(

when (content) {
is ZoomableUrlImage ->
UrlImageView(content, mainImageModifier, accountViewModel = accountViewModel)
SensitivityWarning(content.contentWarning != null, accountViewModel) {
UrlImageView(content, mainImageModifier, accountViewModel = accountViewModel)
}
is ZoomableUrlVideo ->
VideoView(
videoUri = content.url,
title = content.description,
artworkUri = content.artworkUri,
authorName = content.authorName,
dimensions = content.dim,
blurhash = content.blurhash,
roundedCorner = roundedCorner,
nostrUriCallback = content.uri,
onDialog = { dialogOpen = true },
accountViewModel = accountViewModel,
)
SensitivityWarning(content.contentWarning != null, accountViewModel) {
VideoView(
videoUri = content.url,
title = content.description,
artworkUri = content.artworkUri,
authorName = content.authorName,
dimensions = content.dim,
blurhash = content.blurhash,
roundedCorner = roundedCorner,
nostrUriCallback = content.uri,
onDialog = { dialogOpen = true },
accountViewModel = accountViewModel,
)
}
is ZoomableLocalImage ->
LocalImageView(content, mainImageModifier, accountViewModel = accountViewModel)
is ZoomableLocalVideo ->
Expand Down Expand Up @@ -387,13 +393,13 @@ private fun UrlImageView(
val myModifier =
remember {
mainImageModifier.widthIn(max = maxWidth).heightIn(max = maxHeight)
/* Is this necessary? It makes images bleed into other pages
/* Is this necessary? It makes images bleed into other pages
.run {
aspectRatio(content.dim)?.let { ratio ->
this.aspectRatio(ratio, false)
} ?: this
}
*/
*/
}

val contentScale =
Expand Down

0 comments on commit 6638def

Please sign in to comment.