Skip to content

Commit

Permalink
Misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Jul 4, 2023
1 parent 8e776fa commit cf8cb16
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 164 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-zotero-desktop-connector",
"name": "Zotero Integration",
"version": "3.0.1",
"version": "3.0.2",
"minAppVersion": "1.1.1",
"description": "Insert and import citations, bibliographies, notes, and PDF annotations from Zotero.",
"author": "mgmeyers",
Expand Down
1 change: 1 addition & 0 deletions src/bbt/cayw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function getCiteKeyFromAny(item: any): CiteKey | null {

let cachedIsRunning = false;
let lastCheck = 0;

export async function isZoteroRunning(
database: DatabaseWithPort,
silent?: boolean
Expand Down
116 changes: 0 additions & 116 deletions src/bbt/getCiteKeyExport.ts

This file was deleted.

116 changes: 114 additions & 2 deletions src/bbt/jsonRPC.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Notice, htmlToMarkdown, moment, request } from 'obsidian';

import { padNumber } from '../helpers';
import { DatabaseWithPort } from '../types';
import { CiteKeyExport, DatabaseWithPort } from '../types';
import { LoadingModal } from './LoadingModal';
import { CiteKey, getCiteKeyFromAny } from './cayw';
import { CiteKey, getCiteKeyFromAny, isZoteroRunning } from './cayw';
import { defaultHeaders, getPort } from './helpers';

export async function getNotesFromCiteKeys(
Expand Down Expand Up @@ -521,3 +521,115 @@ export async function execSearch(term: string, database: DatabaseWithPort) {
return null;
}
}


const translatorId = 'f4b52ab0-f878-4556-85a0-c7aeedd09dfc';
export async function getCiteKeyExport(
database: DatabaseWithPort,
groupId: string,
groupName: string
) {
try {
const res = await request({
method: 'GET',
url: `http://127.0.0.1:${getPort(
database.database,
database.port
)}/better-bibtex/export/library?/${groupId}/${groupName}.${translatorId}`,
headers: defaultHeaders,
});

const entries = JSON.parse(res);

return Array.isArray(entries)
? entries
.map((e) => {
const out: Record<string, any> = {
libraryID: Number(groupId),
};

if (e['citation-key']) {
out.citekey = e['citation-key'];
} else {
return null;
}

if (e['title']) {
out.title = e['title'];
} else {
return null;
}

return out as { libraryID: number; citekey: string; title: string };
})
.filter((k) => !!k)
: null;
} catch (e) {
return null;
}
}

export async function getUserGroups(database: DatabaseWithPort) {
let res: string;
try {
res = await request({
method: 'POST',
url: `http://127.0.0.1:${getPort(
database.database,
database.port
)}/better-bibtex/json-rpc`,
body: JSON.stringify({
jsonrpc: '2.0',
method: 'user.groups',
params: [],
}),
headers: defaultHeaders,
});
} catch (e) {
console.error(e);
return null;
}

try {
return JSON.parse(res).result;
} catch (e) {
console.error(e);
return null;
}
}

let cachedKeys: CiteKeyExport[] = [];
let lastCheck = 0;

export async function getAllCiteKeys(
database: DatabaseWithPort,
force?: boolean
) {
if (!force && cachedKeys.length && Date.now() - lastCheck < 1000 * 60 * 60) {
return { citekeys: cachedKeys, fromCache: true };
}

if (!(await isZoteroRunning(database, true))) {
return { citekeys: cachedKeys, fromCache: true };
}

const allKeys: CiteKeyExport[] = [];
const userGroups = await getUserGroups(database);

if (!userGroups) {
return { citekeys: cachedKeys, fromCache: true };
}

for (const group of userGroups) {
const keys = await getCiteKeyExport(database, group.id, group.name);

if (keys) {
allKeys.push(...keys);
}
}

cachedKeys = allKeys;
lastCheck = Date.now();

return { citekeys: allKeys, fromCache: false };
}
2 changes: 1 addition & 1 deletion src/citeSuggest/citeSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
debounce,
} from 'obsidian';
import { isZoteroRunning } from 'src/bbt/cayw';
import { getAllCiteKeys } from 'src/bbt/getCiteKeyExport';
import { getAllCiteKeys } from 'src/bbt/jsonRPC';
import ZoteroConnector from 'src/main';
import { CiteKeyExport } from 'src/types';

Expand Down
Loading

0 comments on commit cf8cb16

Please sign in to comment.