@@ -8,6 +8,23 @@ import {
8
8
import { getDateForPage } from 'logseq-dateutils'
9
9
import icon from '../public/icon.png'
10
10
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
+ }
11
28
12
29
const siteNameFromUrl = ( originalArticleUrl : string ) : string => {
13
30
try {
@@ -18,20 +35,31 @@ const siteNameFromUrl = (originalArticleUrl: string): string => {
18
35
}
19
36
20
37
const delay = ( t = 100 ) => new Promise ( ( r ) => setTimeout ( r , t ) )
38
+ const DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
21
39
let loading = false
22
40
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
30
58
31
59
if ( ! apiKey ) {
32
60
await logseq . UI . showMsg ( 'Missing Omnivore api key' , 'warning' )
33
61
34
- return syncAt
62
+ return
35
63
}
36
64
37
65
const pageName = 'Omnivore'
@@ -82,8 +110,8 @@ const fetchOmnivore = async (
82
110
apiKey ,
83
111
after ,
84
112
size ,
85
- syncAt ,
86
- filter
113
+ DateTime . fromFormat ( syncAt , DATE_FORMAT ) . toISO ( ) ,
114
+ getQueryFromFilter ( filter , customQuery )
87
115
)
88
116
89
117
const articleBatch : IBatchBlock [ ] = [ ]
@@ -146,42 +174,33 @@ const fetchOmnivore = async (
146
174
} ) )
147
175
}
148
176
149
- syncAt = new Date ( ) . toISOString ( )
150
177
! inBackground && ( await logseq . UI . showMsg ( '🔖 Articles fetched' ) )
151
-
152
- return syncAt
178
+ logseq . updateSettings ( { syncAt : DateTime . local ( ) . toFormat ( DATE_FORMAT ) } )
153
179
} catch ( e ) {
154
180
! inBackground &&
155
181
( await logseq . UI . showMsg ( 'Failed to fetch articles' , 'warning' ) )
156
182
console . error ( e )
157
-
158
- return syncAt
159
183
} finally {
160
184
loading = false
161
185
targetBlock &&
162
186
( await logseq . Editor . updateBlock ( targetBlock . uuid , blockTitle ) )
163
- logseq . updateSettings ( { 'synced at' : syncAt } )
164
187
}
165
188
}
166
189
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
+
174
193
let intervalID = 0
175
194
// sync every frequency minutes
176
- if ( frequency > 0 ) {
195
+ if ( settings . frequency > 0 ) {
177
196
intervalID = setInterval (
178
197
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 )
181
200
}
182
201
} ,
183
- frequency * 1000 * 60 ,
184
- syncAt
202
+ settings . frequency * 1000 * 60 ,
203
+ settings . syncAt
185
204
)
186
205
}
187
206
@@ -195,21 +214,30 @@ const syncOmnivore = (
195
214
const main = async ( baseInfo : LSPluginBaseInfo ) => {
196
215
console . log ( 'logseq-omnivore loaded' )
197
216
198
- const settings : SettingSchemaDesc [ ] = [
217
+ const settingsSchema : SettingSchemaDesc [ ] = [
199
218
{
200
- key : 'api key ' ,
219
+ key : 'apiKey ' ,
201
220
type : 'string' ,
202
221
title : 'Enter Omnivore Api Key' ,
203
222
description : 'Enter Omnivore Api Key here' ,
204
- default : '' ,
223
+ default : logseq . settings ?. [ 'api key' ] as string ,
205
224
} ,
206
225
{
207
226
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' ,
208
236
type : 'string' ,
209
- title : 'Enter a filter for Omnivore articles ' ,
237
+ title : 'Enter custom query if advanced filter is selected ' ,
210
238
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 : '' ,
213
241
} ,
214
242
{
215
243
key : 'frequency' ,
@@ -227,35 +255,41 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
227
255
// default is the current graph
228
256
default : ( await logseq . App . getCurrentGraph ( ) ) ?. name as string ,
229
257
} ,
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
+ } ,
230
269
]
231
- logseq . useSettingsSchema ( settings )
270
+ logseq . useSettingsSchema ( settingsSchema )
232
271
233
- let apiKey = logseq . settings ?. [ 'api key' ] as string
234
272
let frequency = logseq . settings ?. frequency as number
235
- let filter = logseq . settings ?. filter as string
236
- let syncAt = logseq . settings ?. [ 'synced at' ] as string
237
273
let intervalID : number
238
- let graph = logseq . settings ?. graph as string
239
274
240
275
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
247
278
if ( newFrequency !== frequency ) {
279
+ // remove existing scheduled task and create new one
248
280
if ( intervalID ) {
249
281
clearInterval ( intervalID )
250
282
}
283
+ if ( newFrequency > 0 ) {
284
+ intervalID = syncOmnivore ( )
285
+ }
251
286
frequency = newFrequency
252
- intervalID = syncOmnivore ( apiKey , frequency , filter , syncAt , graph )
253
287
}
254
288
} )
255
289
256
290
logseq . provideModel ( {
257
291
async loadOmnivore ( ) {
258
- await fetchOmnivore ( apiKey , filter , syncAt )
292
+ await fetchOmnivore ( )
259
293
} ,
260
294
} )
261
295
@@ -276,10 +310,10 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
276
310
` )
277
311
278
312
// fetch articles on startup
279
- await fetchOmnivore ( apiKey , filter , syncAt , true )
313
+ await fetchOmnivore ( true )
280
314
281
315
// sync every frequency minutes
282
- intervalID = syncOmnivore ( apiKey , frequency , filter , syncAt , graph )
316
+ intervalID = syncOmnivore ( )
283
317
}
284
318
285
319
// bootstrap
0 commit comments