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"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
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)
|
|
|
|
if err := subscriptionForm.Validate(); err != nil {
|
|
|
|
v.Set("form", subscriptionForm)
|
|
|
|
v.Set("errorMessage", err.Error())
|
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-22 20:10:56 +02:00
|
|
|
var rssbridgeURL string
|
|
|
|
if intg, err := h.store.Integration(user.ID); err == nil && intg != nil && intg.RSSBridgeEnabled {
|
|
|
|
rssbridgeURL = intg.RSSBridgeURL
|
|
|
|
}
|
|
|
|
|
2018-10-15 06:43:48 +02:00
|
|
|
subscriptions, findErr := subscription.FindSubscriptions(
|
2018-06-20 07:58:29 +02:00
|
|
|
subscriptionForm.URL,
|
2018-09-20 03:19:24 +02:00
|
|
|
subscriptionForm.UserAgent,
|
2021-03-23 04:27:58 +01:00
|
|
|
subscriptionForm.Cookie,
|
2018-06-20 07:58:29 +02:00
|
|
|
subscriptionForm.Username,
|
|
|
|
subscriptionForm.Password,
|
2020-09-10 08:28:54 +02:00
|
|
|
subscriptionForm.FetchViaProxy,
|
2021-02-21 22:42:49 +01:00
|
|
|
subscriptionForm.AllowSelfSignedCertificates,
|
2023-10-22 20:10:56 +02:00
|
|
|
rssbridgeURL,
|
2018-06-20 07:58:29 +02:00
|
|
|
)
|
2018-10-15 06:43:48 +02:00
|
|
|
if findErr != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
v.Set("form", subscriptionForm)
|
2018-10-15 06:43:48 +02:00
|
|
|
v.Set("errorMessage", findErr)
|
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)
|
2018-09-22 03:53:29 +02:00
|
|
|
v.Set("errorMessage", "error.subscription_not_found")
|
2018-07-07 05:39:28 +02:00
|
|
|
html.OK(w, r, v.Render("add_subscription"))
|
2018-04-30 01:35:04 +02:00
|
|
|
case n == 1:
|
2021-01-04 22:49:28 +01:00
|
|
|
feed, err := 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
|
|
|
})
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
|
|
|
v.Set("form", subscriptionForm)
|
|
|
|
v.Set("errorMessage", err)
|
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
|
|
|
}
|
|
|
|
}
|