From e7d914f22386f2956f29b549b6812481707728be Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 26 Jun 2017 23:41:16 +0200 Subject: [PATCH] Improved forum database --- layout/layout.pixy | 3 +-- mixins/ThreadLink.pixy | 2 +- pages/threads/threads.go | 32 +++++++++++++++----------------- patches/post-texts/main.go | 29 +++++++++++++++++++++++++++++ patches/thread-posts/main.go | 34 ++++++++++++++++++++++++++++++++++ styles/include/config.scarlet | 3 +++ styles/loading.scarlet | 2 +- 7 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 patches/post-texts/main.go create mode 100644 patches/thread-posts/main.go diff --git a/layout/layout.pixy b/layout/layout.pixy index 65523533..cdcee99a 100644 --- a/layout/layout.pixy +++ b/layout/layout.pixy @@ -12,8 +12,7 @@ component Layout(app *aero.Application, ctx *aero.Context, user *arn.User, conte Navigation(user) #content-container main#content.fade!= content - - LoadingAnimation + LoadingAnimation script(src="/scripts") component LoadingAnimation diff --git a/mixins/ThreadLink.pixy b/mixins/ThreadLink.pixy index 2c0a7952..ce038fc6 100644 --- a/mixins/ThreadLink.pixy +++ b/mixins/ThreadLink.pixy @@ -8,6 +8,6 @@ component ThreadLink(thread *arn.Thread) Icon("thumb-tack") a.thread-link-title.ajax(href="/threads/" + thread.ID)= thread.Title .spacer - .thread-reply-count= thread.Replies + .thread-reply-count= len(thread.Posts) .thread-icons Icon(arn.GetForumIcon(thread.Tags[0])) \ No newline at end of file diff --git a/pages/threads/threads.go b/pages/threads/threads.go index 92d96248..b9b7977e 100644 --- a/pages/threads/threads.go +++ b/pages/threads/threads.go @@ -1,7 +1,7 @@ package threads import ( - "strings" + "net/http" "github.com/aerogo/aero" "github.com/animenotifier/arn" @@ -12,28 +12,26 @@ import ( // Get thread. func Get(ctx *aero.Context) string { id := ctx.Get("id") - thread, err := arn.GetThread(id) user := utils.GetUser(ctx) + // Fetch thread + thread, err := arn.GetThread(id) + if err != nil { - return ctx.Error(404, "Thread not found", err) + return ctx.Error(http.StatusNotFound, "Thread not found", err) } - replies, filterErr := arn.FilterPosts(func(post *arn.Post) bool { - post.Text = strings.Replace(post.Text, "http://", "https://", -1) - return post.ThreadID == thread.ID - }) + // Fetch posts + postObjects, getErr := arn.DB.GetMany("Post", thread.Posts) - arn.SortPostsLatestLast(replies) - - // Benchmark - // for i := 0; i < 7; i++ { - // replies = append(replies, replies...) - // } - - if filterErr != nil { - return ctx.Error(500, "Error fetching thread replies", err) + if getErr != nil { + return ctx.Error(http.StatusInternalServerError, "Could not retrieve posts", getErr) } - return ctx.HTML(components.Thread(thread, replies, user)) + posts := postObjects.([]*arn.Post) + + // Sort posts + arn.SortPostsLatestLast(posts) + + return ctx.HTML(components.Thread(thread, posts, user)) } diff --git a/patches/post-texts/main.go b/patches/post-texts/main.go new file mode 100644 index 00000000..540eaca9 --- /dev/null +++ b/patches/post-texts/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "github.com/animenotifier/arn" + "github.com/fatih/color" +) + +func main() { + // Get a stream of all posts + allPosts, err := arn.AllPosts() + arn.PanicOnError(err) + + // Iterate over the stream + for post := range allPosts { + // Fix text + color.Yellow(post.Text) + post.Text = arn.FixPostText(post.Text) + color.Green(post.Text) + + // Tags + if post.Tags == nil { + post.Tags = []string{} + } + + // Save + err = post.Save() + arn.PanicOnError(err) + } +} diff --git a/patches/thread-posts/main.go b/patches/thread-posts/main.go new file mode 100644 index 00000000..dbaf8dd4 --- /dev/null +++ b/patches/thread-posts/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "github.com/animenotifier/arn" +) + +func main() { + // Get a stream of all posts + allPosts, err := arn.AllPosts() + arn.PanicOnError(err) + + threadToPosts := make(map[string][]string) + + // Iterate over the stream + for post := range allPosts { + _, found := threadToPosts[post.ThreadID] + + if !found { + threadToPosts[post.ThreadID] = []string{post.ID} + } else { + threadToPosts[post.ThreadID] = append(threadToPosts[post.ThreadID], post.ID) + } + } + + // Save new post ID lists + for threadID, posts := range threadToPosts { + thread, err := arn.GetThread(threadID) + arn.PanicOnError(err) + + thread.Posts = posts + err = thread.Save() + arn.PanicOnError(err) + } +} diff --git a/styles/include/config.scarlet b/styles/include/config.scarlet index 7359ff69..2e5732d8 100644 --- a/styles/include/config.scarlet +++ b/styles/include/config.scarlet @@ -40,6 +40,9 @@ nav-link-hover-slide-color = rgb(248, 165, 130) // nav-link-color = rgb(160, 160, 160) // nav-link-hover-color = rgb(80, 80, 80) +// Loading animation +loading-anim-color = nav-link-hover-slide-color + // Shadow shadow-light = 4px 4px 8px rgba(0, 0, 0, 0.05) shadow-medium = 6px 6px 12px rgba(0, 0, 0, 0.13) diff --git a/styles/loading.scarlet b/styles/loading.scarlet index 22b532b5..8169c95b 100644 --- a/styles/loading.scarlet +++ b/styles/loading.scarlet @@ -17,7 +17,7 @@ loading-anim-size = 24px .sk-cube width 33.3% height 33.3% - background-color main-color + background-color loading-anim-color opacity 0.7 border-radius 100% animation sk-pulse loading-anim-duration infinite linear