Skip to content

Commit

Permalink
Adding support for syncing a particular Pocket tag only (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
nybbles authored Jan 6, 2022
1 parent 2729aac commit 5381d7a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ within Obsidian.

You can also sync your Pocket items using an Obsidian command: "Sync Pocket list".

You can either sync all Pocket items or just Pocket items with a particular tag
that you specify, using the "Pocket sync tag" setting. Leave it blank to sync
all Pocket items, or specify a Pocket tag to limit your sync to just Pocket
items with that tag.

### Opening and using the Pocket list

Once the Pocket list is downloaded and stored, open the command palette and
Expand Down
Binary file modified images/obsidian-pocket-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/SettingsManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import update from "immutability-helper";
import log from "loglevel";
import { MultiWordTagConversion } from "./Tags";
import { CallbackId, CallbackRegistry } from "./Types";
import { getUniqueId } from "./utils";
Expand All @@ -8,6 +7,7 @@ export interface PocketSettings {
"item-note-template"?: string;
"item-notes-folder"?: string;
"multi-word-tag-converter"?: MultiWordTagConversion;
"pocket-sync-tag"?: string;
}

const DEFAULT_POCKET_SETTINGS: PocketSettings = {
Expand Down
6 changes: 4 additions & 2 deletions src/data/PocketSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { PocketItemStore } from "./PocketItemStore";
export const doPocketSync = async (
itemStore: PocketItemStore,
pocketAPI: PocketAPI,
accessInfo: AccessInfo
accessInfo: AccessInfo,
pocketSyncTag?: string
) => {
const lastUpdateTimestamp = await itemStore.getLastUpdateTimestamp();

new Notice(`Fetching Pocket updates for ${accessInfo.username}`);

const getPocketItemsResponse = await pocketAPI.getPocketItems(
accessInfo.accessToken,
lastUpdateTimestamp
lastUpdateTimestamp,
pocketSyncTag
);

new Notice(
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ export default class PocketSync extends Plugin {
return;
}

this.pendingSync = doPocketSync(this.itemStore, this.pocketAPI, accessInfo);
const pocketSyncTag = this.settingsManager.getSetting("pocket-sync-tag");
this.pendingSync = doPocketSync(
this.itemStore,
this.pocketAPI,
accessInfo,
pocketSyncTag
);
try {
await this.pendingSync;
} finally {
Expand Down
7 changes: 5 additions & 2 deletions src/pocket_api/PocketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export type GetRequestToken = (
export type GetAccessToken = () => Promise<AccessTokenResponse>;
export type GetPocketItems = (
accessToken: AccessToken,
lastUpdateTimestamp?: UpdateTimestamp
lastUpdateTimestamp?: UpdateTimestamp,
pocketSyncTag?: string
) => Promise<TimestampedPocketGetItemsResponse>;

export interface PocketAPI {
Expand Down Expand Up @@ -120,7 +121,8 @@ export type TimestampedPocketGetItemsResponse = {

export const getPocketItems: GetPocketItems = async (
accessToken,
lastUpdateTimestamp?
lastUpdateTimestamp?,
pocketSyncTag?
) => {
const GET_ITEMS_URL = "https://getpocket.com/v3/get";
const nextTimestamp = Math.floor(Date.now() / 1000);
Expand All @@ -132,6 +134,7 @@ export const getPocketItems: GetPocketItems = async (
? new Number(lastUpdateTimestamp).toString()
: null,
detailType: "complete",
tag: pocketSyncTag,
};

if (!!lastUpdateTimestamp) {
Expand Down
22 changes: 22 additions & 0 deletions src/ui/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ const addMultiWordTagConverterSetting = (
});
};

const SYNC_TAG_CTA = "Pocket sync tag";
const SYNC_TAG_DESC = `Specify a Pocket tag to sync, e.g. adding 'obsidian' here
will result in only Pocket items tagged with 'obsidian' being synced. If this
setting is left blank, all Pocket items will be synced.`;

const addPocketSyncTagSetting = (
settingsManager: SettingsManager,
containerEl: HTMLElement
) => {
new Setting(containerEl)
.setName(SYNC_TAG_CTA)
.setDesc(SYNC_TAG_DESC)
.addText((text) => {
text.setPlaceholder("Specify a tag to limit syncs");
text.setValue(settingsManager.getSetting("pocket-sync-tag"));
text.onChange(async (newValue) => {
await settingsManager.updateSetting("pocket-sync-tag", newValue);
});
});
};

export class PocketSettingTab extends PluginSettingTab {
plugin: PocketSync;
settingsManager: SettingsManager;
Expand All @@ -178,5 +199,6 @@ export class PocketSettingTab extends PluginSettingTab {
addItemNoteTemplateSetting(this.settingsManager, containerEl);
addItemNotesLocationSetting(this.settingsManager, containerEl);
addMultiWordTagConverterSetting(this.settingsManager, containerEl);
addPocketSyncTagSetting(this.settingsManager, containerEl);
}
}

0 comments on commit 5381d7a

Please sign in to comment.