Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat delete article #108

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 +150,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 +164,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
}
33 changes: 32 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,29 @@ export default class OmnivorePlugin extends Plugin {
}
}

private async 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{
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);
}

await this.app.vault.delete(file)
}

private async resetSyncingStateSetting() {
this.settings.syncing = false;
this.settings.intervalId = 0;
Expand Down
Loading