Skip to content

Commit 1404f26

Browse files
authored
Merge pull request #34 from omnivore-app/feature/sync-to-selected-graph
fix: only sync into the graph we were initially opened in
2 parents 6a524e3 + 7586891 commit 1404f26

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

src/index.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,6 @@ import { getDateForPage } from 'logseq-dateutils'
99
import icon from '../public/icon.png'
1010
import { Article, loadArticles } from './util'
1111

12-
const settings: SettingSchemaDesc[] = [
13-
{
14-
key: 'api key',
15-
type: 'string',
16-
title: 'Enter Omnivore Api Key',
17-
description: 'Enter Omnivore Api Key here',
18-
default: '',
19-
},
20-
{
21-
key: 'filter',
22-
type: 'string',
23-
title: 'Enter a filter for Omnivore articles',
24-
description:
25-
'Enter a filter for Omnivore articles here. e.g. "has:highlights"',
26-
default: 'has:highlights',
27-
},
28-
{
29-
key: 'frequency',
30-
type: 'number',
31-
title: 'Enter sync with Omnivore frequency',
32-
description:
33-
'Enter sync with Omnivore frequency in minutes here or 0 to disable',
34-
default: 60,
35-
},
36-
]
37-
3812
const siteNameFromUrl = (originalArticleUrl: string): string => {
3913
try {
4014
return new URL(originalArticleUrl).hostname.replace(/^www\./, '')
@@ -195,14 +169,17 @@ const syncOmnivore = (
195169
apiKey: string,
196170
frequency: number,
197171
filter: string,
198-
syncAt: string
172+
syncAt: string,
173+
graph: string
199174
): number => {
200175
let intervalID = 0
201176
// sync every frequency minutes
202177
if (frequency > 0) {
203178
intervalID = setInterval(
204179
async () => {
205-
syncAt = await fetchOmnivore(apiKey, filter, syncAt, true)
180+
if ((await logseq.App.getCurrentGraph())?.name === graph) {
181+
syncAt = await fetchOmnivore(apiKey, filter, syncAt, true)
182+
}
206183
},
207184
frequency * 1000 * 60,
208185
syncAt
@@ -219,26 +196,61 @@ const syncOmnivore = (
219196
const main = async (baseInfo: LSPluginBaseInfo) => {
220197
console.log('logseq-omnivore loaded')
221198

199+
const settings: SettingSchemaDesc[] = [
200+
{
201+
key: 'api key',
202+
type: 'string',
203+
title: 'Enter Omnivore Api Key',
204+
description: 'Enter Omnivore Api Key here',
205+
default: '',
206+
},
207+
{
208+
key: 'filter',
209+
type: 'string',
210+
title: 'Enter a filter for Omnivore articles',
211+
description:
212+
'Enter a filter for Omnivore articles here. e.g. "has:highlights"',
213+
default: 'has:highlights',
214+
},
215+
{
216+
key: 'frequency',
217+
type: 'number',
218+
title: 'Enter sync with Omnivore frequency',
219+
description:
220+
'Enter sync with Omnivore frequency in minutes here or 0 to disable',
221+
default: 60,
222+
},
223+
{
224+
key: 'graph',
225+
type: 'string',
226+
title: 'Enter the graph to sync with Omnivore',
227+
description: 'Enter the graph to sync Omnivore articles to',
228+
// default is the current graph
229+
default: (await logseq.App.getCurrentGraph())?.name as string,
230+
},
231+
]
222232
logseq.useSettingsSchema(settings)
223233

224234
let apiKey = logseq.settings?.['api key'] as string
225235
let frequency = logseq.settings?.frequency as number
226236
let filter = logseq.settings?.filter as string
227237
let syncAt = logseq.settings?.['synced at'] as string
228238
let intervalID: number
239+
let graph = logseq.settings?.graph as string
229240

230241
logseq.onSettingsChanged(() => {
231242
apiKey = logseq.settings?.['api key'] as string
232243
filter = logseq.settings?.filter as string
233244
syncAt = logseq.settings?.['synced at'] as string
245+
graph = logseq.settings?.graph as string
234246
// remove existing scheduled task and create new one
235247
const newFrequency = logseq.settings?.frequency as number
236248
if (newFrequency !== frequency) {
237249
if (intervalID) {
238250
clearInterval(intervalID)
239251
}
240252
frequency = newFrequency
241-
intervalID = syncOmnivore(apiKey, frequency, filter, syncAt)
253+
intervalID = syncOmnivore(apiKey, frequency, filter, syncAt, graph)
242254
}
243255
})
244256

@@ -268,7 +280,7 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
268280
await fetchOmnivore(apiKey, filter, syncAt, true)
269281

270282
// sync every frequency minutes
271-
intervalID = syncOmnivore(apiKey, frequency, filter, syncAt)
283+
intervalID = syncOmnivore(apiKey, frequency, filter, syncAt, graph)
272284
}
273285

274286
// bootstrap

0 commit comments

Comments
 (0)