From 42f2ca2513fdc34ab8ccfc4805ab63aaf50259d3 Mon Sep 17 00:00:00 2001 From: xyrolaith Date: Wed, 18 Sep 2024 07:16:01 +0200 Subject: [PATCH] feat: Add Jodel support (#462) (#463) * feat: Add Jodel support (#462) * chore: Use alphabetic imports ordering * chore: use KotlinX Serialization --------- Co-authored-by: xyrolaith <18216850+xyrolaith@users.noreply.github.com> Co-authored-by: Sven Jacobs --- .idea/dictionaries/sven.xml | 3 +- .../app/leon/startup/ContainerInitializer.kt | 2 + core-domain/build.gradle.kts | 1 + .../domain/sanitizer/jodel/JodelSanitizer.kt | 34 +++++++++++++ core-domain/src/main/res/values/strings.xml | 1 + .../sanitizer/jodel/JodelSanitizerTest.kt | 50 +++++++++++++++++++ gradle/libs.versions.toml | 3 +- 7 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt create mode 100644 core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt diff --git a/.idea/dictionaries/sven.xml b/.idea/dictionaries/sven.xml index 59599970..841ed08c 100644 --- a/.idea/dictionaries/sven.xml +++ b/.idea/dictionaries/sven.xml @@ -6,6 +6,7 @@ constraintlayout gitversioner jakewharton + jodel kotest léon mikepenz @@ -21,4 +22,4 @@ welsch - + \ No newline at end of file diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt index 6b81a2d4..8252c811 100644 --- a/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt @@ -41,6 +41,7 @@ import com.svenjacobs.app.leon.core.domain.sanitizer.google.GoogleSearchSanitize import com.svenjacobs.app.leon.core.domain.sanitizer.heise.HeiseSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.instagram.InstagramSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.jdoqocy.JdoqocySanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.jodel.JodelSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.lazada.LazadaSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.linksynergy.LinkSynergySanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.netflix.NetflixSanitizer @@ -95,6 +96,7 @@ class ContainerInitializer : DistinctInitializer { HeiseSanitizer(), InstagramSanitizer(), JdoqocySanitizer(), + JodelSanitizer(), LazadaSanitizer(), LinkSynergySanitizer(), NetflixSanitizer(), diff --git a/core-domain/build.gradle.kts b/core-domain/build.gradle.kts index c9da58a9..57757f47 100644 --- a/core-domain/build.gradle.kts +++ b/core-domain/build.gradle.kts @@ -26,4 +26,5 @@ android { dependencies { api(libs.kotlinx.collections.immutable) + api(libs.kotlinx.serialization.json) } diff --git a/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt new file mode 100644 index 00000000..bf20d4e1 --- /dev/null +++ b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizer.kt @@ -0,0 +1,34 @@ +package com.svenjacobs.app.leon.core.domain.sanitizer.jodel + +import android.content.Context +import com.svenjacobs.app.leon.core.common.url.decodeUrl +import com.svenjacobs.app.leon.core.domain.R +import com.svenjacobs.app.leon.core.domain.sanitizer.Sanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerId +import kotlin.io.encoding.Base64 +import kotlin.io.encoding.ExperimentalEncodingApi +import kotlinx.serialization.json.Json + +class JodelSanitizer : Sanitizer { + override val id = SanitizerId("jodel") + + override fun getMetadata(context: Context) = Sanitizer.Metadata( + name = context.getString(R.string.sanitizer_jodel_name), + ) + + @OptIn(ExperimentalEncodingApi::class) + override fun invoke(input: String): String { + val encoded = URL_REGEX.find(input)?.groupValues?.getOrNull(1) ?: return input + val base64Data = decodeUrl(encoded) + val jsonString = Base64.Default.decode(base64Data) + val jsonMap = Json.decodeFromString>(jsonString.decodeToString()) + + return jsonMap["\$android_url"] ?: input + } + + override fun matchesDomain(input: String) = URL_REGEX.containsMatchIn(input) + + private companion object { + private val URL_REGEX = Regex("^https?://shared\\.jodel\\.com/a/key_live_.*&data=([^?&]*)") + } +} diff --git a/core-domain/src/main/res/values/strings.xml b/core-domain/src/main/res/values/strings.xml index 46795690..8f2dd20c 100644 --- a/core-domain/src/main/res/values/strings.xml +++ b/core-domain/src/main/res/values/strings.xml @@ -38,6 +38,7 @@ heise online Instagram Jdoqocy + Jodel Lazada LinkSynergy Netflix diff --git a/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt new file mode 100644 index 00000000..f99df7cd --- /dev/null +++ b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/jodel/JodelSanitizerTest.kt @@ -0,0 +1,50 @@ +/* + * Léon - The URL Cleaner + * Copyright (C) 2023 Sven Jacobs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.svenjacobs.app.leon.core.domain.sanitizer.jodel + +import io.kotest.core.spec.style.WordSpec +import io.kotest.matchers.shouldBe + +class JodelSanitizerTest : + WordSpec( + { + + "invoke" should { + + "extract Jodel sharing URL" { + val sanitizer = JodelSanitizer() + val result = sanitizer( + "http://shared.jodel.com/a/key_live_abZZZZgPxyz82xxxxAAAAdefghi4YYY1" + + "?%24identity_id=123456789012345678&feature=shared_post&campaign=image" + + "_DE_FrontPage&type=0&duration=0&source=android&data=eyIkY2Fub25pY2FsX" + + "2lkZW50aWZpZXIiOiJzYWpcL2JhZGMwZmZlZWJhZGMwZmZlZTAxMjM0NSIsIiRwdWJsaW" + + "NseV9pbmRleGFibGUiOiJ0cnVlIiwicG9zdElkIjoiYmFkYzBmZmVlYmFkYzBmZmVlMDE" + + "yMzQ1IiwiJGRlc2t0b3BfdXJsIjoiaHR0cHM6XC9cL3NoYXJlLmpvZGVsLmNvbVwvcG9z" + + "dD9wb3N0SWQ9YmFkYzBmZmVlYmFkYzBmZmVlMDEyMzQ1IiwicmVmZXJyZXJfaWQiOiJhY" + + "mNkZWYwMTIzNDU2Nzg5ZGVhZGMwZGUiLCIkYW5kcm9pZF91cmwiOiJodHRwczpcL1wvc2" + + "hhcmUuam9kZWwuY29tXC9wb3N0P3Bvc3RJZD1iYWRjMGZmZWViYWRjMGZmZWUwMTIzNDU" + + "iLCIkaW9zX3VybCI6Imh0dHBzOlwvXC9zaGFyZS5qb2RlbC5jb21cL3Bvc3Q%2FcG9zdE" + + "lkPWJhZGMwZmZlZWJhZGMwZmZlZTAxMjM0NSJ9?channel=copy", + ) + + result shouldBe "https://share.jodel.com/post?postId=badc0ffeebadc0ffee012345" + } + } + }, + ) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3f2bd864..1d5e8be5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -28,16 +28,17 @@ androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle- androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidx-lifecycle" } androidx-navigation-compose = "androidx.navigation:navigation-compose:2.8.0" androidx-startup-runtime = "androidx.startup:startup-runtime:1.1.1" +compose-gradle-plugin = { module = "org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin", version.ref = "kotlin" } facebook-stetho = "com.facebook.stetho:stetho:1.6.0" jakewharton-timber = "com.jakewharton.timber:timber:5.0.1" kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" } kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" } kotlin-bom = { module = "org.jetbrains.kotlin:kotlin-bom", version.ref = "kotlin" } kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } -compose-gradle-plugin = { module = "org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin", version.ref = "kotlin" } kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8" } kotlinx-collections-immutable = "org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.8" kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" } +kotlinx-serialization-json = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.2" mikepenz-aboutlibraries-compose-m3 = { module = "com.mikepenz:aboutlibraries-compose-m3", version.ref = "mikepenz-aboutlibraries" } mikepenz-aboutlibraries-gradle-plugin = { module = "com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin", version.ref = "mikepenz-aboutlibraries" } mockk = "io.mockk:mockk:1.13.12"