diff --git a/jobs/sync-anime/main.go b/jobs/sync-anime/main.go index 2f6fa7ec..8e40361e 100644 --- a/jobs/sync-anime/main.go +++ b/jobs/sync-anime/main.go @@ -26,7 +26,16 @@ func main() { } func sync(data *kitsu.Anime) { - anime := arn.Anime{} + anime, err := arn.GetAnime(data.ID) + + if err != nil { + if strings.Contains(err.Error(), "not found") { + anime = &arn.Anime{} + } else { + panic(err) + } + } + attr := data.Attributes // General data @@ -48,6 +57,10 @@ func sync(data *kitsu.Anime) { anime.Status = attr.Status anime.Summary = arn.FixAnimeDescription(attr.Synopsis) + if anime.Mappings == nil { + anime.Mappings = []*arn.Mapping{} + } + // NSFW if attr.Nsfw { anime.NSFW = 1 @@ -75,7 +88,7 @@ func sync(data *kitsu.Anime) { } // Save in database - err := anime.Save() + err = anime.Save() status := "" if err == nil { diff --git a/main.go b/main.go index 7dcf0e0c..c5c0127e 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "github.com/animenotifier/notify.moe/pages/animelist" "github.com/animenotifier/notify.moe/pages/animelistitem" "github.com/animenotifier/notify.moe/pages/dashboard" + "github.com/animenotifier/notify.moe/pages/editanime" "github.com/animenotifier/notify.moe/pages/embed" "github.com/animenotifier/notify.moe/pages/forum" "github.com/animenotifier/notify.moe/pages/forums" @@ -58,6 +59,7 @@ func configure(app *aero.Application) *aero.Application { app.Ajax("/", dashboard.Get) app.Ajax("/anime", popularanime.Get) app.Ajax("/anime/:id", anime.Get) + app.Ajax("/anime/:id/edit", editanime.Get) app.Ajax("/forum", forums.Get) app.Ajax("/forum/:tag", forum.Get) app.Ajax("/threads/:id", threads.Get) diff --git a/pages/anime/anime.pixy b/pages/anime/anime.pixy index 3df5117d..0b311b1e 100644 --- a/pages/anime/anime.pixy +++ b/pages/anime/anime.pixy @@ -21,6 +21,11 @@ component Anime(anime *arn.Anime, tracks []*arn.SoundTrack, user *arn.User) if user != nil .buttons.anime-actions + if user.Role == "editor" || user.Role == "admin" + a.button.ajax(href=anime.Link() + "/edit") + Icon("database") + span Edit anime + if user.AnimeList().Contains(anime.ID) a.button.ajax(href="/+" + user.Nick + "/animelist/" + anime.ID) Icon("pencil") @@ -152,6 +157,11 @@ component Anime(anime *arn.Anime, tracks []*arn.SoundTrack, user *arn.User) a.light-button(href="https://kitsu.io/anime/" + anime.ID, target="_blank", rel="noopener") Icon("external-link") span Kitsu + + each mapping in anime.Mappings + a.light-button(href=mapping.Link(), target="_blank", rel="noopener") + Icon("external-link") + span= mapping.Name() //- if providers.HummingBird //- a.light-button(href="https://hummingbird.me/anime/" + providers.HummingBird.providerId, target="_blank") HummingBird diff --git a/pages/editanime/editanime.go b/pages/editanime/editanime.go new file mode 100644 index 00000000..592d979f --- /dev/null +++ b/pages/editanime/editanime.go @@ -0,0 +1,28 @@ +package editanime + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" +) + +// Get anime edit page. +func Get(ctx *aero.Context) string { + id := ctx.Get("id") + user := utils.GetUser(ctx) + + if user == nil || (user.Role != "editor" && user.Role != "admin") { + return ctx.Error(http.StatusBadRequest, "Not logged in or not auhorized to edit this anime", nil) + } + + anime, err := arn.GetAnime(id) + + if err != nil { + return ctx.Error(http.StatusNotFound, "Anime not found", err) + } + + return ctx.HTML(components.EditAnime(anime)) +} diff --git a/pages/editanime/editanime.pixy b/pages/editanime/editanime.pixy new file mode 100644 index 00000000..1c1cd3b9 --- /dev/null +++ b/pages/editanime/editanime.pixy @@ -0,0 +1,7 @@ +component EditAnime(anime *arn.Anime) + h2= anime.Title.Canonical + + .widgets + .widget(data-api="/api/anime/" + anime.ID) + h3.anime-section-name Mappings + InputText("ShoboiID", anime.GetMapping("shoboi/anime"), "Shoboi TID", "TID on http://cal.syoboi.jp") \ No newline at end of file diff --git a/patches/add-mappings/main.go b/patches/add-mappings/main.go new file mode 100644 index 00000000..70cb7cd4 --- /dev/null +++ b/patches/add-mappings/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + + "github.com/animenotifier/arn" +) + +var mappings = map[string]arn.Mapping{ + "13055": arn.Mapping{ + Service: "shoboi/anime", + ServiceID: "4528", + }, +} + +func main() { + for animeID, mapping := range mappings { + anime, err := arn.GetAnime(animeID) + + if err != nil { + panic(err) + } + + fmt.Println(anime.ID, "=", mapping.Service, mapping.ServiceID) + anime.AddMapping(mapping.Service, mapping.ServiceID) + anime.Save() + } +}