diff --git a/pages/anime/episodes.pixy b/pages/anime/episodes.pixy index eaa338aa..6976092b 100644 --- a/pages/anime/episodes.pixy +++ b/pages/anime/episodes.pixy @@ -27,3 +27,9 @@ component AnimeEpisodes(anime *arn.Anime, episodes []*arn.Episode, episodeToFrie .episode-friends each friend in episodeToFriends[episode.Number] reversed AvatarNoLink(friend) + + if user.Role == "editor" || user.Role == "admin" + .episode-buttons.mountable + button.action(data-action="syncEpisodes", data-trigger="click", data-anime-id=anime.ID) + Icon("refresh") + span Sync diff --git a/pages/anime/episodes.scarlet b/pages/anime/episodes.scarlet index 0a842dd6..472677f3 100644 --- a/pages/anime/episodes.scarlet +++ b/pages/anime/episodes.scarlet @@ -58,3 +58,6 @@ const episode-margin = 0.5rem width calc(avatar-size / 2) height calc(avatar-size / 2) margin-left -0.5rem + +.episode-buttons + margin-top content-padding diff --git a/pages/anime/sync.go b/pages/anime/sync.go new file mode 100644 index 00000000..6a15b234 --- /dev/null +++ b/pages/anime/sync.go @@ -0,0 +1,31 @@ +package anime + +import ( + "net/http" + + "github.com/animenotifier/notify.moe/arn" + + "github.com/aerogo/aero" +) + +// SyncEpisodes syncs the episodes with an external site. +func SyncEpisodes(ctx aero.Context) error { + user := arn.GetUserFromContext(ctx) + animeID := ctx.Get("id") + + if user == nil { + return ctx.Error(http.StatusUnauthorized, "Not logged in") + } + + if user.Role != "editor" && user.Role != "admin" { + return ctx.Error(http.StatusUnauthorized, "Not authorized") + } + + anime, err := arn.GetAnime(animeID) + + if err != nil { + return ctx.Error(http.StatusNotFound, "Anime not found", err) + } + + return anime.RefreshEpisodes() +} diff --git a/pages/index/apiroutes/apiroutes.go b/pages/index/apiroutes/apiroutes.go index fd078093..fb586ce2 100644 --- a/pages/index/apiroutes/apiroutes.go +++ b/pages/index/apiroutes/apiroutes.go @@ -5,6 +5,7 @@ import ( "github.com/aerogo/aero" "github.com/animenotifier/notify.moe/arn" + "github.com/animenotifier/notify.moe/pages/anime" "github.com/animenotifier/notify.moe/pages/animeimport" "github.com/animenotifier/notify.moe/pages/animelist" "github.com/animenotifier/notify.moe/pages/api" @@ -76,4 +77,5 @@ func Register(app *aero.Application) { // Jobs app.Post("/api/job/:job/start", jobs.Start) + app.Post("/api/anime/:id/sync-episodes", anime.SyncEpisodes) } diff --git a/scripts/Actions/Editor.ts b/scripts/Actions/Editor.ts index 6524f680..b3da6cef 100644 --- a/scripts/Actions/Editor.ts +++ b/scripts/Actions/Editor.ts @@ -105,3 +105,21 @@ export async function startJob(arn: AnimeNotifier, button: HTMLButtonElement) { await arn.post(`/api/job/${jobName}/start`) arn.reloadContent() } + +// Sync episodes +export async function syncEpisodes(arn: AnimeNotifier, button: HTMLButtonElement) { + if(!confirm("Are you sure you want to start the episode sync?")) { + return + } + + const animeId = button.dataset.animeId + + arn.statusMessage.showInfo("Started episode sync.", -1) + + try { + await arn.post(`/api/anime/${animeId}/sync-episodes`) + arn.statusMessage.showInfo("Finished episode sync.") + } catch(err) { + arn.statusMessage.showError(err) + } +}