Skip to content

Commit

Permalink
feat: Now I can delete article from omnivore!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
windily-cloud committed Aug 3, 2023
1 parent f87abff commit 3715beb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
51 changes: 49 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { end } from "@popperjs/core";
import { requestUrl } from "obsidian";

export interface SearchResponse {
Expand All @@ -11,6 +12,16 @@ export interface SearchResponse {
};
}

export interface DeleteArticleResponse {
"data": {
"setBookmarkArticle": {
"bookmarkedArticle": {
"id": string
}
}
}
}

export enum PageType {
Article = "ARTICLE",
Book = "BOOK",
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
30 changes: 29 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3715beb

Please sign in to comment.