diff --git a/materialious/src/lib/components/layout/ItemsList.svelte b/materialious/src/lib/components/layout/ItemsList.svelte index 5f7c6cbf..a309d5a6 100644 --- a/materialious/src/lib/components/layout/ItemsList.svelte +++ b/materialious/src/lib/components/layout/ItemsList.svelte @@ -5,7 +5,8 @@ import ContentColumn from '$lib/components/layout/ContentColumn.svelte'; import { onMount } from 'svelte'; import Thumbnail from '$lib/components/thumbnail/VideoThumbnail.svelte'; - import { extractUniqueId, timeout, type feedItems } from '$lib/misc'; + import { extractUniqueId, type FeedItems } from '$lib/feed'; + import { timeout } from '$lib/misc'; import ChannelThumbnail from '$lib/components/thumbnail/ChannelThumbnail.svelte'; import PlaylistThumbnail from '$lib/components/thumbnail/PlaylistThumbnail.svelte'; import HashtagThumbnail from '$lib/components/thumbnail/HashtagThumbnail.svelte'; @@ -15,7 +16,7 @@ import { Capacitor } from '@capacitor/core'; interface Props { - items?: feedItems; + items?: FeedItems; playlistId?: string; playlistAuthor?: string; classes?: string; diff --git a/materialious/src/lib/feed.ts b/materialious/src/lib/feed.ts new file mode 100644 index 00000000..b7b3112e --- /dev/null +++ b/materialious/src/lib/feed.ts @@ -0,0 +1,51 @@ +import type { + Channel, + HashTag, + Playlist, + PlaylistPage, + PlaylistPageVideo, + Video, + VideoBase, + VideoWatchHistory +} from './api/model'; + +export type FeedItem = + | VideoBase + | Video + | PlaylistPageVideo + | Channel + | Video + | Playlist + | HashTag + | PlaylistPage + | VideoWatchHistory; +export type FeedItems = FeedItem[]; + +export function extractUniqueId(item: FeedItem): string { + if ('videoId' in item) { + return item.videoId; + } else if ('playlistId' in item) { + return item.playlistId; + } else if ('authorId' in item) { + return item.authorId; + } else { + return item.title; + } +} + +export function excludeDuplicateFeeds(currentItems: FeedItems, newItems: FeedItems): FeedItems { + const existingIds: string[] = []; + + currentItems.forEach((item) => { + existingIds.push(extractUniqueId(item)); + }); + + const nonDuplicatedNewItems: FeedItems = []; + newItems.forEach((item) => { + if (!existingIds.includes(extractUniqueId(item))) { + nonDuplicatedNewItems.push(item); + } + }); + + return [...nonDuplicatedNewItems, ...currentItems]; +} diff --git a/materialious/src/lib/misc.ts b/materialious/src/lib/misc.ts index e5aa9b91..91a66682 100644 --- a/materialious/src/lib/misc.ts +++ b/materialious/src/lib/misc.ts @@ -14,16 +14,6 @@ import { rawMasterKeyStore, searchCacheStore } from './store'; -import type { - Channel, - HashTag, - Playlist, - PlaylistPage, - PlaylistPageVideo, - Video, - VideoBase, - VideoWatchHistory -} from './api/model'; import { Capacitor } from '@capacitor/core'; import { Share } from '@capacitor/share'; import { Clipboard } from '@capacitor/clipboard'; @@ -89,47 +79,6 @@ export function ensureNoTrailingSlash(url: any): string { return url.endsWith('/') ? url.slice(0, -1) : url; } -export type feedItem = - | VideoBase - | Video - | PlaylistPageVideo - | Channel - | Video - | Playlist - | HashTag - | PlaylistPage - | VideoWatchHistory; -export type feedItems = feedItem[]; - -export function extractUniqueId(item: feedItem): string { - if ('videoId' in item) { - return item.videoId; - } else if ('playlistId' in item) { - return item.playlistId; - } else if ('authorId' in item) { - return item.authorId; - } else { - return item.title; - } -} - -export function excludeDuplicateFeeds(currentItems: feedItems, newItems: feedItems): feedItems { - const existingIds: string[] = []; - - currentItems.forEach((item) => { - existingIds.push(extractUniqueId(item)); - }); - - const nonDuplicatedNewItems: feedItems = []; - newItems.forEach((item) => { - if (!existingIds.includes(extractUniqueId(item))) { - nonDuplicatedNewItems.push(item); - } - }); - - return [...nonDuplicatedNewItems, ...currentItems]; -} - export function expandSummery(id: string) { const element = document.getElementById(id); if (element) { diff --git a/materialious/src/routes/(app)/channel/[slug]/+page.ts b/materialious/src/routes/(app)/channel/[slug]/+page.ts index 8b09d6d7..9fe2e07b 100644 --- a/materialious/src/routes/(app)/channel/[slug]/+page.ts +++ b/materialious/src/routes/(app)/channel/[slug]/+page.ts @@ -1,6 +1,6 @@ import { getChannel, getChannelContent } from '$lib/api/index'; import type { ChannelContentVideos, Video } from '$lib/api/model'; -import { excludeDuplicateFeeds } from '$lib/misc'; +import { excludeDuplicateFeeds } from '$lib/feed'; import { channelCacheStore } from '$lib/store'; import { error } from '@sveltejs/kit'; import { get } from 'svelte/store'; diff --git a/materialious/src/routes/(app)/search/[slug]/+page.ts b/materialious/src/routes/(app)/search/[slug]/+page.ts index c7565a4e..9171df3c 100644 --- a/materialious/src/routes/(app)/search/[slug]/+page.ts +++ b/materialious/src/routes/(app)/search/[slug]/+page.ts @@ -1,6 +1,6 @@ import { getSearch } from '$lib/api/index'; import type { Channel, HashTag, Playlist, Video } from '$lib/api/model'; -import { excludeDuplicateFeeds } from '$lib/misc'; +import { excludeDuplicateFeeds } from '$lib/feed'; import { searchCacheStore } from '$lib/store'; import { error } from '@sveltejs/kit'; import { get } from 'svelte/store'; diff --git a/materialious/src/routes/(app)/subscriptions/+page.ts b/materialious/src/routes/(app)/subscriptions/+page.ts index c311c78c..62273536 100644 --- a/materialious/src/routes/(app)/subscriptions/+page.ts +++ b/materialious/src/routes/(app)/subscriptions/+page.ts @@ -1,7 +1,8 @@ import { getFeed } from '$lib/api/index'; import type { PlaylistPageVideo, Video, VideoBase } from '$lib/api/model'; import { localDb } from '$lib/dexie'; -import { authProtected, excludeDuplicateFeeds } from '$lib/misc'; +import { excludeDuplicateFeeds } from '$lib/feed'; +import { authProtected } from '$lib/misc'; import { feedCacheStore, feedLoadingStore } from '$lib/store'; import { error } from '@sveltejs/kit'; import { get } from 'svelte/store';