entry.go 11 KB

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