Skip to content

Commit

Permalink
Add check to prevent transforming URL while refreshing feeds
Browse files Browse the repository at this point in the history
This is to prevent creating duplicate feed entries while refreshing after updating the app to latest version with transform URL changes
  • Loading branch information
msasikanth committed Sep 15, 2023
1 parent 99dc53f commit 6c25745
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 6c25745

Please sign in to comment.