entry.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api // import "miniflux.app/v2/internal/api"
  4. import (
  5. json_parser "encoding/json"
  6. "errors"
  7. "net/http"
  8. "strconv"
  9. "time"
  10. "miniflux.app/v2/internal/config"
  11. "miniflux.app/v2/internal/http/request"
  12. "miniflux.app/v2/internal/http/response/json"
  13. "miniflux.app/v2/internal/integration"
  14. "miniflux.app/v2/internal/mediaproxy"
  15. "miniflux.app/v2/internal/model"
  16. "miniflux.app/v2/internal/reader/processor"
  17. "miniflux.app/v2/internal/reader/readingtime"
  18. "miniflux.app/v2/internal/storage"
  19. "miniflux.app/v2/internal/validator"
  20. )
  21. func (h *handler) getEntryFromBuilder(w http.ResponseWriter, r *http.Request, b *storage.EntryQueryBuilder) {
  22. entry, err := b.GetEntry()
  23. if err != nil {
  24. json.ServerError(w, r, err)
  25. return
  26. }
  27. if entry == nil {
  28. json.NotFound(w, r)
  29. return
  30. }
  31. entry.Content = mediaproxy.RewriteDocumentWithAbsoluteProxyURL(h.router, entry.Content)
  32. entry.Enclosures.ProxifyEnclosureURL(h.router, config.Opts.MediaProxyMode(), config.Opts.MediaProxyResourceTypes())
  33. json.OK(w, r, entry)
  34. }
  35. func (h *handler) getFeedEntry(w http.ResponseWriter, r *http.Request) {
  36. feedID := request.RouteInt64Param(r, "feedID")
  37. entryID := request.RouteInt64Param(r, "entryID")
  38. builder := h.store.NewEntryQueryBuilder(request.UserID(r))
  39. builder.WithFeedID(feedID)
  40. builder.WithEntryID(entryID)
  41. h.getEntryFromBuilder(w, r, builder)
  42. }
  43. func (h *handler) getCategoryEntry(w http.ResponseWriter, r *http.Request) {
  44. categoryID := request.RouteInt64Param(r, "categoryID")
  45. entryID := request.RouteInt64Param(r, "entryID")
  46. builder := h.store.NewEntryQueryBuilder(request.UserID(r))
  47. builder.WithCategoryID(categoryID)
  48. builder.WithEntryID(entryID)
  49. h.getEntryFromBuilder(w, r, builder)
  50. }
  51. func (h *handler) getEntry(w http.ResponseWriter, r *http.Request) {
  52. entryID := request.RouteInt64Param(r, "entryID")
  53. builder := h.store.NewEntryQueryBuilder(request.UserID(r))
  54. builder.WithEntryID(entryID)
  55. h.getEntryFromBuilder(w, r, builder)
  56. }
  57. func (h *handler) getFeedEntries(w http.ResponseWriter, r *http.Request) {
  58. feedID := request.RouteInt64Param(r, "feedID")
  59. h.findEntries(w, r, feedID, 0)
  60. }
  61. func (h *handler) getCategoryEntries(w http.ResponseWriter, r *http.Request) {
  62. categoryID := request.RouteInt64Param(r, "categoryID")
  63. h.findEntries(w, r, 0, categoryID)
  64. }
  65. func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) {
  66. h.findEntries(w, r, 0, 0)
  67. }
  68. func (h *handler) findEntries(w http.ResponseWriter, r *http.Request, feedID int64, categoryID int64) {
  69. statuses := request.QueryStringParamList(r, "status")
  70. for _, status := range statuses {
  71. if err := validator.ValidateEntryStatus(status); err != nil {
  72. json.BadRequest(w, r, err)
  73. return
  74. }
  75. }
  76. order := request.QueryStringParam(r, "order", model.DefaultSortingOrder)
  77. if err := validator.ValidateEntryOrder(order); err != nil {
  78. json.BadRequest(w, r, err)
  79. return
  80. }
  81. direction := request.QueryStringParam(r, "direction", model.DefaultSortingDirection)
  82. if err := validator.ValidateDirection(direction); err != nil {
  83. json.BadRequest(w, r, err)
  84. return
  85. }
  86. limit := request.QueryIntParam(r, "limit", 100)
  87. offset := request.QueryIntParam(r, "offset", 0)
  88. if err := validator.ValidateRange(offset, limit); err != nil {
  89. json.BadRequest(w, r, err)
  90. return
  91. }
  92. userID := request.UserID(r)
  93. categoryID = request.QueryInt64Param(r, "category_id", categoryID)
  94. if categoryID > 0 && !h.store.CategoryIDExists(userID, categoryID) {
  95. json.BadRequest(w, r, errors.New("invalid category ID"))
  96. return
  97. }
  98. feedID = request.QueryInt64Param(r, "feed_id", feedID)
  99. if feedID > 0 && !h.store.FeedExists(userID, feedID) {
  100. json.BadRequest(w, r, errors.New("invalid feed ID"))
  101. return
  102. }
  103. tags := request.QueryStringParamList(r, "tags")
  104. builder := h.store.NewEntryQueryBuilder(userID)
  105. builder.WithFeedID(feedID)
  106. builder.WithCategoryID(categoryID)
  107. builder.WithStatuses(statuses)
  108. builder.WithSorting(order, direction)
  109. builder.WithOffset(offset)
  110. builder.WithLimit(limit)
  111. builder.WithTags(tags)
  112. builder.WithEnclosures()
  113. if request.HasQueryParam(r, "globally_visible") {
  114. globallyVisible := request.QueryBoolParam(r, "globally_visible", true)
  115. if globallyVisible {
  116. builder.WithGloballyVisible()
  117. }
  118. }
  119. configureFilters(builder, r)
  120. entries, err := builder.GetEntries()
  121. if err != nil {
  122. json.ServerError(w, r, err)
  123. return
  124. }
  125. count, err := builder.CountEntries()
  126. if err != nil {
  127. json.ServerError(w, r, err)
  128. return
  129. }
  130. for i := range entries {
  131. entries[i].Content = mediaproxy.RewriteDocumentWithAbsoluteProxyURL(h.router, entries[i].Content)
  132. }
  133. json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
  134. }
  135. func (h *handler) setEntryStatus(w http.ResponseWriter, r *http.Request) {
  136. var entriesStatusUpdateRequest model.EntriesStatusUpdateRequest
  137. if err := json_parser.NewDecoder(r.Body).Decode(&entriesStatusUpdateRequest); err != nil {
  138. json.BadRequest(w, r, err)
  139. return
  140. }
  141. if err := validator.ValidateEntriesStatusUpdateRequest(&entriesStatusUpdateRequest); err != nil {
  142. json.BadRequest(w, r, err)
  143. return
  144. }
  145. if err := h.store.SetEntriesStatus(request.UserID(r), entriesStatusUpdateRequest.EntryIDs, entriesStatusUpdateRequest.Status); err != nil {
  146. json.ServerError(w, r, err)
  147. return
  148. }
  149. json.NoContent(w, r)
  150. }
  151. func (h *handler) toggleBookmark(w http.ResponseWriter, r *http.Request) {
  152. entryID := request.RouteInt64Param(r, "entryID")
  153. if err := h.store.ToggleBookmark(request.UserID(r), entryID); err != nil {
  154. json.ServerError(w, r, err)
  155. return
  156. }
  157. json.NoContent(w, r)
  158. }
  159. func (h *handler) saveEntry(w http.ResponseWriter, r *http.Request) {
  160. entryID := request.RouteInt64Param(r, "entryID")
  161. builder := h.store.NewEntryQueryBuilder(request.UserID(r))
  162. builder.WithEntryID(entryID)
  163. builder.WithoutStatus(model.EntryStatusRemoved)
  164. if !h.store.HasSaveEntry(request.UserID(r)) {
  165. json.BadRequest(w, r, errors.New("no third-party integration enabled"))
  166. return
  167. }
  168. entry, err := builder.GetEntry()
  169. if err != nil {
  170. json.ServerError(w, r, err)
  171. return
  172. }
  173. if entry == nil {
  174. json.NotFound(w, r)
  175. return
  176. }
  177. settings, err := h.store.Integration(request.UserID(r))
  178. if err != nil {
  179. json.ServerError(w, r, err)
  180. return
  181. }
  182. go integration.SendEntry(entry, settings)
  183. json.Accepted(w, r)
  184. }
  185. func (h *handler) updateEntry(w http.ResponseWriter, r *http.Request) {
  186. var entryUpdateRequest model.EntryUpdateRequest
  187. if err := json_parser.NewDecoder(r.Body).Decode(&entryUpdateRequest); err != nil {
  188. json.BadRequest(w, r, err)
  189. return
  190. }
  191. if err := validator.ValidateEntryModification(&entryUpdateRequest); err != nil {
  192. json.BadRequest(w, r, err)
  193. return
  194. }
  195. loggedUserID := request.UserID(r)
  196. entryID := request.RouteInt64Param(r, "entryID")
  197. entryBuilder := h.store.NewEntryQueryBuilder(loggedUserID)
  198. entryBuilder.WithEntryID(entryID)
  199. entryBuilder.WithoutStatus(model.EntryStatusRemoved)
  200. entry, err := entryBuilder.GetEntry()
  201. if err != nil {
  202. json.ServerError(w, r, err)
  203. return
  204. }
  205. if entry == nil {
  206. json.NotFound(w, r)
  207. return
  208. }
  209. user, err := h.store.UserByID(loggedUserID)
  210. if err != nil {
  211. json.ServerError(w, r, err)
  212. return
  213. }
  214. if user == nil {
  215. json.NotFound(w, r)
  216. return
  217. }
  218. entryUpdateRequest.Patch(entry)
  219. if user.ShowReadingTime {
  220. entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
  221. }
  222. if err := h.store.UpdateEntryTitleAndContent(entry); err != nil {
  223. json.ServerError(w, r, err)
  224. return
  225. }
  226. json.Created(w, r, entry)
  227. }
  228. func (h *handler) fetchContent(w http.ResponseWriter, r *http.Request) {
  229. loggedUserID := request.UserID(r)
  230. entryID := request.RouteInt64Param(r, "entryID")
  231. entryBuilder := h.store.NewEntryQueryBuilder(loggedUserID)
  232. entryBuilder.WithEntryID(entryID)
  233. entryBuilder.WithoutStatus(model.EntryStatusRemoved)
  234. entry, err := entryBuilder.GetEntry()
  235. if err != nil {
  236. json.ServerError(w, r, err)
  237. return
  238. }
  239. if entry == nil {
  240. json.NotFound(w, r)
  241. return
  242. }
  243. user, err := h.store.UserByID(loggedUserID)
  244. if err != nil {
  245. json.ServerError(w, r, err)
  246. return
  247. }
  248. if user == nil {
  249. json.NotFound(w, r)
  250. return
  251. }
  252. feedBuilder := storage.NewFeedQueryBuilder(h.store, loggedUserID)
  253. feedBuilder.WithFeedID(entry.FeedID)
  254. feed, err := feedBuilder.GetFeed()
  255. if err != nil {
  256. json.ServerError(w, r, err)
  257. return
  258. }
  259. if feed == nil {
  260. json.NotFound(w, r)
  261. return
  262. }
  263. if err := processor.ProcessEntryWebPage(feed, entry, user); err != nil {
  264. json.ServerError(w, r, err)
  265. return
  266. }
  267. shouldUpdateContent := request.QueryBoolParam(r, "update_content", false)
  268. if shouldUpdateContent {
  269. if err := h.store.UpdateEntryTitleAndContent(entry); err != nil {
  270. json.ServerError(w, r, err)
  271. return
  272. }
  273. json.OK(w, r, map[string]interface{}{"content": mediaproxy.RewriteDocumentWithRelativeProxyURL(h.router, entry.Content), "reading_time": entry.ReadingTime})
  274. return
  275. }
  276. json.OK(w, r, map[string]string{"content": entry.Content})
  277. }
  278. func (h *handler) flushHistory(w http.ResponseWriter, r *http.Request) {
  279. loggedUserID := request.UserID(r)
  280. go h.store.FlushHistory(loggedUserID)
  281. json.Accepted(w, r)
  282. }
  283. func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
  284. if beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0); beforeEntryID > 0 {
  285. builder.BeforeEntryID(beforeEntryID)
  286. }
  287. if afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0); afterEntryID > 0 {
  288. builder.AfterEntryID(afterEntryID)
  289. }
  290. if beforePublishedTimestamp := request.QueryInt64Param(r, "before", 0); beforePublishedTimestamp > 0 {
  291. builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0))
  292. }
  293. if afterPublishedTimestamp := request.QueryInt64Param(r, "after", 0); afterPublishedTimestamp > 0 {
  294. builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0))
  295. }
  296. if beforePublishedTimestamp := request.QueryInt64Param(r, "published_before", 0); beforePublishedTimestamp > 0 {
  297. builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0))
  298. }
  299. if afterPublishedTimestamp := request.QueryInt64Param(r, "published_after", 0); afterPublishedTimestamp > 0 {
  300. builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0))
  301. }
  302. if beforeChangedTimestamp := request.QueryInt64Param(r, "changed_before", 0); beforeChangedTimestamp > 0 {
  303. builder.BeforeChangedDate(time.Unix(beforeChangedTimestamp, 0))
  304. }
  305. if afterChangedTimestamp := request.QueryInt64Param(r, "changed_after", 0); afterChangedTimestamp > 0 {
  306. builder.AfterChangedDate(time.Unix(afterChangedTimestamp, 0))
  307. }
  308. if categoryID := request.QueryInt64Param(r, "category_id", 0); categoryID > 0 {
  309. builder.WithCategoryID(categoryID)
  310. }
  311. if request.HasQueryParam(r, "starred") {
  312. starred, err := strconv.ParseBool(r.URL.Query().Get("starred"))
  313. if err == nil {
  314. builder.WithStarred(starred)
  315. }
  316. }
  317. if searchQuery := request.QueryStringParam(r, "search", ""); searchQuery != "" {
  318. builder.WithSearchQuery(searchQuery)
  319. }
  320. }