2023-06-19 14:42:47 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2023-08-10 19:46:45 -07:00
|
|
|
package api // import "miniflux.app/v2/internal/api"
|
2017-11-19 21:10:04 -08:00
|
|
|
|
|
|
|
import (
|
2021-01-04 13:49:28 -08:00
|
|
|
json_parser "encoding/json"
|
2018-04-29 16:35:04 -07:00
|
|
|
"net/http"
|
2017-12-12 21:48:13 -08:00
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
"miniflux.app/v2/internal/config"
|
2023-10-22 14:10:56 -04:00
|
|
|
"miniflux.app/v2/internal/http/request"
|
2023-08-10 19:46:45 -07:00
|
|
|
"miniflux.app/v2/internal/http/response/json"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2023-10-22 16:07:06 -07:00
|
|
|
"miniflux.app/v2/internal/reader/fetcher"
|
2023-08-10 19:46:45 -07:00
|
|
|
"miniflux.app/v2/internal/reader/subscription"
|
|
|
|
"miniflux.app/v2/internal/validator"
|
2017-11-19 21:10:04 -08:00
|
|
|
)
|
|
|
|
|
2021-01-04 13:49:28 -08:00
|
|
|
func (h *handler) discoverSubscriptions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var subscriptionDiscoveryRequest model.SubscriptionDiscoveryRequest
|
|
|
|
if err := json_parser.NewDecoder(r.Body).Decode(&subscriptionDiscoveryRequest); err != nil {
|
|
|
|
json.BadRequest(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if validationErr := validator.ValidateSubscriptionDiscovery(&subscriptionDiscoveryRequest); validationErr != nil {
|
|
|
|
json.BadRequest(w, r, validationErr.Error())
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-22 14:10:56 -04:00
|
|
|
var rssbridgeURL string
|
|
|
|
intg, err := h.store.Integration(request.UserID(r))
|
|
|
|
if err == nil && intg != nil && intg.RSSBridgeEnabled {
|
|
|
|
rssbridgeURL = intg.RSSBridgeURL
|
|
|
|
}
|
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
2023-11-15 22:12:00 -05:00
|
|
|
requestBuilder.WithUserAgent(subscriptionDiscoveryRequest.UserAgent, config.Opts.HTTPClientUserAgent())
|
2023-10-22 16:07:06 -07:00
|
|
|
requestBuilder.WithCookie(subscriptionDiscoveryRequest.Cookie)
|
|
|
|
requestBuilder.WithUsernameAndPassword(subscriptionDiscoveryRequest.Username, subscriptionDiscoveryRequest.Password)
|
|
|
|
requestBuilder.UseProxy(subscriptionDiscoveryRequest.FetchViaProxy)
|
|
|
|
requestBuilder.IgnoreTLSErrors(subscriptionDiscoveryRequest.AllowSelfSignedCertificates)
|
2024-02-24 22:08:23 -08:00
|
|
|
requestBuilder.DisableHTTP2(subscriptionDiscoveryRequest.DisableHTTP2)
|
2023-10-22 16:07:06 -07:00
|
|
|
|
|
|
|
subscriptions, localizedError := subscription.NewSubscriptionFinder(requestBuilder).FindSubscriptions(
|
2021-01-04 13:49:28 -08:00
|
|
|
subscriptionDiscoveryRequest.URL,
|
2023-10-22 14:10:56 -04:00
|
|
|
rssbridgeURL,
|
2018-06-19 22:58:29 -07:00
|
|
|
)
|
2023-10-21 19:50:29 -07:00
|
|
|
|
|
|
|
if localizedError != nil {
|
|
|
|
json.ServerError(w, r, localizedError.Error())
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:50:29 -07:00
|
|
|
if len(subscriptions) == 0 {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.NotFound(w, r)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-19 19:27:05 -07:00
|
|
|
json.OK(w, r, subscriptions)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|