entry.go 16 KB

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