entry.go 6.4 KB

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