From 984e5bc4158fc623f38bf16b392d3fdb90e70da2 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 21 Oct 2024 11:49:04 -0400 Subject: [PATCH] Show number of Likes on posts and Stats pages Ref T906 --- database.go | 19 +++++++++++++++++++ posts.go | 3 +++ templates/collection-post.tmpl | 1 + templates/user/stats.tmpl | 2 ++ 4 files changed, 25 insertions(+) diff --git a/database.go b/database.go index c5f239f..d715fd4 100644 --- a/database.go +++ b/database.go @@ -115,6 +115,7 @@ type writestore interface { DispersePosts(userID int64, postIDs []string) (*[]ClaimPostResult, error) ClaimPosts(cfg *config.Config, userID int64, collAlias string, posts *[]ClaimPostRequest) (*[]ClaimPostResult, error) + GetPostLikeCounts(postID string) (int64, error) GetPostsCount(c *CollectionObj, includeFuture bool) GetPosts(cfg *config.Config, c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error) GetAllPostsTaggedIDs(c *Collection, tag string, includeFuture bool) ([]string, error) @@ -1174,6 +1175,12 @@ func (db *datastore) GetPost(id string, collectionID int64) (*PublicPost, error) return nil, ErrPostUnpublished } + // Get additional information needed before processing post data + p.LikeCount, err = db.GetPostLikeCounts(p.ID) + if err != nil { + return nil, err + } + res := p.processPost() if ownerName.Valid { res.Owner = &PublicUser{Username: ownerName.String} @@ -1236,6 +1243,18 @@ func (db *datastore) GetPostProperty(id string, collectionID int64, property str return res, nil } +func (db *datastore) GetPostLikeCounts(postID string) (int64, error) { + var count int64 + err := db.QueryRow("SELECT COUNT(*) FROM remote_likes WHERE post_id = ?", postID).Scan(&count) + switch { + case err == sql.ErrNoRows: + count = 0 + case err != nil: + return 0, err + } + return count, nil +} + // GetPostsCount modifies the CollectionObj to include the correct number of // standard (non-pinned) posts. It will return future posts if `includeFuture` // is true. diff --git a/posts.go b/posts.go index 19f01fd..1cfc1f0 100644 --- a/posts.go +++ b/posts.go @@ -105,6 +105,7 @@ type ( Created time.Time `db:"created" json:"created"` Updated time.Time `db:"updated" json:"updated"` ViewCount int64 `db:"view_count" json:"-"` + LikeCount int64 `db:"like_count" json:"likes"` Title zero.String `db:"title" json:"title"` HTMLTitle template.HTML `db:"title" json:"-"` Content string `db:"content" json:"body"` @@ -127,6 +128,7 @@ type ( IsTopLevel bool `json:"-"` DisplayDate string `json:"-"` Views int64 `json:"views"` + Likes int64 `json:"likes"` Owner *PublicUser `json:"-"` IsOwner bool `json:"-"` URL string `json:"url,omitempty"` @@ -1184,6 +1186,7 @@ func fetchPostProperty(app *App, w http.ResponseWriter, r *http.Request) error { func (p *Post) processPost() PublicPost { res := &PublicPost{Post: p, Views: 0} res.Views = p.ViewCount + res.Likes = p.LikeCount // TODO: move to own function loc := monday.FuzzyLocale(p.Language.String) res.DisplayDate = monday.Format(p.Created, monday.LongFormatsByLocale[loc], loc) diff --git a/templates/collection-post.tmpl b/templates/collection-post.tmpl index 54d5298..455dcfd 100644 --- a/templates/collection-post.tmpl +++ b/templates/collection-post.tmpl @@ -55,6 +55,7 @@ {{range .PinnedPosts}}{{.PlainDisplayTitle}}{{end}} {{end}} {{ if and .IsOwner .IsFound }}{{largeNumFmt .Views}} {{pluralize "view" "views" .Views}} + {{if .Likes}}{{largeNumFmt .Likes}} {{pluralize "like" "likes" .Likes}}{{end}} Edit {{if .IsPinned}}Unpin{{end}} {{ end }} diff --git a/templates/user/stats.tmpl b/templates/user/stats.tmpl index b7f3322..a0c08ec 100644 --- a/templates/user/stats.tmpl +++ b/templates/user/stats.tmpl @@ -51,11 +51,13 @@ td.none { Post {{if not .Collection}}Blog{{end}} Total Views + {{if .Federation}}Likes{{end}} {{range .TopPosts}} {{if ne .DisplayTitle ""}}{{.DisplayTitle}}{{else}}{{.ID}}{{end}} {{ if not $.Collection }}{{if .Collection}}{{.Collection.Title}}{{else}}Draft{{end}}{{ end }} {{.ViewCount}} + {{if $.Federation}}{{.LikeCount}}{{end}} {{end}}