entry.go 16 KB

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