Various AT Protocol integrations with obsidian

pds setting and don't fetch until view activated (#2)

authored by

treethought and committed by
GitHub
d3af633a 27b660b3

+37 -15
+1 -1
manifest.json
··· 1 1 { 2 2 "id": "atmark", 3 3 "name": "ATmark", 4 - "version": "0.1.5", 4 + "version": "0.1.6", 5 5 "minAppVersion": "0.15.0", 6 6 "description": "AT Protocol bookmarking platforms in Obsidian", 7 7 "author": "treethought",
+1 -1
package.json
··· 1 1 { 2 2 "name": "obsidian-atmark", 3 - "version": "0.1.4", 3 + "version": "0.1.6", 4 4 "description": "Obsidian plugin for AT Protocol bookmark platforms", 5 5 "main": "main.js", 6 6 "type": "module",
+7 -4
src/auth.ts
··· 1 1 import { Client, CredentialManager, simpleFetchHandler } from "@atcute/client"; 2 2 3 - const DEFAULT_PDS = "https://bsky.social"; 3 + const DEFAULT_SERVICE = "https://bsky.social"; 4 4 5 5 export interface Credentials { 6 6 identifier: string; 7 7 password: string; 8 + serviceUrl?: string; 8 9 } 9 10 10 11 export async function createAuthenticatedClient(creds: Credentials): Promise<Client> { 11 - const manager = new CredentialManager({ service: DEFAULT_PDS }); 12 + const service = creds.serviceUrl || DEFAULT_SERVICE; 13 + const manager = new CredentialManager({ service }); 12 14 await manager.login(creds); 13 15 return new Client({ handler: manager }); 14 16 } 15 17 16 - export function createPublicClient(): Client { 17 - return new Client({ handler: simpleFetchHandler({ service: DEFAULT_PDS }) }); 18 + export function createPublicClient(serviceUrl?: string): Client { 19 + const service = serviceUrl || DEFAULT_SERVICE; 20 + return new Client({ handler: simpleFetchHandler({ service }) }); 18 21 }
+10 -8
src/main.ts
··· 35 35 36 36 37 37 private async initClient() { 38 - const { identifier, appPassword } = this.settings; 38 + const { identifier, appPassword, serviceUrl } = this.settings; 39 39 if (identifier && appPassword) { 40 40 try { 41 - this.client = await createAuthenticatedClient({ identifier, password: appPassword }); 42 - await this.fetchProfile(); 41 + this.client = await createAuthenticatedClient({ identifier, password: appPassword, serviceUrl }); 43 42 new Notice("Connected"); 44 43 } catch (err) { 45 44 const message = err instanceof Error ? err.message : String(err); 46 - new Notice(`Auth failed: ${message}`); 47 - this.client = createPublicClient(); 45 + new Notice(`Failed to login as ${identifier}: ${message}`); 46 + this.client = createPublicClient(serviceUrl); 48 47 this.profile = null; 49 48 } 50 49 } else { 51 - this.client = createPublicClient(); 50 + this.client = createPublicClient(serviceUrl); 52 51 this.profile = null; 53 52 } 54 53 } ··· 70 69 } else { 71 70 this.profile = null; 72 71 } 73 - } catch (e) { 74 - console.error("Failed to fetch profile:", e); 72 + } catch { 75 73 this.profile = null; 76 74 } 77 75 } ··· 82 80 83 81 84 82 async activateView(v: string) { 83 + if (!this.profile && this.client && this.settings.identifier) { 84 + await this.fetchProfile(); 85 + } 86 + 85 87 const { workspace } = this.app; 86 88 87 89 let leaf: WorkspaceLeaf | null = null;
+18 -1
src/settings.ts
··· 4 4 export interface AtProtoSettings { 5 5 identifier: string; 6 6 appPassword: string; 7 + serviceUrl: string; 7 8 } 8 9 9 10 export const DEFAULT_SETTINGS: AtProtoSettings = { 10 11 identifier: "", 11 12 appPassword: "", 13 + serviceUrl: "", 12 14 }; 13 15 14 16 export class SettingTab extends PluginSettingTab { ··· 24 26 containerEl.empty(); 25 27 26 28 new Setting(containerEl) 27 - // eslint-disable-next-line obsidianmd/ui/sentence-case 29 + // eslint-disable-next-line obsidianmd/ui/sentence-case 28 30 .setName("Handle or DID") 29 31 .setDesc("Your handle or did (e.g., user.bsky.social)") 30 32 .addText((text) => ··· 53 55 await this.plugin.saveSettings(); 54 56 }); 55 57 }); 58 + 59 + new Setting(containerEl) 60 + .setName("Auth service") 61 + // eslint-disable-next-line obsidianmd/ui/sentence-case 62 + .setDesc("PDS or PDS entryway URL (leave empty to use bsky.social service) ") 63 + .addText((text) => 64 + text 65 + // eslint-disable-next-line obsidianmd/ui/sentence-case 66 + .setPlaceholder("https://bsky.social") 67 + .setValue(this.plugin.settings.serviceUrl) 68 + .onChange(async (value) => { 69 + this.plugin.settings.serviceUrl = value; 70 + await this.plugin.saveSettings(); 71 + }) 72 + ); 56 73 } 57 74 }