forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.ts
More file actions
197 lines (173 loc) · 4.85 KB
/
post.ts
File metadata and controls
197 lines (173 loc) · 4.85 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { defineSyncMapActions } from '@logux/actions'
import {
changeSyncMapById,
createFilter,
createSyncMap,
deleteSyncMapById,
type Filter,
type FilterStore,
loadValue,
type SyncMapStore,
syncMapTemplate
} from '@logux/client'
import { formatter } from '@nanostores/i18n'
import { nanoid } from 'nanoid'
import { atom, onMount } from 'nanostores'
import { getClient } from './client.ts'
import { getEnvironment } from './environment.ts'
import { getFeed } from './feed.ts'
import { loadFilters } from './filter.ts'
import { $locale } from './i18n.ts'
import { sanitizeHtml, stripHTML, truncateHTML } from './lib/html.ts'
import type { OptionalId } from './lib/stores.ts'
import { truncateText } from './lib/text.ts'
export type OriginPost = {
full?: string
intro?: string
media?: string
originId: string
publishedAt?: number
read?: boolean
title?: string
url?: string
}
export type PostMedia = {
fromText?: boolean
type: string
url: string
}
export type PostValue = {
feedId: string
id: string
publishedAt: number
reading: 'fast' | 'slow'
} & Omit<OriginPost, 'publishedAt'>
export const Post = syncMapTemplate<PostValue>('posts', {
offline: true,
remote: false
})
export async function addPost(fields: OptionalId<PostValue>): Promise<string> {
let id = fields.id ?? nanoid()
await createSyncMap(getClient(), Post, { id, ...fields })
return id
}
export function getPost(id: string): SyncMapStore<PostValue> {
return Post(id, getClient())
}
export function getPosts(
filter: Filter<PostValue> = {}
): FilterStore<PostValue> {
return createFilter(getClient(), Post, filter)
}
export function deletePost(postId: string): Promise<void> {
return deleteSyncMapById(getClient(), Post, postId)
}
export async function changePost(
postId: string,
changes: Partial<PostValue>
): Promise<void> {
return changeSyncMapById(getClient(), Post, postId, changes)
}
export function processOriginPost(
origin: OriginPost,
feedId: string,
reading: PostValue['reading']
): PostValue {
return {
...origin,
feedId,
id: nanoid(),
publishedAt: origin.publishedAt ?? Date.now(),
reading
}
}
export function getPostContent(post: OriginPost): string {
return post.full ?? post.intro ?? ''
}
export function getPostIntro(post: OriginPost): [string, boolean] {
if (post.intro) {
let more = !!post.full && post.full !== post.intro
return [post.intro, more]
} else if (post.full) {
let sanitized = sanitizeHtml(post.full)
let truncated = truncateHTML(sanitized, 300, 500)
return [truncated, truncated !== sanitized]
} else {
return ['', false]
}
}
export function getPostTitle(post: OriginPost): string {
if (post.title) {
return stripHTML(post.title)
} else if (post.intro) {
return truncateText(stripHTML(post.intro), 40, 80)
} else if (post.full) {
return truncateText(stripHTML(post.full), 40, 80)
} else if (post.publishedAt) {
return formatter($locale)
.get()
.time(new Date(post.publishedAt * 1000))
} else {
return stripHTML(post.originId)
}
}
export async function recalcPostsReading(feedId: string): Promise<void> {
let feed = await loadValue(getFeed(feedId))
if (!feed) return
let [filters, posts] = await Promise.all([
loadFilters({ feedId }),
loadValue(getPosts({ feedId }))
])
for (let post of posts.list) {
let reading = filters(post) ?? feed.reading
if (reading !== 'delete') {
await changePost(post.id, { reading })
}
}
}
let testPostId = 0
export function testPost(feed: Partial<PostValue> = {}): PostValue {
testPostId += 1
return {
feedId: 'feed-1',
id: `post-${testPostId}`,
intro: `Post ${testPostId}`,
originId: `test-${testPostId}`,
publishedAt: 1000,
reading: 'fast',
url: `http://example.com/${testPostId}`,
...feed
}
}
export const fastPostsCount = atom<number | undefined>(undefined)
onMount(fastPostsCount, () => {
return getPosts({ reading: 'fast' }).subscribe(posts => {
fastPostsCount.set(posts.isLoading ? undefined : posts.list.length)
})
})
export const slowPostsCount = atom<number | undefined>(undefined)
onMount(slowPostsCount, () => {
return getPosts({ reading: 'slow' }).subscribe(posts => {
slowPostsCount.set(posts.isLoading ? undefined : posts.list.length)
})
})
export const postsChangedAction = defineSyncMapActions<PostValue>('posts')[4]
export function stringifyMedia(media: PostMedia[]): string | undefined {
if (media.length === 0) return undefined
let used = new Set<string>()
let unique = media.filter(i => {
let already = used.has(i.url)
if (!already) used.add(i.url)
return !already
})
return JSON.stringify(unique)
}
export function parseMedia(value: string | undefined): PostMedia[] {
if (!value) return []
try {
return JSON.parse(value) as PostMedia[]
} catch (e) {
getEnvironment().warn(e)
return []
}
}