subscription_submit.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "net/http"
  6. "miniflux.app/v2/internal/config"
  7. "miniflux.app/v2/internal/http/request"
  8. "miniflux.app/v2/internal/http/response/html"
  9. "miniflux.app/v2/internal/http/route"
  10. "miniflux.app/v2/internal/locale"
  11. "miniflux.app/v2/internal/model"
  12. "miniflux.app/v2/internal/proxyrotator"
  13. "miniflux.app/v2/internal/reader/fetcher"
  14. feedHandler "miniflux.app/v2/internal/reader/handler"
  15. "miniflux.app/v2/internal/reader/subscription"
  16. "miniflux.app/v2/internal/ui/form"
  17. "miniflux.app/v2/internal/ui/session"
  18. "miniflux.app/v2/internal/ui/view"
  19. )
  20. func (h *handler) submitSubscription(w http.ResponseWriter, r *http.Request) {
  21. user, err := h.store.UserByID(request.UserID(r))
  22. if err != nil {
  23. html.ServerError(w, r, err)
  24. return
  25. }
  26. categories, err := h.store.Categories(user.ID)
  27. if err != nil {
  28. html.ServerError(w, r, err)
  29. return
  30. }
  31. sess := session.New(h.store, request.SessionID(r))
  32. v := view.New(h.tpl, r, sess)
  33. v.Set("categories", categories)
  34. v.Set("menu", "feeds")
  35. v.Set("user", user)
  36. v.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  37. v.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  38. v.Set("defaultUserAgent", config.Opts.HTTPClientUserAgent())
  39. v.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyURLConfigured())
  40. subscriptionForm := form.NewSubscriptionForm(r)
  41. if validationErr := subscriptionForm.Validate(); validationErr != nil {
  42. v.Set("form", subscriptionForm)
  43. v.Set("errorMessage", validationErr.Translate(user.Language))
  44. html.OK(w, r, v.Render("add_subscription"))
  45. return
  46. }
  47. var rssBridgeURL string
  48. var rssBridgeToken string
  49. if intg, err := h.store.Integration(user.ID); err == nil && intg != nil && intg.RSSBridgeEnabled {
  50. rssBridgeURL = intg.RSSBridgeURL
  51. rssBridgeToken = intg.RSSBridgeToken
  52. }
  53. requestBuilder := fetcher.NewRequestBuilder()
  54. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  55. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  56. requestBuilder.WithCustomFeedProxyURL(subscriptionForm.ProxyURL)
  57. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  58. requestBuilder.UseCustomApplicationProxyURL(subscriptionForm.FetchViaProxy)
  59. requestBuilder.WithUserAgent(subscriptionForm.UserAgent, config.Opts.HTTPClientUserAgent())
  60. requestBuilder.WithCookie(subscriptionForm.Cookie)
  61. requestBuilder.WithUsernameAndPassword(subscriptionForm.Username, subscriptionForm.Password)
  62. requestBuilder.IgnoreTLSErrors(subscriptionForm.AllowSelfSignedCertificates)
  63. requestBuilder.DisableHTTP2(subscriptionForm.DisableHTTP2)
  64. subscriptionFinder := subscription.NewSubscriptionFinder(requestBuilder)
  65. subscriptions, localizedError := subscriptionFinder.FindSubscriptions(
  66. subscriptionForm.URL,
  67. rssBridgeURL,
  68. rssBridgeToken,
  69. )
  70. if localizedError != nil {
  71. v.Set("form", subscriptionForm)
  72. v.Set("errorMessage", localizedError.Translate(user.Language))
  73. html.OK(w, r, v.Render("add_subscription"))
  74. return
  75. }
  76. n := len(subscriptions)
  77. switch {
  78. case n == 0:
  79. v.Set("form", subscriptionForm)
  80. v.Set("errorMessage", locale.NewLocalizedError("error.subscription_not_found").Translate(user.Language))
  81. html.OK(w, r, v.Render("add_subscription"))
  82. case n == 1 && subscriptionFinder.IsFeedAlreadyDownloaded():
  83. feed, localizedError := feedHandler.CreateFeedFromSubscriptionDiscovery(h.store, user.ID, &model.FeedCreationRequestFromSubscriptionDiscovery{
  84. Content: subscriptionFinder.FeedResponseInfo().Content,
  85. ETag: subscriptionFinder.FeedResponseInfo().ETag,
  86. LastModified: subscriptionFinder.FeedResponseInfo().LastModified,
  87. FeedCreationRequest: model.FeedCreationRequest{
  88. CategoryID: subscriptionForm.CategoryID,
  89. FeedURL: subscriptions[0].URL,
  90. AllowSelfSignedCertificates: subscriptionForm.AllowSelfSignedCertificates,
  91. Crawler: subscriptionForm.Crawler,
  92. IgnoreEntryUpdates: subscriptionForm.IgnoreEntryUpdates,
  93. UserAgent: subscriptionForm.UserAgent,
  94. Cookie: subscriptionForm.Cookie,
  95. Username: subscriptionForm.Username,
  96. Password: subscriptionForm.Password,
  97. ScraperRules: subscriptionForm.ScraperRules,
  98. RewriteRules: subscriptionForm.RewriteRules,
  99. UrlRewriteRules: subscriptionForm.UrlRewriteRules,
  100. BlocklistRules: subscriptionForm.BlocklistRules,
  101. KeeplistRules: subscriptionForm.KeeplistRules,
  102. KeepFilterEntryRules: subscriptionForm.KeepFilterEntryRules,
  103. BlockFilterEntryRules: subscriptionForm.BlockFilterEntryRules,
  104. FetchViaProxy: subscriptionForm.FetchViaProxy,
  105. DisableHTTP2: subscriptionForm.DisableHTTP2,
  106. ProxyURL: subscriptionForm.ProxyURL,
  107. },
  108. })
  109. if localizedError != nil {
  110. v.Set("form", subscriptionForm)
  111. v.Set("errorMessage", localizedError.Translate(user.Language))
  112. html.OK(w, r, v.Render("add_subscription"))
  113. return
  114. }
  115. html.Redirect(w, r, route.Path(h.router, "feedEntries", "feedID", feed.ID))
  116. case n == 1 && !subscriptionFinder.IsFeedAlreadyDownloaded():
  117. feed, localizedError := feedHandler.CreateFeed(h.store, user.ID, &model.FeedCreationRequest{
  118. CategoryID: subscriptionForm.CategoryID,
  119. FeedURL: subscriptions[0].URL,
  120. Crawler: subscriptionForm.Crawler,
  121. IgnoreEntryUpdates: subscriptionForm.IgnoreEntryUpdates,
  122. AllowSelfSignedCertificates: subscriptionForm.AllowSelfSignedCertificates,
  123. UserAgent: subscriptionForm.UserAgent,
  124. Cookie: subscriptionForm.Cookie,
  125. Username: subscriptionForm.Username,
  126. Password: subscriptionForm.Password,
  127. ScraperRules: subscriptionForm.ScraperRules,
  128. RewriteRules: subscriptionForm.RewriteRules,
  129. UrlRewriteRules: subscriptionForm.UrlRewriteRules,
  130. BlocklistRules: subscriptionForm.BlocklistRules,
  131. KeeplistRules: subscriptionForm.KeeplistRules,
  132. KeepFilterEntryRules: subscriptionForm.KeepFilterEntryRules,
  133. BlockFilterEntryRules: subscriptionForm.BlockFilterEntryRules,
  134. FetchViaProxy: subscriptionForm.FetchViaProxy,
  135. DisableHTTP2: subscriptionForm.DisableHTTP2,
  136. ProxyURL: subscriptionForm.ProxyURL,
  137. })
  138. if localizedError != nil {
  139. v.Set("form", subscriptionForm)
  140. v.Set("errorMessage", localizedError.Translate(user.Language))
  141. html.OK(w, r, v.Render("add_subscription"))
  142. return
  143. }
  144. html.Redirect(w, r, route.Path(h.router, "feedEntries", "feedID", feed.ID))
  145. case n > 1:
  146. view := view.New(h.tpl, r, sess)
  147. view.Set("subscriptions", subscriptions)
  148. view.Set("form", subscriptionForm)
  149. view.Set("menu", "feeds")
  150. view.Set("user", user)
  151. view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  152. view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  153. view.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyURLConfigured())
  154. html.OK(w, r, view.Render("choose_subscription"))
  155. }
  156. }