140 lines
3.3 KiB
Go
Raw Normal View History

2017-07-13 06:23:20 +00:00
package character
import (
"net/http"
2017-11-20 08:01:28 +00:00
"sort"
2017-07-13 06:23:20 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
2017-11-19 14:15:44 +00:00
"github.com/animenotifier/notify.moe/utils"
"github.com/fatih/color"
2017-07-13 06:23:20 +00:00
)
const (
maxRelevantCharacters = 12
)
2017-11-21 14:04:17 +00:00
2017-07-13 06:23:20 +00:00
// Get character.
func Get(ctx *aero.Context) string {
2017-11-19 14:15:44 +00:00
user := utils.GetUser(ctx)
2017-07-13 06:23:20 +00:00
id := ctx.Get("id")
character, err := arn.GetCharacter(id)
if err != nil {
return ctx.Error(http.StatusNotFound, "Character not found", err)
}
// Anime
2017-11-20 08:01:28 +00:00
characterAnime := character.Anime()
sort.Slice(characterAnime, func(i, j int) bool {
if characterAnime[i].StartDate == "" {
return false
}
if characterAnime[j].StartDate == "" {
return true
}
return characterAnime[i].StartDate < characterAnime[j].StartDate
})
// Characters from the same anime
characterAppearances := map[string]int{}
for _, anime := range characterAnime {
for _, animeCharacter := range anime.Characters().Items {
if animeCharacter.CharacterID == character.ID {
continue
}
characterAppearances[animeCharacter.CharacterID]++
}
}
relevantCharacters := []*arn.Character{}
for characterID := range characterAppearances {
relevantCharacter, err := arn.GetCharacter(characterID)
if err != nil {
color.Red(err.Error())
continue
}
if !relevantCharacter.HasImage() {
continue
}
relevantCharacters = append(relevantCharacters, relevantCharacter)
}
sort.Slice(relevantCharacters, func(i, j int) bool {
aRelevance := characterAppearances[relevantCharacters[i].ID]
bRelevance := characterAppearances[relevantCharacters[j].ID]
if aRelevance == bRelevance {
aLikes := len(relevantCharacters[i].Likes)
bLikes := len(relevantCharacters[j].Likes)
if aLikes == bLikes {
return relevantCharacters[i].Name.Canonical < relevantCharacters[j].Name.Canonical
}
return aLikes > bLikes
}
return aRelevance > bRelevance
})
if len(relevantCharacters) > maxRelevantCharacters {
relevantCharacters = relevantCharacters[:maxRelevantCharacters]
}
// Quotes
2018-04-10 18:56:56 +00:00
mainQuote := character.MainQuote()
2018-02-25 17:45:51 +00:00
quotes := character.Quotes()
2018-04-10 18:56:56 +00:00
arn.SortQuotesPopularFirst(quotes)
2017-11-21 13:55:55 +00:00
// Set OpenGraph attributes
2018-11-01 00:43:12 +00:00
description := utils.CutLongDescription(character.Description)
2017-11-21 14:04:17 +00:00
2017-11-21 13:55:55 +00:00
ctx.Data = &arn.OpenGraph{
Tags: map[string]string{
2018-03-27 03:25:25 +00:00
"og:title": character.Name.Canonical,
"og:image": "https:" + character.ImageLink("large"),
2017-11-21 13:55:55 +00:00
"og:url": "https://" + ctx.App.Config.Domain + character.Link(),
"og:site_name": "notify.moe",
2017-11-21 14:04:17 +00:00
"og:description": description,
// The OpenGraph type "profile" is meant for real-life persons but I think it's okay in this context.
// An alternative would be to use "article" which is mostly used for blog posts and news.
"og:type": "profile",
2017-11-21 13:55:55 +00:00
},
Meta: map[string]string{
2017-11-21 14:04:17 +00:00
"description": description,
2018-03-27 03:25:25 +00:00
"keywords": character.Name.Canonical + ",anime,character",
2017-11-21 13:55:55 +00:00
},
}
2018-04-19 15:48:15 +00:00
// Friends
2018-04-19 15:50:20 +00:00
var friends []*arn.User
2018-04-19 15:48:15 +00:00
2018-04-19 15:50:20 +00:00
if user != nil {
friendIDs := utils.Intersection(character.Likes, user.Follows().Items)
friendObjects := arn.DB.GetMany("User", friendIDs)
for _, obj := range friendObjects {
if obj == nil {
continue
}
2018-04-19 15:48:15 +00:00
2018-04-19 15:50:20 +00:00
friends = append(friends, obj.(*arn.User))
}
2018-04-19 15:48:15 +00:00
}
return ctx.HTML(components.CharacterDetails(character, characterAnime, quotes, friends, relevantCharacters, mainQuote, user))
2017-07-13 06:23:20 +00:00
}