99 lines
2.4 KiB
Go
Raw Normal View History

2016-11-19 14:54:31 +00:00
package anime
import (
2017-06-19 18:59:02 +00:00
"net/http"
2016-11-19 14:54:31 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
2017-06-19 18:59:02 +00:00
"github.com/animenotifier/notify.moe/utils"
2016-11-19 14:54:31 +00:00
)
const maxEpisodes = 26
const maxEpisodesLongSeries = 5
2017-07-06 19:38:19 +00:00
const maxDescriptionLength = 170
2017-06-16 23:25:02 +00:00
// Get anime page.
2016-11-19 14:54:31 +00:00
func Get(ctx *aero.Context) string {
2017-06-03 23:17:00 +00:00
id := ctx.Get("id")
2017-06-19 18:59:02 +00:00
user := utils.GetUser(ctx)
2016-11-19 14:54:31 +00:00
anime, err := arn.GetAnime(id)
if err != nil {
2017-06-19 18:59:02 +00:00
return ctx.Error(http.StatusNotFound, "Anime not found", err)
2016-11-19 14:54:31 +00:00
}
2017-06-27 23:05:39 +00:00
tracks, err := arn.GetSoundTracksByTag("anime:" + anime.ID)
if err != nil {
return ctx.Error(http.StatusNotFound, "Error fetching soundtracks", err)
}
episodesReversed := false
if len(anime.Episodes().Items) > maxEpisodes {
episodesReversed = true
anime.Episodes().Items = anime.Episodes().Items[len(anime.Episodes().Items)-maxEpisodesLongSeries:]
for i, j := 0, len(anime.Episodes().Items)-1; i < j; i, j = i+1, j-1 {
anime.Episodes().Items[i], anime.Episodes().Items[j] = anime.Episodes().Items[j], anime.Episodes().Items[i]
}
}
2017-07-21 09:25:53 +00:00
// Friends watching
var friends []*arn.User
2017-07-21 09:43:54 +00:00
friendsAnimeListItems := map[*arn.User]*arn.AnimeListItem{}
2017-07-21 09:25:53 +00:00
if user != nil {
friends = user.Follows().Users()
deleted := 0
for i := range friends {
j := i - deleted
2017-07-21 09:43:54 +00:00
friendAnimeList := friends[j].AnimeList()
obj, err := friendAnimeList.Get(anime.ID)
if err != nil {
2017-07-21 09:25:53 +00:00
friends = friends[:j+copy(friends[j:], friends[j+1:])]
deleted++
2017-07-21 09:43:54 +00:00
} else {
friendsAnimeListItems[friends[j]] = obj.(*arn.AnimeListItem)
2017-07-21 09:25:53 +00:00
}
}
arn.SortUsersLastSeen(friends)
}
2017-07-06 18:33:46 +00:00
// Open Graph
2017-07-06 19:38:19 +00:00
description := anime.Summary
if len(description) > maxDescriptionLength {
description = description[:maxDescriptionLength-3] + "..."
}
2017-07-06 18:56:37 +00:00
openGraph := &arn.OpenGraph{
Tags: map[string]string{
"og:title": anime.Title.Canonical,
"og:image": anime.Image.Large,
"og:url": "https://" + ctx.App.Config.Domain + anime.Link(),
"og:site_name": "notify.moe",
2017-07-06 19:38:19 +00:00
"og:description": description,
2017-07-06 18:56:37 +00:00
},
Meta: map[string]string{
2017-07-06 19:38:19 +00:00
"description": description,
"keywords": anime.Title.Canonical + ",anime",
2017-07-06 18:56:37 +00:00
},
2017-07-06 18:33:46 +00:00
}
switch anime.Type {
case "tv":
2017-07-06 18:56:37 +00:00
openGraph.Tags["og:type"] = "video.tv_show"
2017-07-06 18:33:46 +00:00
case "movie":
2017-07-06 18:56:37 +00:00
openGraph.Tags["og:type"] = "video.movie"
2017-07-06 18:33:46 +00:00
}
2017-07-06 18:56:37 +00:00
ctx.Data = openGraph
2017-07-06 18:33:46 +00:00
2017-07-21 09:43:54 +00:00
return ctx.HTML(components.Anime(anime, friends, friendsAnimeListItems, tracks, user, episodesReversed))
2016-11-19 14:54:31 +00:00
}