From 53a400b4fad76552699d7c17dcbdaf1150bd12c1 Mon Sep 17 00:00:00 2001 From: Sven Jacobs Date: Tue, 12 Sep 2023 09:25:25 +0200 Subject: [PATCH] feat: Add sanitizer for Reddit --- .../app/leon/startup/ContainerInitializer.kt | 2 + .../sanitizer/reddit/RedditSanitizer.kt | 40 +++++++++++++++ core-domain/src/main/res/values/strings.xml | 1 + .../reddit/RedditOutSanitizerTest.kt | 4 ++ .../sanitizer/reddit/RedditSanitizerTest.kt | 51 +++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizer.kt create mode 100644 core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizerTest.kt 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 8b0fd030..4e664fb2 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 @@ -46,6 +46,7 @@ import com.svenjacobs.app.leon.core.domain.sanitizer.newegg.NewEggSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.pearl.PearlSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.reddit.RedditMailSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.reddit.RedditOutSanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.reddit.RedditSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.sessionids.SessionIdsSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.shopee.ShopeeSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.spiegel.SpiegelSanitizer @@ -95,6 +96,7 @@ class ContainerInitializer : DistinctInitializer { PearlSanitizer(), RedditMailSanitizer(), RedditOutSanitizer(), + RedditSanitizer(), SessionIdsSanitizer(), ShopeeSanitizer(), SpiegelSanitizer(), diff --git a/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizer.kt b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizer.kt new file mode 100644 index 00000000..9d0fbc45 --- /dev/null +++ b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizer.kt @@ -0,0 +1,40 @@ +/* + * 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.reddit + +import android.content.Context +import com.svenjacobs.app.leon.core.common.domain.matchesDomain +import com.svenjacobs.app.leon.core.common.regex.RegexFactory +import com.svenjacobs.app.leon.core.domain.R +import com.svenjacobs.app.leon.core.domain.sanitizer.RegexSanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.Sanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerId + +class RedditSanitizer : RegexSanitizer( + RegexFactory.AllParameters, +) { + + override val id = SanitizerId("reddit") + + override fun getMetadata(context: Context) = Sanitizer.Metadata( + name = context.getString(R.string.sanitizer_reddit), + ) + + override fun matchesDomain(input: String) = input.matchesDomain("reddit.com") +} diff --git a/core-domain/src/main/res/values/strings.xml b/core-domain/src/main/res/values/strings.xml index 3d846b46..bf707583 100644 --- a/core-domain/src/main/res/values/strings.xml +++ b/core-domain/src/main/res/values/strings.xml @@ -41,6 +41,7 @@ Netflix NewEgg Pearl + Reddit Reddit (click.redditmail.com) Reddit (out.reddit.com) Session IDs diff --git a/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditOutSanitizerTest.kt b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditOutSanitizerTest.kt index 15c25dcf..e1f329f0 100644 --- a/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditOutSanitizerTest.kt +++ b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditOutSanitizerTest.kt @@ -43,6 +43,10 @@ class RedditOutSanitizerTest : WordSpec( "match for out.reddit.com" { sanitizer.matchesDomain("https://out.reddit.com") shouldBe true } + + "not match for reddit.com" { + sanitizer.matchesDomain("https://reddit.com") shouldBe false + } } }, ) diff --git a/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizerTest.kt b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizerTest.kt new file mode 100644 index 00000000..b8c1dec2 --- /dev/null +++ b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/reddit/RedditSanitizerTest.kt @@ -0,0 +1,51 @@ +/* + * 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.reddit + +import io.kotest.core.spec.style.WordSpec +import io.kotest.matchers.shouldBe + +class RedditSanitizerTest : WordSpec( + { + val sanitizer = RedditSanitizer() + + "invoke" should { + + "clean reddit.com URLs" { + sanitizer( + "https://www.reddit.com/r/fossdroid/comments/1659ic4/material_files_is_" + + "still_maintained/?share_id=Toc_TMpn88yOUd7Z-y0xv&utm_content=1&utm_mediu" + + "m=android_app&utm_name=androidcss&utm_source=share&utm_term=1", + ) shouldBe "https://www.reddit.com/r/fossdroid/comments/1659ic4/material_files_is" + + "_still_maintained/" + } + } + + "matchesDomain" should { + + "match for reddit.com" { + sanitizer.matchesDomain("https://reddit.com") shouldBe true + } + + "not match for out.reddit.com" { + sanitizer.matchesDomain("https://out.reddit.com") shouldBe false + } + } + }, +)