diff --git a/core/popups/feed.ts b/core/popups/feed.ts index a25d433c..7415c497 100644 --- a/core/popups/feed.ts +++ b/core/popups/feed.ts @@ -1,3 +1,4 @@ +import type { FilterStore } from '@logux/client' import { atom } from 'nanostores' import { getCategories } from '../category.ts' @@ -31,9 +32,30 @@ async function loadFeedFromURL( } } +function swapHttpProtocol(url: string): string { + let u = new URL(url) + u.protocol = u.protocol === 'https:' ? 'http:' : 'https:' + return u.toString() +} + +async function getFeedsFilter(url: string): Promise> { + let exactFilter = getFeeds({ url }) + let feeds = await waitSyncLoading(exactFilter) + + if (feeds.get().list.length === 0) { + let fallbackFilter = getFeeds({ url: swapHttpProtocol(url) }) + feeds = await waitSyncLoading(fallbackFilter) + if (feeds.get().list.length > 0) { + return fallbackFilter + } + } + + return exactFilter +} + export const feed = definePopup('feed', async url => { let task = createDownloadTask({ cache: 'read' }) - let feedsFilter = getFeeds({ url }) + let feedsFilter = await getFeedsFilter(url) let error: string | undefined let [responseOrError, categoriesFilter, feeds] = await Promise.all([ loadFeedFromURL(task, url), diff --git a/core/test/pages/add.test.ts b/core/test/pages/add.test.ts index ccd9cd66..72620b85 100644 --- a/core/test/pages/add.test.ts +++ b/core/test/pages/add.test.ts @@ -620,8 +620,6 @@ test('deduplicates feed links differing only by protocol', async () => { route: 'add' }) - keepMount(page.candidates) - expectRequest('http://example.com').andRespond( 200, ` @@ -642,6 +640,9 @@ test('deduplicates feed links differing only by protocol', async () => { page.params.url.set('http://example.com') await waitLoading(page.searching) + let popup = getPopup('feed') + await waitLoading(popup.loading) + equal(page.candidates.get().length, 1) equalWithText(page.candidates.get(), [ { diff --git a/core/test/popups/feed.test.ts b/core/test/popups/feed.test.ts index 725870e3..3b2c156e 100644 --- a/core/test/popups/feed.test.ts +++ b/core/test/popups/feed.test.ts @@ -188,3 +188,34 @@ test('has helpers to generate popup ID', () => { getPopupId('feed', 'http://example.com/') ) }) + +test('ignores url protocol while checking for existing feeds', async () => { + keepMount(openedPopups) + expectRequest('http://a.com/atom').andRespond( + 200, + 'Atom' + + '22023-07-01T00:00:00Z' + + '12023-06-01T00:00:00Z' + + '', + 'text/xml' + ) + let popup = openTestPopup('feed', 'http://a.com/atom') + await waitLoading(popup.loading) + + let addedId = await checkLoadedPopup(popup).add() + let feedId = checkLoadedPopup(popup).feed.get()!.id + equal(addedId, feedId) + equal((await loadValue(getFeed(feedId)))!.url, 'http://a.com/atom') + + expectRequest('https://a.com/atom').andRespond( + 200, + 'Atom' + + '22023-07-01T00:00:00Z' + + '12023-06-01T00:00:00Z' + + '', + 'text/xml' + ) + let popup2 = openTestPopup('feed', 'https://a.com/atom') + await waitLoading(popup2.loading) + equal(checkLoadedPopup(popup2).feed.get()!.url, 'http://a.com/atom') +})