feed.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "net/http"
  8. "github.com/miniflux/miniflux/http/context"
  9. "github.com/miniflux/miniflux/http/request"
  10. "github.com/miniflux/miniflux/http/response/json"
  11. )
  12. // CreateFeed is the API handler to create a new feed.
  13. func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
  14. feedURL, categoryID, err := decodeFeedCreationPayload(r.Body)
  15. if err != nil {
  16. json.BadRequest(w, err)
  17. return
  18. }
  19. if feedURL == "" {
  20. json.BadRequest(w, errors.New("The feed_url is required"))
  21. return
  22. }
  23. if categoryID <= 0 {
  24. json.BadRequest(w, errors.New("The category_id is required"))
  25. return
  26. }
  27. ctx := context.New(r)
  28. userID := ctx.UserID()
  29. if c.store.FeedURLExists(userID, feedURL) {
  30. json.BadRequest(w, errors.New("This feed_url already exists"))
  31. return
  32. }
  33. if !c.store.CategoryExists(userID, categoryID) {
  34. json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
  35. return
  36. }
  37. feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL, false)
  38. if err != nil {
  39. json.ServerError(w, errors.New("Unable to create this feed"))
  40. return
  41. }
  42. type result struct {
  43. FeedID int64 `json:"feed_id"`
  44. }
  45. json.Created(w, &result{FeedID: feed.ID})
  46. }
  47. // RefreshFeed is the API handler to refresh a feed.
  48. func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) {
  49. feedID, err := request.IntParam(r, "feedID")
  50. if err != nil {
  51. json.BadRequest(w, err)
  52. return
  53. }
  54. ctx := context.New(r)
  55. userID := ctx.UserID()
  56. if !c.store.FeedExists(userID, feedID) {
  57. json.NotFound(w, errors.New("Unable to find this feed"))
  58. return
  59. }
  60. err = c.feedHandler.RefreshFeed(userID, feedID)
  61. if err != nil {
  62. json.ServerError(w, errors.New("Unable to refresh this feed"))
  63. return
  64. }
  65. json.NoContent(w)
  66. }
  67. // UpdateFeed is the API handler that is used to update a feed.
  68. func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) {
  69. feedID, err := request.IntParam(r, "feedID")
  70. if err != nil {
  71. json.BadRequest(w, err)
  72. return
  73. }
  74. newFeed, err := decodeFeedModificationPayload(r.Body)
  75. if err != nil {
  76. json.BadRequest(w, err)
  77. return
  78. }
  79. ctx := context.New(r)
  80. userID := ctx.UserID()
  81. if newFeed.Category != nil && newFeed.Category.ID != 0 && !c.store.CategoryExists(userID, newFeed.Category.ID) {
  82. json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
  83. return
  84. }
  85. originalFeed, err := c.store.FeedByID(userID, feedID)
  86. if err != nil {
  87. json.NotFound(w, errors.New("Unable to find this feed"))
  88. return
  89. }
  90. if originalFeed == nil {
  91. json.NotFound(w, errors.New("Feed not found"))
  92. return
  93. }
  94. originalFeed.Merge(newFeed)
  95. if err := c.store.UpdateFeed(originalFeed); err != nil {
  96. json.ServerError(w, errors.New("Unable to update this feed"))
  97. return
  98. }
  99. originalFeed, err = c.store.FeedByID(userID, feedID)
  100. if err != nil {
  101. json.ServerError(w, errors.New("Unable to fetch this feed"))
  102. return
  103. }
  104. json.Created(w, originalFeed)
  105. }
  106. // GetFeeds is the API handler that get all feeds that belongs to the given user.
  107. func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) {
  108. feeds, err := c.store.Feeds(context.New(r).UserID())
  109. if err != nil {
  110. json.ServerError(w, errors.New("Unable to fetch feeds from the database"))
  111. return
  112. }
  113. json.OK(w, feeds)
  114. }
  115. // GetFeed is the API handler to get a feed.
  116. func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) {
  117. feedID, err := request.IntParam(r, "feedID")
  118. if err != nil {
  119. json.BadRequest(w, err)
  120. return
  121. }
  122. feed, err := c.store.FeedByID(context.New(r).UserID(), feedID)
  123. if err != nil {
  124. json.ServerError(w, errors.New("Unable to fetch this feed"))
  125. return
  126. }
  127. if feed == nil {
  128. json.NotFound(w, errors.New("Feed not found"))
  129. return
  130. }
  131. json.OK(w, feed)
  132. }
  133. // RemoveFeed is the API handler to remove a feed.
  134. func (c *Controller) RemoveFeed(w http.ResponseWriter, r *http.Request) {
  135. feedID, err := request.IntParam(r, "feedID")
  136. if err != nil {
  137. json.BadRequest(w, err)
  138. return
  139. }
  140. ctx := context.New(r)
  141. userID := ctx.UserID()
  142. if !c.store.FeedExists(userID, feedID) {
  143. json.NotFound(w, errors.New("Feed not found"))
  144. return
  145. }
  146. if err := c.store.RemoveFeed(userID, feedID); err != nil {
  147. json.ServerError(w, errors.New("Unable to remove this feed"))
  148. return
  149. }
  150. json.NoContent(w)
  151. }