mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 21:29:11 +00: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:
@@ -2,6 +2,15 @@ import { App, Notice, PluginSettingTab, Setting, TFile } from 'obsidian';
|
|||||||
import Khoj from 'src/main';
|
import Khoj from 'src/main';
|
||||||
import { canConnectToBackend, getBackendStatusMessage, updateContentIndex } from './utils';
|
import { canConnectToBackend, getBackendStatusMessage, updateContentIndex } from './utils';
|
||||||
|
|
||||||
|
export interface UserInfo {
|
||||||
|
username?: string;
|
||||||
|
photo?: string;
|
||||||
|
is_active?: boolean;
|
||||||
|
has_documents?: boolean;
|
||||||
|
email?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface KhojSetting {
|
export interface KhojSetting {
|
||||||
resultsCount: number;
|
resultsCount: number;
|
||||||
khojUrl: string;
|
khojUrl: string;
|
||||||
@@ -9,7 +18,7 @@ export interface KhojSetting {
|
|||||||
connectedToBackend: boolean;
|
connectedToBackend: boolean;
|
||||||
autoConfigure: boolean;
|
autoConfigure: boolean;
|
||||||
lastSync: Map<TFile, number>;
|
lastSync: Map<TFile, number>;
|
||||||
userEmail: string;
|
userInfo: UserInfo | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: KhojSetting = {
|
export const DEFAULT_SETTINGS: KhojSetting = {
|
||||||
@@ -19,7 +28,7 @@ export const DEFAULT_SETTINGS: KhojSetting = {
|
|||||||
connectedToBackend: false,
|
connectedToBackend: false,
|
||||||
autoConfigure: true,
|
autoConfigure: true,
|
||||||
lastSync: new Map(),
|
lastSync: new Map(),
|
||||||
userEmail: '',
|
userInfo: null,
|
||||||
}
|
}
|
||||||
|
|
||||||
export class KhojSettingTab extends PluginSettingTab {
|
export class KhojSettingTab extends PluginSettingTab {
|
||||||
@@ -38,7 +47,7 @@ export class KhojSettingTab extends PluginSettingTab {
|
|||||||
let backendStatusEl = containerEl.createEl('small', {
|
let backendStatusEl = containerEl.createEl('small', {
|
||||||
text: getBackendStatusMessage(
|
text: getBackendStatusMessage(
|
||||||
this.plugin.settings.connectedToBackend,
|
this.plugin.settings.connectedToBackend,
|
||||||
this.plugin.settings.userEmail,
|
this.plugin.settings.userInfo?.email,
|
||||||
this.plugin.settings.khojUrl,
|
this.plugin.settings.khojUrl,
|
||||||
this.plugin.settings.khojApiKey
|
this.plugin.settings.khojApiKey
|
||||||
)}
|
)}
|
||||||
@@ -55,7 +64,7 @@ export class KhojSettingTab extends PluginSettingTab {
|
|||||||
this.plugin.settings.khojUrl = value.trim().replace(/\/$/, '');
|
this.plugin.settings.khojUrl = value.trim().replace(/\/$/, '');
|
||||||
({
|
({
|
||||||
connectedToBackend: this.plugin.settings.connectedToBackend,
|
connectedToBackend: this.plugin.settings.connectedToBackend,
|
||||||
userEmail: this.plugin.settings.userEmail,
|
userInfo: this.plugin.settings.userInfo,
|
||||||
statusMessage: backendStatusMessage,
|
statusMessage: backendStatusMessage,
|
||||||
} = await canConnectToBackend(this.plugin.settings.khojUrl, this.plugin.settings.khojApiKey));
|
} = 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();
|
this.plugin.settings.khojApiKey = value.trim();
|
||||||
({
|
({
|
||||||
connectedToBackend: this.plugin.settings.connectedToBackend,
|
connectedToBackend: this.plugin.settings.connectedToBackend,
|
||||||
userEmail: this.plugin.settings.userEmail,
|
userInfo: this.plugin.settings.userInfo,
|
||||||
statusMessage: backendStatusMessage,
|
statusMessage: backendStatusMessage,
|
||||||
} = await canConnectToBackend(this.plugin.settings.khojUrl, this.plugin.settings.khojApiKey));
|
} = await canConnectToBackend(this.plugin.settings.khojUrl, this.plugin.settings.khojApiKey));
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { FileSystemAdapter, Notice, Vault, Modal, TFile, request } from 'obsidian';
|
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 {
|
export function getVaultAbsolutePath(vault: Vault): string {
|
||||||
let adaptor = vault.adapter;
|
let adaptor = vault.adapter;
|
||||||
@@ -173,31 +173,30 @@ export async function canConnectToBackend(
|
|||||||
khojUrl: string,
|
khojUrl: string,
|
||||||
khojApiKey: string,
|
khojApiKey: string,
|
||||||
showNotice: boolean = false
|
showNotice: boolean = false
|
||||||
): Promise<{ connectedToBackend: boolean; statusMessage: string, userEmail: string }> {
|
): Promise<{ connectedToBackend: boolean; statusMessage: string, userInfo: UserInfo | null }> {
|
||||||
let connectedToBackend = false;
|
let connectedToBackend = false;
|
||||||
let userEmail: string = '';
|
let userInfo: UserInfo | null = null;
|
||||||
|
|
||||||
if (!!khojUrl) {
|
if (!!khojUrl) {
|
||||||
let headers = !!khojApiKey ? { "Authorization": `Bearer ${khojApiKey}` } : undefined;
|
let headers = !!khojApiKey ? { "Authorization": `Bearer ${khojApiKey}` } : undefined;
|
||||||
await request({ url: `${khojUrl}/api/health`, method: "GET", headers: headers })
|
try {
|
||||||
.then(response => {
|
let response = await request({ url: `${khojUrl}/api/v1/user`, method: "GET", headers: headers })
|
||||||
connectedToBackend = true;
|
connectedToBackend = true;
|
||||||
userEmail = JSON.parse(response)?.email;
|
userInfo = JSON.parse(response);
|
||||||
})
|
} catch (error) {
|
||||||
.catch(error => {
|
|
||||||
connectedToBackend = false;
|
connectedToBackend = false;
|
||||||
console.log(`Khoj connection error:\n\n${error}`);
|
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);
|
if (showNotice) new Notice(statusMessage);
|
||||||
return { connectedToBackend, statusMessage, userEmail };
|
return { connectedToBackend, statusMessage, userInfo };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBackendStatusMessage(
|
export function getBackendStatusMessage(
|
||||||
connectedToServer: boolean,
|
connectedToServer: boolean,
|
||||||
userEmail: string,
|
userEmail: string | undefined,
|
||||||
khojUrl: string,
|
khojUrl: string,
|
||||||
khojApiKey: string
|
khojApiKey: string
|
||||||
): string {
|
): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user