X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FLinkedElementsFilter.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FLinkedElementsFilter.kt;h=c9414ea8f7b261e8af6a54b0630fe2e9616b49f4;hp=ca94423a9929683f55d194afa98472219d0e7406;hb=885f3980c49778280a748c630788212a68ac4ed6;hpb=ab14aa498b45ecd2222b03d6c3607461a0ceb3f4 diff --git a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementsFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementsFilter.kt index ca94423..c9414ea 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementsFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementsFilter.kt @@ -2,6 +2,13 @@ package net.pterodactylus.sone.template import net.pterodactylus.sone.core.ElementLoader import net.pterodactylus.sone.core.LinkedElement +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.ALWAYS +import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.FOLLOWED +import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.MANUALLY_TRUSTED +import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.NEVER +import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.TRUSTED +import net.pterodactylus.sone.freenet.wot.OwnIdentity import net.pterodactylus.sone.text.FreenetLinkPart import net.pterodactylus.sone.text.Part import net.pterodactylus.util.template.Filter @@ -15,10 +22,36 @@ class LinkedElementsFilter(private val elementLoader: ElementLoader) : Filter { @Suppress("UNCHECKED_CAST") override fun format(templateContext: TemplateContext?, data: Any?, parameters: MutableMap?) = - (data as? Iterable) - ?.filterIsInstance() - ?.map { elementLoader.loadElement(it.link) } - ?.filter { !it.failed } - ?: listOf() + if (showLinkedImages(templateContext?.get("currentSone") as Sone?, parameters?.get("sone") as Sone?)) { + (data as? Iterable) + ?.filterIsInstance() + ?.map { elementLoader.loadElement(it.link) } + ?.filter { !it.failed } + ?: listOf() + } else { + listOf() + } + + private fun showLinkedImages(currentSone: Sone?, sone: Sone?): Boolean { + return (currentSone != null) && (sone != null) && ((currentSone == sone) || currentSoneAllowsImagesFromSone(currentSone, sone)) + } + + private fun currentSoneAllowsImagesFromSone(currentSone: Sone, externalSone: Sone) = + when (currentSone.options.loadLinkedImages) { + NEVER -> false + MANUALLY_TRUSTED -> externalSone.isLocal || currentSone.explicitelyTrusts(externalSone) + FOLLOWED -> externalSone.isLocal || currentSone.hasFriend(externalSone.id) + TRUSTED -> externalSone.isLocal || currentSone.implicitelyTrusts(externalSone) + ALWAYS -> true + } + + private fun Sone.implicitelyTrusts(other: Sone): Boolean { + val explicitTrust = other.identity.getTrust(this.identity as OwnIdentity)?.explicit + val implicitTrust = other.identity.getTrust(this.identity as OwnIdentity)?.implicit + return ((explicitTrust != null) && (explicitTrust > 0)) || ((explicitTrust == null) && (implicitTrust != null) && (implicitTrust > 0)) + } + + private fun Sone.explicitelyTrusts(other: Sone) = + other.identity.getTrust(this.identity as OwnIdentity)?.explicit ?: -1 > 0 }