subscription_submit.go 2.7 KB

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