diff --git a/src/api.ts b/src/api.ts index 0067f2a..e7a9687 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,3 +1,4 @@ +import { end } from "@popperjs/core"; import { requestUrl } from "obsidian"; export interface SearchResponse { @@ -11,6 +12,16 @@ export interface SearchResponse { }; } +export interface DeleteArticleResponse { + "data": { + "setBookmarkArticle": { + "bookmarkedArticle": { + "id": string + } + } + } +} + export enum PageType { Article = "ARTICLE", Book = "BOOK", @@ -140,8 +151,7 @@ export const loadArticles = async ( variables: { after: `${after}`, first, - query: `${ - updatedAt ? "updated:" + updatedAt : "" + query: `${updatedAt ? "updated:" + updatedAt : "" } sort:saved-asc ${query}`, includeContent, format, @@ -155,3 +165,40 @@ export const loadArticles = async ( return [articles, jsonRes.data.search.pageInfo.hasNextPage]; }; + + +export const deleteArticleById = async (endpoint: string, apiKey: string, articleId: string) => { + const res = await requestUrl({ + url: endpoint, + headers: requestHeaders(apiKey), + body: JSON.stringify({ + query: ` + mutation SetBookmarkArticle($input: SetBookmarkArticleInput!) { + setBookmarkArticle(input: $input) { + ... on SetBookmarkArticleSuccess { + bookmarkedArticle { + id + } + } + ... on SetBookmarkArticleError { + errorCodes + } + } + }`, + variables: { + input: { + "articleID": articleId, + "bookmark": false + } + }, + }), + method: "POST", + }); + + const jsonRes = res.json as DeleteArticleResponse; + if (jsonRes.data.setBookmarkArticle.bookmarkedArticle.id === articleId) { + return true; + } + + return false +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 6e8a82f..db189bc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,7 +12,7 @@ import { TFile, TFolder, } from "obsidian"; -import { Article, loadArticles, PageType } from "./api"; +import { Article, deleteArticleById, loadArticles, PageType } from "./api"; import { DEFAULT_SETTINGS, Filter, @@ -66,6 +66,14 @@ export default class OmnivorePlugin extends Plugin { }, }); + this.addCommand({ + id: "deleteArticle", + name: "Delete Current Article from Omnivore", + callback: () => { + this.deleteCurrentArticle(this.app.workspace.getActiveFile()); + } + }) + this.addCommand({ id: "resync", name: "Resync all articles", @@ -370,6 +378,26 @@ export default class OmnivorePlugin extends Plugin { } } + private deleteCurrentArticle(file: TFile | null) { + if(!file) { + return + } + //use frontmatter id to find the file + const articleId = this.app.metadataCache.getFileCache(file)?.frontmatter?.id + if (!articleId) { + new Notice("Failed to delete article: article id not found"); + } + + try{ + deleteArticleById(this.settings.endpoint, this.settings.apiKey, articleId) + } catch (e) { + new Notice("Failed to delete article in Omnivore"); + console.error(e); + } + + this.app.vault.delete(file) + } + private async resetSyncingStateSetting() { this.settings.syncing = false; this.settings.intervalId = 0;