entry.go 11 KB

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