feed.go 4.8 KB

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