2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
package ui
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
2018-01-03 07:04:48 +01:00
|
|
|
"github.com/miniflux/miniflux/http/handler"
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
|
|
|
"github.com/miniflux/miniflux/reader/subscription"
|
2018-01-03 07:04:48 +01:00
|
|
|
"github.com/miniflux/miniflux/ui/form"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-22 04:37:47 +01:00
|
|
|
// Bookmarklet prefill the form to add a subscription from the URL provided by the bookmarklet.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) Bookmarklet(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 04:37:47 +01:00
|
|
|
user := ctx.LoggedUser()
|
|
|
|
args, err := c.getSubscriptionFormTemplateArgs(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
response.HTML().ServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarkletURL := request.QueryStringParam("uri", "")
|
|
|
|
response.HTML().Render("add_subscription", args.Merge(tplParams{
|
|
|
|
"form": &form.SubscriptionForm{URL: bookmarkletURL},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSubscription shows the form to add a new feed.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) AddSubscription(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
args, err := c.getSubscriptionFormTemplateArgs(ctx, user)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("add_subscription", args)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 04:37:47 +01:00
|
|
|
// SubmitSubscription try to find a feed from the URL provided by the user.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) SubmitSubscription(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
args, err := c.getSubscriptionFormTemplateArgs(ctx, user)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
subscriptionForm := form.NewSubscriptionForm(request.Request())
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := subscriptionForm.Validate(); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("add_subscription", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"form": subscriptionForm,
|
|
|
|
"errorMessage": err.Error(),
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptions, err := subscription.FindSubscriptions(subscriptionForm.URL)
|
|
|
|
if err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Error("[Controller:SubmitSubscription] %v", err)
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("add_subscription", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"form": subscriptionForm,
|
|
|
|
"errorMessage": err,
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:46:01 +01:00
|
|
|
logger.Debug("[UI:SubmitSubscription] %s", subscriptions)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
n := len(subscriptions)
|
|
|
|
switch {
|
|
|
|
case n == 0:
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("add_subscription", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"form": subscriptionForm,
|
|
|
|
"errorMessage": "Unable to find any subscription.",
|
|
|
|
}))
|
|
|
|
case n == 1:
|
2017-12-13 04:19:36 +01:00
|
|
|
feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptions[0].URL, subscriptionForm.Crawler)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("add_subscription", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"form": subscriptionForm,
|
|
|
|
"errorMessage": err,
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
response.Redirect(ctx.Route("feedEntries", "feedID", feed.ID))
|
2017-11-20 06:10:04 +01:00
|
|
|
case n > 1:
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("choose_subscription", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"categoryID": subscriptionForm.CategoryID,
|
|
|
|
"subscriptions": subscriptions,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 04:37:47 +01:00
|
|
|
// ChooseSubscription shows a page to choose a subscription.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) ChooseSubscription(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
args, err := c.getSubscriptionFormTemplateArgs(ctx, user)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
subscriptionForm := form.NewSubscriptionForm(request.Request())
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := subscriptionForm.Validate(); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("add_subscription", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"form": subscriptionForm,
|
|
|
|
"errorMessage": err.Error(),
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:19:36 +01:00
|
|
|
feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptionForm.URL, subscriptionForm.Crawler)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("add_subscription", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"form": subscriptionForm,
|
|
|
|
"errorMessage": err,
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
response.Redirect(ctx.Route("feedEntries", "feedID", feed.ID))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) getSubscriptionFormTemplateArgs(ctx *handler.Context, user *model.User) (tplParams, error) {
|
2017-11-20 06:10:04 +01:00
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
categories, err := c.store.Categories(user.ID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
args["categories"] = categories
|
|
|
|
args["menu"] = "feeds"
|
|
|
|
return args, nil
|
|
|
|
}
|