132 lines
3.1 KiB
Go
Raw Normal View History

2016-11-20 10:26:11 +00:00
package profile
import (
2018-11-15 11:19:40 +00:00
"sort"
"time"
2018-11-15 11:19:40 +00:00
2016-11-20 10:26:11 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
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
)
2017-06-16 23:25:02 +00:00
// Get user profile page.
2016-11-20 10:26:11 +00:00
func Get(ctx *aero.Context) string {
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.
func Profile(ctx *aero.Context, viewUser *arn.User) string {
2017-11-28 23:15:43 +00:00
user := utils.GetUser(ctx)
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()
}
2017-11-28 23:15:43 +00:00
animeList.SortByRating()
2017-06-22 14:21:26 +00:00
2018-11-15 11:19:40 +00:00
// Genres
topGenres := animeList.TopGenres(5)
// 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"),
2017-07-21 06:09:22 +00:00
"og:url": "https://" + ctx.App.Config.Domain + viewUser.Link(),
"og:site_name": "notify.moe",
"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{
"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
friends := viewUser.Follows().UsersWhoFollowBack()
arn.SortUsersFollowers(friends)
if len(friends) > maxFriends {
friends = friends[:maxFriends]
}
2017-07-21 06:09:22 +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())
currentYearDay := int(now.YearDay())
// 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]++
}
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]
}
ctx.Data = openGraph
return ctx.HTML(components.Profile(viewUser, user, animeList, characters, friends, topGenres, dayToActivityCount, ctx.URI()))
2016-11-20 10:26:11 +00:00
}