2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package ui // import "miniflux.app/v2/internal/ui"
|
2018-04-30 01:35:04 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/http/request"
|
|
|
|
"miniflux.app/v2/internal/http/response/html"
|
|
|
|
"miniflux.app/v2/internal/http/route"
|
2023-10-22 04:50:29 +02:00
|
|
|
"miniflux.app/v2/internal/locale"
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/model"
|
2023-10-23 01:07:06 +02:00
|
|
|
"miniflux.app/v2/internal/reader/fetcher"
|
2023-08-11 04:46:45 +02:00
|
|
|
feedHandler "miniflux.app/v2/internal/reader/handler"
|
|
|
|
"miniflux.app/v2/internal/reader/subscription"
|
|
|
|
"miniflux.app/v2/internal/ui/form"
|
|
|
|
"miniflux.app/v2/internal/ui/session"
|
|
|
|
"miniflux.app/v2/internal/ui/view"
|
2018-04-30 01:35:04 +02:00
|
|
|
)
|
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
func (h *handler) submitSubscription(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := session.New(h.store, request.SessionID(r))
|
|
|
|
v := view.New(h.tpl, r, sess)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
user, err := h.store.UserByID(request.UserID(r))
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
categories, err := h.store.Categories(user.ID)
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Set("categories", categories)
|
|
|
|
v.Set("menu", "feeds")
|
|
|
|
v.Set("user", user)
|
2018-11-11 20:28:29 +01:00
|
|
|
v.Set("countUnread", h.store.CountUnreadEntries(user.ID))
|
2020-09-28 01:01:06 +02:00
|
|
|
v.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
|
2020-12-17 06:16:04 +01:00
|
|
|
v.Set("defaultUserAgent", config.Opts.HTTPClientUserAgent())
|
2020-09-10 08:28:54 +02:00
|
|
|
v.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyConfigured())
|
2018-04-30 01:35:04 +02:00
|
|
|
|
|
|
|
subscriptionForm := form.NewSubscriptionForm(r)
|
2023-10-22 04:50:29 +02:00
|
|
|
if validationErr := subscriptionForm.Validate(); validationErr != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
v.Set("form", subscriptionForm)
|
2023-10-22 04:50:29 +02:00
|
|
|
v.Set("errorMessage", validationErr.Translate(user.Language))
|
2018-07-07 05:39:28 +02:00
|
|
|
html.OK(w, r, v.Render("add_subscription"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:07:06 +02:00
|
|
|
var rssBridgeURL string
|
2023-10-22 20:10:56 +02:00
|
|
|
if intg, err := h.store.Integration(user.ID); err == nil && intg != nil && intg.RSSBridgeEnabled {
|
2023-10-23 01:07:06 +02:00
|
|
|
rssBridgeURL = intg.RSSBridgeURL
|
2023-10-22 20:10:56 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 01:07:06 +02:00
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
requestBuilder.WithUserAgent(subscriptionForm.UserAgent)
|
|
|
|
requestBuilder.WithCookie(subscriptionForm.Cookie)
|
|
|
|
requestBuilder.WithUsernameAndPassword(subscriptionForm.Username, subscriptionForm.Password)
|
|
|
|
requestBuilder.UseProxy(subscriptionForm.FetchViaProxy)
|
|
|
|
requestBuilder.IgnoreTLSErrors(subscriptionForm.AllowSelfSignedCertificates)
|
|
|
|
|
|
|
|
subscriptionFinder := subscription.NewSubscriptionFinder(requestBuilder)
|
|
|
|
subscriptions, localizedError := subscriptionFinder.FindSubscriptions(
|
2018-06-20 07:58:29 +02:00
|
|
|
subscriptionForm.URL,
|
2023-10-23 01:07:06 +02:00
|
|
|
rssBridgeURL,
|
2018-06-20 07:58:29 +02:00
|
|
|
)
|
2023-10-22 04:50:29 +02:00
|
|
|
if localizedError != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
v.Set("form", subscriptionForm)
|
2023-10-22 04:50:29 +02:00
|
|
|
v.Set("errorMessage", localizedError.Translate(user.Language))
|
2018-07-07 05:39:28 +02:00
|
|
|
html.OK(w, r, v.Render("add_subscription"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n := len(subscriptions)
|
|
|
|
switch {
|
|
|
|
case n == 0:
|
|
|
|
v.Set("form", subscriptionForm)
|
2023-10-22 04:50:29 +02:00
|
|
|
v.Set("errorMessage", locale.NewLocalizedError("error.subscription_not_found").Translate(user.Language))
|
2018-07-07 05:39:28 +02:00
|
|
|
html.OK(w, r, v.Render("add_subscription"))
|
2023-10-23 01:07:06 +02:00
|
|
|
case n == 1 && subscriptionFinder.IsFeedAlreadyDownloaded():
|
|
|
|
feed, localizedError := feedHandler.CreateFeedFromSubscriptionDiscovery(h.store, user.ID, &model.FeedCreationRequestFromSubscriptionDiscovery{
|
|
|
|
Content: subscriptionFinder.FeedResponseInfo().Content,
|
|
|
|
ETag: subscriptionFinder.FeedResponseInfo().ETag,
|
|
|
|
LastModified: subscriptionFinder.FeedResponseInfo().LastModified,
|
|
|
|
CategoryID: subscriptionForm.CategoryID,
|
|
|
|
FeedURL: subscriptions[0].URL,
|
|
|
|
Crawler: subscriptionForm.Crawler,
|
|
|
|
AllowSelfSignedCertificates: subscriptionForm.AllowSelfSignedCertificates,
|
|
|
|
UserAgent: subscriptionForm.UserAgent,
|
|
|
|
Cookie: subscriptionForm.Cookie,
|
|
|
|
Username: subscriptionForm.Username,
|
|
|
|
Password: subscriptionForm.Password,
|
|
|
|
ScraperRules: subscriptionForm.ScraperRules,
|
|
|
|
RewriteRules: subscriptionForm.RewriteRules,
|
|
|
|
BlocklistRules: subscriptionForm.BlocklistRules,
|
|
|
|
KeeplistRules: subscriptionForm.KeeplistRules,
|
|
|
|
UrlRewriteRules: subscriptionForm.UrlRewriteRules,
|
|
|
|
FetchViaProxy: subscriptionForm.FetchViaProxy,
|
|
|
|
})
|
|
|
|
if localizedError != nil {
|
|
|
|
v.Set("form", subscriptionForm)
|
|
|
|
v.Set("errorMessage", localizedError.Translate(user.Language))
|
|
|
|
html.OK(w, r, v.Render("add_subscription"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
html.Redirect(w, r, route.Path(h.router, "feedEntries", "feedID", feed.ID))
|
|
|
|
case n == 1 && !subscriptionFinder.IsFeedAlreadyDownloaded():
|
2023-10-22 04:50:29 +02:00
|
|
|
feed, localizedError := feedHandler.CreateFeed(h.store, user.ID, &model.FeedCreationRequest{
|
2021-02-21 22:42:49 +01:00
|
|
|
CategoryID: subscriptionForm.CategoryID,
|
|
|
|
FeedURL: subscriptions[0].URL,
|
|
|
|
Crawler: subscriptionForm.Crawler,
|
|
|
|
AllowSelfSignedCertificates: subscriptionForm.AllowSelfSignedCertificates,
|
|
|
|
UserAgent: subscriptionForm.UserAgent,
|
2021-03-23 04:27:58 +01:00
|
|
|
Cookie: subscriptionForm.Cookie,
|
2021-02-21 22:42:49 +01:00
|
|
|
Username: subscriptionForm.Username,
|
|
|
|
Password: subscriptionForm.Password,
|
|
|
|
ScraperRules: subscriptionForm.ScraperRules,
|
|
|
|
RewriteRules: subscriptionForm.RewriteRules,
|
|
|
|
BlocklistRules: subscriptionForm.BlocklistRules,
|
|
|
|
KeeplistRules: subscriptionForm.KeeplistRules,
|
2022-07-12 06:12:26 +02:00
|
|
|
UrlRewriteRules: subscriptionForm.UrlRewriteRules,
|
2021-02-21 22:42:49 +01:00
|
|
|
FetchViaProxy: subscriptionForm.FetchViaProxy,
|
2021-01-03 01:33:41 +01:00
|
|
|
})
|
2023-10-22 04:50:29 +02:00
|
|
|
if localizedError != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
v.Set("form", subscriptionForm)
|
2023-10-22 04:50:29 +02:00
|
|
|
v.Set("errorMessage", localizedError.Translate(user.Language))
|
2018-07-07 05:39:28 +02:00
|
|
|
html.OK(w, r, v.Render("add_subscription"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "feedEntries", "feedID", feed.ID))
|
2018-04-30 01:35:04 +02:00
|
|
|
case n > 1:
|
2018-11-11 20:28:29 +01:00
|
|
|
v := view.New(h.tpl, r, sess)
|
2018-04-30 01:35:04 +02:00
|
|
|
v.Set("subscriptions", subscriptions)
|
2018-06-20 07:58:29 +02:00
|
|
|
v.Set("form", subscriptionForm)
|
2018-04-30 01:35:04 +02:00
|
|
|
v.Set("menu", "feeds")
|
|
|
|
v.Set("user", user)
|
2018-11-11 20:28:29 +01:00
|
|
|
v.Set("countUnread", h.store.CountUnreadEntries(user.ID))
|
2020-09-28 01:01:06 +02:00
|
|
|
v.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
|
2020-09-10 08:28:54 +02:00
|
|
|
v.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyConfigured())
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2018-07-07 05:39:28 +02:00
|
|
|
html.OK(w, r, v.Render("choose_subscription"))
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|
|
|
|
}
|