Skip to content

Commit

Permalink
Merge pull request #113 from writeas/fix-json-export-2
Browse files Browse the repository at this point in the history
Modify GetPost() to allow inclusion of pinned posts
  • Loading branch information
thebaer committed May 29, 2019
2 parents b6da5d9 + f271e53 commit cf51fc4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion activitypub.go
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions collections.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down
13 changes: 9 additions & 4 deletions database.go
Expand Up @@ -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)
Expand Down Expand Up @@ -1062,10 +1062,11 @@ 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 bool) (*[]PublicPost, error) {
func (db *datastore) GetPosts(c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error) {
collID := c.ID

cf := c.NewFormat()
Expand All @@ -1089,7 +1090,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."}
Expand Down
2 changes: 1 addition & 1 deletion export.go
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion feed.go
Expand Up @@ -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 := ""
Expand Down
2 changes: 1 addition & 1 deletion sitemap.go
Expand Up @@ -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
Expand Down

0 comments on commit cf51fc4

Please sign in to comment.