feed.go 4.5 KB

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