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.
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/miniflux/miniflux2/model"
|
|
|
|
"github.com/miniflux/miniflux2/reader/subscription"
|
|
|
|
"github.com/miniflux/miniflux2/server/core"
|
|
|
|
"github.com/miniflux/miniflux2/server/ui/form"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Controller) AddSubscription(ctx *core.Context, request *core.Request, response *core.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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request, response *core.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 {
|
|
|
|
log.Println(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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("[UI:SubmitSubscription]", subscriptions)
|
|
|
|
|
|
|
|
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:
|
|
|
|
feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptions[0].URL)
|
|
|
|
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,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) ChooseSubscription(ctx *core.Context, request *core.Request, response *core.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
|
|
|
|
}
|
|
|
|
|
|
|
|
feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptionForm.URL)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) getSubscriptionFormTemplateArgs(ctx *core.Context, user *model.User) (tplParams, error) {
|
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
categories, err := c.store.GetCategories(user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
args["categories"] = categories
|
|
|
|
args["menu"] = "feeds"
|
|
|
|
return args, nil
|
|
|
|
}
|