forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.ts
141 lines (121 loc) · 3.11 KB
/
post.ts
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
import {
changeSyncMapById,
createFilter,
createSyncMap,
deleteSyncMapById,
type Filter,
type FilterStore,
loadValue,
type SyncMapStore,
syncMapTemplate
} from '@logux/client'
import { nanoid } from 'nanoid'
import { getClient } from './client.ts'
import { getFeed } from './feed.ts'
import { loadFilters, prepareFilters } from './filter.ts'
import type { OptionalId } from './lib/stores.ts'
export type OriginPost = {
full?: string
intro?: string
media: string[]
originId: string
publishedAt?: number
title?: 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 getPosts(
filter: Filter<PostValue> = {}
): FilterStore<PostValue> {
return createFilter(getClient(), Post, filter)
}
export async function loadPosts(
filter: Filter<PostValue> = {}
): Promise<PostValue[]> {
let value = await loadValue(getPosts(filter))
return value.list
}
export function getPost(postId: string): SyncMapStore<PostValue> {
return Post(postId, getClient())
}
export async function loadPost(postId: string): Promise<PostValue | undefined> {
return loadValue(Post(postId, getClient()))
}
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 {
if (post.intro) {
return post.intro
} else if (post.full && post.full.length <= 2000) {
return post.full
} else {
return ''
}
}
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 }),
loadPosts({ feedId })
])
let checker = prepareFilters(filters)
for (let post of posts) {
let reading = checker(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}`,
media: [],
originId: `test-${testPostId}`,
publishedAt: 1000,
reading: 'fast',
url: `http://example.com/${testPostId}`,
...feed
}
}