entry.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package storage
  5. import (
  6. "errors"
  7. "fmt"
  8. "log"
  9. "time"
  10. "github.com/miniflux/miniflux2/helper"
  11. "github.com/miniflux/miniflux2/model"
  12. "github.com/lib/pq"
  13. )
  14. // GetEntryQueryBuilder returns a new EntryQueryBuilder
  15. func (s *Storage) GetEntryQueryBuilder(userID int64, timezone string) *EntryQueryBuilder {
  16. return NewEntryQueryBuilder(s, userID, timezone)
  17. }
  18. // CreateEntry add a new entry.
  19. func (s *Storage) CreateEntry(entry *model.Entry) error {
  20. query := `
  21. INSERT INTO entries
  22. (title, hash, url, published_at, content, author, user_id, feed_id)
  23. VALUES
  24. ($1, $2, $3, $4, $5, $6, $7, $8)
  25. RETURNING id
  26. `
  27. err := s.db.QueryRow(
  28. query,
  29. entry.Title,
  30. entry.Hash,
  31. entry.URL,
  32. entry.Date,
  33. entry.Content,
  34. entry.Author,
  35. entry.UserID,
  36. entry.FeedID,
  37. ).Scan(&entry.ID)
  38. if err != nil {
  39. return fmt.Errorf("unable to create entry: %v", err)
  40. }
  41. entry.Status = "unread"
  42. for i := 0; i < len(entry.Enclosures); i++ {
  43. entry.Enclosures[i].EntryID = entry.ID
  44. entry.Enclosures[i].UserID = entry.UserID
  45. err := s.CreateEnclosure(entry.Enclosures[i])
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. return nil
  51. }
  52. // UpdateEntry update an entry when a feed is refreshed.
  53. func (s *Storage) UpdateEntry(entry *model.Entry) error {
  54. query := `
  55. UPDATE entries SET
  56. title=$1, url=$2, published_at=$3, content=$4, author=$5
  57. WHERE user_id=$6 AND feed_id=$7 AND hash=$8
  58. RETURNING id
  59. `
  60. err := s.db.QueryRow(
  61. query,
  62. entry.Title,
  63. entry.URL,
  64. entry.Date,
  65. entry.Content,
  66. entry.Author,
  67. entry.UserID,
  68. entry.FeedID,
  69. entry.Hash,
  70. ).Scan(&entry.ID)
  71. if err != nil {
  72. return err
  73. }
  74. for _, enclosure := range entry.Enclosures {
  75. enclosure.UserID = entry.UserID
  76. enclosure.EntryID = entry.ID
  77. }
  78. return s.UpdateEnclosures(entry.Enclosures)
  79. }
  80. // EntryExists checks if an entry already exists based on its hash when refreshing a feed.
  81. func (s *Storage) EntryExists(entry *model.Entry) bool {
  82. var result int
  83. query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`
  84. s.db.QueryRow(query, entry.UserID, entry.FeedID, entry.Hash).Scan(&result)
  85. return result >= 1
  86. }
  87. // UpdateEntries update a list of entries while refreshing a feed.
  88. func (s *Storage) UpdateEntries(userID, feedID int64, entries model.Entries) (err error) {
  89. var entryHashes []string
  90. for _, entry := range entries {
  91. entry.UserID = userID
  92. entry.FeedID = feedID
  93. if s.EntryExists(entry) {
  94. err = s.UpdateEntry(entry)
  95. } else {
  96. err = s.CreateEntry(entry)
  97. }
  98. if err != nil {
  99. return err
  100. }
  101. entryHashes = append(entryHashes, entry.Hash)
  102. }
  103. if err := s.CleanupEntries(feedID, entryHashes); err != nil {
  104. log.Println(err)
  105. }
  106. return nil
  107. }
  108. // CleanupEntries deletes from the database entries marked as "removed" and not visible anymore in the feed.
  109. func (s *Storage) CleanupEntries(feedID int64, entryHashes []string) error {
  110. query := `
  111. DELETE FROM entries
  112. WHERE feed_id=$1 AND
  113. id IN (SELECT id FROM entries WHERE feed_id=$2 AND status=$3 AND NOT (hash=ANY($4)))
  114. `
  115. if _, err := s.db.Exec(query, feedID, feedID, model.EntryStatusRemoved, pq.Array(entryHashes)); err != nil {
  116. return fmt.Errorf("unable to cleanup entries: %v", err)
  117. }
  118. return nil
  119. }
  120. // SetEntriesStatus update the status of the given list of entries.
  121. func (s *Storage) SetEntriesStatus(userID int64, entryIDs []int64, status string) error {
  122. defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:SetEntriesStatus] userID=%d, entryIDs=%v, status=%s", userID, entryIDs, status))
  123. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND id=ANY($3)`
  124. result, err := s.db.Exec(query, status, userID, pq.Array(entryIDs))
  125. if err != nil {
  126. return fmt.Errorf("unable to update entries status: %v", err)
  127. }
  128. count, err := result.RowsAffected()
  129. if err != nil {
  130. return fmt.Errorf("unable to update these entries: %v", err)
  131. }
  132. if count == 0 {
  133. return errors.New("nothing has been updated")
  134. }
  135. return nil
  136. }
  137. // FlushHistory set all entries with the status "read" to "removed".
  138. func (s *Storage) FlushHistory(userID int64) error {
  139. defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FlushHistory] userID=%d", userID))
  140. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3`
  141. _, err := s.db.Exec(query, model.EntryStatusRemoved, userID, model.EntryStatusRead)
  142. if err != nil {
  143. return fmt.Errorf("unable to flush history: %v", err)
  144. }
  145. return nil
  146. }