143 lines
3.5 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"
2019-04-23 05:52:55 +00:00
"github.com/akyoto/color"
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"
2017-07-13 06:23:20 +00:00
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/server/middleware"
2017-11-19 14:15:44 +00:00
"github.com/animenotifier/notify.moe/utils"
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.
2019-06-01 04:55:49 +00:00
func Get(ctx aero.Context) error {
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(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
2019-06-01 04:55:49 +00:00
customCtx := ctx.(*middleware.OpenGraphContext)
customCtx.OpenGraph = &arn.OpenGraph{
2017-11-21 13:55:55 +00:00
Tags: map[string]string{
2018-03-27 03:25:25 +00:00
"og:title": character.Name.Canonical,
"og:image": "https:" + character.ImageLink("large"),
2019-06-01 04:55:49 +00:00
"og:url": "https://" + assets.Domain + character.Link(),
2017-11-21 13:55:55 +00:00
"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 {
2019-11-19 04:51:54 +00:00
friendIDs := utils.Intersection(character.Likes, user.FollowIDs)
2018-04-19 15:50:20 +00:00
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
}