feed.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 api
  5. import (
  6. "errors"
  7. "github.com/miniflux/miniflux2/server/api/payload"
  8. "github.com/miniflux/miniflux2/server/core"
  9. )
  10. // CreateFeed is the API handler to create a new feed.
  11. func (c *Controller) CreateFeed(ctx *core.Context, request *core.Request, response *core.Response) {
  12. userID := ctx.UserID()
  13. feedURL, categoryID, err := payload.DecodeFeedCreationPayload(request.Body())
  14. if err != nil {
  15. response.JSON().BadRequest(err)
  16. return
  17. }
  18. feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL)
  19. if err != nil {
  20. response.JSON().ServerError(errors.New("Unable to create this feed"))
  21. return
  22. }
  23. type result struct {
  24. FeedID int64 `json:"feed_id"`
  25. }
  26. response.JSON().Created(&result{FeedID: feed.ID})
  27. }
  28. // RefreshFeed is the API handler to refresh a feed.
  29. func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, response *core.Response) {
  30. userID := ctx.UserID()
  31. feedID, err := request.IntegerParam("feedID")
  32. if err != nil {
  33. response.JSON().BadRequest(err)
  34. return
  35. }
  36. err = c.feedHandler.RefreshFeed(userID, feedID)
  37. if err != nil {
  38. response.JSON().ServerError(errors.New("Unable to refresh this feed"))
  39. return
  40. }
  41. response.JSON().NoContent()
  42. }
  43. // UpdateFeed is the API handler that is used to update a feed.
  44. func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, response *core.Response) {
  45. userID := ctx.UserID()
  46. feedID, err := request.IntegerParam("feedID")
  47. if err != nil {
  48. response.JSON().BadRequest(err)
  49. return
  50. }
  51. newFeed, err := payload.DecodeFeedModificationPayload(request.Body())
  52. if err != nil {
  53. response.JSON().BadRequest(err)
  54. return
  55. }
  56. originalFeed, err := c.store.FeedByID(userID, feedID)
  57. if err != nil {
  58. response.JSON().NotFound(errors.New("Unable to find this feed"))
  59. return
  60. }
  61. if originalFeed == nil {
  62. response.JSON().NotFound(errors.New("Feed not found"))
  63. return
  64. }
  65. originalFeed.Merge(newFeed)
  66. if err := c.store.UpdateFeed(originalFeed); err != nil {
  67. response.JSON().ServerError(errors.New("Unable to update this feed"))
  68. return
  69. }
  70. response.JSON().Created(originalFeed)
  71. }
  72. // GetFeeds is the API handler that get all feeds that belongs to the given user.
  73. func (c *Controller) GetFeeds(ctx *core.Context, request *core.Request, response *core.Response) {
  74. feeds, err := c.store.Feeds(ctx.UserID())
  75. if err != nil {
  76. response.JSON().ServerError(errors.New("Unable to fetch feeds from the database"))
  77. return
  78. }
  79. response.JSON().Standard(feeds)
  80. }
  81. // GetFeed is the API handler to get a feed.
  82. func (c *Controller) GetFeed(ctx *core.Context, request *core.Request, response *core.Response) {
  83. userID := ctx.UserID()
  84. feedID, err := request.IntegerParam("feedID")
  85. if err != nil {
  86. response.JSON().BadRequest(err)
  87. return
  88. }
  89. feed, err := c.store.FeedByID(userID, feedID)
  90. if err != nil {
  91. response.JSON().ServerError(errors.New("Unable to fetch this feed"))
  92. return
  93. }
  94. if feed == nil {
  95. response.JSON().NotFound(errors.New("Feed not found"))
  96. return
  97. }
  98. response.JSON().Standard(feed)
  99. }
  100. // RemoveFeed is the API handler to remove a feed.
  101. func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, response *core.Response) {
  102. userID := ctx.UserID()
  103. feedID, err := request.IntegerParam("feedID")
  104. if err != nil {
  105. response.JSON().BadRequest(err)
  106. return
  107. }
  108. if !c.store.FeedExists(userID, feedID) {
  109. response.JSON().NotFound(errors.New("Feed not found"))
  110. return
  111. }
  112. if err := c.store.RemoveFeed(userID, feedID); err != nil {
  113. response.JSON().ServerError(errors.New("Unable to remove this feed"))
  114. return
  115. }
  116. response.JSON().NoContent()
  117. }