forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed.ts
More file actions
138 lines (123 loc) · 3.52 KB
/
feed.ts
File metadata and controls
138 lines (123 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
changeSyncMapById,
createFilter,
createSyncMap,
deleteSyncMapById,
type Filter,
type FilterStore,
loadValue,
type SyncMapStore,
syncMapTemplate
} from '@logux/client'
import { nanoid } from 'nanoid'
import { atom, onMount } from 'nanostores'
import { getClient } from './client.ts'
import { createDownloadTask, type TextResponse } from './lib/download.ts'
import type { OptionalId } from './lib/stores.ts'
import { type FeedLoader, type LoaderName, loaders } from './loader/index.ts'
import { deletePost, getPosts, recalcPostsReading } from './post.ts'
import type { PostsList } from './posts-list.ts'
import type { UsefulReaderName } from './readers/common.ts'
export type FeedValue = {
categoryId: string
fastReader?: UsefulReaderName
id: string
lastOriginId?: string
lastPublishedAt?: number
loader: LoaderName
reading: 'fast' | 'slow'
slowReader?: UsefulReaderName
title: string
url: string
}
export const Feed = syncMapTemplate<FeedValue>('feeds', {
offline: true,
remote: false
})
export function getFeed(feedId: string): SyncMapStore<FeedValue> {
return Feed(feedId, getClient())
}
export function getFeeds(
filter: Filter<FeedValue> = {}
): FilterStore<FeedValue> {
return createFilter(getClient(), Feed, filter)
}
export async function addFeed(fields: OptionalId<FeedValue>): Promise<string> {
let id = fields.id ?? nanoid()
await createSyncMap(getClient(), Feed, { id, ...fields })
return id
}
export async function deleteFeed(feedId: string): Promise<void> {
let feed = Feed.cache[feedId]
if (feed) feed.deleted = true
let posts = await loadValue(getPosts({ feedId }))
await Promise.all(posts.list.map(post => deletePost(post.id)))
return deleteSyncMapById(getClient(), Feed, feedId)
}
export async function changeFeed(
feedId: string,
changes: Partial<FeedValue>
): Promise<void> {
await changeSyncMapById(getClient(), Feed, feedId, changes)
await recalcPostsReading(feedId)
}
/**
* Subscribes to a feed which is currently being previewed.
*/
export async function addCandidate(
candidate: FeedLoader,
fields: Partial<FeedValue> = {},
task = createDownloadTask(),
response?: TextResponse
): Promise<string> {
let posts = candidate.loader.getPosts(task, candidate.url, response)
if (posts.get().isLoading) await posts.loading
let lastPost = posts.get().list[0]
return await addFeed({
categoryId: 'general',
lastOriginId: lastPost?.originId,
lastPublishedAt: lastPost?.publishedAt ?? Math.round(Date.now() / 1000),
loader: candidate.name,
reading: 'slow',
title: candidate.title,
url: candidate.url,
...fields
})
}
export function getFeedLatestPosts(
feed: FeedValue,
task = createDownloadTask()
): PostsList {
return loaders[feed.loader].getPosts(task, feed.url)
}
let testFeedId = 0
export function testFeed(feed: Partial<FeedValue> = {}): FeedValue {
testFeedId += 1
return {
categoryId: 'general',
id: `feed-${testFeedId}`,
lastOriginId: undefined,
lastPublishedAt: undefined,
loader: 'rss',
reading: 'fast',
title: `Test ${testFeedId}`,
url: `http://example.com/${testFeedId}`,
...feed
}
}
export const BROKEN_FEED: FeedValue = {
categoryId: 'general',
id: 'missed',
lastOriginId: undefined,
lastPublishedAt: undefined,
loader: 'atom',
reading: 'slow',
title: 'Missed',
url: ''
}
export const needWelcome = atom<boolean | undefined>()
onMount(needWelcome, () => {
return getFeeds().subscribe(feeds => {
needWelcome.set(feeds.isLoading ? undefined : feeds.isEmpty)
})
})