From 8cb44131cf7432e65a8f2d384c4c8bc02333b9a1 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 17 Apr 2018 12:27:54 +0200 Subject: [PATCH] Added deletion to Kitsu imports --- pages/animeimport/deletekitsu.go | 43 +++++++++++++++++++ .../animeimport/{animeimport.go => kitsu.go} | 0 pages/editor/kitsu.go | 7 ++- pages/editor/kitsu.pixy | 9 ++-- pages/index.go | 1 + scripts/Actions/Editor.ts | 15 +++++++ scripts/AnimeNotifier.ts | 4 +- 7 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 pages/animeimport/deletekitsu.go rename pages/animeimport/{animeimport.go => kitsu.go} (100%) diff --git a/pages/animeimport/deletekitsu.go b/pages/animeimport/deletekitsu.go new file mode 100644 index 00000000..c754caa4 --- /dev/null +++ b/pages/animeimport/deletekitsu.go @@ -0,0 +1,43 @@ +package animeimport + +import ( + "net/http" + + "github.com/animenotifier/arn" + + "github.com/aerogo/aero" + "github.com/animenotifier/notify.moe/utils" +) + +// DeleteKitsu marks an anime for deletion. +func DeleteKitsu(ctx *aero.Context) string { + id := ctx.Get("id") + + // Is the user allowed to delete? + user := utils.GetUser(ctx) + + if user == nil || (user.Role != "editor" && user.Role != "admin") { + return ctx.Error(http.StatusUnauthorized, "Not authorized", nil) + } + + // Check that the anime really exists + kitsuAnimeObj, err := arn.Kitsu.Get("Anime", id) + + if kitsuAnimeObj == nil { + return ctx.Error(http.StatusNotFound, "Kitsu anime not found", err) + } + + // Add to deleted IDs list + deletedKitsuAnime, err := arn.GetIDList("deleted kitsu anime") + + if err != nil { + deletedKitsuAnime = arn.IDList{} + } + + deletedKitsuAnime = deletedKitsuAnime.Append(id) + + // Save in database + arn.DB.Set("IDList", "deleted kitsu anime", &deletedKitsuAnime) + + return "" +} diff --git a/pages/animeimport/animeimport.go b/pages/animeimport/kitsu.go similarity index 100% rename from pages/animeimport/animeimport.go rename to pages/animeimport/kitsu.go diff --git a/pages/editor/kitsu.go b/pages/editor/kitsu.go index e93c6b02..e61fb68b 100644 --- a/pages/editor/kitsu.go +++ b/pages/editor/kitsu.go @@ -14,9 +14,14 @@ import ( func NewKitsuAnime(ctx *aero.Context) string { user := utils.GetUser(ctx) finder := arn.NewAnimeFinder("kitsu/anime") + deletedIDs, err := arn.GetIDList("deleted kitsu anime") + + if err != nil { + deletedIDs = arn.IDList{} + } animes := arn.FilterKitsuAnime(func(anime *kitsu.Anime) bool { - return finder.GetAnime(anime.ID) == nil + return finder.GetAnime(anime.ID) == nil && !arn.Contains(deletedIDs, anime.ID) }) sort.Slice(animes, func(i, j int) bool { diff --git a/pages/editor/kitsu.pixy b/pages/editor/kitsu.pixy index 59c3d069..83c7d61b 100644 --- a/pages/editor/kitsu.pixy +++ b/pages/editor/kitsu.pixy @@ -28,6 +28,9 @@ component NewKitsuAnime(animes []*kitsu.Anime, url string, user *arn.User) if len(anime.Attributes.StartDate) >= 4 span= anime.Attributes.StartDate[:4] td - button.action(data-action="importKitsuAnime", data-trigger="click", data-id=anime.ID) - Icon("download") - span Import \ No newline at end of file + .buttons + button.action(data-action="importKitsuAnime", data-trigger="click", data-id=anime.ID) + RawIcon("download") + + button.action(data-action="deleteKitsuAnime", data-trigger="click", data-id=anime.ID) + RawIcon("trash") \ No newline at end of file diff --git a/pages/index.go b/pages/index.go index e4a76c14..c1721622 100644 --- a/pages/index.go +++ b/pages/index.go @@ -266,6 +266,7 @@ func Configure(app *aero.Application) { // Import anime app.Post("/api/import/kitsu/anime/:id", animeimport.Kitsu) + app.Post("/api/delete/kitsu/anime/:id", animeimport.DeleteKitsu) // Upload app.Post("/api/upload/avatar", upload.Avatar) diff --git a/scripts/Actions/Editor.ts b/scripts/Actions/Editor.ts index 4bc545b9..e765d73f 100644 --- a/scripts/Actions/Editor.ts +++ b/scripts/Actions/Editor.ts @@ -23,6 +23,10 @@ export function newAnimeDiffIgnore(arn: AnimeNotifier, button: HTMLButtonElement // Import Kitsu anime export async function importKitsuAnime(arn: AnimeNotifier, button: HTMLButtonElement) { + if(!confirm("Are you sure you want to import this anime?")) { + return + } + let newTab = window.open() let animeId = button.dataset.id let response = await fetch(`/api/import/kitsu/anime/${animeId}`, { @@ -38,6 +42,17 @@ export async function importKitsuAnime(arn: AnimeNotifier, button: HTMLButtonEle } } +// Delete Kitsu anime +export async function deleteKitsuAnime(arn: AnimeNotifier, button: HTMLButtonElement) { + if(!confirm("Are you sure you want to delete this anime?")) { + return + } + + let animeId = button.dataset.id + await arn.post(`/api/delete/kitsu/anime/${animeId}`) + arn.reloadContent() +} + // Multi-search anime export async function multiSearchAnime(arn: AnimeNotifier, textarea: HTMLTextAreaElement) { let results = document.getElementById("multi-search-anime") as HTMLDivElement diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index ba4ecca4..6907e1af 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -804,12 +804,12 @@ export default class AnimeNotifier { return Diff.innerHTML(element, html) } - post(url: string, body: any) { + post(url: string, body?: any) { if(this.isLoading) { return Promise.resolve(null) } - if(typeof body !== "string") { + if(body !== undefined && typeof body !== "string") { body = JSON.stringify(body) }