entry.go 6.3 KB

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