2017-11-20 00:10:04 -05: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 (
|
|
|
|
"errors"
|
2017-11-21 20:40:29 -05:00
|
|
|
"log"
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
"github.com/miniflux/miniflux2/model"
|
|
|
|
"github.com/miniflux/miniflux2/server/core"
|
|
|
|
"github.com/miniflux/miniflux2/server/ui/form"
|
|
|
|
)
|
|
|
|
|
2017-11-22 01:36:00 -05:00
|
|
|
// RefreshAllFeeds refresh all feeds in the background.
|
|
|
|
func (c *Controller) RefreshAllFeeds(ctx *core.Context, request *core.Request, response *core.Response) {
|
|
|
|
user := ctx.LoggedUser()
|
|
|
|
jobs, err := c.store.NewBatch(c.store.CountFeeds(user.ID))
|
|
|
|
if err != nil {
|
|
|
|
response.HTML().ServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
c.pool.Push(jobs)
|
|
|
|
}()
|
|
|
|
|
|
|
|
response.Redirect(ctx.Route("feeds"))
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:40:29 -05:00
|
|
|
// ShowFeedsPage shows the page with all subscriptions.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (c *Controller) ShowFeedsPage(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-21 21:37:08 -05:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
feeds, err := c.store.Feeds(user.ID)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().Render("feeds", args.Merge(tplParams{
|
2017-11-20 00:10:04 -05:00
|
|
|
"feeds": feeds,
|
|
|
|
"total": len(feeds),
|
|
|
|
"menu": "feeds",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:40:29 -05:00
|
|
|
// ShowFeedEntries shows all entries for the given feed.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (c *Controller) ShowFeedEntries(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-21 21:37:08 -05:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-21 21:14:45 -05:00
|
|
|
offset := request.QueryIntegerParam("offset", 0)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
feed, err := c.getFeedFromURL(request, response, user)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
builder := c.store.GetEntryQueryBuilder(user.ID, user.Timezone)
|
|
|
|
builder.WithFeedID(feed.ID)
|
2017-11-21 20:40:29 -05:00
|
|
|
builder.WithoutStatus(model.EntryStatusRemoved)
|
2017-11-20 00:10:04 -05:00
|
|
|
builder.WithOrder(model.DefaultSortingOrder)
|
2017-12-02 20:04:01 -05:00
|
|
|
builder.WithDirection(user.EntryDirection)
|
2017-11-20 00:10:04 -05:00
|
|
|
builder.WithOffset(offset)
|
2017-11-28 00:30:04 -05:00
|
|
|
builder.WithLimit(nbItemsPerPage)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
entries, err := builder.GetEntries()
|
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := builder.CountEntries()
|
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().Render("feed_entries", args.Merge(tplParams{
|
2017-11-20 00:10:04 -05:00
|
|
|
"feed": feed,
|
|
|
|
"entries": entries,
|
|
|
|
"total": count,
|
2017-11-21 21:37:08 -05:00
|
|
|
"pagination": c.getPagination(ctx.Route("feedEntries", "feedID", feed.ID), count, offset),
|
2017-11-20 00:10:04 -05:00
|
|
|
"menu": "feeds",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:40:29 -05:00
|
|
|
// EditFeed shows the form to modify a subscription.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (c *Controller) EditFeed(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-21 21:37:08 -05:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
feed, err := c.getFeedFromURL(request, response, user)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args, err := c.getFeedFormTemplateArgs(ctx, user, feed, nil)
|
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().Render("edit_feed", args)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 20:40:29 -05:00
|
|
|
// UpdateFeed update a subscription and redirect to the feed entries page.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-21 21:37:08 -05:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
feed, err := c.getFeedFromURL(request, response, user)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:14:45 -05:00
|
|
|
feedForm := form.NewFeedForm(request.Request())
|
2017-11-20 00:10:04 -05:00
|
|
|
args, err := c.getFeedFormTemplateArgs(ctx, user, feed, feedForm)
|
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := feedForm.ValidateModification(); err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().Render("edit_feed", args.Merge(tplParams{
|
2017-11-20 00:10:04 -05:00
|
|
|
"errorMessage": err.Error(),
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.store.UpdateFeed(feedForm.Merge(feed))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().Render("edit_feed", args.Merge(tplParams{
|
2017-11-20 00:10:04 -05:00
|
|
|
"errorMessage": "Unable to update this feed.",
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:37:08 -05:00
|
|
|
response.Redirect(ctx.Route("feedEntries", "feedID", feed.ID))
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 20:40:29 -05:00
|
|
|
// RemoveFeed delete a subscription from the database and redirect to the list of feeds page.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-21 21:14:45 -05:00
|
|
|
feedID, err := request.IntegerParam("feedID")
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:37:08 -05:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := c.store.RemoveFeed(user.ID, feedID); err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:37:08 -05:00
|
|
|
response.Redirect(ctx.Route("feeds"))
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 20:40:29 -05:00
|
|
|
// RefreshFeed refresh a subscription and redirect to the feed entries page.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, response *core.Response) {
|
2017-11-21 21:14:45 -05:00
|
|
|
feedID, err := request.IntegerParam("feedID")
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().BadRequest(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:37:08 -05:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := c.feedHandler.RefreshFeed(user.ID, feedID); err != nil {
|
|
|
|
log.Println("[UI:RefreshFeed]", err)
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:37:08 -05:00
|
|
|
response.Redirect(ctx.Route("feedEntries", "feedID", feedID))
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) getFeedFromURL(request *core.Request, response *core.Response, user *model.User) (*model.Feed, error) {
|
2017-11-21 21:14:45 -05:00
|
|
|
feedID, err := request.IntegerParam("feedID")
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().BadRequest(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
feed, err := c.store.FeedByID(user.ID, feedID)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if feed == nil {
|
2017-11-21 21:30:16 -05:00
|
|
|
response.HTML().NotFound()
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil, errors.New("Feed not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) getFeedFormTemplateArgs(ctx *core.Context, user *model.User, feed *model.Feed, feedForm *form.FeedForm) (tplParams, error) {
|
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
categories, err := c.store.Categories(user.ID)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if feedForm == nil {
|
|
|
|
args["form"] = form.FeedForm{
|
|
|
|
SiteURL: feed.SiteURL,
|
|
|
|
FeedURL: feed.FeedURL,
|
|
|
|
Title: feed.Title,
|
|
|
|
CategoryID: feed.Category.ID,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args["form"] = feedForm
|
|
|
|
}
|
|
|
|
|
|
|
|
args["categories"] = categories
|
|
|
|
args["feed"] = feed
|
|
|
|
args["menu"] = "feeds"
|
|
|
|
return args, nil
|
|
|
|
}
|