Added redirects for other list services

This commit is contained in:
Eduard Urbach 2018-04-04 16:45:02 +02:00
parent 74daef0df7
commit 534372de24
8 changed files with 53 additions and 38 deletions

24
pages/anime/redirect.go Normal file
View File

@ -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)
}
}

View File

@ -1,8 +1,6 @@
package home package home
import ( import (
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/pages/frontpage" "github.com/animenotifier/notify.moe/pages/frontpage"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
@ -16,12 +14,5 @@ func Get(ctx *aero.Context) string {
return frontpage.Get(ctx) return frontpage.Get(ctx)
} }
// Redirect return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching")
prefix := "/"
if strings.HasPrefix(ctx.URI(), "/_") {
prefix = "/_/"
}
return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching")
} }

View File

@ -125,6 +125,11 @@ func Configure(app *aero.Application) {
l.Page("/anime/:id/tracks", anime.Tracks) l.Page("/anime/:id/tracks", anime.Tracks)
l.Page("/anime/:id/episode/:episode-number", episode.Get) 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 // Edit anime
l.Page("/anime/:id/edit", editanime.Main) l.Page("/anime/:id/edit", editanime.Main)
l.Page("/anime/:id/edit/images", editanime.Images) l.Page("/anime/:id/edit/images", editanime.Images)

View File

@ -2,7 +2,6 @@ package listimportanilist
import ( import (
"net/http" "net/http"
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/anilist" "github.com/animenotifier/anilist"
@ -67,14 +66,7 @@ func Finish(ctx *aero.Context) string {
animeList.Save() animeList.Save()
// Redirect return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching")
prefix := "/"
if strings.HasPrefix(ctx.URI(), "/_") {
prefix = "/_/"
}
return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching")
} }
// getMatches finds and returns all matches for the logged in user. // getMatches finds and returns all matches for the logged in user.

View File

@ -2,7 +2,6 @@ package listimportkitsu
import ( import (
"net/http" "net/http"
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
@ -80,14 +79,7 @@ func Finish(ctx *aero.Context) string {
animeList.Save() animeList.Save()
// Redirect return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching")
prefix := "/"
if strings.HasPrefix(ctx.URI(), "/_") {
prefix = "/_/"
}
return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching")
} }
// getMatches finds and returns all matches for the logged in user. // getMatches finds and returns all matches for the logged in user.

View File

@ -3,7 +3,6 @@ package listimportmyanimelist
import ( import (
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
@ -76,14 +75,7 @@ func Finish(ctx *aero.Context) string {
animeList.Save() animeList.Save()
// Redirect return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching")
prefix := "/"
if strings.HasPrefix(ctx.URI(), "/_") {
prefix = "/_/"
}
return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching")
} }
// getMatches finds and returns all matches for the logged in user. // getMatches finds and returns all matches for the logged in user.

View File

@ -29,7 +29,7 @@ export async function importKitsuAnime(arn: AnimeNotifier, button: HTMLButtonEle
}) })
if(response.ok) { if(response.ok) {
newTab.location.href = `/anime/${animeId}` newTab.location.href = `/kitsu/anime/${animeId}`
arn.reloadContent() arn.reloadContent()
} else { } else {
arn.statusMessage.showError(await response.text()) arn.statusMessage.showError(await response.text())

19
utils/SmartRedirect.go Normal file
View File

@ -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)
}