Fixed article order, latest 3 instead of 4 on about page and moved article date.

This commit is contained in:
Marvin Blum
2020-07-29 20:39:05 +02:00
committed by Marvin Blum
parent f4a3b27d25
commit c1bc4d7cda
4 changed files with 25 additions and 16 deletions

View File

@@ -28,8 +28,9 @@ var (
type Blog struct { type Blog struct {
client *emvi.Client client *emvi.Client
articles map[string]emvi.Article // id -> article articles []emvi.Article // sorted by date published (descending)
articlesYear map[int][]emvi.Article // year -> articles articleMap map[string]emvi.Article // id -> article
articlesYear map[int][]emvi.Article // year -> articleMap
nextUpdate time.Time nextUpdate time.Time
cache *tpl.Cache cache *tpl.Cache
m sync.Mutex m sync.Mutex
@@ -54,7 +55,7 @@ func NewBlog(cache *tpl.Cache) *Blog {
func (blog *Blog) GetArticle(id string) emvi.Article { func (blog *Blog) GetArticle(id string) emvi.Article {
blog.refreshIfRequired() blog.refreshIfRequired()
return blog.articles[id] return blog.articleMap[id]
} }
func (blog *Blog) GetArticles() map[int][]emvi.Article { func (blog *Blog) GetArticles() map[int][]emvi.Article {
@@ -65,7 +66,7 @@ func (blog *Blog) GetArticles() map[int][]emvi.Article {
func (blog *Blog) GetLatestArticles() []emvi.Article { func (blog *Blog) GetLatestArticles() []emvi.Article {
blog.refreshIfRequired() blog.refreshIfRequired()
articles := make([]emvi.Article, 0, 3) articles := make([]emvi.Article, 0, 3)
i := 0 i := 1
for _, v := range blog.articles { for _, v := range blog.articles {
articles = append(articles, v) articles = append(articles, v)
@@ -82,8 +83,8 @@ func (blog *Blog) GetLatestArticles() []emvi.Article {
func (blog *Blog) loadArticles() { func (blog *Blog) loadArticles() {
blog.m.Lock() blog.m.Lock()
defer blog.m.Unlock() defer blog.m.Unlock()
logbuch.Info("Refreshing blog articles...") logbuch.Info("Refreshing blog articleMap...")
articles, offset, count := make(map[string]emvi.Article), 0, 1 articles, offset, count := make([]emvi.Article, 0), 0, 1
var err error var err error
for count > 0 { for count > 0 {
@@ -95,7 +96,7 @@ func (blog *Blog) loadArticles() {
}) })
if err != nil { if err != nil {
logbuch.Error("Error loading blog articles", logbuch.Fields{"err": err}) logbuch.Error("Error loading blog articleMap", logbuch.Fields{"err": err})
break break
} }
@@ -103,14 +104,14 @@ func (blog *Blog) loadArticles() {
count = len(results) count = len(results)
for _, article := range results { for _, article := range results {
articles[article.Id] = article articles = append(articles, article)
} }
} }
if err == nil { if err == nil {
for k, v := range articles { for i, article := range articles {
v.LatestArticleContent = blog.loadArticle(v) article.LatestArticleContent = blog.loadArticle(article)
articles[k] = v articles[i] = article
} }
blog.setArticles(articles) blog.setArticles(articles)
@@ -121,7 +122,7 @@ func (blog *Blog) loadArticles() {
} }
func (blog *Blog) loadArticle(article emvi.Article) *emvi.ArticleContent { func (blog *Blog) loadArticle(article emvi.Article) *emvi.ArticleContent {
existingArticle := blog.articles[article.Id] existingArticle := blog.articleMap[article.Id]
var content *emvi.ArticleContent var content *emvi.ArticleContent
if len(existingArticle.Id) == 0 || !existingArticle.ModTime.Equal(article.ModTime) { if len(existingArticle.Id) == 0 || !existingArticle.ModTime.Equal(article.ModTime) {
@@ -164,7 +165,6 @@ func (blog *Blog) downloadAttachments(id, content string) {
continue continue
} }
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
@@ -172,6 +172,10 @@ func (blog *Blog) downloadAttachments(id, content string) {
continue continue
} }
if err := resp.Body.Close(); err != nil {
logbuch.Error("Error closing response body on attachment download", logbuch.Fields{"err": err, "id": id})
}
if err := ioutil.WriteFile(filepath.Join(blogFileCache, id, attachment[3]), data, 0755); err != nil { if err := ioutil.WriteFile(filepath.Join(blogFileCache, id, attachment[3]), data, 0755); err != nil {
logbuch.Error("Error saving blog attachment on disk", logbuch.Fields{"err": err, "id": id}) logbuch.Error("Error saving blog attachment on disk", logbuch.Fields{"err": err, "id": id})
} }
@@ -179,8 +183,9 @@ func (blog *Blog) downloadAttachments(id, content string) {
} }
} }
func (blog *Blog) setArticles(articles map[string]emvi.Article) { func (blog *Blog) setArticles(articles []emvi.Article) {
blog.articles = articles blog.articles = articles
blog.articleMap = make(map[string]emvi.Article)
blog.articlesYear = make(map[int][]emvi.Article) blog.articlesYear = make(map[int][]emvi.Article)
for _, article := range articles { for _, article := range articles {
@@ -189,6 +194,7 @@ func (blog *Blog) setArticles(articles map[string]emvi.Article) {
} }
blog.articlesYear[article.Published.Year()] = append(blog.articlesYear[article.Published.Year()], article) blog.articlesYear[article.Published.Year()] = append(blog.articlesYear[article.Published.Year()], article)
blog.articleMap[article.Id] = article
} }
} }

1
dev.sh
View File

@@ -2,6 +2,7 @@
# This file is for local development only! # This file is for local development only!
# It configures and starts the website for local development. # It configures and starts the website for local development.
# The "secret" for the Emvi API can be shared, as it gives access to public content only.
export MB_LOGLEVEL=debug export MB_LOGLEVEL=debug
export MB_ALLOWED_ORIGINS=* export MB_ALLOWED_ORIGINS=*

View File

@@ -21,8 +21,9 @@
<h2>Latest Blog Posts</h2> <h2>Latest Blog Posts</h2>
{{range $article := .Articles}} {{range $article := .Articles}}
<p> <p>
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
<small>{{format $article.Published "2. January 2006"}}</small> <small>{{format $article.Published "2. January 2006"}}</small>
<br />
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
</p> </p>
{{else}} {{else}}
<p>There are no blog posts yet...</p> <p>There are no blog posts yet...</p>

View File

@@ -10,8 +10,9 @@
{{range $article := $articles}} {{range $article := $articles}}
<p> <p>
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
<small>{{format $article.Published "2. January 2006"}}</small> <small>{{format $article.Published "2. January 2006"}}</small>
<br />
<a href="/blog/{{slug $article.LatestArticleContent.Title}}-{{$article.Id}}">{{$article.LatestArticleContent.Title}}</a>
</p> </p>
{{end}} {{end}}
</section> </section>