MCPcopy
hub / github.com/writefreely/writefreely / GetPostsTagged

Method GetPostsTagged

database.go:1420–1487  ·  view source on GitHub ↗

GetPostsTagged retrieves all posts on the given Collection that contain the given tag. It will return future posts if `includeFuture` is true. TODO: change includeFuture to isOwner, since that's how it's used

(cfg *config.Config, c *Collection, tag string, page int, includeFuture bool)

Source from the content-addressed store, hash-verified

1418// It will return future posts if `includeFuture` is true.
1419// TODO: change includeFuture to isOwner, since that's how it's used
1420func (db *datastore) GetPostsTagged(cfg *config.Config, c *Collection, tag string, page int, includeFuture bool) (*[]PublicPost, error) {
1421 collID := c.ID
1422
1423 cf := c.NewFormat()
1424 order := "DESC"
1425 if cf.Ascending() {
1426 order = "ASC"
1427 }
1428
1429 pagePosts := cf.PostsPerPage()
1430 start := page*pagePosts - pagePosts
1431 if page == 0 {
1432 start = 0
1433 pagePosts = 1000
1434 }
1435
1436 limitStr := ""
1437 if page > 0 {
1438 limitStr = fmt.Sprintf(" LIMIT %d, %d", start, pagePosts)
1439 }
1440 timeCondition := ""
1441 if !includeFuture {
1442 timeCondition = "AND created <= " + db.now()
1443 }
1444
1445 var rows *sql.Rows
1446 var err error
1447 if db.driverName == driverSQLite {
1448 rows, err = db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? AND LOWER(content) regexp ? "+timeCondition+" ORDER BY created "+order+limitStr, collID, `.*#`+strings.ToLower(tag)+`\b.*`)
1449 } else {
1450 var boundaryRegex string
1451 if db.useSpencerRegex {
1452 // MySQL earlier than 8.0.4, Henry Spencer's regex implementation
1453 boundaryRegex = "[[:>:]]"
1454 } else {
1455 // MySQL 8.0.4+, International Components for Unicode (ICU) syntax
1456 boundaryRegex = "\\b"
1457 }
1458 rows, err = db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? AND LOWER(content) RLIKE ? "+timeCondition+" ORDER BY created "+order+limitStr, collID, "#"+strings.ToLower(tag)+boundaryRegex)
1459 }
1460 if err != nil {
1461 log.Error("Failed selecting from posts: %v", err)
1462 return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve collection posts."}
1463 }
1464 defer rows.Close()
1465
1466 // TODO: extract this common row scanning logic for queries using `postCols`
1467 posts := []PublicPost{}
1468 for rows.Next() {
1469 p := &Post{}
1470 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)
1471 if err != nil {
1472 log.Error("Failed scanning row: %v", err)
1473 break
1474 }
1475 p.extractData()
1476 p.augmentContent(c)
1477 p.formatContent(cfg, c, includeFuture, false)

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected