Added characters page

This commit is contained in:
Eduard Urbach 2018-04-22 17:43:20 +02:00
parent e8c66f28ab
commit 8a8b363e03
10 changed files with 203 additions and 9 deletions

View File

@ -205,5 +205,9 @@ func importCharacter(malCharacter *mal.Character) *arn.Character {
// Save character in DB
character.Save()
// Add to character finder so we don't create duplicates of this character
characterFinder.Add(character)
return character
}

View File

@ -37,6 +37,8 @@ component Sidebar(user *arn.User)
SidebarButton("AMVs", "/amvs", "video-camera")
SidebarButton("Soundtracks", "/soundtracks", "headphones")
SidebarButton("Quotes", "/quotes", "quote-left")
if user != nil && (user.Role == "editor" || user.Role == "admin")
SidebarButton("Characters", "/characters", "child")
SidebarButton("Companies", "/companies", "building")
SidebarButton("Users", "/users", "globe")

22
pages/characters/best.go Normal file
View File

@ -0,0 +1,22 @@
package characters
import (
"sort"
"github.com/aerogo/aero"
)
// Best characters.
func Best(ctx *aero.Context) string {
characters := fetchAll()
sort.Slice(characters, func(i, j int) bool {
if len(characters[i].Likes) == len(characters[j].Likes) {
return characters[i].Name.Canonical < characters[j].Name.Canonical
}
return len(characters[i].Likes) > len(characters[j].Likes)
})
return render(ctx, characters)
}

View File

@ -14,19 +14,19 @@ component Characters(characters []*arn.Character, nextIndex int, tag string, use
Icon("pencil")
span Edit draft
//- #load-more-target.characters.characters-page
//- CharactersScrollable(characters, user)
#load-more-target.characters.characters-page
CharactersScrollable(characters, user)
//- if nextIndex != -1
//- .buttons
//- LoadMore(nextIndex)
if nextIndex != -1
.buttons
LoadMore(nextIndex)
component CharactersScrollable(characters []*arn.Character, user *arn.User)
each character in characters
Character(character, user)
component CharactersTabs(tag string)
//- .tab-groups
//- .tabs
//- Tab("Latest", "video-camera", "/characters")
//- Tab("Best", "heart", "/characters/best")
.tab-groups
.tabs
Tab("Latest", "child", "/characters")
Tab("Best", "heart", "/characters/best")

View File

@ -0,0 +1,9 @@
package characters
import "github.com/animenotifier/arn"
func fetchAll() []*arn.Character {
return arn.FilterCharacters(func(character *arn.Character) bool {
return !character.IsDraft
})
}

View File

@ -0,0 +1,18 @@
package characters
import (
"sort"
"github.com/aerogo/aero"
)
// Latest characters.
func Latest(ctx *aero.Context) string {
characters := fetchAll()
sort.Slice(characters, func(i, j int) bool {
return characters[i].Created > characters[j].Created
})
return render(ctx, characters)
}

View File

@ -0,0 +1,44 @@
package characters
import (
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
)
const (
charactersFirstLoad = 104
charactersPerScroll = 39
)
// render renders the characters page with the given characters.
func render(ctx *aero.Context, allCharacters []*arn.Character) string {
user := utils.GetUser(ctx)
index, _ := ctx.GetInt("index")
tag := ctx.Get("tag")
// Slice the part that we need
characters := allCharacters[index:]
maxLength := charactersFirstLoad
if index > 0 {
maxLength = charactersPerScroll
}
if len(characters) > maxLength {
characters = characters[:maxLength]
}
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allCharacters), maxLength, index)
// In case we're scrolling, send Characters only (without the page frame)
if index > 0 {
return ctx.HTML(components.CharactersScrollable(characters, user))
}
// Otherwise, send the full page
return ctx.HTML(components.Characters(characters, nextIndex, tag, user))
}

View File

@ -19,6 +19,7 @@ import (
"github.com/animenotifier/notify.moe/pages/apiview/apidocs"
"github.com/animenotifier/notify.moe/pages/calendar"
"github.com/animenotifier/notify.moe/pages/character"
"github.com/animenotifier/notify.moe/pages/characters"
"github.com/animenotifier/notify.moe/pages/charge"
"github.com/animenotifier/notify.moe/pages/companies"
"github.com/animenotifier/notify.moe/pages/company"
@ -140,6 +141,12 @@ func Configure(app *aero.Application) {
l.Page("/anime/:id/edit/history", editanime.History)
// Characters
l.Page("/characters", characters.Latest)
l.Page("/characters/from/:index", characters.Latest)
l.Page("/characters/best", characters.Best)
l.Page("/characters/best/from/:index", characters.Best)
// Character
l.Page("/character/:id", character.Get)
l.Page("/character/:id/edit", character.Edit)
l.Page("/character/:id/edit/images", character.EditImages)
@ -150,6 +157,8 @@ func Configure(app *aero.Application) {
l.Page("/amvs/from/:index", amvs.Latest)
l.Page("/amvs/best", amvs.Best)
l.Page("/amvs/best/from/:index", amvs.Best)
// AMV
l.Page("/amv/:id", amv.Get)
l.Page("/amv/:id/edit", amv.Edit)
l.Page("/amv/:id/history", amv.History)

View File

@ -0,0 +1,40 @@
package main
import (
"strconv"
"time"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
func main() {
color.Yellow("Adding creation dates")
defer color.Green("Finished")
defer arn.Node.Close()
baseTime := time.Now().Add(-2 * 30 * 24 * time.Hour)
irregular := time.Duration(0)
for character := range arn.StreamCharacters() {
malID := character.GetMapping("myanimelist/character")
if malID != "" {
malIDNumber, err := strconv.Atoi(malID)
if err != nil {
panic(err)
}
character.Created = baseTime.Add(time.Duration(malIDNumber) * time.Minute).Format(time.RFC3339)
} else {
irregular++
character.Created = baseTime.Add(-irregular * time.Minute).Format(time.RFC3339)
}
character.Save()
}
time.Sleep(1 * time.Second)
}

View File

@ -0,0 +1,46 @@
package main
import (
"fmt"
"sort"
"time"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
func main() {
color.Yellow("Merging duplicate characters")
defer color.Green("Finished")
defer arn.Node.Close()
malIDToCharacters := map[string][]*arn.Character{}
for character := range arn.StreamCharacters() {
malID := character.GetMapping("myanimelist/character")
if malID != "" {
malIDToCharacters[malID] = append(malIDToCharacters[malID], character)
}
}
for _, characters := range malIDToCharacters {
if len(characters) > 1 {
sort.Slice(characters, func(i, j int) bool {
return len(characters[i].Likes) > len(characters[j].Likes)
})
for index, character := range characters {
if index == 0 {
continue
}
fmt.Printf("Merging '%s' with '%s' (%s to %s)\n", color.YellowString(character.String()), color.YellowString(characters[0].String()), character.ID, characters[0].ID)
character.Merge(characters[0])
}
}
}
time.Sleep(1 * time.Second)
}