Skip to content

Commit a34af95

Browse files
committed
refactor(prose): only display 1 post per user in feed
1 parent eea5880 commit a34af95

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

pkg/apps/prose/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func readHandler(w http.ResponseWriter, r *http.Request) {
548548
var pager *db.Paginate[*db.Post]
549549
var err error
550550
if tag == "" {
551-
pager, err = dbpool.FindAllPosts(&db.Pager{Num: 30, Page: page}, cfg.Space)
551+
pager, err = dbpool.FindPostsForFeed(&db.Pager{Num: 30, Page: page}, cfg.Space)
552552
} else {
553553
pager, err = dbpool.FindPostsByTag(&db.Pager{Num: 30, Page: page}, tag, cfg.Space)
554554
}
@@ -762,7 +762,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
762762
logger := shared.GetLogger(r)
763763
cfg := shared.GetCfg(r)
764764

765-
pager, err := dbpool.FindAllPosts(&db.Pager{Num: 25, Page: 0}, cfg.Space)
765+
pager, err := dbpool.FindPostsForFeed(&db.Pager{Num: 25, Page: 0}, cfg.Space)
766766
if err != nil {
767767
logger.Error("find all posts", "err", err.Error())
768768
http.Error(w, err.Error(), http.StatusInternalServerError)

pkg/db/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ type DB interface {
413413
FindUpdatedPostsForUser(userID string, space string) ([]*Post, error)
414414
FindPostWithFilename(filename string, userID string, space string) (*Post, error)
415415
FindPostWithSlug(slug string, userID string, space string) (*Post, error)
416-
FindAllPosts(pager *Pager, space string) (*Paginate[*Post], error)
416+
FindPostsForFeed(pager *Pager, space string) (*Paginate[*Post], error)
417417
FindAllUpdatedPosts(pager *Pager, space string) (*Paginate[*Post], error)
418418
InsertPost(post *Post) (*Post, error)
419419
UpdatePost(post *Post) (*Post, error)

pkg/db/postgres/storage.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,28 @@ const (
183183
// add some users to deny list since they are robogenerating a bunch of posts
184184
// per day and are creating a lot of noise.
185185
sqlSelectPostsByRank = `
186-
SELECT
187-
posts.id,
188-
user_id,
189-
filename,
190-
slug,
191-
title,
192-
text,
193-
description,
194-
publish_at,
195-
app_users.name as username,
196-
posts.updated_at,
197-
posts.mime_type
198-
FROM posts
199-
LEFT JOIN app_users ON app_users.id = posts.user_id
200-
WHERE
201-
hidden = FALSE AND
202-
publish_at::date <= CURRENT_DATE AND
203-
cur_space = $3 AND
204-
app_users.name NOT IN ('algiegray', 'mrrccc')
186+
SELECT *
187+
FROM (
188+
SELECT DISTINCT ON (posts.user_id)
189+
posts.id,
190+
posts.user_id,
191+
posts.filename,
192+
posts.slug,
193+
posts.title,
194+
posts.text,
195+
posts.description,
196+
posts.publish_at,
197+
app_users.name AS username,
198+
posts.updated_at,
199+
posts.mime_type
200+
FROM posts
201+
LEFT JOIN app_users ON app_users.id = posts.user_id
202+
WHERE
203+
hidden = FALSE
204+
AND publish_at::date <= CURRENT_DATE
205+
AND cur_space = $3
206+
ORDER BY posts.user_id, publish_at DESC
207+
) AS latest_posts
205208
ORDER BY publish_at DESC
206209
LIMIT $1 OFFSET $2`
207210

@@ -757,7 +760,7 @@ func (me *PsqlDB) postPager(rs *sql.Rows, pageNum int, space string, tag string)
757760
return pager, nil
758761
}
759762

760-
func (me *PsqlDB) FindAllPosts(page *db.Pager, space string) (*db.Paginate[*db.Post], error) {
763+
func (me *PsqlDB) FindPostsForFeed(page *db.Pager, space string) (*db.Paginate[*db.Post], error) {
761764
rs, err := me.Db.Query(sqlSelectPostsByRank, page.Num, page.Num*page.Page, space)
762765
if err != nil {
763766
return nil, err

pkg/db/stub/stub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (me *StubDB) FindPost(postID string) (*db.Post, error) {
105105
return nil, errNotImpl
106106
}
107107

108-
func (me *StubDB) FindAllPosts(page *db.Pager, space string) (*db.Paginate[*db.Post], error) {
108+
func (me *StubDB) FindPostsForFeed(page *db.Pager, space string) (*db.Paginate[*db.Post], error) {
109109
return &db.Paginate[*db.Post]{}, errNotImpl
110110
}
111111

0 commit comments

Comments
 (0)