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
|
|
|
|
})
|
|
|
|
|
2018-01-25 14:29:52 +00:00
|
|
|
// Quotes
|
|
|
|
quotes := arn.FilterQuotes(func(quote *arn.Quote) bool {
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:29:52 +00:00
|
|
|
return ctx.HTML(components.CharacterDetails(character, characterAnime, quotes, user))
|
2017-07-13 06:23:20 +00:00
|
|
|
}
|