subscription_submit.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2018 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 ui
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/http/context"
  8. "github.com/miniflux/miniflux/http/response"
  9. "github.com/miniflux/miniflux/http/response/html"
  10. "github.com/miniflux/miniflux/http/route"
  11. "github.com/miniflux/miniflux/logger"
  12. "github.com/miniflux/miniflux/reader/subscription"
  13. "github.com/miniflux/miniflux/ui/form"
  14. "github.com/miniflux/miniflux/ui/session"
  15. "github.com/miniflux/miniflux/ui/view"
  16. )
  17. // SubmitSubscription try to find a feed from the URL provided by the user.
  18. func (c *Controller) SubmitSubscription(w http.ResponseWriter, r *http.Request) {
  19. ctx := context.New(r)
  20. sess := session.New(c.store, ctx)
  21. v := view.New(c.tpl, ctx, sess)
  22. user, err := c.store.UserByID(ctx.UserID())
  23. if err != nil {
  24. html.ServerError(w, err)
  25. return
  26. }
  27. categories, err := c.store.Categories(user.ID)
  28. if err != nil {
  29. html.ServerError(w, err)
  30. return
  31. }
  32. v.Set("categories", categories)
  33. v.Set("menu", "feeds")
  34. v.Set("user", user)
  35. v.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  36. subscriptionForm := form.NewSubscriptionForm(r)
  37. if err := subscriptionForm.Validate(); err != nil {
  38. v.Set("form", subscriptionForm)
  39. v.Set("errorMessage", err.Error())
  40. html.OK(w, v.Render("add_subscription"))
  41. return
  42. }
  43. subscriptions, err := subscription.FindSubscriptions(subscriptionForm.URL)
  44. if err != nil {
  45. logger.Error("[Controller:SubmitSubscription] %v", err)
  46. v.Set("form", subscriptionForm)
  47. v.Set("errorMessage", err)
  48. html.OK(w, v.Render("add_subscription"))
  49. return
  50. }
  51. logger.Debug("[UI:SubmitSubscription] %s", subscriptions)
  52. n := len(subscriptions)
  53. switch {
  54. case n == 0:
  55. v.Set("form", subscriptionForm)
  56. v.Set("errorMessage", "Unable to find any subscription.")
  57. html.OK(w, v.Render("add_subscription"))
  58. case n == 1:
  59. feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptions[0].URL, subscriptionForm.Crawler)
  60. if err != nil {
  61. v.Set("form", subscriptionForm)
  62. v.Set("errorMessage", err)
  63. html.OK(w, v.Render("add_subscription"))
  64. return
  65. }
  66. response.Redirect(w, r, route.Path(c.router, "feedEntries", "feedID", feed.ID))
  67. case n > 1:
  68. v := view.New(c.tpl, ctx, sess)
  69. v.Set("subscriptions", subscriptions)
  70. v.Set("categoryID", subscriptionForm.CategoryID)
  71. v.Set("menu", "feeds")
  72. v.Set("user", user)
  73. v.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  74. html.OK(w, v.Render("choose_subscription"))
  75. }
  76. }