144 lines
3.3 KiB
Go
Raw Normal View History

2016-11-19 23:54:31 +09:00
package anime
import (
2017-06-19 20:59:02 +02:00
"net/http"
2017-10-17 16:58:28 +02:00
"sort"
2017-06-19 20:59:02 +02:00
2016-11-19 23:54:31 +09:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
2017-06-19 20:59:02 +02:00
"github.com/animenotifier/notify.moe/utils"
2016-11-19 23:54:31 +09:00
)
2017-11-05 09:32:46 +01:00
const maxEpisodes = 26
2017-12-04 21:58:55 +01:00
const maxEpisodesLongSeries = 12
2017-07-06 21:38:19 +02:00
const maxDescriptionLength = 170
2017-06-17 01:25:02 +02:00
// Get anime page.
2016-11-19 23:54:31 +09:00
func Get(ctx *aero.Context) string {
2017-06-04 01:17:00 +02:00
id := ctx.Get("id")
2017-06-19 20:59:02 +02:00
user := utils.GetUser(ctx)
2016-11-19 23:54:31 +09:00
anime, err := arn.GetAnime(id)
if err != nil {
2017-06-19 20:59:02 +02:00
return ctx.Error(http.StatusNotFound, "Anime not found", err)
2016-11-19 23:54:31 +09:00
}
2017-11-05 09:32:46 +01:00
episodes := anime.Episodes().Items
2017-06-28 01:05:39 +02:00
2017-11-05 09:32:46 +01:00
if len(episodes) > maxEpisodes {
2017-12-04 21:58:55 +01:00
episodes = anime.Episodes().LastReversed(maxEpisodesLongSeries)
2017-11-05 09:32:46 +01:00
}
2017-07-21 11:25:53 +02:00
// Friends watching
var friends []*arn.User
2017-07-21 11:43:54 +02:00
friendsAnimeListItems := map[*arn.User]*arn.AnimeListItem{}
2017-07-21 11:25:53 +02:00
if user != nil {
friends = user.Follows().Users()
deleted := 0
for i := range friends {
j := i - deleted
2017-07-21 11:43:54 +02:00
friendAnimeList := friends[j].AnimeList()
2017-10-13 13:55:33 +02:00
friendAnimeListItem := friendAnimeList.Find(anime.ID)
2017-07-21 11:43:54 +02:00
2018-04-20 00:06:13 +02:00
if friendAnimeListItem == nil || friendAnimeListItem.Private {
2017-07-21 11:25:53 +02:00
friends = friends[:j+copy(friends[j:], friends[j+1:])]
deleted++
2017-07-21 11:43:54 +02:00
} else {
2017-10-13 13:55:33 +02:00
friendsAnimeListItems[friends[j]] = friendAnimeListItem
2017-07-21 11:25:53 +02:00
}
}
arn.SortUsersLastSeen(friends)
}
2017-10-17 16:58:28 +02:00
// Sort relations by start date
relations := anime.Relations()
if relations != nil {
2017-12-04 21:58:55 +01:00
relations.SortByStartDate()
2017-10-17 16:58:28 +02:00
}
2017-11-04 11:09:19 +01:00
// Soundtracks
2017-11-18 11:37:29 +01:00
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
2017-11-04 11:09:19 +01:00
return !track.IsDraft && len(track.Media) > 0 && arn.Contains(track.Tags, "anime:"+anime.ID)
})
2017-11-04 17:11:47 +01:00
sort.Slice(tracks, func(i, j int) bool {
2018-04-15 10:36:51 +02:00
if len(tracks[i].Likes) == len(tracks[j].Likes) {
return tracks[i].Title.ByUser(user) < tracks[j].Title.ByUser(user)
}
return len(tracks[i].Likes) > len(tracks[j].Likes)
})
// AMVs
amvs := []*arn.AMV{}
amvAppearances := []*arn.AMV{}
for amv := range arn.StreamAMVs() {
if amv.IsDraft {
continue
}
if amv.MainAnimeID == anime.ID {
amvs = append(amvs, amv)
} else if arn.Contains(amv.ExtraAnimeIDs, anime.ID) {
amvAppearances = append(amvAppearances, amv)
}
}
2018-04-15 10:36:51 +02:00
sort.Slice(amvs, func(i, j int) bool {
if len(amvs[i].Likes) == len(amvs[j].Likes) {
return amvs[i].Title.ByUser(user) < amvs[j].Title.ByUser(user)
}
return len(amvs[i].Likes) > len(amvs[j].Likes)
2017-11-04 17:11:47 +01:00
})
// Anime list item
var animeListItem *arn.AnimeListItem
if user != nil {
animeListItem = user.AnimeList().Find(anime.ID)
}
2017-07-06 20:33:46 +02:00
// Open Graph
2018-04-26 03:18:36 +02:00
ctx.Data = getOpenGraph(ctx, anime)
return ctx.HTML(components.Anime(anime, animeListItem, tracks, amvs, amvAppearances, episodes, friends, friendsAnimeListItems, user))
}
func getOpenGraph(ctx *aero.Context, anime *arn.Anime) *arn.OpenGraph {
2017-07-06 21:38:19 +02:00
description := anime.Summary
if len(description) > maxDescriptionLength {
description = description[:maxDescriptionLength-3] + "..."
}
2017-07-06 20:56:37 +02:00
openGraph := &arn.OpenGraph{
Tags: map[string]string{
"og:title": anime.Title.Canonical,
2018-03-16 22:40:38 +01:00
"og:image": "https:" + anime.ImageLink("large"),
2017-07-06 20:56:37 +02:00
"og:url": "https://" + ctx.App.Config.Domain + anime.Link(),
"og:site_name": "notify.moe",
2017-07-06 21:38:19 +02:00
"og:description": description,
2017-07-06 20:56:37 +02:00
},
Meta: map[string]string{
2017-07-06 21:38:19 +02:00
"description": description,
"keywords": anime.Title.Canonical + ",anime",
2017-07-06 20:56:37 +02:00
},
2017-07-06 20:33:46 +02:00
}
switch anime.Type {
case "tv":
2017-07-06 20:56:37 +02:00
openGraph.Tags["og:type"] = "video.tv_show"
2017-07-06 20:33:46 +02:00
case "movie":
2017-07-06 20:56:37 +02:00
openGraph.Tags["og:type"] = "video.movie"
2017-07-06 20:33:46 +02:00
}
2018-04-26 03:19:00 +02:00
return openGraph
2016-11-19 23:54:31 +09:00
}