161 lines
3.9 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"
2017-10-17 14:58:28 +00:00
"sort"
2017-06-19 18:59:02 +00:00
2016-11-19 14:54:31 +00:00
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2019-06-01 04:55:49 +00:00
"github.com/animenotifier/notify.moe/assets"
2016-11-19 14:54:31 +00:00
"github.com/animenotifier/notify.moe/components"
2019-06-01 04:55:49 +00:00
"github.com/animenotifier/notify.moe/middleware"
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
maxEpisodesLongSeries = 12
maxDescriptionLength = 170
maxFriendsPerEpisode = 9
)
2017-06-16 23:25:02 +00:00
// Get anime page.
2019-06-01 04:55:49 +00:00
func Get(ctx aero.Context) error {
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
}
// Anime list item
var animeListItem *arn.AnimeListItem
if user != nil {
animeListItem = user.AnimeList().Find(anime.ID)
}
2018-11-13 09:11:03 +00:00
// Episodes
2019-08-28 08:06:42 +00:00
episodes := anime.Episodes()
2017-06-27 23:05:39 +00:00
2017-11-05 08:32:46 +00:00
if len(episodes) > maxEpisodes {
2019-08-28 08:06:42 +00:00
episodes = episodes[len(episodes)-maxEpisodesLongSeries:]
2017-11-05 08:32:46 +00:00
}
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{}
episodeToFriends := map[int][]*arn.User{}
2017-07-21 09:25:53 +00:00
if user != nil {
friends = user.Follows().Users()
deleted := 0
2018-11-16 08:57:56 +00:00
if animeListItem != nil {
episodeToFriends[animeListItem.Episodes] = append(episodeToFriends[animeListItem.Episodes], user)
}
2017-07-21 09:25:53 +00:00
for i := range friends {
j := i - deleted
friend := friends[j]
friendAnimeList := friend.AnimeList()
2017-10-13 11:55:33 +00:00
friendAnimeListItem := friendAnimeList.Find(anime.ID)
2017-07-21 09:43:54 +00:00
2018-04-19 22:06:13 +00:00
if friendAnimeListItem == nil || friendAnimeListItem.Private {
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[friend] = friendAnimeListItem
if len(episodeToFriends[friendAnimeListItem.Episodes]) < maxFriendsPerEpisode {
episodeToFriends[friendAnimeListItem.Episodes] = append(episodeToFriends[friendAnimeListItem.Episodes], friend)
}
2017-07-21 09:25:53 +00:00
}
}
2018-11-08 18:41:54 +00:00
arn.SortUsersLastSeenFirst(friends)
2017-07-21 09:25:53 +00:00
}
2017-10-17 14:58:28 +00:00
// Sort relations by start date
relations := anime.Relations()
if relations != nil {
2017-12-04 20:58:55 +00:00
relations.SortByStartDate()
2017-10-17 14:58:28 +00:00
}
2017-11-04 10:09:19 +00:00
// Soundtracks
2017-11-18 10:37:29 +00:00
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
2017-11-04 10:09:19 +00:00
return !track.IsDraft && len(track.Media) > 0 && arn.Contains(track.Tags, "anime:"+anime.ID)
})
2017-11-04 16:11:47 +00:00
sort.Slice(tracks, func(i, j int) bool {
2018-04-15 08:36:51 +00: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 08:36:51 +00: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 16:11:47 +00:00
})
2017-07-06 18:33:46 +00:00
// Open Graph
2019-06-01 04:55:49 +00:00
customCtx := ctx.(*middleware.OpenGraphContext)
2019-06-05 07:18:04 +00:00
customCtx.OpenGraph = getOpenGraph(anime)
2018-04-26 01:18:36 +00:00
return ctx.HTML(components.Anime(anime, animeListItem, tracks, amvs, amvAppearances, episodes, friends, friendsAnimeListItems, episodeToFriends, user))
2018-04-26 01:18:36 +00:00
}
2019-06-05 07:18:04 +00:00
func getOpenGraph(anime *arn.Anime) *arn.OpenGraph {
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,
2018-03-16 21:40:38 +00:00
"og:image": "https:" + anime.ImageLink("large"),
2019-06-01 04:55:49 +00:00
"og:url": "https://" + assets.Domain + anime.Link(),
2017-07-06 18:56:37 +00:00
"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
}
2018-04-26 01:19:00 +00:00
return openGraph
2016-11-19 14:54:31 +00:00
}