From 8a8b363e03c40143eff0ca403cf1e933f95bb019 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 22 Apr 2018 17:43:20 +0200 Subject: [PATCH] Added characters page --- jobs/mal-sync/mal-sync.go | 4 ++ layout/sidebar/sidebar.pixy | 2 + pages/characters/best.go | 22 +++++++++ pages/characters/characters.pixy | 18 ++++---- pages/characters/fetch.go | 9 ++++ pages/characters/latest.go | 18 ++++++++ pages/characters/render.go | 44 ++++++++++++++++++ pages/index.go | 9 ++++ .../add-character-created-date.go | 40 ++++++++++++++++ .../merge-duplicate-characters.go | 46 +++++++++++++++++++ 10 files changed, 203 insertions(+), 9 deletions(-) create mode 100644 pages/characters/best.go create mode 100644 pages/characters/fetch.go create mode 100644 pages/characters/latest.go create mode 100644 pages/characters/render.go create mode 100644 patches/add-character-created-date/add-character-created-date.go create mode 100644 patches/merge-duplicate-characters/merge-duplicate-characters.go diff --git a/jobs/mal-sync/mal-sync.go b/jobs/mal-sync/mal-sync.go index ee8975ff..8f97d9ca 100644 --- a/jobs/mal-sync/mal-sync.go +++ b/jobs/mal-sync/mal-sync.go @@ -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 } diff --git a/layout/sidebar/sidebar.pixy b/layout/sidebar/sidebar.pixy index dc603cb6..316b0279 100644 --- a/layout/sidebar/sidebar.pixy +++ b/layout/sidebar/sidebar.pixy @@ -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") diff --git a/pages/characters/best.go b/pages/characters/best.go new file mode 100644 index 00000000..bb49de45 --- /dev/null +++ b/pages/characters/best.go @@ -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) +} diff --git a/pages/characters/characters.pixy b/pages/characters/characters.pixy index 3c3c6317..74e115b2 100644 --- a/pages/characters/characters.pixy +++ b/pages/characters/characters.pixy @@ -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") \ No newline at end of file + .tab-groups + .tabs + Tab("Latest", "child", "/characters") + Tab("Best", "heart", "/characters/best") \ No newline at end of file diff --git a/pages/characters/fetch.go b/pages/characters/fetch.go new file mode 100644 index 00000000..8d16d668 --- /dev/null +++ b/pages/characters/fetch.go @@ -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 + }) +} diff --git a/pages/characters/latest.go b/pages/characters/latest.go new file mode 100644 index 00000000..5d30f49b --- /dev/null +++ b/pages/characters/latest.go @@ -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) +} diff --git a/pages/characters/render.go b/pages/characters/render.go new file mode 100644 index 00000000..141a5df2 --- /dev/null +++ b/pages/characters/render.go @@ -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)) +} diff --git a/pages/index.go b/pages/index.go index 3529394e..fdda298c 100644 --- a/pages/index.go +++ b/pages/index.go @@ -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) diff --git a/patches/add-character-created-date/add-character-created-date.go b/patches/add-character-created-date/add-character-created-date.go new file mode 100644 index 00000000..75c71938 --- /dev/null +++ b/patches/add-character-created-date/add-character-created-date.go @@ -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) +} diff --git a/patches/merge-duplicate-characters/merge-duplicate-characters.go b/patches/merge-duplicate-characters/merge-duplicate-characters.go new file mode 100644 index 00000000..2fa8aca5 --- /dev/null +++ b/patches/merge-duplicate-characters/merge-duplicate-characters.go @@ -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) +}