handler.go 14 KB

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