entry.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. "time"
  9. "github.com/miniflux/miniflux/logger"
  10. "github.com/miniflux/miniflux/model"
  11. "github.com/miniflux/miniflux/timer"
  12. "github.com/lib/pq"
  13. )
  14. // NewEntryQueryBuilder returns a new EntryQueryBuilder
  15. func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
  16. return NewEntryQueryBuilder(s, userID)
  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, comments_url, published_at, content, author, user_id, feed_id)
  23. VALUES
  24. ($1, $2, $3, $4, $5, $6, $7, $8, $9)
  25. RETURNING id
  26. `
  27. err := s.db.QueryRow(
  28. query,
  29. entry.Title,
  30. entry.Hash,
  31. entry.URL,
  32. entry.CommentsURL,
  33. entry.Date,
  34. entry.Content,
  35. entry.Author,
  36. entry.UserID,
  37. entry.FeedID,
  38. ).Scan(&entry.ID)
  39. if err != nil {
  40. return fmt.Errorf("unable to create entry: %v", err)
  41. }
  42. entry.Status = "unread"
  43. for i := 0; i < len(entry.Enclosures); i++ {
  44. entry.Enclosures[i].EntryID = entry.ID
  45. entry.Enclosures[i].UserID = entry.UserID
  46. err := s.CreateEnclosure(entry.Enclosures[i])
  47. if err != nil {
  48. return err
  49. }
  50. }
  51. return nil
  52. }
  53. // UpdateEntryContent updates entry content.
  54. func (s *Storage) UpdateEntryContent(entry *model.Entry) error {
  55. query := `
  56. UPDATE entries SET
  57. content=$1
  58. WHERE user_id=$2 AND id=$3
  59. `
  60. _, err := s.db.Exec(
  61. query,
  62. entry.Content,
  63. entry.UserID,
  64. entry.ID,
  65. )
  66. return err
  67. }
  68. // updateEntry updates an entry when a feed is refreshed.
  69. // Note: we do not update the published date because some feeds do not contains any date,
  70. // it default to time.Now() which could change the order of items on the history page.
  71. func (s *Storage) updateEntry(entry *model.Entry) error {
  72. query := `
  73. UPDATE entries SET
  74. title=$1, url=$2, comments_url=$3, content=$4, author=$5
  75. WHERE user_id=$6 AND feed_id=$7 AND hash=$8
  76. RETURNING id
  77. `
  78. err := s.db.QueryRow(
  79. query,
  80. entry.Title,
  81. entry.URL,
  82. entry.CommentsURL,
  83. entry.Content,
  84. entry.Author,
  85. entry.UserID,
  86. entry.FeedID,
  87. entry.Hash,
  88. ).Scan(&entry.ID)
  89. if err != nil {
  90. return err
  91. }
  92. for _, enclosure := range entry.Enclosures {
  93. enclosure.UserID = entry.UserID
  94. enclosure.EntryID = entry.ID
  95. }
  96. return s.UpdateEnclosures(entry.Enclosures)
  97. }
  98. // entryExists checks if an entry already exists based on its hash when refreshing a feed.
  99. func (s *Storage) entryExists(entry *model.Entry) bool {
  100. var result int
  101. query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`
  102. s.db.QueryRow(query, entry.UserID, entry.FeedID, entry.Hash).Scan(&result)
  103. return result >= 1
  104. }
  105. // UpdateEntries updates a list of entries while refreshing a feed.
  106. func (s *Storage) UpdateEntries(userID, feedID int64, entries model.Entries, updateExistingEntries bool) (err error) {
  107. var entryHashes []string
  108. for _, entry := range entries {
  109. entry.UserID = userID
  110. entry.FeedID = feedID
  111. if s.entryExists(entry) {
  112. if updateExistingEntries {
  113. err = s.updateEntry(entry)
  114. }
  115. } else {
  116. err = s.createEntry(entry)
  117. }
  118. if err != nil {
  119. return err
  120. }
  121. entryHashes = append(entryHashes, entry.Hash)
  122. }
  123. if err := s.cleanupEntries(feedID, entryHashes); err != nil {
  124. logger.Error("[Storage:CleanupEntries] %v", err)
  125. }
  126. return nil
  127. }
  128. // cleanupEntries deletes from the database entries marked as "removed" and not visible anymore in the feed.
  129. func (s *Storage) cleanupEntries(feedID int64, entryHashes []string) error {
  130. query := `
  131. DELETE FROM entries
  132. WHERE feed_id=$1 AND
  133. id IN (SELECT id FROM entries WHERE feed_id=$2 AND status=$3 AND NOT (hash=ANY($4)))
  134. `
  135. if _, err := s.db.Exec(query, feedID, feedID, model.EntryStatusRemoved, pq.Array(entryHashes)); err != nil {
  136. return fmt.Errorf("unable to cleanup entries: %v", err)
  137. }
  138. return nil
  139. }
  140. // SetEntriesStatus update the status of the given list of entries.
  141. func (s *Storage) SetEntriesStatus(userID int64, entryIDs []int64, status string) error {
  142. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:SetEntriesStatus] userID=%d, entryIDs=%v, status=%s", userID, entryIDs, status))
  143. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND id=ANY($3)`
  144. result, err := s.db.Exec(query, status, userID, pq.Array(entryIDs))
  145. if err != nil {
  146. return fmt.Errorf("unable to update entries status: %v", err)
  147. }
  148. count, err := result.RowsAffected()
  149. if err != nil {
  150. return fmt.Errorf("unable to update these entries: %v", err)
  151. }
  152. if count == 0 {
  153. return errors.New("nothing has been updated")
  154. }
  155. return nil
  156. }
  157. // ToggleBookmark toggles entry bookmark value.
  158. func (s *Storage) ToggleBookmark(userID int64, entryID int64) error {
  159. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:ToggleBookmark] userID=%d, entryID=%d", userID, entryID))
  160. query := `UPDATE entries SET starred = NOT starred WHERE user_id=$1 AND id=$2`
  161. result, err := s.db.Exec(query, userID, entryID)
  162. if err != nil {
  163. return fmt.Errorf("unable to toggle bookmark flag: %v", err)
  164. }
  165. count, err := result.RowsAffected()
  166. if err != nil {
  167. return fmt.Errorf("unable to toogle bookmark flag: %v", err)
  168. }
  169. if count == 0 {
  170. return errors.New("nothing has been updated")
  171. }
  172. return nil
  173. }
  174. // FlushHistory set all entries with the status "read" to "removed".
  175. func (s *Storage) FlushHistory(userID int64) error {
  176. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FlushHistory] userID=%d", userID))
  177. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3 AND starred='f'`
  178. _, err := s.db.Exec(query, model.EntryStatusRemoved, userID, model.EntryStatusRead)
  179. if err != nil {
  180. return fmt.Errorf("unable to flush history: %v", err)
  181. }
  182. return nil
  183. }
  184. // MarkAllAsRead set all entries with the status "unread" to "read".
  185. func (s *Storage) MarkAllAsRead(userID int64) error {
  186. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:MarkAllAsRead] userID=%d", userID))
  187. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3`
  188. _, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread)
  189. if err != nil {
  190. return fmt.Errorf("unable to mark all entries as read: %v", err)
  191. }
  192. return nil
  193. }
  194. // EntryURLExists returns true if an entry with this URL already exists.
  195. func (s *Storage) EntryURLExists(userID int64, entryURL string) bool {
  196. var result int
  197. query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND url=$2`
  198. s.db.QueryRow(query, userID, entryURL).Scan(&result)
  199. return result >= 1
  200. }