mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 01:56:27 +08:00
@@ -30,6 +30,13 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
|
||||
|
||||
importing$ = new LiveData(false);
|
||||
settings$ = LiveData.from(this.readwiseStore.watchSetting(), undefined);
|
||||
|
||||
setting$<T extends keyof ReadwiseConfig>(
|
||||
key: T
|
||||
): LiveData<ReadwiseConfig[T]> {
|
||||
return this.settings$.selector(setting => setting?.[key]);
|
||||
}
|
||||
|
||||
updateSetting<T extends keyof ReadwiseConfig>(
|
||||
key: T,
|
||||
value: ReadwiseConfig[T]
|
||||
@@ -77,6 +84,8 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
|
||||
localRefs.map(ref => [ref.refMeta.highlightId, ref])
|
||||
);
|
||||
const updateStrategy = this.readwiseStore.getSetting('updateStrategy');
|
||||
const syncNewHighlights =
|
||||
this.readwiseStore.getSetting('syncNewHighlights');
|
||||
const chunks = chunk(highlights, 2);
|
||||
const total = highlights.length;
|
||||
let finished = 0;
|
||||
@@ -99,8 +108,14 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
|
||||
const refMeta = localRef?.refMeta;
|
||||
const localUpdatedAt = refMeta?.updatedAt;
|
||||
const localDocId = localRef?.id;
|
||||
const action = this.getAction({
|
||||
localUpdatedAt,
|
||||
remoteUpdatedAt: highlight.updated_at,
|
||||
updateStrategy,
|
||||
syncNewHighlights,
|
||||
});
|
||||
// write if not matched
|
||||
if (localUpdatedAt !== highlight.updated_at && !signal?.aborted) {
|
||||
if (action !== 'skip' && !signal?.aborted) {
|
||||
await this.highlightToAffineDoc(highlight, book, localDocId, {
|
||||
updateStrategy,
|
||||
integrationId,
|
||||
@@ -139,7 +154,7 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
|
||||
title: book.title,
|
||||
docId,
|
||||
comment: highlight.note,
|
||||
updateStrategy,
|
||||
updateStrategy: updateStrategy ?? 'append',
|
||||
});
|
||||
|
||||
// write failed
|
||||
@@ -168,8 +183,43 @@ export class ReadwiseIntegration extends Entity<{ writer: IntegrationWriter }> {
|
||||
});
|
||||
}
|
||||
|
||||
getAction(info: {
|
||||
localUpdatedAt?: string;
|
||||
remoteUpdatedAt?: string;
|
||||
updateStrategy?: ReadwiseConfig['updateStrategy'];
|
||||
syncNewHighlights?: ReadwiseConfig['syncNewHighlights'];
|
||||
}) {
|
||||
const {
|
||||
localUpdatedAt,
|
||||
remoteUpdatedAt,
|
||||
updateStrategy,
|
||||
syncNewHighlights,
|
||||
} = info;
|
||||
|
||||
return !localUpdatedAt
|
||||
? syncNewHighlights
|
||||
? 'new'
|
||||
: 'skip'
|
||||
: localUpdatedAt !== remoteUpdatedAt
|
||||
? updateStrategy
|
||||
? 'update'
|
||||
: 'skip'
|
||||
: 'skip';
|
||||
}
|
||||
|
||||
connect(token: string) {
|
||||
this.readwiseStore.setSettings({
|
||||
token,
|
||||
updateStrategy: 'append',
|
||||
syncNewHighlights: true,
|
||||
});
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.readwiseStore.setSetting('token', undefined);
|
||||
this.readwiseStore.setSetting('lastImportedAt', undefined);
|
||||
this.readwiseStore.setSettings({
|
||||
token: undefined,
|
||||
updateStrategy: undefined,
|
||||
syncNewHighlights: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,4 +81,11 @@ export class ReadwiseStore extends Store {
|
||||
[key]: value,
|
||||
});
|
||||
}
|
||||
|
||||
setSettings(settings: Partial<ReadwiseConfig>) {
|
||||
this.globalState.set(this.getStorageKey(), {
|
||||
...this.getSetting(),
|
||||
...settings,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,10 @@ export interface ReadwiseConfig {
|
||||
* The last import time
|
||||
*/
|
||||
lastImportedAt?: string;
|
||||
/**
|
||||
* Whether to sync new highlights
|
||||
*/
|
||||
syncNewHighlights?: boolean;
|
||||
/**
|
||||
* The update strategy
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user