fever.go 17 KB

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