Skip to content

Commit 3715beb

Browse files
committed
feat: Now I can delete article from omnivore!!!
1 parent f87abff commit 3715beb

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/api.ts

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { end } from "@popperjs/core";
12
import { requestUrl } from "obsidian";
23

34
export interface SearchResponse {
@@ -11,6 +12,16 @@ export interface SearchResponse {
1112
};
1213
}
1314

15+
export interface DeleteArticleResponse {
16+
"data": {
17+
"setBookmarkArticle": {
18+
"bookmarkedArticle": {
19+
"id": string
20+
}
21+
}
22+
}
23+
}
24+
1425
export enum PageType {
1526
Article = "ARTICLE",
1627
Book = "BOOK",
@@ -140,8 +151,7 @@ export const loadArticles = async (
140151
variables: {
141152
after: `${after}`,
142153
first,
143-
query: `${
144-
updatedAt ? "updated:" + updatedAt : ""
154+
query: `${updatedAt ? "updated:" + updatedAt : ""
145155
} sort:saved-asc ${query}`,
146156
includeContent,
147157
format,
@@ -155,3 +165,40 @@ export const loadArticles = async (
155165

156166
return [articles, jsonRes.data.search.pageInfo.hasNextPage];
157167
};
168+
169+
170+
export const deleteArticleById = async (endpoint: string, apiKey: string, articleId: string) => {
171+
const res = await requestUrl({
172+
url: endpoint,
173+
headers: requestHeaders(apiKey),
174+
body: JSON.stringify({
175+
query: `
176+
mutation SetBookmarkArticle($input: SetBookmarkArticleInput!) {
177+
setBookmarkArticle(input: $input) {
178+
... on SetBookmarkArticleSuccess {
179+
bookmarkedArticle {
180+
id
181+
}
182+
}
183+
... on SetBookmarkArticleError {
184+
errorCodes
185+
}
186+
}
187+
}`,
188+
variables: {
189+
input: {
190+
"articleID": articleId,
191+
"bookmark": false
192+
}
193+
},
194+
}),
195+
method: "POST",
196+
});
197+
198+
const jsonRes = res.json as DeleteArticleResponse;
199+
if (jsonRes.data.setBookmarkArticle.bookmarkedArticle.id === articleId) {
200+
return true;
201+
}
202+
203+
return false
204+
}

src/main.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
TFile,
1313
TFolder,
1414
} from "obsidian";
15-
import { Article, loadArticles, PageType } from "./api";
15+
import { Article, deleteArticleById, loadArticles, PageType } from "./api";
1616
import {
1717
DEFAULT_SETTINGS,
1818
Filter,
@@ -66,6 +66,14 @@ export default class OmnivorePlugin extends Plugin {
6666
},
6767
});
6868

69+
this.addCommand({
70+
id: "deleteArticle",
71+
name: "Delete Current Article from Omnivore",
72+
callback: () => {
73+
this.deleteCurrentArticle(this.app.workspace.getActiveFile());
74+
}
75+
})
76+
6977
this.addCommand({
7078
id: "resync",
7179
name: "Resync all articles",
@@ -370,6 +378,26 @@ export default class OmnivorePlugin extends Plugin {
370378
}
371379
}
372380

381+
private deleteCurrentArticle(file: TFile | null) {
382+
if(!file) {
383+
return
384+
}
385+
//use frontmatter id to find the file
386+
const articleId = this.app.metadataCache.getFileCache(file)?.frontmatter?.id
387+
if (!articleId) {
388+
new Notice("Failed to delete article: article id not found");
389+
}
390+
391+
try{
392+
deleteArticleById(this.settings.endpoint, this.settings.apiKey, articleId)
393+
} catch (e) {
394+
new Notice("Failed to delete article in Omnivore");
395+
console.error(e);
396+
}
397+
398+
this.app.vault.delete(file)
399+
}
400+
373401
private async resetSyncingStateSetting() {
374402
this.settings.syncing = false;
375403
this.settings.intervalId = 0;

0 commit comments

Comments
 (0)