miniflux/server/ui/controller/feed.go

236 lines
5.8 KiB
Go
Raw Normal View History

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