MCPcopy
hub / github.com/writefreely/writefreely / GetPosts

Method GetPosts

database.go:1303–1369  ·  view source on GitHub ↗

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

(cfg *config.Config, c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool, contentType PostType)

Source from the content-addressed store, hash-verified

1301// It will include only standard (non-pinned) posts unless `includePinned` is true.
1302// TODO: change includeFuture to isOwner, since that's how it's used
1303func (db *datastore) GetPosts(cfg *config.Config, c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool, contentType PostType) (*[]PublicPost, error) {
1304 collID := c.ID
1305
1306 cf := c.NewFormat()
1307 order := "DESC"
1308 if cf.Ascending() && !forceRecentFirst {
1309 order = "ASC"
1310 }
1311
1312 pagePosts := cf.PostsPerPage()
1313 start := page*pagePosts - pagePosts
1314 if page == 0 {
1315 start = 0
1316 pagePosts = 1000
1317 } else if contentType == postArch {
1318 pagePosts = postsPerArchPage
1319 start = page*pagePosts - pagePosts
1320 }
1321
1322 limitStr := ""
1323 if page > 0 {
1324 limitStr = fmt.Sprintf(" LIMIT %d, %d", start, pagePosts)
1325 }
1326 timeCondition := ""
1327 if !includeFuture {
1328 timeCondition = "AND created <= " + db.now()
1329 }
1330 pinnedCondition := ""
1331 if !includePinned {
1332 pinnedCondition = "AND pinned_position IS NULL"
1333 }
1334 // FUTURE: handle different post contentType's here
1335 rows, err := db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? "+pinnedCondition+" "+timeCondition+" ORDER BY created "+order+limitStr, collID)
1336 if err != nil {
1337 log.Error("Failed selecting from posts: %v", err)
1338 return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve collection posts."}
1339 }
1340 defer rows.Close()
1341
1342 // TODO: extract this common row scanning logic for queries using `postCols`
1343 posts := []PublicPost{}
1344 for rows.Next() {
1345 p := &Post{}
1346 err = rows.Scan(&p.ID, &p.Slug, &p.Font, &p.Language, &p.RTL, &p.Privacy, &p.OwnerID, &p.CollectionID, &p.PinnedPosition, &p.Created, &p.Updated, &p.ViewCount, &p.Title, &p.Content)
1347 if err != nil {
1348 log.Error("Failed scanning row: %v", err)
1349 break
1350 }
1351 p.extractData()
1352 p.augmentContent(c)
1353 p.formatContent(cfg, c, includeFuture, false)
1354
1355 pubPost := p.processPost()
1356 if contentType == postArch {
1357 // Overwrite DisplayDate with special Archive page version
1358 loc := monday.FuzzyLocale(pubPost.Language.String)
1359 pubPost.DisplayDate = monday.Format(pubPost.Created, monday.LongNoYrFormatsByLocale[loc], loc)
1360 }

Callers

nothing calls this directly

Calls 10

nowMethod · 0.95
extractDataMethod · 0.95
augmentContentMethod · 0.95
formatContentMethod · 0.95
processPostMethod · 0.95
NewFormatMethod · 0.80
AscendingMethod · 0.80
PostsPerPageMethod · 0.80
CloseMethod · 0.80
FormatMethod · 0.80

Tested by

no test coverage detected