From 3715beba7d1ee407315a9a7439c6ffce7f1676a2 Mon Sep 17 00:00:00 2001 From: cloud Date: Thu, 3 Aug 2023 13:01:47 +0800 Subject: [PATCH 1/2] feat: Now I can delete article from omnivore!!! --- src/api.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/main.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) 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; From 92e264a0698df94548a89ac66047501858462a2b Mon Sep 17 00:00:00 2001 From: cloud Date: Thu, 3 Aug 2023 13:05:53 +0800 Subject: [PATCH 2/2] chore: remove extra char --- src/api.ts | 63 ++++++++++++++++++++++++++--------------------------- src/main.ts | 11 ++++++---- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/api.ts b/src/api.ts index e7a9687..9d96d7f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,3 @@ -import { end } from "@popperjs/core"; import { requestUrl } from "obsidian"; export interface SearchResponse { @@ -168,37 +167,37 @@ export const loadArticles = async ( 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 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; - } + const jsonRes = res.json as DeleteArticleResponse; + if (jsonRes.data.setBookmarkArticle.bookmarkedArticle.id === articleId) { + return true; + } - return false + return false } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index db189bc..021bf9f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -70,7 +70,7 @@ export default class OmnivorePlugin extends Plugin { id: "deleteArticle", name: "Delete Current Article from Omnivore", callback: () => { - this.deleteCurrentArticle(this.app.workspace.getActiveFile()); + this.deleteCurrentArticle(this.app.workspace.getActiveFile()); } }) @@ -378,7 +378,7 @@ export default class OmnivorePlugin extends Plugin { } } - private deleteCurrentArticle(file: TFile | null) { + private async deleteCurrentArticle(file: TFile | null) { if(!file) { return } @@ -389,13 +389,16 @@ export default class OmnivorePlugin extends Plugin { } try{ - deleteArticleById(this.settings.endpoint, this.settings.apiKey, articleId) + const isDeleted = deleteArticleById(this.settings.endpoint, this.settings.apiKey, articleId) + if(!isDeleted) { + new Notice("Failed to delete article in Omnivore"); + } } catch (e) { new Notice("Failed to delete article in Omnivore"); console.error(e); } - this.app.vault.delete(file) + await this.app.vault.delete(file) } private async resetSyncingStateSetting() {