From 95e84a1d0e3e23a3156ab624b2f5e9b51ef9a8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=ABlle=20Anthony?= Date: Tue, 28 May 2019 14:54:56 -0400 Subject: [PATCH 1/2] Change GetPosts() to have includePinned parameter, change all calls to match --- activitypub.go | 2 +- collections.go | 4 ++-- database.go | 10 +++++++--- export.go | 2 +- feed.go | 2 +- sitemap.go | 2 +- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/activitypub.go b/activitypub.go index 3b0ac82..926902c 100644 --- a/activitypub.go +++ b/activitypub.go @@ -127,7 +127,7 @@ func handleFetchCollectionOutbox(app *app, w http.ResponseWriter, r *http.Reques ocp := activitystreams.NewOrderedCollectionPage(accountRoot, "outbox", res.TotalPosts, p) ocp.OrderedItems = []interface{}{} - posts, err := app.db.GetPosts(c, p, false, true) + posts, err := app.db.GetPosts(c, p, false, true, false) for _, pp := range *posts { pp.Collection = res o := pp.ActivityObject() diff --git a/collections.go b/collections.go index cd7be58..391d153 100644 --- a/collections.go +++ b/collections.go @@ -492,7 +492,7 @@ func fetchCollectionPosts(app *app, w http.ResponseWriter, r *http.Request) erro } } - posts, err := app.db.GetPosts(c, page, isCollOwner, false) + posts, err := app.db.GetPosts(c, page, isCollOwner, false, false) if err != nil { return err } @@ -723,7 +723,7 @@ func handleViewCollection(app *app, w http.ResponseWriter, r *http.Request) erro return impart.HTTPError{http.StatusFound, redirURL} } - coll.Posts, _ = app.db.GetPosts(c, page, cr.isCollOwner, false) + coll.Posts, _ = app.db.GetPosts(c, page, cr.isCollOwner, false, false) // Serve collection displayPage := CollectionPage{ diff --git a/database.go b/database.go index 4e64dbb..31f8c06 100644 --- a/database.go +++ b/database.go @@ -104,7 +104,7 @@ type writestore interface { ClaimPosts(userID int64, collAlias string, posts *[]ClaimPostRequest) (*[]ClaimPostResult, error) GetPostsCount(c *CollectionObj, includeFuture bool) - GetPosts(c *Collection, page int, includeFuture, forceRecentFirst bool) (*[]PublicPost, error) + GetPosts(c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error) GetPostsTagged(c *Collection, tag string, page int, includeFuture bool) (*[]PublicPost, error) GetAPFollowers(c *Collection) (*[]RemoteUser, error) @@ -1065,7 +1065,7 @@ func (db *datastore) GetPostsCount(c *CollectionObj, includeFuture bool) { // GetPosts retrieves all standard (non-pinned) posts for the given Collection. // It will return future posts if `includeFuture` is true. // TODO: change includeFuture to isOwner, since that's how it's used -func (db *datastore) GetPosts(c *Collection, page int, includeFuture, forceRecentFirst bool) (*[]PublicPost, error) { +func (db *datastore) GetPosts(c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error) { collID := c.ID cf := c.NewFormat() @@ -1089,7 +1089,11 @@ func (db *datastore) GetPosts(c *Collection, page int, includeFuture, forceRecen if !includeFuture { timeCondition = "AND created <= " + db.now() } - rows, err := db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? AND pinned_position IS NULL "+timeCondition+" ORDER BY created "+order+limitStr, collID) + pinnedCondition := "" + if !includePinned { + pinnedCondition = "AND pinned_position IS NULL" + } + rows, err := db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? "+pinnedCondition+" "+timeCondition+" ORDER BY created "+order+limitStr, collID) if err != nil { log.Error("Failed selecting from posts: %v", err) return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve collection posts."} diff --git a/export.go b/export.go index e23b850..4aa00fd 100644 --- a/export.go +++ b/export.go @@ -111,7 +111,7 @@ func compileFullExport(app *app, u *User) *ExportUser { var collObjs []CollectionObj for _, c := range *colls { co := &CollectionObj{Collection: c} - co.Posts, err = app.db.GetPosts(&c, 0, true, false) + co.Posts, err = app.db.GetPosts(&c, 0, true, false, true) if err != nil { log.Error("unable to get collection posts: %v", err) } diff --git a/feed.go b/feed.go index 89043d3..fc6478d 100644 --- a/feed.go +++ b/feed.go @@ -56,7 +56,7 @@ func ViewFeed(app *app, w http.ResponseWriter, req *http.Request) error { if tag != "" { coll.Posts, _ = app.db.GetPostsTagged(c, tag, 1, false) } else { - coll.Posts, _ = app.db.GetPosts(c, 1, false, true) + coll.Posts, _ = app.db.GetPosts(c, 1, false, true, false) } author := "" diff --git a/sitemap.go b/sitemap.go index 9bb168a..517a379 100644 --- a/sitemap.go +++ b/sitemap.go @@ -64,7 +64,7 @@ func handleViewSitemap(app *app, w http.ResponseWriter, r *http.Request) error { host = c.CanonicalURL() sm := buildSitemap(host, pre) - posts, err := app.db.GetPosts(c, 0, false, false) + posts, err := app.db.GetPosts(c, 0, false, false, false) if err != nil { log.Error("Error getting posts: %v", err) return err From f271e53925d77c4f183759757f2452baf5902b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=ABlle=20Anthony?= Date: Wed, 29 May 2019 12:03:01 -0400 Subject: [PATCH 2/2] Update GetPosts() docstring --- database.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database.go b/database.go index 31f8c06..e26c6c6 100644 --- a/database.go +++ b/database.go @@ -1062,8 +1062,9 @@ func (db *datastore) GetPostsCount(c *CollectionObj, includeFuture bool) { c.TotalPosts = int(count) } -// GetPosts retrieves all standard (non-pinned) posts for the given Collection. +// GetPosts retrieves all posts for the given Collection. // It will return future posts if `includeFuture` is true. +// It will include only standard (non-pinned) posts unless `includePinned` is true. // TODO: change includeFuture to isOwner, since that's how it's used func (db *datastore) GetPosts(c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error) { collID := c.ID