2016-11-20 10:26:11 +00:00
|
|
|
package profile
|
|
|
|
|
|
|
|
import (
|
2018-11-15 11:19:40 +00:00
|
|
|
"sort"
|
2018-11-16 16:54:53 +00:00
|
|
|
"time"
|
2018-11-15 11:19:40 +00:00
|
|
|
|
2016-11-20 10:26:11 +00:00
|
|
|
"github.com/aerogo/aero"
|
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"
|
2016-11-20 10:26:11 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
2019-11-18 09:39:59 +00:00
|
|
|
"github.com/animenotifier/notify.moe/server/middleware"
|
2017-06-08 12:46:38 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2016-11-20 10:26:11 +00:00
|
|
|
)
|
|
|
|
|
2018-11-15 11:19:40 +00:00
|
|
|
const (
|
|
|
|
maxCharacters = 6
|
|
|
|
maxFriends = 7
|
2018-11-25 06:53:52 +00:00
|
|
|
maxStudios = 4
|
2019-06-23 22:52:06 +00:00
|
|
|
maxGroups = 6
|
2018-11-15 11:19:40 +00:00
|
|
|
)
|
|
|
|
|
2017-06-16 23:25:02 +00:00
|
|
|
// Get user profile page.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Get(ctx aero.Context) error {
|
2016-11-20 10:26:11 +00:00
|
|
|
nick := ctx.Get("nick")
|
2017-06-08 12:46:38 +00:00
|
|
|
viewUser, err := arn.GetUserByNick(nick)
|
2016-11-20 10:26:11 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-11-23 05:26:59 +00:00
|
|
|
return ctx.Error(404, "User not found", err)
|
2016-11-20 10:26:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 12:16:23 +00:00
|
|
|
return Profile(ctx, viewUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profile renders the user profile page of the given viewUser.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Profile(ctx aero.Context, viewUser *arn.User) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2019-08-29 03:23:58 +00:00
|
|
|
sortBy := arn.SortByRating
|
|
|
|
|
|
|
|
if user != nil {
|
|
|
|
sortBy = user.Settings().SortBy
|
|
|
|
}
|
2018-11-15 11:19:40 +00:00
|
|
|
|
|
|
|
// Anime list
|
2017-11-28 23:15:43 +00:00
|
|
|
animeList := viewUser.AnimeList()
|
2018-04-19 22:06:13 +00:00
|
|
|
|
|
|
|
if user == nil || user.ID != viewUser.ID {
|
|
|
|
animeList = animeList.WithoutPrivateItems()
|
|
|
|
}
|
|
|
|
|
2018-11-24 06:57:52 +00:00
|
|
|
completedList := animeList.FilterStatus(arn.AnimeListStatusCompleted)
|
2019-08-29 03:23:58 +00:00
|
|
|
completedList.Sort(sortBy)
|
2017-06-22 14:21:26 +00:00
|
|
|
|
2018-11-15 11:19:40 +00:00
|
|
|
// Genres
|
|
|
|
topGenres := animeList.TopGenres(5)
|
|
|
|
|
2018-11-25 06:53:52 +00:00
|
|
|
// Studios
|
|
|
|
animeWatchingTime := time.Duration(0)
|
|
|
|
studios := map[string]float64{}
|
|
|
|
var topStudios []*arn.Company
|
|
|
|
|
|
|
|
for _, item := range animeList.Items {
|
2018-11-29 06:07:17 +00:00
|
|
|
if item.Status != arn.AnimeListStatusCompleted {
|
2018-11-25 06:53:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
currentWatch := item.Episodes * item.Anime().EpisodeLength
|
|
|
|
reWatch := item.RewatchCount * item.Anime().EpisodeCount * item.Anime().EpisodeLength
|
|
|
|
duration := time.Duration(currentWatch + reWatch)
|
|
|
|
animeWatchingTime += duration * time.Minute
|
2018-11-29 06:07:17 +00:00
|
|
|
rating := 0.0
|
|
|
|
|
|
|
|
if item.Rating.Overall != 0 {
|
|
|
|
rating = item.Rating.Overall - arn.AverageRating
|
|
|
|
} else {
|
|
|
|
// Add 0.1 to avoid all affinities being 0 when a user doesn't have any rated anime.
|
|
|
|
rating = 0.1
|
|
|
|
}
|
2018-11-25 06:53:52 +00:00
|
|
|
|
|
|
|
for _, studio := range item.Anime().Studios() {
|
2018-11-29 06:07:17 +00:00
|
|
|
affinity, exists := studios[studio.ID]
|
2018-11-25 06:53:52 +00:00
|
|
|
|
|
|
|
if !exists {
|
|
|
|
topStudios = append(topStudios, studio)
|
|
|
|
}
|
|
|
|
|
2018-11-29 06:07:17 +00:00
|
|
|
studios[studio.ID] = affinity + rating
|
2018-11-25 06:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(topStudios, func(i, j int) bool {
|
|
|
|
affinityA := studios[topStudios[i].ID]
|
|
|
|
affinityB := studios[topStudios[j].ID]
|
|
|
|
|
|
|
|
if affinityA == affinityB {
|
|
|
|
return topStudios[i].Name.English < topStudios[j].Name.English
|
|
|
|
}
|
|
|
|
|
|
|
|
return affinityA > affinityB
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(topStudios) > maxStudios {
|
|
|
|
topStudios = topStudios[:maxStudios]
|
|
|
|
}
|
|
|
|
|
2018-11-15 11:19:40 +00:00
|
|
|
// Open graph
|
2017-07-21 06:09:22 +00:00
|
|
|
openGraph := &arn.OpenGraph{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"og:title": viewUser.Nick,
|
2018-03-05 16:49:24 +00:00
|
|
|
"og:image": viewUser.AvatarLink("large"),
|
2019-06-01 04:55:49 +00:00
|
|
|
"og:url": "https://" + assets.Domain + viewUser.Link(),
|
2017-07-21 06:09:22 +00:00
|
|
|
"og:site_name": "notify.moe",
|
2018-11-01 02:06:44 +00:00
|
|
|
"og:description": utils.CutLongDescription(viewUser.Introduction),
|
2017-07-21 06:09:22 +00:00
|
|
|
"og:type": "profile",
|
|
|
|
"profile:username": viewUser.Nick,
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
2018-11-01 02:06:44 +00:00
|
|
|
"description": utils.CutLongDescription(viewUser.Introduction),
|
2017-07-21 06:09:22 +00:00
|
|
|
"keywords": viewUser.Nick + ",profile",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-15 11:19:40 +00:00
|
|
|
// Friends
|
2019-11-19 04:51:54 +00:00
|
|
|
friends := viewUser.Friends()
|
2018-11-15 11:19:40 +00:00
|
|
|
|
|
|
|
arn.SortUsersFollowers(friends)
|
|
|
|
|
|
|
|
if len(friends) > maxFriends {
|
|
|
|
friends = friends[:maxFriends]
|
|
|
|
}
|
2017-07-21 06:09:22 +00:00
|
|
|
|
2018-11-16 16:54:53 +00:00
|
|
|
// Activities
|
|
|
|
activities := arn.FilterActivities(func(activity arn.Activity) bool {
|
|
|
|
return activity.GetCreatedBy() == viewUser.ID
|
|
|
|
})
|
|
|
|
|
|
|
|
// Time zone offset
|
|
|
|
var timeZoneOffset time.Duration
|
|
|
|
analytics := viewUser.Analytics()
|
|
|
|
|
|
|
|
if analytics != nil {
|
|
|
|
timeZoneOffset = time.Duration(-analytics.General.TimezoneOffset) * time.Minute
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC().Add(timeZoneOffset)
|
|
|
|
weekDay := int(now.Weekday())
|
2019-06-06 23:56:06 +00:00
|
|
|
currentYearDay := now.YearDay()
|
2018-11-16 16:54:53 +00:00
|
|
|
|
|
|
|
// Day offset is the number of days we need to reach Sunday
|
|
|
|
dayOffset := 0
|
|
|
|
|
|
|
|
if weekDay > 0 {
|
|
|
|
dayOffset = 7 - weekDay
|
|
|
|
}
|
|
|
|
|
|
|
|
dayToActivityCount := map[int]int{}
|
|
|
|
|
|
|
|
for _, activity := range activities {
|
|
|
|
activityTime := activity.GetCreatedTime().Add(timeZoneOffset)
|
|
|
|
activityYearDay := activityTime.YearDay()
|
|
|
|
days := currentYearDay - activityYearDay
|
|
|
|
dayToActivityCount[days+dayOffset]++
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:57:22 +00:00
|
|
|
// Groups
|
|
|
|
groups := []*arn.Group{}
|
|
|
|
|
|
|
|
for group := range arn.StreamGroups() {
|
2019-06-19 10:00:42 +00:00
|
|
|
if !group.IsDraft && group.HasMember(viewUser.ID) {
|
2019-06-19 09:57:22 +00:00
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(groups, func(i, j int) bool {
|
|
|
|
aMembers := len(groups[i].Members)
|
|
|
|
bMembers := len(groups[j].Members)
|
|
|
|
|
|
|
|
if aMembers == bMembers {
|
|
|
|
return groups[i].Name < groups[j].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return aMembers > bMembers
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(groups) > maxGroups {
|
|
|
|
groups = groups[:maxGroups]
|
|
|
|
}
|
|
|
|
|
2018-11-15 11:19:40 +00:00
|
|
|
// Characters
|
|
|
|
characters := []*arn.Character{}
|
|
|
|
|
|
|
|
for character := range arn.StreamCharacters() {
|
|
|
|
if arn.Contains(character.Likes, viewUser.ID) {
|
|
|
|
characters = append(characters, character)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(characters, func(i, j int) bool {
|
|
|
|
aLikes := len(characters[i].Likes)
|
|
|
|
bLikes := len(characters[j].Likes)
|
|
|
|
|
|
|
|
if aLikes == bLikes {
|
|
|
|
return characters[i].Name.Canonical < characters[j].Name.Canonical
|
|
|
|
}
|
|
|
|
|
|
|
|
return aLikes > bLikes
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(characters) > maxCharacters {
|
|
|
|
characters = characters[:maxCharacters]
|
|
|
|
}
|
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
customCtx := ctx.(*middleware.OpenGraphContext)
|
|
|
|
customCtx.OpenGraph = openGraph
|
2018-11-25 06:53:52 +00:00
|
|
|
|
|
|
|
return ctx.HTML(components.Profile(
|
|
|
|
viewUser,
|
|
|
|
user,
|
|
|
|
animeList,
|
|
|
|
completedList,
|
|
|
|
characters,
|
2019-06-19 09:57:22 +00:00
|
|
|
groups,
|
2018-11-25 06:53:52 +00:00
|
|
|
friends,
|
|
|
|
topGenres,
|
|
|
|
topStudios,
|
|
|
|
animeWatchingTime,
|
|
|
|
dayToActivityCount,
|
2019-06-01 04:55:49 +00:00
|
|
|
ctx.Path(),
|
2018-11-25 06:53:52 +00:00
|
|
|
))
|
2016-11-20 10:26:11 +00:00
|
|
|
}
|