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