entry.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/v2/internal/storage"
  4. import (
  5. "database/sql"
  6. "errors"
  7. "fmt"
  8. "log/slog"
  9. "time"
  10. "miniflux.app/v2/internal/crypto"
  11. "miniflux.app/v2/internal/model"
  12. "github.com/lib/pq"
  13. )
  14. // CountAllEntries returns the number of entries for each status in the database.
  15. func (s *Storage) CountAllEntries() map[string]int64 {
  16. rows, err := s.db.Query(`SELECT status, count(*) FROM entries GROUP BY status`)
  17. if err != nil {
  18. return nil
  19. }
  20. defer rows.Close()
  21. results := make(map[string]int64)
  22. results[model.EntryStatusUnread] = 0
  23. results[model.EntryStatusRead] = 0
  24. results[model.EntryStatusRemoved] = 0
  25. for rows.Next() {
  26. var status string
  27. var count int64
  28. if err := rows.Scan(&status, &count); err != nil {
  29. continue
  30. }
  31. results[status] = count
  32. }
  33. results["total"] = results[model.EntryStatusUnread] + results[model.EntryStatusRead] + results[model.EntryStatusRemoved]
  34. return results
  35. }
  36. // CountUnreadEntries returns the number of unread entries.
  37. func (s *Storage) CountUnreadEntries(userID int64) int {
  38. builder := s.NewEntryQueryBuilder(userID)
  39. builder.WithStatus(model.EntryStatusUnread)
  40. builder.WithGloballyVisible()
  41. n, err := builder.CountEntries()
  42. if err != nil {
  43. slog.Error("Unable to count unread entries",
  44. slog.Int64("user_id", userID),
  45. slog.Any("error", err),
  46. )
  47. return 0
  48. }
  49. return n
  50. }
  51. // NewEntryQueryBuilder returns a new EntryQueryBuilder
  52. func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
  53. return NewEntryQueryBuilder(s, userID)
  54. }
  55. // UpdateEntryTitleAndContent updates entry title and content.
  56. func (s *Storage) UpdateEntryTitleAndContent(entry *model.Entry) error {
  57. query := `
  58. UPDATE
  59. entries
  60. SET
  61. title=$1,
  62. content=$2,
  63. reading_time=$3,
  64. document_vectors = setweight(to_tsvector($4), 'A') || setweight(to_tsvector($5), 'B')
  65. WHERE
  66. id=$6 AND user_id=$7
  67. `
  68. if _, err := s.db.Exec(
  69. query,
  70. entry.Title,
  71. entry.Content,
  72. entry.ReadingTime,
  73. truncateStringForTSVectorField(entry.Title),
  74. truncateStringForTSVectorField(entry.Content),
  75. entry.ID,
  76. entry.UserID); err != nil {
  77. return fmt.Errorf(`store: unable to update entry #%d: %v`, entry.ID, err)
  78. }
  79. return nil
  80. }
  81. // createEntry add a new entry.
  82. func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
  83. query := `
  84. INSERT INTO entries
  85. (
  86. title,
  87. hash,
  88. url,
  89. comments_url,
  90. published_at,
  91. content,
  92. author,
  93. user_id,
  94. feed_id,
  95. reading_time,
  96. changed_at,
  97. document_vectors,
  98. tags
  99. )
  100. VALUES
  101. (
  102. $1,
  103. $2,
  104. $3,
  105. $4,
  106. $5,
  107. $6,
  108. $7,
  109. $8,
  110. $9,
  111. $10,
  112. now(),
  113. setweight(to_tsvector($11), 'A') || setweight(to_tsvector($12), 'B'),
  114. $13
  115. )
  116. RETURNING
  117. id, status, created_at, changed_at
  118. `
  119. err := tx.QueryRow(
  120. query,
  121. entry.Title,
  122. entry.Hash,
  123. entry.URL,
  124. entry.CommentsURL,
  125. entry.Date,
  126. entry.Content,
  127. entry.Author,
  128. entry.UserID,
  129. entry.FeedID,
  130. entry.ReadingTime,
  131. truncateStringForTSVectorField(entry.Title),
  132. truncateStringForTSVectorField(entry.Content),
  133. pq.Array(entry.Tags),
  134. ).Scan(
  135. &entry.ID,
  136. &entry.Status,
  137. &entry.CreatedAt,
  138. &entry.ChangedAt,
  139. )
  140. if err != nil {
  141. return fmt.Errorf(`store: unable to create entry %q (feed #%d): %v`, entry.URL, entry.FeedID, err)
  142. }
  143. for _, enclosure := range entry.Enclosures {
  144. enclosure.EntryID = entry.ID
  145. enclosure.UserID = entry.UserID
  146. err := s.createEnclosure(tx, enclosure)
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. return nil
  152. }
  153. // updateEntry updates an entry when a feed is refreshed.
  154. // Note: we do not update the published date because some feeds do not contains any date,
  155. // it default to time.Now() which could change the order of items on the history page.
  156. func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
  157. query := `
  158. UPDATE
  159. entries
  160. SET
  161. title=$1,
  162. url=$2,
  163. comments_url=$3,
  164. content=$4,
  165. author=$5,
  166. reading_time=$6,
  167. document_vectors = setweight(to_tsvector($7), 'A') || setweight(to_tsvector($8), 'B'),
  168. tags=$12
  169. WHERE
  170. user_id=$9 AND feed_id=$10 AND hash=$11
  171. RETURNING
  172. id
  173. `
  174. err := tx.QueryRow(
  175. query,
  176. entry.Title,
  177. entry.URL,
  178. entry.CommentsURL,
  179. entry.Content,
  180. entry.Author,
  181. entry.ReadingTime,
  182. truncateStringForTSVectorField(entry.Title),
  183. truncateStringForTSVectorField(entry.Content),
  184. entry.UserID,
  185. entry.FeedID,
  186. entry.Hash,
  187. pq.Array(entry.Tags),
  188. ).Scan(&entry.ID)
  189. if err != nil {
  190. return fmt.Errorf(`store: unable to update entry %q: %v`, entry.URL, err)
  191. }
  192. for _, enclosure := range entry.Enclosures {
  193. enclosure.UserID = entry.UserID
  194. enclosure.EntryID = entry.ID
  195. }
  196. return s.updateEnclosures(tx, entry)
  197. }
  198. // entryExists checks if an entry already exists based on its hash when refreshing a feed.
  199. func (s *Storage) entryExists(tx *sql.Tx, entry *model.Entry) (bool, error) {
  200. var result bool
  201. // Note: This query uses entries_feed_id_hash_key index (filtering on user_id is not necessary).
  202. err := tx.QueryRow(`SELECT true FROM entries WHERE feed_id=$1 AND hash=$2`, entry.FeedID, entry.Hash).Scan(&result)
  203. if err != nil && err != sql.ErrNoRows {
  204. return result, fmt.Errorf(`store: unable to check if entry exists: %v`, err)
  205. }
  206. return result, nil
  207. }
  208. func (s *Storage) IsNewEntry(feedID int64, entryHash string) bool {
  209. var result bool
  210. s.db.QueryRow(`SELECT true FROM entries WHERE feed_id=$1 AND hash=$2`, feedID, entryHash).Scan(&result)
  211. return !result
  212. }
  213. func (s *Storage) GetReadTime(feedID int64, entryHash string) int {
  214. var result int
  215. // Note: This query uses entries_feed_id_hash_key index
  216. s.db.QueryRow(
  217. `SELECT
  218. reading_time
  219. FROM
  220. entries
  221. WHERE
  222. feed_id=$1 AND
  223. hash=$2
  224. `,
  225. feedID,
  226. entryHash,
  227. ).Scan(&result)
  228. return result
  229. }
  230. // cleanupEntries deletes from the database entries marked as "removed" and not visible anymore in the feed.
  231. func (s *Storage) cleanupEntries(feedID int64, entryHashes []string) error {
  232. query := `
  233. DELETE FROM
  234. entries
  235. WHERE
  236. feed_id=$1 AND
  237. status=$2 AND
  238. NOT (hash=ANY($3))
  239. `
  240. if _, err := s.db.Exec(query, feedID, model.EntryStatusRemoved, pq.Array(entryHashes)); err != nil {
  241. return fmt.Errorf(`store: unable to cleanup entries: %v`, err)
  242. }
  243. return nil
  244. }
  245. // RefreshFeedEntries updates feed entries while refreshing a feed.
  246. func (s *Storage) RefreshFeedEntries(userID, feedID int64, entries model.Entries, updateExistingEntries bool) (newEntries model.Entries, err error) {
  247. entryHashes := make([]string, 0, len(entries))
  248. for _, entry := range entries {
  249. entry.UserID = userID
  250. entry.FeedID = feedID
  251. tx, err := s.db.Begin()
  252. if err != nil {
  253. return nil, fmt.Errorf(`store: unable to start transaction: %v`, err)
  254. }
  255. entryExists, err := s.entryExists(tx, entry)
  256. if err != nil {
  257. if rollbackErr := tx.Rollback(); rollbackErr != nil {
  258. return nil, fmt.Errorf(`store: unable to rollback transaction: %v (rolled back due to: %v)`, rollbackErr, err)
  259. }
  260. return nil, err
  261. }
  262. if entryExists {
  263. if updateExistingEntries {
  264. err = s.updateEntry(tx, entry)
  265. }
  266. } else {
  267. err = s.createEntry(tx, entry)
  268. if err == nil {
  269. newEntries = append(newEntries, entry)
  270. }
  271. }
  272. if err != nil {
  273. if rollbackErr := tx.Rollback(); rollbackErr != nil {
  274. return nil, fmt.Errorf(`store: unable to rollback transaction: %v (rolled back due to: %v)`, rollbackErr, err)
  275. }
  276. return nil, err
  277. }
  278. if err := tx.Commit(); err != nil {
  279. return nil, fmt.Errorf(`store: unable to commit transaction: %v`, err)
  280. }
  281. entryHashes = append(entryHashes, entry.Hash)
  282. }
  283. go func() {
  284. if err := s.cleanupEntries(feedID, entryHashes); err != nil {
  285. slog.Error("Unable to cleanup entries",
  286. slog.Int64("user_id", userID),
  287. slog.Int64("feed_id", feedID),
  288. slog.Any("error", err),
  289. )
  290. }
  291. }()
  292. return newEntries, nil
  293. }
  294. // ArchiveEntries changes the status of entries to "removed" after the given number of days.
  295. func (s *Storage) ArchiveEntries(status string, days, limit int) (int64, error) {
  296. if days < 0 || limit <= 0 {
  297. return 0, nil
  298. }
  299. query := `
  300. UPDATE
  301. entries
  302. SET
  303. status=$1
  304. WHERE
  305. id IN (
  306. SELECT
  307. id
  308. FROM
  309. entries
  310. WHERE
  311. status=$2 AND
  312. starred is false AND
  313. share_code='' AND
  314. created_at < now () - $3::interval
  315. ORDER BY
  316. created_at ASC LIMIT $4
  317. )
  318. `
  319. result, err := s.db.Exec(query, model.EntryStatusRemoved, status, fmt.Sprintf("%d days", days), limit)
  320. if err != nil {
  321. return 0, fmt.Errorf(`store: unable to archive %s entries: %v`, status, err)
  322. }
  323. count, err := result.RowsAffected()
  324. if err != nil {
  325. return 0, fmt.Errorf(`store: unable to get the number of rows affected: %v`, err)
  326. }
  327. return count, nil
  328. }
  329. // SetEntriesStatus update the status of the given list of entries.
  330. func (s *Storage) SetEntriesStatus(userID int64, entryIDs []int64, status string) error {
  331. query := `UPDATE entries SET status=$1, changed_at=now() WHERE user_id=$2 AND id=ANY($3)`
  332. if _, err := s.db.Exec(query, status, userID, pq.Array(entryIDs)); err != nil {
  333. return fmt.Errorf(`store: unable to update entries statuses %v: %v`, entryIDs, err)
  334. }
  335. return nil
  336. }
  337. func (s *Storage) SetEntriesStatusCount(userID int64, entryIDs []int64, status string) (int, error) {
  338. if err := s.SetEntriesStatus(userID, entryIDs, status); err != nil {
  339. return 0, err
  340. }
  341. query := `
  342. SELECT count(*)
  343. FROM entries e
  344. JOIN feeds f ON (f.id = e.feed_id)
  345. JOIN categories c ON (c.id = f.category_id)
  346. WHERE e.user_id = $1
  347. AND e.id = ANY($2)
  348. AND NOT f.hide_globally
  349. AND NOT c.hide_globally
  350. `
  351. row := s.db.QueryRow(query, userID, pq.Array(entryIDs))
  352. visible := 0
  353. if err := row.Scan(&visible); err != nil {
  354. return 0, fmt.Errorf(`store: unable to query entries visibility %v: %v`, entryIDs, err)
  355. }
  356. return visible, nil
  357. }
  358. // SetEntriesBookmarked update the bookmarked state for the given list of entries.
  359. func (s *Storage) SetEntriesBookmarkedState(userID int64, entryIDs []int64, starred bool) error {
  360. query := `UPDATE entries SET starred=$1, changed_at=now() WHERE user_id=$2 AND id=ANY($3)`
  361. result, err := s.db.Exec(query, starred, userID, pq.Array(entryIDs))
  362. if err != nil {
  363. return fmt.Errorf(`store: unable to update the bookmarked state %v: %v`, entryIDs, err)
  364. }
  365. count, err := result.RowsAffected()
  366. if err != nil {
  367. return fmt.Errorf(`store: unable to update these entries %v: %v`, entryIDs, err)
  368. }
  369. if count == 0 {
  370. return errors.New(`store: nothing has been updated`)
  371. }
  372. return nil
  373. }
  374. // ToggleBookmark toggles entry bookmark value.
  375. func (s *Storage) ToggleBookmark(userID int64, entryID int64) error {
  376. query := `UPDATE entries SET starred = NOT starred, changed_at=now() WHERE user_id=$1 AND id=$2`
  377. result, err := s.db.Exec(query, userID, entryID)
  378. if err != nil {
  379. return fmt.Errorf(`store: unable to toggle bookmark flag for entry #%d: %v`, entryID, err)
  380. }
  381. count, err := result.RowsAffected()
  382. if err != nil {
  383. return fmt.Errorf(`store: unable to toggle bookmark flag for entry #%d: %v`, entryID, err)
  384. }
  385. if count == 0 {
  386. return errors.New(`store: nothing has been updated`)
  387. }
  388. return nil
  389. }
  390. // FlushHistory changes all entries with the status "read" to "removed".
  391. func (s *Storage) FlushHistory(userID int64) error {
  392. query := `
  393. UPDATE
  394. entries
  395. SET
  396. status=$1,
  397. changed_at=now()
  398. WHERE
  399. user_id=$2 AND status=$3 AND starred is false AND share_code=''
  400. `
  401. _, err := s.db.Exec(query, model.EntryStatusRemoved, userID, model.EntryStatusRead)
  402. if err != nil {
  403. return fmt.Errorf(`store: unable to flush history: %v`, err)
  404. }
  405. return nil
  406. }
  407. // MarkAllAsRead updates all user entries to the read status.
  408. func (s *Storage) MarkAllAsRead(userID int64) error {
  409. query := `UPDATE entries SET status=$1, changed_at=now() WHERE user_id=$2 AND status=$3`
  410. result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread)
  411. if err != nil {
  412. return fmt.Errorf(`store: unable to mark all entries as read: %v`, err)
  413. }
  414. count, _ := result.RowsAffected()
  415. slog.Debug("Marked all entries as read",
  416. slog.Int64("user_id", userID),
  417. slog.Int64("nb_entries", count),
  418. )
  419. return nil
  420. }
  421. // MarkAllAsReadBeforeDate updates all user entries to the read status before the given date.
  422. func (s *Storage) MarkAllAsReadBeforeDate(userID int64, before time.Time) error {
  423. query := `
  424. UPDATE
  425. entries
  426. SET
  427. status=$1,
  428. changed_at=now()
  429. WHERE
  430. user_id=$2 AND status=$3 AND published_at < $4
  431. `
  432. result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread, before)
  433. if err != nil {
  434. return fmt.Errorf(`store: unable to mark all entries as read before %s: %v`, before.Format(time.RFC3339), err)
  435. }
  436. count, _ := result.RowsAffected()
  437. slog.Debug("Marked all entries as read before date",
  438. slog.Int64("user_id", userID),
  439. slog.Int64("nb_entries", count),
  440. slog.String("before", before.Format(time.RFC3339)),
  441. )
  442. return nil
  443. }
  444. // MarkGloballyVisibleFeedsAsRead updates all user entries to the read status.
  445. func (s *Storage) MarkGloballyVisibleFeedsAsRead(userID int64) error {
  446. query := `
  447. UPDATE
  448. entries
  449. SET
  450. status=$1,
  451. changed_at=now()
  452. FROM
  453. feeds
  454. WHERE
  455. entries.feed_id = feeds.id
  456. AND entries.user_id=$2
  457. AND entries.status=$3
  458. AND feeds.hide_globally=$4
  459. `
  460. result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread, false)
  461. if err != nil {
  462. return fmt.Errorf(`store: unable to mark globally visible feeds as read: %v`, err)
  463. }
  464. count, _ := result.RowsAffected()
  465. slog.Debug("Marked globally visible feed entries as read",
  466. slog.Int64("user_id", userID),
  467. slog.Int64("nb_entries", count),
  468. )
  469. return nil
  470. }
  471. // MarkFeedAsRead updates all feed entries to the read status.
  472. func (s *Storage) MarkFeedAsRead(userID, feedID int64, before time.Time) error {
  473. query := `
  474. UPDATE
  475. entries
  476. SET
  477. status=$1,
  478. changed_at=now()
  479. WHERE
  480. user_id=$2 AND feed_id=$3 AND status=$4 AND published_at < $5
  481. `
  482. result, err := s.db.Exec(query, model.EntryStatusRead, userID, feedID, model.EntryStatusUnread, before)
  483. if err != nil {
  484. return fmt.Errorf(`store: unable to mark feed entries as read: %v`, err)
  485. }
  486. count, _ := result.RowsAffected()
  487. slog.Debug("Marked feed entries as read",
  488. slog.Int64("user_id", userID),
  489. slog.Int64("feed_id", feedID),
  490. slog.Int64("nb_entries", count),
  491. slog.String("before", before.Format(time.RFC3339)),
  492. )
  493. return nil
  494. }
  495. // MarkCategoryAsRead updates all category entries to the read status.
  496. func (s *Storage) MarkCategoryAsRead(userID, categoryID int64, before time.Time) error {
  497. query := `
  498. UPDATE
  499. entries
  500. SET
  501. status=$1,
  502. changed_at=now()
  503. FROM
  504. feeds
  505. WHERE
  506. feed_id=feeds.id
  507. AND
  508. feeds.user_id=$2
  509. AND
  510. status=$3
  511. AND
  512. published_at < $4
  513. AND
  514. feeds.category_id=$5
  515. `
  516. result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread, before, categoryID)
  517. if err != nil {
  518. return fmt.Errorf(`store: unable to mark category entries as read: %v`, err)
  519. }
  520. count, _ := result.RowsAffected()
  521. slog.Debug("Marked category entries as read",
  522. slog.Int64("user_id", userID),
  523. slog.Int64("category_id", categoryID),
  524. slog.Int64("nb_entries", count),
  525. slog.String("before", before.Format(time.RFC3339)),
  526. )
  527. return nil
  528. }
  529. // EntryShareCode returns the share code of the provided entry.
  530. // It generates a new one if not already defined.
  531. func (s *Storage) EntryShareCode(userID int64, entryID int64) (shareCode string, err error) {
  532. query := `SELECT share_code FROM entries WHERE user_id=$1 AND id=$2`
  533. err = s.db.QueryRow(query, userID, entryID).Scan(&shareCode)
  534. if err != nil {
  535. err = fmt.Errorf(`store: unable to get share code for entry #%d: %v`, entryID, err)
  536. return
  537. }
  538. if shareCode == "" {
  539. shareCode = crypto.GenerateRandomStringHex(20)
  540. query = `UPDATE entries SET share_code = $1 WHERE user_id=$2 AND id=$3`
  541. _, err = s.db.Exec(query, shareCode, userID, entryID)
  542. if err != nil {
  543. err = fmt.Errorf(`store: unable to set share code for entry #%d: %v`, entryID, err)
  544. return
  545. }
  546. }
  547. return
  548. }
  549. // UnshareEntry removes the share code for the given entry.
  550. func (s *Storage) UnshareEntry(userID int64, entryID int64) (err error) {
  551. query := `UPDATE entries SET share_code='' WHERE user_id=$1 AND id=$2`
  552. _, err = s.db.Exec(query, userID, entryID)
  553. if err != nil {
  554. err = fmt.Errorf(`store: unable to remove share code for entry #%d: %v`, entryID, err)
  555. }
  556. return
  557. }
  558. // truncateStringForTSVectorField truncates a string to fit within the maximum size for a TSVector field in PostgreSQL.
  559. func truncateStringForTSVectorField(s string) string {
  560. // The length of a tsvector (lexemes + positions) must be less than 1 megabyte.
  561. const maxTSVectorSize = 1024 * 1024
  562. if len(s) < maxTSVectorSize {
  563. return s
  564. }
  565. // Truncate to fit under the limit, ensuring we don't break UTF-8 characters
  566. truncated := s[:maxTSVectorSize-1]
  567. // Walk backwards to find the last complete UTF-8 character
  568. for i := len(truncated) - 1; i >= 0; i-- {
  569. if (truncated[i] & 0x80) == 0 {
  570. // ASCII character, we can stop here
  571. return truncated[:i+1]
  572. }
  573. if (truncated[i] & 0xC0) == 0xC0 {
  574. // Start of a multi-byte UTF-8 character
  575. return truncated[:i]
  576. }
  577. }
  578. // Fallback: return empty string if we can't find a valid UTF-8 boundary
  579. return ""
  580. }