feed.go 4.1 KB

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