handler.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fever // import "miniflux.app/v2/internal/fever"
  4. import (
  5. "log/slog"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "miniflux.app/v2/internal/http/request"
  11. "miniflux.app/v2/internal/http/response"
  12. "miniflux.app/v2/internal/integration"
  13. "miniflux.app/v2/internal/mediaproxy"
  14. "miniflux.app/v2/internal/model"
  15. "miniflux.app/v2/internal/storage"
  16. )
  17. // NewHandler returns an http.Handler for Fever API calls.
  18. func NewHandler(store *storage.Storage) http.Handler {
  19. h := &feverHandler{store: store}
  20. return http.HandlerFunc(h.serve)
  21. }
  22. type feverHandler struct {
  23. store *storage.Storage
  24. }
  25. func (h *feverHandler) serve(w http.ResponseWriter, r *http.Request) {
  26. switch {
  27. case request.HasQueryParam(r, "groups"):
  28. h.handleGroups(w, r)
  29. case request.HasQueryParam(r, "feeds"):
  30. h.handleFeeds(w, r)
  31. case request.HasQueryParam(r, "favicons"):
  32. h.handleFavicons(w, r)
  33. case request.HasQueryParam(r, "unread_item_ids"):
  34. h.handleUnreadItems(w, r)
  35. case request.HasQueryParam(r, "saved_item_ids"):
  36. h.handleSavedItems(w, r)
  37. case request.HasQueryParam(r, "items"):
  38. h.handleItems(w, r)
  39. case r.FormValue("mark") == "item":
  40. h.handleWriteItems(w, r)
  41. case r.FormValue("mark") == "feed":
  42. h.handleWriteFeeds(w, r)
  43. case r.FormValue("mark") == "group":
  44. h.handleWriteGroups(w, r)
  45. default:
  46. response.JSON(w, r, newBaseResponse())
  47. }
  48. }
  49. /*
  50. A request with the groups argument will return two additional members:
  51. groups contains an array of group objects
  52. feeds_groups contains an array of feeds_group objects
  53. A group object has the following members:
  54. id (positive integer)
  55. title (utf-8 string)
  56. The feeds_group object is documented under “Feeds/Groups Relationships.”
  57. The “Kindling” super group is not included in this response and is composed of all feeds with
  58. an is_spark equal to 0.
  59. The “Sparks” super group is not included in this response and is composed of all feeds with an
  60. is_spark equal to 1.
  61. */
  62. func (h *feverHandler) handleGroups(w http.ResponseWriter, r *http.Request) {
  63. userID := request.UserID(r)
  64. slog.Debug("[Fever] Fetching groups",
  65. slog.Int64("user_id", userID),
  66. )
  67. categories, err := h.store.Categories(userID)
  68. if err != nil {
  69. response.JSONServerError(w, r, err)
  70. return
  71. }
  72. feeds, err := h.store.Feeds(userID)
  73. if err != nil {
  74. response.JSONServerError(w, r, err)
  75. return
  76. }
  77. var result groupsResponse
  78. for _, category := range categories {
  79. result.Groups = append(result.Groups, group{ID: category.ID, Title: category.Title})
  80. }
  81. result.FeedsGroups = buildFeedGroups(feeds)
  82. result.SetCommonValues()
  83. response.JSON(w, r, result)
  84. }
  85. /*
  86. A request with the feeds argument will return two additional members:
  87. feeds contains an array of group objects
  88. feeds_groups contains an array of feeds_group objects
  89. A feed object has the following members:
  90. id (positive integer)
  91. favicon_id (positive integer)
  92. title (utf-8 string)
  93. url (utf-8 string)
  94. site_url (utf-8 string)
  95. is_spark (boolean integer)
  96. last_updated_on_time (Unix timestamp/integer)
  97. The feeds_group object is documented under “Feeds/Groups Relationships.”
  98. The “All Items” super feed is not included in this response and is composed of all items from all feeds
  99. that belong to a given group. For the “Kindling” super group and all user created groups the items
  100. should be limited to feeds with an is_spark equal to 0.
  101. For the “Sparks” super group the items should be limited to feeds with an is_spark equal to 1.
  102. */
  103. func (h *feverHandler) handleFeeds(w http.ResponseWriter, r *http.Request) {
  104. userID := request.UserID(r)
  105. slog.Debug("[Fever] Fetching feeds",
  106. slog.Int64("user_id", userID),
  107. )
  108. feeds, err := h.store.Feeds(userID)
  109. if err != nil {
  110. response.JSONServerError(w, r, err)
  111. return
  112. }
  113. var result feedsResponse
  114. result.Feeds = make([]feed, 0, len(feeds))
  115. for _, f := range feeds {
  116. subscription := feed{
  117. ID: f.ID,
  118. Title: f.Title,
  119. URL: f.FeedURL,
  120. SiteURL: f.SiteURL,
  121. IsSpark: 0,
  122. LastUpdated: f.CheckedAt.Unix(),
  123. }
  124. if f.Icon != nil {
  125. subscription.FaviconID = f.Icon.IconID
  126. }
  127. result.Feeds = append(result.Feeds, subscription)
  128. }
  129. result.FeedsGroups = buildFeedGroups(feeds)
  130. result.SetCommonValues()
  131. response.JSON(w, r, result)
  132. }
  133. /*
  134. A request with the favicons argument will return one additional member:
  135. favicons contains an array of favicon objects
  136. A favicon object has the following members:
  137. id (positive integer)
  138. data (base64 encoded image data; prefixed by image type)
  139. An example data value:
  140. image/gif;base64,R0lGODlhAQABAIAAAObm5gAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==
  141. The data member of a favicon object can be used with the data: protocol to embed an image in CSS or HTML.
  142. A PHP/HTML example:
  143. echo '<img src="data:'.$favicon['data'].'">';
  144. */
  145. func (h *feverHandler) handleFavicons(w http.ResponseWriter, r *http.Request) {
  146. userID := request.UserID(r)
  147. slog.Debug("[Fever] Fetching favicons",
  148. slog.Int64("user_id", userID),
  149. )
  150. icons, err := h.store.Icons(userID)
  151. if err != nil {
  152. response.JSONServerError(w, r, err)
  153. return
  154. }
  155. var result faviconsResponse
  156. for _, i := range icons {
  157. result.Favicons = append(result.Favicons, favicon{
  158. ID: i.ID,
  159. Data: i.DataURL(),
  160. })
  161. }
  162. result.SetCommonValues()
  163. response.JSON(w, r, result)
  164. }
  165. /*
  166. A request with the items argument will return two additional members:
  167. items contains an array of item objects
  168. total_items contains the total number of items stored in the database (added in API version 2)
  169. An item object has the following members:
  170. id (positive integer)
  171. feed_id (positive integer)
  172. title (utf-8 string)
  173. author (utf-8 string)
  174. html (utf-8 string)
  175. url (utf-8 string)
  176. is_saved (boolean integer)
  177. is_read (boolean integer)
  178. created_on_time (Unix timestamp/integer)
  179. Most servers won’t have enough memory allocated to PHP to dump all items at once.
  180. Three optional arguments control determine the items included in the response.
  181. Use the since_id argument with the highest id of locally cached items to request 50 additional items.
  182. Repeat until the items array in the response is empty.
  183. Use the max_id argument with the lowest id of locally cached items (or 0 initially) to request 50 previous items.
  184. Repeat until the items array in the response is empty. (added in API version 2)
  185. Use the with_ids argument with a comma-separated list of item ids to request (a maximum of 50) specific items.
  186. (added in API version 2)
  187. */
  188. func (h *feverHandler) handleItems(w http.ResponseWriter, r *http.Request) {
  189. var result itemsResponse
  190. userID := request.UserID(r)
  191. builder := h.store.NewEntryQueryBuilder(userID)
  192. builder.WithoutStatus(model.EntryStatusRemoved)
  193. builder.WithLimit(50)
  194. switch {
  195. case request.HasQueryParam(r, "since_id"):
  196. sinceID := request.QueryInt64Param(r, "since_id", 0)
  197. if sinceID > 0 {
  198. slog.Debug("[Fever] Fetching items since a given date",
  199. slog.Int64("user_id", userID),
  200. slog.Int64("since_id", sinceID),
  201. )
  202. builder.AfterEntryID(sinceID)
  203. builder.WithSorting("id", "ASC")
  204. }
  205. case request.HasQueryParam(r, "max_id"):
  206. maxID := request.QueryInt64Param(r, "max_id", 0)
  207. if maxID == 0 {
  208. slog.Debug("[Fever] Fetching most recent items",
  209. slog.Int64("user_id", userID),
  210. )
  211. builder.WithSorting("id", "DESC")
  212. } else if maxID > 0 {
  213. slog.Debug("[Fever] Fetching items before a given item ID",
  214. slog.Int64("user_id", userID),
  215. slog.Int64("max_id", maxID),
  216. )
  217. builder.BeforeEntryID(maxID)
  218. builder.WithSorting("id", "DESC")
  219. }
  220. case request.HasQueryParam(r, "with_ids"):
  221. csvItemIDs := request.QueryStringParam(r, "with_ids", "")
  222. if csvItemIDs != "" {
  223. var itemIDs []int64
  224. for strItemID := range strings.SplitSeq(csvItemIDs, ",") {
  225. strItemID = strings.TrimSpace(strItemID)
  226. itemID, _ := strconv.ParseInt(strItemID, 10, 64)
  227. itemIDs = append(itemIDs, itemID)
  228. }
  229. builder.WithEntryIDs(itemIDs)
  230. }
  231. default:
  232. slog.Debug("[Fever] Fetching oldest items",
  233. slog.Int64("user_id", userID),
  234. )
  235. }
  236. entries, err := builder.GetEntries()
  237. if err != nil {
  238. response.JSONServerError(w, r, err)
  239. return
  240. }
  241. builder = h.store.NewEntryQueryBuilder(userID)
  242. builder.WithoutStatus(model.EntryStatusRemoved)
  243. result.Total, err = builder.CountEntries()
  244. if err != nil {
  245. response.JSONServerError(w, r, err)
  246. return
  247. }
  248. result.Items = make([]item, 0, len(entries))
  249. for _, entry := range entries {
  250. isRead := 0
  251. if entry.Status == model.EntryStatusRead {
  252. isRead = 1
  253. }
  254. isSaved := 0
  255. if entry.Starred {
  256. isSaved = 1
  257. }
  258. result.Items = append(result.Items, item{
  259. ID: entry.ID,
  260. FeedID: entry.FeedID,
  261. Title: entry.Title,
  262. Author: entry.Author,
  263. HTML: mediaproxy.RewriteDocumentWithAbsoluteProxyURL(entry.Content),
  264. URL: entry.URL,
  265. IsSaved: isSaved,
  266. IsRead: isRead,
  267. CreatedAt: entry.Date.Unix(),
  268. })
  269. }
  270. result.SetCommonValues()
  271. response.JSON(w, r, result)
  272. }
  273. /*
  274. The unread_item_ids and saved_item_ids arguments can be used to keep your local cache synced
  275. with the remote Fever installation.
  276. A request with the unread_item_ids argument will return one additional member:
  277. unread_item_ids (string/comma-separated list of positive integers)
  278. */
  279. func (h *feverHandler) handleUnreadItems(w http.ResponseWriter, r *http.Request) {
  280. userID := request.UserID(r)
  281. slog.Debug("[Fever] Fetching unread items",
  282. slog.Int64("user_id", userID),
  283. )
  284. builder := h.store.NewEntryQueryBuilder(userID)
  285. builder.WithStatus(model.EntryStatusUnread)
  286. rawEntryIDs, err := builder.GetEntryIDs()
  287. if err != nil {
  288. response.JSONServerError(w, r, err)
  289. return
  290. }
  291. itemIDs := make([]string, 0, len(rawEntryIDs))
  292. for _, entryID := range rawEntryIDs {
  293. itemIDs = append(itemIDs, strconv.FormatInt(entryID, 10))
  294. }
  295. var result unreadResponse
  296. result.ItemIDs = strings.Join(itemIDs, ",")
  297. result.SetCommonValues()
  298. response.JSON(w, r, result)
  299. }
  300. /*
  301. The unread_item_ids and saved_item_ids arguments can be used to keep your local cache synced
  302. with the remote Fever installation.
  303. A request with the saved_item_ids argument will return one additional member:
  304. saved_item_ids (string/comma-separated list of positive integers)
  305. */
  306. func (h *feverHandler) handleSavedItems(w http.ResponseWriter, r *http.Request) {
  307. userID := request.UserID(r)
  308. slog.Debug("[Fever] Fetching saved items",
  309. slog.Int64("user_id", userID),
  310. )
  311. builder := h.store.NewEntryQueryBuilder(userID)
  312. builder.WithStarred(true)
  313. entryIDs, err := builder.GetEntryIDs()
  314. if err != nil {
  315. response.JSONServerError(w, r, err)
  316. return
  317. }
  318. itemsIDs := make([]string, 0, len(entryIDs))
  319. for _, entryID := range entryIDs {
  320. itemsIDs = append(itemsIDs, strconv.FormatInt(entryID, 10))
  321. }
  322. result := &savedResponse{ItemIDs: strings.Join(itemsIDs, ",")}
  323. result.SetCommonValues()
  324. response.JSON(w, r, result)
  325. }
  326. /*
  327. mark=item
  328. as=? where ? is replaced with read, saved or unsaved
  329. id=? where ? is replaced with the id of the item to modify
  330. */
  331. func (h *feverHandler) handleWriteItems(w http.ResponseWriter, r *http.Request) {
  332. userID := request.UserID(r)
  333. slog.Debug("[Fever] Receiving mark=item call",
  334. slog.Int64("user_id", userID),
  335. )
  336. entryID := request.FormInt64Value(r, "id")
  337. if entryID <= 0 {
  338. return
  339. }
  340. builder := h.store.NewEntryQueryBuilder(userID)
  341. builder.WithEntryID(entryID)
  342. builder.WithoutStatus(model.EntryStatusRemoved)
  343. entry, err := builder.GetEntry()
  344. if err != nil {
  345. response.JSONServerError(w, r, err)
  346. return
  347. }
  348. if entry == nil {
  349. slog.Debug("[Fever] Entry not found",
  350. slog.Int64("user_id", userID),
  351. slog.Int64("entry_id", entryID),
  352. )
  353. response.JSON(w, r, newBaseResponse())
  354. return
  355. }
  356. switch r.FormValue("as") {
  357. case "read":
  358. slog.Debug("[Fever] Mark entry as read",
  359. slog.Int64("user_id", userID),
  360. slog.Int64("entry_id", entryID),
  361. )
  362. h.store.SetEntriesStatus(userID, []int64{entryID}, model.EntryStatusRead)
  363. case "unread":
  364. slog.Debug("[Fever] Mark entry as unread",
  365. slog.Int64("user_id", userID),
  366. slog.Int64("entry_id", entryID),
  367. )
  368. h.store.SetEntriesStatus(userID, []int64{entryID}, model.EntryStatusUnread)
  369. case "saved":
  370. slog.Debug("[Fever] Mark entry as saved",
  371. slog.Int64("user_id", userID),
  372. slog.Int64("entry_id", entryID),
  373. )
  374. if err := h.store.ToggleStarred(userID, entryID); err != nil {
  375. response.JSONServerError(w, r, err)
  376. return
  377. }
  378. settings, err := h.store.Integration(userID)
  379. if err != nil {
  380. response.JSONServerError(w, r, err)
  381. return
  382. }
  383. go func() {
  384. integration.SendEntry(entry, settings)
  385. }()
  386. case "unsaved":
  387. slog.Debug("[Fever] Mark entry as unsaved",
  388. slog.Int64("user_id", userID),
  389. slog.Int64("entry_id", entryID),
  390. )
  391. if err := h.store.ToggleStarred(userID, entryID); err != nil {
  392. response.JSONServerError(w, r, err)
  393. return
  394. }
  395. }
  396. response.JSON(w, r, newBaseResponse())
  397. }
  398. /*
  399. mark=feed
  400. as=read
  401. id=? where ? is replaced with the id of the feed or group to modify
  402. before=? where ? is replaced with the Unix timestamp of the the local client’s most recent items API request
  403. */
  404. func (h *feverHandler) handleWriteFeeds(w http.ResponseWriter, r *http.Request) {
  405. userID := request.UserID(r)
  406. feedID := request.FormInt64Value(r, "id")
  407. before := time.Unix(request.FormInt64Value(r, "before"), 0)
  408. slog.Debug("[Fever] Mark feed as read before a given date",
  409. slog.Int64("user_id", userID),
  410. slog.Int64("feed_id", feedID),
  411. slog.Time("before_ts", before),
  412. )
  413. if feedID <= 0 {
  414. return
  415. }
  416. if err := h.store.MarkFeedAsRead(userID, feedID, before); err != nil {
  417. response.JSONServerError(w, r, err)
  418. return
  419. }
  420. response.JSON(w, r, newBaseResponse())
  421. }
  422. /*
  423. mark=group
  424. as=read
  425. id=? where ? is replaced with the id of the feed or group to modify
  426. before=? where ? is replaced with the Unix timestamp of the the local client’s most recent items API request
  427. */
  428. func (h *feverHandler) handleWriteGroups(w http.ResponseWriter, r *http.Request) {
  429. userID := request.UserID(r)
  430. groupID := request.FormInt64Value(r, "id")
  431. if groupID < 0 {
  432. return
  433. }
  434. var err error
  435. if groupID == 0 {
  436. err = h.store.MarkAllAsRead(userID)
  437. slog.Debug("[Fever] Mark all items as read",
  438. slog.Int64("user_id", userID),
  439. )
  440. } else {
  441. before := time.Unix(request.FormInt64Value(r, "before"), 0)
  442. err = h.store.MarkCategoryAsRead(userID, groupID, before)
  443. slog.Debug("[Fever] Mark group as read before a given date",
  444. slog.Int64("user_id", userID),
  445. slog.Int64("group_id", groupID),
  446. slog.Time("before_ts", before),
  447. )
  448. }
  449. if err != nil {
  450. response.JSONServerError(w, r, err)
  451. return
  452. }
  453. response.JSON(w, r, newBaseResponse())
  454. }
  455. /*
  456. A feeds_group object has the following members:
  457. group_id (positive integer)
  458. feed_ids (string/comma-separated list of positive integers)
  459. */
  460. func buildFeedGroups(feeds model.Feeds) []feedsGroups {
  461. feedsGroupedByCategory := make(map[int64][]string, len(feeds))
  462. for _, feed := range feeds {
  463. feedsGroupedByCategory[feed.Category.ID] = append(feedsGroupedByCategory[feed.Category.ID], strconv.FormatInt(feed.ID, 10))
  464. }
  465. result := make([]feedsGroups, 0, len(feedsGroupedByCategory))
  466. for categoryID, feedIDs := range feedsGroupedByCategory {
  467. result = append(result, feedsGroups{
  468. GroupID: categoryID,
  469. FeedIDs: strings.Join(feedIDs, ","),
  470. })
  471. }
  472. return result
  473. }