entry.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 // import "miniflux.app/storage"
  5. import (
  6. "errors"
  7. "fmt"
  8. "time"
  9. "miniflux.app/logger"
  10. "miniflux.app/model"
  11. "miniflux.app/timer"
  12. "github.com/lib/pq"
  13. )
  14. // CountUnreadEntries returns the number of unread entries.
  15. func (s *Storage) CountUnreadEntries(userID int64) int {
  16. builder := s.NewEntryQueryBuilder(userID)
  17. builder.WithStatus(model.EntryStatusUnread)
  18. n, err := builder.CountEntries()
  19. if err != nil {
  20. logger.Error("unable to count unread entries for user #%d: %v", userID, err)
  21. return 0
  22. }
  23. return n
  24. }
  25. // NewEntryQueryBuilder returns a new EntryQueryBuilder
  26. func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
  27. return NewEntryQueryBuilder(s, userID)
  28. }
  29. // UpdateEntryContent updates entry content.
  30. func (s *Storage) UpdateEntryContent(entry *model.Entry) error {
  31. tx, err := s.db.Begin()
  32. if err != nil {
  33. return err
  34. }
  35. _, err = tx.Exec(`UPDATE entries SET content=$1 WHERE id=$2 AND user_id=$3`, entry.Content, entry.ID, entry.UserID)
  36. if err != nil {
  37. tx.Rollback()
  38. return fmt.Errorf(`unable to update content of entry #%d: %v`, entry.ID, err)
  39. }
  40. query := `
  41. UPDATE entries
  42. SET document_vectors = setweight(to_tsvector(substring(coalesce(title, '') for 1000000)), 'A') || setweight(to_tsvector(substring(coalesce(content, '') for 1000000)), 'B')
  43. WHERE id=$1 AND user_id=$2
  44. `
  45. _, err = tx.Exec(query, entry.ID, entry.UserID)
  46. if err != nil {
  47. tx.Rollback()
  48. return fmt.Errorf(`unable to update content of entry #%d: %v`, entry.ID, err)
  49. }
  50. return tx.Commit()
  51. }
  52. // createEntry add a new entry.
  53. func (s *Storage) createEntry(entry *model.Entry) error {
  54. query := `
  55. INSERT INTO entries
  56. (title, hash, url, comments_url, published_at, content, author, user_id, feed_id, document_vectors)
  57. VALUES
  58. ($1, $2, $3, $4, $5, $6, $7, $8, $9, setweight(to_tsvector(substring(coalesce($1, '') for 1000000)), 'A') || setweight(to_tsvector(substring(coalesce($6, '') for 1000000)), 'B'))
  59. RETURNING id, status
  60. `
  61. err := s.db.QueryRow(
  62. query,
  63. entry.Title,
  64. entry.Hash,
  65. entry.URL,
  66. entry.CommentsURL,
  67. entry.Date,
  68. entry.Content,
  69. entry.Author,
  70. entry.UserID,
  71. entry.FeedID,
  72. ).Scan(&entry.ID, &entry.Status)
  73. if err != nil {
  74. return fmt.Errorf("unable to create entry %q (feed #%d): %v", entry.URL, entry.FeedID, err)
  75. }
  76. for i := 0; i < len(entry.Enclosures); i++ {
  77. entry.Enclosures[i].EntryID = entry.ID
  78. entry.Enclosures[i].UserID = entry.UserID
  79. err := s.CreateEnclosure(entry.Enclosures[i])
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. // updateEntry updates an entry when a feed is refreshed.
  87. // Note: we do not update the published date because some feeds do not contains any date,
  88. // it default to time.Now() which could change the order of items on the history page.
  89. func (s *Storage) updateEntry(entry *model.Entry) error {
  90. query := `
  91. UPDATE entries SET
  92. title=$1, url=$2, comments_url=$3, content=$4, author=$5,
  93. document_vectors = setweight(to_tsvector(substring(coalesce($1, '') for 1000000)), 'A') || setweight(to_tsvector(substring(coalesce($4, '') for 1000000)), 'B')
  94. WHERE user_id=$6 AND feed_id=$7 AND hash=$8
  95. RETURNING id
  96. `
  97. err := s.db.QueryRow(
  98. query,
  99. entry.Title,
  100. entry.URL,
  101. entry.CommentsURL,
  102. entry.Content,
  103. entry.Author,
  104. entry.UserID,
  105. entry.FeedID,
  106. entry.Hash,
  107. ).Scan(&entry.ID)
  108. if err != nil {
  109. return fmt.Errorf(`unable to update entry %q: %v`, entry.URL, err)
  110. }
  111. for _, enclosure := range entry.Enclosures {
  112. enclosure.UserID = entry.UserID
  113. enclosure.EntryID = entry.ID
  114. }
  115. return s.UpdateEnclosures(entry.Enclosures)
  116. }
  117. // entryExists checks if an entry already exists based on its hash when refreshing a feed.
  118. func (s *Storage) entryExists(entry *model.Entry) bool {
  119. var result int
  120. query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`
  121. s.db.QueryRow(query, entry.UserID, entry.FeedID, entry.Hash).Scan(&result)
  122. return result >= 1
  123. }
  124. // cleanupEntries deletes from the database entries marked as "removed" and not visible anymore in the feed.
  125. func (s *Storage) cleanupEntries(feedID int64, entryHashes []string) error {
  126. query := `
  127. DELETE FROM entries
  128. WHERE feed_id=$1 AND
  129. id IN (SELECT id FROM entries WHERE feed_id=$2 AND status=$3 AND NOT (hash=ANY($4)))
  130. `
  131. if _, err := s.db.Exec(query, feedID, feedID, model.EntryStatusRemoved, pq.Array(entryHashes)); err != nil {
  132. return fmt.Errorf("unable to cleanup entries: %v", err)
  133. }
  134. return nil
  135. }
  136. // UpdateEntries updates a list of entries while refreshing a feed.
  137. func (s *Storage) UpdateEntries(userID, feedID int64, entries model.Entries, updateExistingEntries bool) (err error) {
  138. var entryHashes []string
  139. for _, entry := range entries {
  140. entry.UserID = userID
  141. entry.FeedID = feedID
  142. if s.entryExists(entry) {
  143. if updateExistingEntries {
  144. err = s.updateEntry(entry)
  145. }
  146. } else {
  147. err = s.createEntry(entry)
  148. }
  149. if err != nil {
  150. return err
  151. }
  152. entryHashes = append(entryHashes, entry.Hash)
  153. }
  154. if err := s.cleanupEntries(feedID, entryHashes); err != nil {
  155. logger.Error("[Storage:CleanupEntries] feed #%d: %v", feedID, err)
  156. }
  157. return nil
  158. }
  159. // ArchiveEntries changes the status of read items to "removed" after specified days.
  160. func (s *Storage) ArchiveEntries(days int) error {
  161. query := fmt.Sprintf(`
  162. UPDATE entries SET status='removed'
  163. WHERE id=ANY(SELECT id FROM entries WHERE status='read' AND starred is false AND published_at < now () - '%d days'::interval LIMIT 5000)
  164. `, days)
  165. if _, err := s.db.Exec(query); err != nil {
  166. return fmt.Errorf("unable to archive read entries: %v", err)
  167. }
  168. return nil
  169. }
  170. // SetEntriesStatus update the status of the given list of entries.
  171. func (s *Storage) SetEntriesStatus(userID int64, entryIDs []int64, status string) error {
  172. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:SetEntriesStatus] userID=%d, entryIDs=%v, status=%s", userID, entryIDs, status))
  173. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND id=ANY($3)`
  174. result, err := s.db.Exec(query, status, userID, pq.Array(entryIDs))
  175. if err != nil {
  176. return fmt.Errorf("unable to update entries statuses %v: %v", entryIDs, err)
  177. }
  178. count, err := result.RowsAffected()
  179. if err != nil {
  180. return fmt.Errorf("unable to update these entries %v: %v", entryIDs, err)
  181. }
  182. if count == 0 {
  183. return errors.New("nothing has been updated")
  184. }
  185. return nil
  186. }
  187. // ToggleBookmark toggles entry bookmark value.
  188. func (s *Storage) ToggleBookmark(userID int64, entryID int64) error {
  189. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:ToggleBookmark] userID=%d, entryID=%d", userID, entryID))
  190. query := `UPDATE entries SET starred = NOT starred WHERE user_id=$1 AND id=$2`
  191. result, err := s.db.Exec(query, userID, entryID)
  192. if err != nil {
  193. return fmt.Errorf("unable to toggle bookmark flag for entry #%d: %v", entryID, err)
  194. }
  195. count, err := result.RowsAffected()
  196. if err != nil {
  197. return fmt.Errorf("unable to toogle bookmark flag for entry #%d: %v", entryID, err)
  198. }
  199. if count == 0 {
  200. return errors.New("nothing has been updated")
  201. }
  202. return nil
  203. }
  204. // FlushHistory set all entries with the status "read" to "removed".
  205. func (s *Storage) FlushHistory(userID int64) error {
  206. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FlushHistory] userID=%d", userID))
  207. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3 AND starred='f'`
  208. _, err := s.db.Exec(query, model.EntryStatusRemoved, userID, model.EntryStatusRead)
  209. if err != nil {
  210. return fmt.Errorf("unable to flush history: %v", err)
  211. }
  212. return nil
  213. }
  214. // MarkAllAsRead updates all user entries to the read status.
  215. func (s *Storage) MarkAllAsRead(userID int64) error {
  216. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:MarkAllAsRead] userID=%d", userID))
  217. query := `UPDATE entries SET status=$1 WHERE user_id=$2 AND status=$3`
  218. result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread)
  219. if err != nil {
  220. return fmt.Errorf("unable to mark all entries as read: %v", err)
  221. }
  222. count, _ := result.RowsAffected()
  223. logger.Debug("[Storage:MarkAllAsRead] %d items marked as read", count)
  224. return nil
  225. }
  226. // MarkFeedAsRead updates all feed entries to the read status.
  227. func (s *Storage) MarkFeedAsRead(userID, feedID int64, before time.Time) error {
  228. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:MarkFeedAsRead] userID=%d, feedID=%d, before=%v", userID, feedID, before))
  229. query := `
  230. UPDATE entries
  231. SET status=$1
  232. WHERE user_id=$2 AND feed_id=$3 AND status=$4 AND published_at < $5
  233. `
  234. result, err := s.db.Exec(query, model.EntryStatusRead, userID, feedID, model.EntryStatusUnread, before)
  235. if err != nil {
  236. return fmt.Errorf("unable to mark feed entries as read: %v", err)
  237. }
  238. count, _ := result.RowsAffected()
  239. logger.Debug("[Storage:MarkFeedAsRead] %d items marked as read", count)
  240. return nil
  241. }
  242. // MarkCategoryAsRead updates all category entries to the read status.
  243. func (s *Storage) MarkCategoryAsRead(userID, categoryID int64, before time.Time) error {
  244. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:MarkCategoryAsRead] userID=%d, categoryID=%d, before=%v", userID, categoryID, before))
  245. query := `
  246. UPDATE entries
  247. SET status=$1
  248. WHERE
  249. user_id=$2 AND status=$3 AND published_at < $4 AND feed_id IN (SELECT id FROM feeds WHERE user_id=$2 AND category_id=$5)
  250. `
  251. result, err := s.db.Exec(query, model.EntryStatusRead, userID, model.EntryStatusUnread, before, categoryID)
  252. if err != nil {
  253. return fmt.Errorf("unable to mark category entries as read: %v", err)
  254. }
  255. count, _ := result.RowsAffected()
  256. logger.Debug("[Storage:MarkCategoryAsRead] %d items marked as read", count)
  257. return nil
  258. }
  259. // EntryURLExists returns true if an entry with this URL already exists.
  260. func (s *Storage) EntryURLExists(userID int64, entryURL string) bool {
  261. var result int
  262. query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND url=$2`
  263. s.db.QueryRow(query, userID, entryURL).Scan(&result)
  264. return result >= 1
  265. }