138 lines
3.3 KiB
Go
Raw Normal View History

2017-07-13 08:23:20 +02:00
package character
import (
"net/http"
2017-11-20 09:01:28 +01:00
"sort"
2017-07-13 08:23:20 +02:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
2017-11-19 15:15:44 +01:00
"github.com/animenotifier/notify.moe/utils"
"github.com/fatih/color"
2017-07-13 08:23:20 +02:00
)
const (
maxDescriptionLength = 170
maxRelevantCharacters = 12
)
2017-11-21 15:04:17 +01:00
2017-07-13 08:23:20 +02:00
// Get character.
func Get(ctx *aero.Context) string {
2017-11-19 15:15:44 +01:00
user := utils.GetUser(ctx)
2017-07-13 08:23:20 +02: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 09:01:28 +01: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 !relevantCharacter.HasImage() {
continue
}
if err != nil {
color.Red(err.Error())
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 {
return relevantCharacters[i].Name.Canonical < relevantCharacters[j].Name.Canonical
}
return aRelevance > bRelevance
})
if len(relevantCharacters) > maxRelevantCharacters {
relevantCharacters = relevantCharacters[:maxRelevantCharacters]
}
// Quotes
2018-04-10 20:56:56 +02:00
mainQuote := character.MainQuote()
2018-02-25 18:45:51 +01:00
quotes := character.Quotes()
2018-04-10 20:56:56 +02:00
arn.SortQuotesPopularFirst(quotes)
2017-11-21 14:55:55 +01:00
// Set OpenGraph attributes
2017-11-21 15:04:17 +01:00
description := character.Description
if len(description) > maxDescriptionLength {
description = description[:maxDescriptionLength-3] + "..."
}
2017-11-21 14:55:55 +01:00
ctx.Data = &arn.OpenGraph{
Tags: map[string]string{
2018-03-27 05:25:25 +02:00
"og:title": character.Name.Canonical,
"og:image": "https:" + character.ImageLink("large"),
2017-11-21 14:55:55 +01:00
"og:url": "https://" + ctx.App.Config.Domain + character.Link(),
"og:site_name": "notify.moe",
2017-11-21 15:04:17 +01: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 14:55:55 +01:00
},
Meta: map[string]string{
2017-11-21 15:04:17 +01:00
"description": description,
2018-03-27 05:25:25 +02:00
"keywords": character.Name.Canonical + ",anime,character",
2017-11-21 14:55:55 +01:00
},
}
2018-04-19 17:48:15 +02:00
// Friends
2018-04-19 17:50:20 +02:00
var friends []*arn.User
2018-04-19 17:48:15 +02:00
2018-04-19 17:50:20 +02: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 17:48:15 +02:00
2018-04-19 17:50:20 +02:00
friends = append(friends, obj.(*arn.User))
}
2018-04-19 17:48:15 +02:00
}
return ctx.HTML(components.CharacterDetails(character, characterAnime, quotes, friends, relevantCharacters, mainQuote, user))
2017-07-13 08:23:20 +02:00
}