Show number of Likes on posts and Stats pages

Ref T906
pull/1122/head
Matt Baer 1 month ago
parent 7f1cc6bf8f
commit 984e5bc415
  1. 19
      database.go
  2. 3
      posts.go
  3. 1
      templates/collection-post.tmpl
  4. 2
      templates/user/stats.tmpl

@ -115,6 +115,7 @@ type writestore interface {
DispersePosts(userID int64, postIDs []string) (*[]ClaimPostResult, error) DispersePosts(userID int64, postIDs []string) (*[]ClaimPostResult, error)
ClaimPosts(cfg *config.Config, userID int64, collAlias string, posts *[]ClaimPostRequest) (*[]ClaimPostResult, error) ClaimPosts(cfg *config.Config, userID int64, collAlias string, posts *[]ClaimPostRequest) (*[]ClaimPostResult, error)
GetPostLikeCounts(postID string) (int64, error)
GetPostsCount(c *CollectionObj, includeFuture bool) GetPostsCount(c *CollectionObj, includeFuture bool)
GetPosts(cfg *config.Config, c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error) GetPosts(cfg *config.Config, c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error)
GetAllPostsTaggedIDs(c *Collection, tag string, includeFuture bool) ([]string, 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 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() res := p.processPost()
if ownerName.Valid { if ownerName.Valid {
res.Owner = &PublicUser{Username: ownerName.String} res.Owner = &PublicUser{Username: ownerName.String}
@ -1236,6 +1243,18 @@ func (db *datastore) GetPostProperty(id string, collectionID int64, property str
return res, nil 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 // GetPostsCount modifies the CollectionObj to include the correct number of
// standard (non-pinned) posts. It will return future posts if `includeFuture` // standard (non-pinned) posts. It will return future posts if `includeFuture`
// is true. // is true.

@ -105,6 +105,7 @@ type (
Created time.Time `db:"created" json:"created"` Created time.Time `db:"created" json:"created"`
Updated time.Time `db:"updated" json:"updated"` Updated time.Time `db:"updated" json:"updated"`
ViewCount int64 `db:"view_count" json:"-"` ViewCount int64 `db:"view_count" json:"-"`
LikeCount int64 `db:"like_count" json:"likes"`
Title zero.String `db:"title" json:"title"` Title zero.String `db:"title" json:"title"`
HTMLTitle template.HTML `db:"title" json:"-"` HTMLTitle template.HTML `db:"title" json:"-"`
Content string `db:"content" json:"body"` Content string `db:"content" json:"body"`
@ -127,6 +128,7 @@ type (
IsTopLevel bool `json:"-"` IsTopLevel bool `json:"-"`
DisplayDate string `json:"-"` DisplayDate string `json:"-"`
Views int64 `json:"views"` Views int64 `json:"views"`
Likes int64 `json:"likes"`
Owner *PublicUser `json:"-"` Owner *PublicUser `json:"-"`
IsOwner bool `json:"-"` IsOwner bool `json:"-"`
URL string `json:"url,omitempty"` 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 { func (p *Post) processPost() PublicPost {
res := &PublicPost{Post: p, Views: 0} res := &PublicPost{Post: p, Views: 0}
res.Views = p.ViewCount res.Views = p.ViewCount
res.Likes = p.LikeCount
// TODO: move to own function // TODO: move to own function
loc := monday.FuzzyLocale(p.Language.String) loc := monday.FuzzyLocale(p.Language.String)
res.DisplayDate = monday.Format(p.Created, monday.LongFormatsByLocale[loc], loc) res.DisplayDate = monday.Format(p.Created, monday.LongFormatsByLocale[loc], loc)

@ -55,6 +55,7 @@
{{range .PinnedPosts}}<a class="pinned{{if eq .Slug.String $.Slug.String}} selected{{end}}" href="{{if not $.SingleUser}}/{{$.Collection.Alias}}/{{.Slug.String}}{{else}}{{.CanonicalURL $.Host}}{{end}}">{{.PlainDisplayTitle}}</a>{{end}} {{range .PinnedPosts}}<a class="pinned{{if eq .Slug.String $.Slug.String}} selected{{end}}" href="{{if not $.SingleUser}}/{{$.Collection.Alias}}/{{.Slug.String}}{{else}}{{.CanonicalURL $.Host}}{{end}}">{{.PlainDisplayTitle}}</a>{{end}}
{{end}} {{end}}
{{ if and .IsOwner .IsFound }}<span class="views" dir="ltr"><strong>{{largeNumFmt .Views}}</strong> {{pluralize "view" "views" .Views}}</span> {{ if and .IsOwner .IsFound }}<span class="views" dir="ltr"><strong>{{largeNumFmt .Views}}</strong> {{pluralize "view" "views" .Views}}</span>
{{if .Likes}}<span class="views" dir="ltr"><strong>{{largeNumFmt .Likes}}</strong> {{pluralize "like" "likes" .Likes}}</span>{{end}}
<a class="xtra-feature" href="/{{if not .SingleUser}}{{.Collection.Alias}}/{{end}}{{.Slug.String}}/edit" dir="{{.Direction}}">Edit</a> <a class="xtra-feature" href="/{{if not .SingleUser}}{{.Collection.Alias}}/{{end}}{{.Slug.String}}/edit" dir="{{.Direction}}">Edit</a>
{{if .IsPinned}}<a class="xtra-feature unpin" href="/{{.Collection.Alias}}/{{.Slug.String}}/unpin" dir="{{.Direction}}" onclick="unpinPost(event, '{{.ID}}')">Unpin</a>{{end}} {{if .IsPinned}}<a class="xtra-feature unpin" href="/{{.Collection.Alias}}/{{.Slug.String}}/unpin" dir="{{.Direction}}" onclick="unpinPost(event, '{{.ID}}')">Unpin</a>{{end}}
{{ end }} {{ end }}

@ -51,11 +51,13 @@ td.none {
<th>Post</th> <th>Post</th>
{{if not .Collection}}<th>Blog</th>{{end}} {{if not .Collection}}<th>Blog</th>{{end}}
<th class="num">Total Views</th> <th class="num">Total Views</th>
{{if .Federation}}<th class="num">Likes</th>{{end}}
</tr> </tr>
{{range .TopPosts}}<tr> {{range .TopPosts}}<tr>
<td style="word-break: break-all;"><a href="{{if .Collection}}{{.Collection.CanonicalURL}}{{.Slug.String}}{{else}}/{{.ID}}{{end}}">{{if ne .DisplayTitle ""}}{{.DisplayTitle}}{{else}}<em>{{.ID}}</em>{{end}}</a></td> <td style="word-break: break-all;"><a href="{{if .Collection}}{{.Collection.CanonicalURL}}{{.Slug.String}}{{else}}/{{.ID}}{{end}}">{{if ne .DisplayTitle ""}}{{.DisplayTitle}}{{else}}<em>{{.ID}}</em>{{end}}</a></td>
{{ if not $.Collection }}<td>{{if .Collection}}<a href="{{.Collection.CanonicalURL}}">{{.Collection.Title}}</a>{{else}}<em>Draft</em>{{end}}</td>{{ end }} {{ if not $.Collection }}<td>{{if .Collection}}<a href="{{.Collection.CanonicalURL}}">{{.Collection.Title}}</a>{{else}}<em>Draft</em>{{end}}</td>{{ end }}
<td class="num">{{.ViewCount}}</td> <td class="num">{{.ViewCount}}</td>
{{if $.Federation}}<td class="num">{{.LikeCount}}</td>{{end}}
</tr>{{end}} </tr>{{end}}
</table> </table>

Loading…
Cancel
Save