From 534372de249c4a3f33f7155d693de98f36e15048 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 4 Apr 2018 16:45:02 +0200 Subject: [PATCH] Added redirects for other list services --- pages/anime/redirect.go | 24 +++++++++++++++++++ pages/home/home.go | 11 +-------- pages/index.go | 5 ++++ pages/listimport/listimportanilist/anilist.go | 10 +------- pages/listimport/listimportkitsu/kitsu.go | 10 +------- .../listimportmyanimelist/myanimelist.go | 10 +------- scripts/Actions/Editor.ts | 2 +- utils/SmartRedirect.go | 19 +++++++++++++++ 8 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 pages/anime/redirect.go create mode 100644 utils/SmartRedirect.go diff --git a/pages/anime/redirect.go b/pages/anime/redirect.go new file mode 100644 index 00000000..1a4290ac --- /dev/null +++ b/pages/anime/redirect.go @@ -0,0 +1,24 @@ +package anime + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/utils" +) + +// RedirectByMapping redirects to the anime with the given mapping ID. +func RedirectByMapping(mappingName string) func(*aero.Context) string { + return func(ctx *aero.Context) string { + id := ctx.Get("id") + finder := arn.NewAnimeFinder(mappingName) + anime := finder.GetAnime(id) + + if anime == nil { + return ctx.Error(http.StatusNotFound, "Anime not found", nil) + } + + return utils.SmartRedirect(ctx, "/anime/"+anime.ID) + } +} diff --git a/pages/home/home.go b/pages/home/home.go index 91cd137c..7a6d82eb 100644 --- a/pages/home/home.go +++ b/pages/home/home.go @@ -1,8 +1,6 @@ package home import ( - "strings" - "github.com/aerogo/aero" "github.com/animenotifier/notify.moe/pages/frontpage" "github.com/animenotifier/notify.moe/utils" @@ -16,12 +14,5 @@ func Get(ctx *aero.Context) string { return frontpage.Get(ctx) } - // Redirect - prefix := "/" - - if strings.HasPrefix(ctx.URI(), "/_") { - prefix = "/_/" - } - - return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching") + return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching") } diff --git a/pages/index.go b/pages/index.go index 78876360..af9a0ea5 100644 --- a/pages/index.go +++ b/pages/index.go @@ -125,6 +125,11 @@ func Configure(app *aero.Application) { l.Page("/anime/:id/tracks", anime.Tracks) l.Page("/anime/:id/episode/:episode-number", episode.Get) + // Anime redirects + l.Page("/kitsu/anime/:id", anime.RedirectByMapping("kitsu/anime")) + l.Page("/mal/anime/:id", anime.RedirectByMapping("myanimelist/anime")) + l.Page("/anilist/anime/:id", anime.RedirectByMapping("anilist/anime")) + // Edit anime l.Page("/anime/:id/edit", editanime.Main) l.Page("/anime/:id/edit/images", editanime.Images) diff --git a/pages/listimport/listimportanilist/anilist.go b/pages/listimport/listimportanilist/anilist.go index adcc7cd7..ef64528f 100644 --- a/pages/listimport/listimportanilist/anilist.go +++ b/pages/listimport/listimportanilist/anilist.go @@ -2,7 +2,6 @@ package listimportanilist import ( "net/http" - "strings" "github.com/aerogo/aero" "github.com/animenotifier/anilist" @@ -67,14 +66,7 @@ func Finish(ctx *aero.Context) string { animeList.Save() - // Redirect - prefix := "/" - - if strings.HasPrefix(ctx.URI(), "/_") { - prefix = "/_/" - } - - return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching") + return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching") } // getMatches finds and returns all matches for the logged in user. diff --git a/pages/listimport/listimportkitsu/kitsu.go b/pages/listimport/listimportkitsu/kitsu.go index 20f6ec72..dc4245c2 100644 --- a/pages/listimport/listimportkitsu/kitsu.go +++ b/pages/listimport/listimportkitsu/kitsu.go @@ -2,7 +2,6 @@ package listimportkitsu import ( "net/http" - "strings" "github.com/aerogo/aero" "github.com/animenotifier/arn" @@ -80,14 +79,7 @@ func Finish(ctx *aero.Context) string { animeList.Save() - // Redirect - prefix := "/" - - if strings.HasPrefix(ctx.URI(), "/_") { - prefix = "/_/" - } - - return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching") + return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching") } // getMatches finds and returns all matches for the logged in user. diff --git a/pages/listimport/listimportmyanimelist/myanimelist.go b/pages/listimport/listimportmyanimelist/myanimelist.go index 6f025c26..b770200d 100644 --- a/pages/listimport/listimportmyanimelist/myanimelist.go +++ b/pages/listimport/listimportmyanimelist/myanimelist.go @@ -3,7 +3,6 @@ package listimportmyanimelist import ( "net/http" "strconv" - "strings" "github.com/aerogo/aero" "github.com/animenotifier/arn" @@ -76,14 +75,7 @@ func Finish(ctx *aero.Context) string { animeList.Save() - // Redirect - prefix := "/" - - if strings.HasPrefix(ctx.URI(), "/_") { - prefix = "/_/" - } - - return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching") + return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching") } // getMatches finds and returns all matches for the logged in user. diff --git a/scripts/Actions/Editor.ts b/scripts/Actions/Editor.ts index 4d530dae..f1fc94f9 100644 --- a/scripts/Actions/Editor.ts +++ b/scripts/Actions/Editor.ts @@ -29,7 +29,7 @@ export async function importKitsuAnime(arn: AnimeNotifier, button: HTMLButtonEle }) if(response.ok) { - newTab.location.href = `/anime/${animeId}` + newTab.location.href = `/kitsu/anime/${animeId}` arn.reloadContent() } else { arn.statusMessage.showError(await response.text()) diff --git a/utils/SmartRedirect.go b/utils/SmartRedirect.go new file mode 100644 index 00000000..ae5c5f0f --- /dev/null +++ b/utils/SmartRedirect.go @@ -0,0 +1,19 @@ +package utils + +import ( + "strings" + + "github.com/aerogo/aero" +) + +// SmartRedirect automatically adds the /_ prefix to the URI if required. +func SmartRedirect(ctx *aero.Context, uri string) string { + // Redirect + prefix := "" + + if strings.HasPrefix(ctx.URI(), "/_") { + prefix = "/_" + } + + return ctx.Redirect(prefix + uri) +}