feed.go 4.1 KB

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