subscription_submit.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 // import "miniflux.app/ui"
  5. import (
  6. "net/http"
  7. "miniflux.app/http/context"
  8. "miniflux.app/http/response"
  9. "miniflux.app/http/response/html"
  10. "miniflux.app/http/route"
  11. "miniflux.app/logger"
  12. "miniflux.app/reader/subscription"
  13. "miniflux.app/ui/form"
  14. "miniflux.app/ui/session"
  15. "miniflux.app/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. v.Set("countErrorFeeds", c.store.CountErrorFeeds(user.ID))
  37. subscriptionForm := form.NewSubscriptionForm(r)
  38. if err := subscriptionForm.Validate(); err != nil {
  39. v.Set("form", subscriptionForm)
  40. v.Set("errorMessage", err.Error())
  41. html.OK(w, r, v.Render("add_subscription"))
  42. return
  43. }
  44. subscriptions, err := subscription.FindSubscriptions(
  45. subscriptionForm.URL,
  46. subscriptionForm.Username,
  47. subscriptionForm.Password,
  48. )
  49. if err != nil {
  50. logger.Error("[Controller:SubmitSubscription] %v", err)
  51. v.Set("form", subscriptionForm)
  52. v.Set("errorMessage", err)
  53. html.OK(w, r, v.Render("add_subscription"))
  54. return
  55. }
  56. logger.Debug("[UI:SubmitSubscription] %s", subscriptions)
  57. n := len(subscriptions)
  58. switch {
  59. case n == 0:
  60. v.Set("form", subscriptionForm)
  61. v.Set("errorMessage", "Unable to find any subscription.")
  62. html.OK(w, r, v.Render("add_subscription"))
  63. case n == 1:
  64. feed, err := c.feedHandler.CreateFeed(
  65. user.ID,
  66. subscriptionForm.CategoryID,
  67. subscriptions[0].URL,
  68. subscriptionForm.Crawler,
  69. subscriptionForm.Username,
  70. subscriptionForm.Password,
  71. )
  72. if err != nil {
  73. v.Set("form", subscriptionForm)
  74. v.Set("errorMessage", err)
  75. html.OK(w, r, v.Render("add_subscription"))
  76. return
  77. }
  78. response.Redirect(w, r, route.Path(c.router, "feedEntries", "feedID", feed.ID))
  79. case n > 1:
  80. v := view.New(c.tpl, ctx, sess)
  81. v.Set("subscriptions", subscriptions)
  82. v.Set("form", subscriptionForm)
  83. v.Set("menu", "feeds")
  84. v.Set("user", user)
  85. v.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  86. v.Set("countErrorFeeds", c.store.CountErrorFeeds(user.ID))
  87. html.OK(w, r, v.Render("choose_subscription"))
  88. }
  89. }