diff --git a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedFetcher.kt b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedFetcher.kt index 6483d8c8b..b737aabba 100644 --- a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedFetcher.kt +++ b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedFetcher.kt @@ -37,22 +37,28 @@ class FeedFetcher(private val httpClient: HttpClient, private val feedParser: Fe private var redirectCount = 0 - suspend fun fetch(url: String): FeedFetchResult { + suspend fun fetch(url: String, transformUrl: Boolean = true): FeedFetchResult { return try { - // Currently Ktor Url parses relative URLs, - // if it fails to properly parse the given URL, it - // default to localhost. - // - // This will cause the network call to fail, - // so we are setting the host manually - // https://youtrack.jetbrains.com/issue/KTOR-360 + // We are mainly doing this check to avoid creating duplicates while refreshing feeds + // after the app update val transformedUrl = - URLBuilder() - .apply { - protocol = URLProtocol.HTTPS - host = url.replace(Regex("^https?://"), "").replace(Regex("^www\\."), "") - } - .build() + if (transformUrl) { + // Currently Ktor Url parses relative URLs, + // if it fails to properly parse the given URL, it + // default to localhost. + // + // This will cause the network call to fail, + // so we are setting the host manually + // https://youtrack.jetbrains.com/issue/KTOR-360 + URLBuilder() + .apply { + protocol = URLProtocol.HTTPS + host = url.replace(Regex("^https?://"), "").replace(Regex("^www\\."), "") + } + .build() + } else { + URLBuilder(url).apply { protocol = URLProtocol.HTTPS }.build() + } val response = httpClient.get(transformedUrl.toString()) diff --git a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/repository/RssRepository.kt b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/repository/RssRepository.kt index de7120fbb..cd4502115 100644 --- a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/repository/RssRepository.kt +++ b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/repository/RssRepository.kt @@ -56,9 +56,9 @@ class RssRepository( private val ioDispatcher = dispatchersProvider.io - suspend fun addFeed(feedLink: String): FeedAddResult { + suspend fun addFeed(feedLink: String, transformUrl: Boolean = true): FeedAddResult { return withContext(ioDispatcher) { - when (val feedFetchResult = feedFetcher.fetch(feedLink)) { + when (val feedFetchResult = feedFetcher.fetch(feedLink, transformUrl)) { is FeedFetchResult.Success -> { return@withContext try { val feedPayload = feedFetchResult.feedPayload @@ -105,7 +105,7 @@ class RssRepository( val results = withContext(ioDispatcher) { val feeds = feedQueries.feeds().executeAsList() - feeds.map { feed -> launch { addFeed(feed.link) } } + feeds.map { feed -> launch { addFeed(feed.link, false) } } } results.joinAll() }