73 lines
1.8 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"
2017-07-13 06:23:20 +00:00
)
2017-11-21 14:04:17 +00:00
const maxDescriptionLength = 170
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)
}
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
})
// Quotes
quotes := arn.FilterQuotes(func(quote *arn.Quote) bool {
2018-02-24 12:11:12 +00:00
return !quote.IsDraft && len(quote.Description) > 0 && quote.CharacterID == character.ID
})
arn.SortQuotesPopularFirst(quotes)
2017-11-21 13:55:55 +00:00
// Set OpenGraph attributes
2017-11-21 14:04:17 +00:00
description := character.Description
if len(description) > maxDescriptionLength {
description = description[:maxDescriptionLength-3] + "..."
}
2017-11-21 13:55:55 +00:00
ctx.Data = &arn.OpenGraph{
Tags: map[string]string{
"og:title": character.Name,
2017-11-21 14:07:58 +00:00
"og:image": "https:" + character.Image,
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,
2017-11-21 13:55:55 +00:00
"keywords": character.Name + ",anime,character",
},
}
return ctx.HTML(components.CharacterDetails(character, characterAnime, quotes, user))
2017-07-13 06:23:20 +00:00
}