mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-27 17:35:07 +01:00
Get detailed user info in Obsidian from the new v1/user API
Previously we were just getting user email from the /health API Instead store the retrieved user info in the user settings
This commit is contained in:
parent
f8f9d066db
commit
0a1a6cd041
2 changed files with 25 additions and 17 deletions
|
@ -2,6 +2,15 @@ import { App, Notice, PluginSettingTab, Setting, TFile } from 'obsidian';
|
|||
import Khoj from 'src/main';
|
||||
import { canConnectToBackend, getBackendStatusMessage, updateContentIndex } from './utils';
|
||||
|
||||
export interface UserInfo {
|
||||
username?: string;
|
||||
photo?: string;
|
||||
is_active?: boolean;
|
||||
has_documents?: boolean;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
|
||||
export interface KhojSetting {
|
||||
resultsCount: number;
|
||||
khojUrl: string;
|
||||
|
@ -9,7 +18,7 @@ export interface KhojSetting {
|
|||
connectedToBackend: boolean;
|
||||
autoConfigure: boolean;
|
||||
lastSync: Map<TFile, number>;
|
||||
userEmail: string;
|
||||
userInfo: UserInfo | null;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: KhojSetting = {
|
||||
|
@ -19,7 +28,7 @@ export const DEFAULT_SETTINGS: KhojSetting = {
|
|||
connectedToBackend: false,
|
||||
autoConfigure: true,
|
||||
lastSync: new Map(),
|
||||
userEmail: '',
|
||||
userInfo: null,
|
||||
}
|
||||
|
||||
export class KhojSettingTab extends PluginSettingTab {
|
||||
|
@ -38,7 +47,7 @@ export class KhojSettingTab extends PluginSettingTab {
|
|||
let backendStatusEl = containerEl.createEl('small', {
|
||||
text: getBackendStatusMessage(
|
||||
this.plugin.settings.connectedToBackend,
|
||||
this.plugin.settings.userEmail,
|
||||
this.plugin.settings.userInfo?.email,
|
||||
this.plugin.settings.khojUrl,
|
||||
this.plugin.settings.khojApiKey
|
||||
)}
|
||||
|
@ -55,7 +64,7 @@ export class KhojSettingTab extends PluginSettingTab {
|
|||
this.plugin.settings.khojUrl = value.trim().replace(/\/$/, '');
|
||||
({
|
||||
connectedToBackend: this.plugin.settings.connectedToBackend,
|
||||
userEmail: this.plugin.settings.userEmail,
|
||||
userInfo: this.plugin.settings.userInfo,
|
||||
statusMessage: backendStatusMessage,
|
||||
} = await canConnectToBackend(this.plugin.settings.khojUrl, this.plugin.settings.khojApiKey));
|
||||
|
||||
|
@ -71,7 +80,7 @@ export class KhojSettingTab extends PluginSettingTab {
|
|||
this.plugin.settings.khojApiKey = value.trim();
|
||||
({
|
||||
connectedToBackend: this.plugin.settings.connectedToBackend,
|
||||
userEmail: this.plugin.settings.userEmail,
|
||||
userInfo: this.plugin.settings.userInfo,
|
||||
statusMessage: backendStatusMessage,
|
||||
} = await canConnectToBackend(this.plugin.settings.khojUrl, this.plugin.settings.khojApiKey));
|
||||
await this.plugin.saveSettings();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { FileSystemAdapter, Notice, Vault, Modal, TFile, request } from 'obsidian';
|
||||
import { KhojSetting } from 'src/settings'
|
||||
import { KhojSetting, UserInfo } from 'src/settings'
|
||||
|
||||
export function getVaultAbsolutePath(vault: Vault): string {
|
||||
let adaptor = vault.adapter;
|
||||
|
@ -173,31 +173,30 @@ export async function canConnectToBackend(
|
|||
khojUrl: string,
|
||||
khojApiKey: string,
|
||||
showNotice: boolean = false
|
||||
): Promise<{ connectedToBackend: boolean; statusMessage: string, userEmail: string }> {
|
||||
): Promise<{ connectedToBackend: boolean; statusMessage: string, userInfo: UserInfo | null }> {
|
||||
let connectedToBackend = false;
|
||||
let userEmail: string = '';
|
||||
let userInfo: UserInfo | null = null;
|
||||
|
||||
if (!!khojUrl) {
|
||||
let headers = !!khojApiKey ? { "Authorization": `Bearer ${khojApiKey}` } : undefined;
|
||||
await request({ url: `${khojUrl}/api/health`, method: "GET", headers: headers })
|
||||
.then(response => {
|
||||
try {
|
||||
let response = await request({ url: `${khojUrl}/api/v1/user`, method: "GET", headers: headers })
|
||||
connectedToBackend = true;
|
||||
userEmail = JSON.parse(response)?.email;
|
||||
})
|
||||
.catch(error => {
|
||||
userInfo = JSON.parse(response);
|
||||
} catch (error) {
|
||||
connectedToBackend = false;
|
||||
console.log(`Khoj connection error:\n\n${error}`);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
let statusMessage: string = getBackendStatusMessage(connectedToBackend, userEmail, khojUrl, khojApiKey);
|
||||
let statusMessage: string = getBackendStatusMessage(connectedToBackend, userInfo?.email, khojUrl, khojApiKey);
|
||||
if (showNotice) new Notice(statusMessage);
|
||||
return { connectedToBackend, statusMessage, userEmail };
|
||||
return { connectedToBackend, statusMessage, userInfo };
|
||||
}
|
||||
|
||||
export function getBackendStatusMessage(
|
||||
connectedToServer: boolean,
|
||||
userEmail: string,
|
||||
userEmail: string | undefined,
|
||||
khojUrl: string,
|
||||
khojApiKey: string
|
||||
): string {
|
||||
|
|
Loading…
Reference in a new issue