Skip to content

Commit 9eea6fb

Browse files
authored
Merge pull request #38 from omnivore-app/feature/config-enum
Make filter an enum option and add syncAt to settings
2 parents 9f69830 + e43063a commit 9eea6fb

File tree

4 files changed

+98
-52
lines changed

4 files changed

+98
-52
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
},
2525
"dependencies": {
2626
"@logseq/libs": "^0.0.6",
27-
"logseq-dateutils": "^0.0.22"
27+
"logseq-dateutils": "^0.0.22",
28+
"luxon": "^3.0.1"
2829
},
2930
"devDependencies": {
3031
"@semantic-release/changelog": "^6.0.1",
3132
"@semantic-release/exec": "^6.0.3",
3233
"@semantic-release/git": "^10.0.1",
3334
"@tsconfig/node16": "^1.0.1",
35+
"@types/luxon": "^2.3.2",
3436
"@typescript-eslint/eslint-plugin": "^5.9.0",
3537
"@typescript-eslint/parser": "^5.9.0",
3638
"eslint": "^8.6.0",

src/index.ts

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ import {
88
import { getDateForPage } from 'logseq-dateutils'
99
import icon from '../public/icon.png'
1010
import { Article, loadArticles } from './util'
11+
import { DateTime } from 'luxon'
12+
13+
enum Filter {
14+
ALL = 'import all my articles',
15+
HIGHLIGHTS = 'import just highlights',
16+
ADVANCED = 'advanced',
17+
}
18+
19+
interface Settings {
20+
apiKey: string
21+
filter: Filter
22+
syncAt: string
23+
frequency: number
24+
graph: string
25+
customQuery: string
26+
disabled: boolean
27+
}
1128

1229
const siteNameFromUrl = (originalArticleUrl: string): string => {
1330
try {
@@ -18,20 +35,31 @@ const siteNameFromUrl = (originalArticleUrl: string): string => {
1835
}
1936

2037
const delay = (t = 100) => new Promise((r) => setTimeout(r, t))
38+
const DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
2139
let loading = false
2240

23-
const fetchOmnivore = async (
24-
apiKey: string,
25-
filter: string,
26-
syncAt: string,
27-
inBackground = false
28-
): Promise<string> => {
29-
if (loading) return syncAt
41+
const getQueryFromFilter = (filter: Filter, customQuery: string): string => {
42+
switch (filter) {
43+
case Filter.ALL:
44+
return ''
45+
case Filter.HIGHLIGHTS:
46+
return `has:highlights`
47+
case Filter.ADVANCED:
48+
return customQuery
49+
default:
50+
return ''
51+
}
52+
}
53+
54+
const fetchOmnivore = async (inBackground = false) => {
55+
if (loading) return
56+
57+
const { syncAt, apiKey, filter, customQuery } = logseq.settings as Settings
3058

3159
if (!apiKey) {
3260
await logseq.UI.showMsg('Missing Omnivore api key', 'warning')
3361

34-
return syncAt
62+
return
3563
}
3664

3765
const pageName = 'Omnivore'
@@ -82,8 +110,8 @@ const fetchOmnivore = async (
82110
apiKey,
83111
after,
84112
size,
85-
syncAt,
86-
filter
113+
DateTime.fromFormat(syncAt, DATE_FORMAT).toISO(),
114+
getQueryFromFilter(filter, customQuery)
87115
)
88116

89117
const articleBatch: IBatchBlock[] = []
@@ -146,42 +174,33 @@ const fetchOmnivore = async (
146174
}))
147175
}
148176

149-
syncAt = new Date().toISOString()
150177
!inBackground && (await logseq.UI.showMsg('🔖 Articles fetched'))
151-
152-
return syncAt
178+
logseq.updateSettings({ syncAt: DateTime.local().toFormat(DATE_FORMAT) })
153179
} catch (e) {
154180
!inBackground &&
155181
(await logseq.UI.showMsg('Failed to fetch articles', 'warning'))
156182
console.error(e)
157-
158-
return syncAt
159183
} finally {
160184
loading = false
161185
targetBlock &&
162186
(await logseq.Editor.updateBlock(targetBlock.uuid, blockTitle))
163-
logseq.updateSettings({ 'synced at': syncAt })
164187
}
165188
}
166189

167-
const syncOmnivore = (
168-
apiKey: string,
169-
frequency: number,
170-
filter: string,
171-
syncAt: string,
172-
graph: string
173-
): number => {
190+
const syncOmnivore = (): number => {
191+
const settings = logseq.settings as Settings
192+
174193
let intervalID = 0
175194
// sync every frequency minutes
176-
if (frequency > 0) {
195+
if (settings.frequency > 0) {
177196
intervalID = setInterval(
178197
async () => {
179-
if ((await logseq.App.getCurrentGraph())?.name === graph) {
180-
syncAt = await fetchOmnivore(apiKey, filter, syncAt, true)
198+
if ((await logseq.App.getCurrentGraph())?.name === settings.graph) {
199+
await fetchOmnivore(true)
181200
}
182201
},
183-
frequency * 1000 * 60,
184-
syncAt
202+
settings.frequency * 1000 * 60,
203+
settings.syncAt
185204
)
186205
}
187206

@@ -195,21 +214,30 @@ const syncOmnivore = (
195214
const main = async (baseInfo: LSPluginBaseInfo) => {
196215
console.log('logseq-omnivore loaded')
197216

198-
const settings: SettingSchemaDesc[] = [
217+
const settingsSchema: SettingSchemaDesc[] = [
199218
{
200-
key: 'api key',
219+
key: 'apiKey',
201220
type: 'string',
202221
title: 'Enter Omnivore Api Key',
203222
description: 'Enter Omnivore Api Key here',
204-
default: '',
223+
default: logseq.settings?.['api key'] as string,
205224
},
206225
{
207226
key: 'filter',
227+
type: 'enum',
228+
title: 'Select a filter for Omnivore articles',
229+
description: 'Select a filter for Omnivore articles',
230+
default: Filter.HIGHLIGHTS.toString(),
231+
enumPicker: 'select',
232+
enumChoices: Object.values(Filter),
233+
},
234+
{
235+
key: 'customQuery',
208236
type: 'string',
209-
title: 'Enter a filter for Omnivore articles',
237+
title: 'Enter custom query if advanced filter is selected',
210238
description:
211-
'Enter a filter for Omnivore articles here. e.g. "has:highlights"',
212-
default: 'has:highlights',
239+
'Enter a custom query for Omnivore articles here. e.g. "has:highlights"',
240+
default: '',
213241
},
214242
{
215243
key: 'frequency',
@@ -227,35 +255,41 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
227255
// default is the current graph
228256
default: (await logseq.App.getCurrentGraph())?.name as string,
229257
},
258+
{
259+
key: 'syncAt',
260+
type: 'string',
261+
title: 'Last Sync',
262+
description:
263+
'The last time Omnivore was synced. Clear this value to completely refresh the sync.',
264+
default: DateTime.fromISO(logseq.settings?.['synced at'] as string)
265+
.toLocal()
266+
.toFormat(DATE_FORMAT),
267+
inputAs: 'datetime-local',
268+
},
230269
]
231-
logseq.useSettingsSchema(settings)
270+
logseq.useSettingsSchema(settingsSchema)
232271

233-
let apiKey = logseq.settings?.['api key'] as string
234272
let frequency = logseq.settings?.frequency as number
235-
let filter = logseq.settings?.filter as string
236-
let syncAt = logseq.settings?.['synced at'] as string
237273
let intervalID: number
238-
let graph = logseq.settings?.graph as string
239274

240275
logseq.onSettingsChanged(() => {
241-
apiKey = logseq.settings?.['api key'] as string
242-
filter = logseq.settings?.filter as string
243-
syncAt = logseq.settings?.['synced at'] as string
244-
graph = logseq.settings?.graph as string
245-
// remove existing scheduled task and create new one
246-
const newFrequency = logseq.settings?.frequency as number
276+
const settings = logseq.settings as Settings
277+
const newFrequency = settings.frequency
247278
if (newFrequency !== frequency) {
279+
// remove existing scheduled task and create new one
248280
if (intervalID) {
249281
clearInterval(intervalID)
250282
}
283+
if (newFrequency > 0) {
284+
intervalID = syncOmnivore()
285+
}
251286
frequency = newFrequency
252-
intervalID = syncOmnivore(apiKey, frequency, filter, syncAt, graph)
253287
}
254288
})
255289

256290
logseq.provideModel({
257291
async loadOmnivore() {
258-
await fetchOmnivore(apiKey, filter, syncAt)
292+
await fetchOmnivore()
259293
},
260294
})
261295

@@ -276,10 +310,10 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
276310
`)
277311

278312
// fetch articles on startup
279-
await fetchOmnivore(apiKey, filter, syncAt, true)
313+
await fetchOmnivore(true)
280314

281315
// sync every frequency minutes
282-
intervalID = syncOmnivore(apiKey, frequency, filter, syncAt, graph)
316+
intervalID = syncOmnivore()
283317
}
284318

285319
// bootstrap

src/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const loadArticles = async (
6464
after = 0,
6565
first = 10,
6666
updatedAt = '',
67-
filter = ''
67+
query = ''
6868
): Promise<[Article[], boolean]> => {
6969
const res = await fetch(endpoint, {
7070
headers: {
@@ -73,7 +73,7 @@ export const loadArticles = async (
7373
},
7474
body: `{"query":"\\n query Search($after: String, $first: Int, $query: String) {\\n search(first: $first, after: $after, query: $query) {\\n ... on SearchSuccess {\\n edges {\\n node {\\n title\\n slug\\n siteName\\n originalArticleUrl\\n url\\n author\\n updatedAt\\n description\\n savedAt\\n highlights {\\n id\\n quote\\n annotation\\n }\\n labels {\\n name\\n }\\n }\\n }\\n pageInfo {\\n hasNextPage\\n }\\n }\\n ... on SearchError {\\n errorCodes\\n }\\n }\\n }\\n ","variables":{"after":"${after}","first":${first}, "query":"${
7575
updatedAt ? 'updated:' + updatedAt : ''
76-
} sort:saved-asc ${filter}"}}`,
76+
} sort:saved-asc ${query}"}}`,
7777
method: 'POST',
7878
})
7979

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,11 @@
12101210
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz"
12111211
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
12121212

1213+
"@types/luxon@^2.3.2":
1214+
version "2.3.2"
1215+
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-2.3.2.tgz#8a3f2cdd4858ce698b56cd8597d9243b8e9d3c65"
1216+
integrity sha512-WOehptuhKIXukSUUkRgGbj2c997Uv/iUgYgII8U7XLJqq9W2oF0kQ6frEznRQbdurioz+L/cdaIm4GutTQfgmA==
1217+
12131218
"@types/minimist@^1.2.0":
12141219
version "1.2.2"
12151220
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz"
@@ -3202,6 +3207,11 @@ lru-cache@^7.4.4, lru-cache@^7.5.1, lru-cache@^7.7.1:
32023207
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.12.0.tgz#be2649a992c8a9116efda5c487538dcf715f3476"
32033208
integrity sha512-OIP3DwzRZDfLg9B9VP/huWBlpvbkmbfiBy8xmsXp4RPmE4A3MhwNozc5ZJ3fWnSg8fDcdlE/neRTPG2ycEKliw==
32043209

3210+
luxon@^3.0.1:
3211+
version "3.0.1"
3212+
resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.0.1.tgz#6901111d10ad06fd267ad4e4128a84bef8a77299"
3213+
integrity sha512-hF3kv0e5gwHQZKz4wtm4c+inDtyc7elkanAsBq+fundaCdUBNJB1dHEGUZIM6SfSBUlbVFduPwEtNjFK8wLtcw==
3214+
32053215
make-fetch-happen@^10.0.3, make-fetch-happen@^10.0.6, make-fetch-happen@^10.1.8:
32063216
version "10.1.8"
32073217
resolved "https://registry.npmjs.org/make-fetch-happen/-/make-fetch-happen-10.1.8.tgz"

0 commit comments

Comments
 (0)