Skip to content

Commit

Permalink
Merge pull request #108 from windily-cloud/feat-delete-article
Browse files Browse the repository at this point in the history
Feat delete article
  • Loading branch information
sywhb committed Aug 3, 2023
2 parents f87abff + 92e264a commit b52907d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
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

0 comments on commit b52907d

Please sign in to comment.