Skip to content

Commit c80bde0

Browse files
committed
improved login process, integrated some review comments
1 parent e0ba9f8 commit c80bde0

File tree

13 files changed

+145
-80
lines changed

13 files changed

+145
-80
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "pocketbook-cloud-highlight-importer",
33
"name": "Pocketbook Cloud Highlight Importer",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"minAppVersion": "1.1.16",
66
"description": "Imports notes and highlights from your Pocketbook Cloud account.",
77
"author": "Lena Brüder",

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "obsidian-sample-plugin",
3-
"version": "1.0.0",
4-
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
2+
"name": "pocketbook-cloud-highlight-importer",
3+
"version": "0.1.3",
4+
"description": "Pocketbook Cloud Highlight Importer",
55
"main": "main.js",
66
"scripts": {
77
"dev": "node esbuild.config.mjs",

src/apiclient.ts

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export interface PocketbookCloudNote {
6161
uuid: string; //TODO: uuid type?
6262
}
6363

64+
interface PocketbookCloudShopInfo {
65+
name: string;
66+
shop_id: string;
67+
}
68+
6469
/**
6570
* Main things to know about the API:
6671
*
@@ -144,7 +149,7 @@ export class PocketbookCloudLoginClient {
144149
) {}
145150

146151
async login() {
147-
const shop_id = await fetch(
152+
const shops: PocketbookCloudShopInfo[] = await fetch(
148153
'https://cloud.pocketbook.digital/api/v1.0/auth/login?' +
149154
new URLSearchParams({
150155
username: this.username,
@@ -156,51 +161,64 @@ export class PocketbookCloudLoginClient {
156161
.then(data => {
157162
return data;
158163
})
159-
.then(data => data.providers.filter((item: any) => item.name.includes(this.shop_name)))
160-
.then(data => data[0].shop_id);
161-
162-
console.log(`shop_id: ${shop_id}`);
163-
164-
let login_response;
165-
if (this.refresh_token) {
166-
login_response = await requestUrl({
167-
url: 'https://cloud.pocketbook.digital/api/v1.0/auth/renew-token',
168-
method: 'POST',
169-
contentType: 'application/x-www-form-urlencoded',
170-
headers: {
171-
Authorization: `Bearer ${this.access_token}`,
172-
},
173-
body: new URLSearchParams({
174-
grant_type: 'refresh_token',
175-
refresh_token: this.refresh_token,
176-
}).toString(),
177-
}).then(response => response.json);
178-
} else if (this.password) {
179-
login_response = await requestUrl({
180-
url: 'https://cloud.pocketbook.digital/api/v1.0/auth/login/knv',
181-
method: 'POST',
182-
contentType: 'application/x-www-form-urlencoded',
183-
body: new URLSearchParams({
184-
shop_id,
185-
username: this.username,
186-
password: this.password,
187-
client_id: this.client_id,
188-
client_secret: this.client_secret,
189-
}).toString(),
190-
}).then(response => response.json);
191-
} else {
192-
throw new Error('No password or refresh token provided, one is necessary to login.');
164+
.then(data => data.providers.filter((item: PocketbookCloudShopInfo) => item.name.includes(this.shop_name)));
165+
166+
const login_responses = await Promise.all(
167+
shops.map(async shop => {
168+
let result;
169+
try {
170+
if (this.refresh_token) {
171+
result = await requestUrl({
172+
url: 'https://cloud.pocketbook.digital/api/v1.0/auth/renew-token',
173+
method: 'POST',
174+
contentType: 'application/x-www-form-urlencoded',
175+
headers: {
176+
Authorization: `Bearer ${this.access_token}`,
177+
},
178+
body: new URLSearchParams({
179+
grant_type: 'refresh_token',
180+
refresh_token: this.refresh_token,
181+
}).toString(),
182+
}).then(response => response.json);
183+
} else if (this.password) {
184+
result = await requestUrl({
185+
url: 'https://cloud.pocketbook.digital/api/v1.0/auth/login/knv',
186+
method: 'POST',
187+
contentType: 'application/x-www-form-urlencoded',
188+
body: new URLSearchParams({
189+
shop_id: shop.shop_id,
190+
username: this.username,
191+
password: this.password,
192+
client_id: this.client_id,
193+
client_secret: this.client_secret,
194+
grant_type: 'password',
195+
language: 'en',
196+
}).toString(),
197+
}).then(response => response.json);
198+
}
199+
} catch (error) {
200+
result = null;
201+
}
202+
return { shop, result };
203+
})
204+
);
205+
206+
// use first defined response, if any
207+
const login_response = login_responses.filter(response => response.result).first();
208+
if (!login_response) {
209+
throw new Error('Could not log in to Pocketbook Cloud');
193210
}
194211

195-
this.access_token = login_response.access_token;
196-
this.refresh_token = login_response.refresh_token;
212+
this.access_token = login_response.result.access_token;
213+
this.refresh_token = login_response.result.refresh_token;
197214

198215
// sets the access token to expire 5 minutes before it actually does
199-
this.access_token_valid_until = new Date(Date.now() + login_response.expires_in * 1000 - 5 * 60 * 1000);
216+
this.access_token_valid_until = new Date(Date.now() + login_response.result.expires_in * 1000 - 5 * 60 * 1000);
200217

201218
this.plugin.settings.access_token = this.access_token!!;
202219
this.plugin.settings.refresh_token = this.refresh_token!!;
203220
this.plugin.settings.access_token_valid_until = this.access_token_valid_until;
221+
this.plugin.settings.shop_name = login_response.shop.name;
204222
await this.plugin.saveSettings();
205223
}
206224

src/import.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,14 @@ export class PocketbookCloudHighlightsImporter {
123123
}
124124

125125
private async writeFile(file_name: string, content: string) {
126-
let file = this.app.vault.getAbstractFileByPath(file_name) as TFile;
126+
const file = this.app.vault.getAbstractFileByPath(file_name);
127127
if (!file) {
128-
file = await this.app.vault.create(file_name, '');
128+
await this.app.vault.create(file_name, content);
129+
} else if (file instanceof TFile) {
130+
await this.app.vault.modify(file, content);
131+
} else {
132+
throw new Error(`File ${file_name} is not a TFile, can only write to files.`);
129133
}
130-
131-
this.app.vault.modify(file, content);
132134
}
133135

134136
private async writeFileBinary(file_name: string, content: ArrayBuffer) {

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ export default class PocketbookCloudHighlightsImporterPlugin extends Plugin {
3333

3434
async saveSettings() {
3535
await this.saveData(this.settings);
36+
this.importer = new PocketbookCloudHighlightsImporter(this.app, this, this.settings);
3637
}
3738
}

src/settings.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, Modal, Notice, PluginSettingTab, Setting } from 'obsidian';
1+
import { App, Modal, Notice, PluginSettingTab, Setting, TextComponent } from 'obsidian';
22
import { PocketbookCloudLoginClient } from './apiclient';
33
import PocketbookCloudHighlightsImporterPlugin from './main';
44

@@ -48,6 +48,7 @@ export class PocketbookCloudHighlightsImporterSettingTab extends PluginSettingTa
4848
})
4949
);
5050

51+
let shop_name_text_field: TextComponent;
5152
new Setting(containerEl)
5253
.setName('Credentials')
5354
.setDesc('Use this to log in')
@@ -70,33 +71,40 @@ export class PocketbookCloudHighlightsImporterSettingTab extends PluginSettingTa
7071
this.plugin.settings.refresh_token = await api_client.getRefreshToken();
7172
this.plugin.saveSettings();
7273

74+
shop_name_text_field.setValue(this.plugin.settings.shop_name);
75+
7376
new Notice('Logged in successfully');
7477
}).open();
7578
})
7679
);
7780

7881
new Setting(containerEl)
7982
.setName('Shop name')
80-
.setDesc('The name of the shop you are logging in to. Leave empty if you only have one; you can use a substring of the shop name here.')
81-
.addText(text =>
83+
.setDesc(
84+
'The name of the shop you are logging in to. Will be auto-filled on login - only change if that does not work well and you know what you are doing.'
85+
)
86+
.addText(text => {
87+
shop_name_text_field = text;
8288
text
8389
.setPlaceholder('')
8490
.setValue(this.plugin.settings.shop_name)
8591
.onChange(async value => {
8692
this.plugin.settings.shop_name = value;
8793
await this.plugin.saveSettings();
88-
})
89-
);
94+
});
95+
return text;
96+
});
9097

9198
new Setting(containerEl)
9299
.setName('Import Folder')
93-
.setDesc('The folder the plugin will write to. Should be empty, do not store other data here.')
100+
.setDesc('The folder the plugin will write to. The folder should be empty, do not store other data here.')
94101
.addText(text =>
95102
text
96103
.setPlaceholder('Enter your folder path from vault root')
97104
.setValue(this.plugin.settings.import_folder)
98105
.onChange(async value => {
99-
this.plugin.settings.import_folder = value;
106+
// filter out leading slashes, they are not needed
107+
this.plugin.settings.import_folder = value.replace(/^\//, '');
100108
await this.plugin.saveSettings();
101109
})
102110
);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
22
"dataview",
3-
"obsidian-pocketbook-cloud-highlight-importer"
3+
"obsidian-pocketbook-cloud-highlight-importer",
4+
"pocketbook-cloud-highlight-importer"
45
]

test-vault/.obsidian/core-plugins-migration.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
"workspaces": false,
2626
"file-recovery": true,
2727
"publish": false,
28-
"sync": false
28+
"sync": false,
29+
"bookmarks": true
2930
}

test-vault/.obsidian/core-plugins.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"note-composer",
1414
"command-palette",
1515
"editor-status",
16-
"starred",
16+
"bookmarks",
1717
"outline",
1818
"word-count",
1919
"file-recovery"
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"id": "obsidian-pocketbook-cloud-highlight-importer",
2+
"id": "pocketbook-cloud-highlight-importer",
33
"name": "Pocketbook Cloud Highlight Importer",
4-
"version": "0.1.0",
5-
"minAppVersion": "0.15.0",
4+
"version": "0.1.3",
5+
"minAppVersion": "1.1.16",
66
"description": "Imports notes and highlights from your Pocketbook Cloud account.",
77
"author": "Lena Brüder",
8-
"authorUrl": "https://github.com/lenalebt/obsidian-pocketbook-cloud-highlight-importer",
9-
"fundingUrl": "https://github.com/lenalebt/obsidian-pocketbook-cloud-highlight-importer",
8+
"authorUrl": "https://github.com/lenalebt",
9+
"fundingUrl": "https://www.buymeacoffee.com/lenalebt",
1010
"isDesktopOnly": false
1111
}

0 commit comments

Comments
 (0)