1
0
Fork 0
miniflux/ui/entry.go

494 lines
12 KiB
Go
Raw Normal View History

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 ui
2017-11-20 00:10:04 -05:00
import (
"errors"
2017-11-21 18:46:59 -05:00
"github.com/miniflux/miniflux/http/handler"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/integration"
"github.com/miniflux/miniflux/logger"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/model"
"github.com/miniflux/miniflux/reader/sanitizer"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/reader/scraper"
"github.com/miniflux/miniflux/storage"
2017-11-20 00:10:04 -05:00
)
// FetchContent downloads the original HTML page and returns relevant contents.
func (c *Controller) FetchContent(ctx *handler.Context, request *handler.Request, response *handler.Response) {
entryID, err := request.IntegerParam("entryID")
if err != nil {
response.HTML().BadRequest(err)
return
}
user := ctx.LoggedUser()
2017-12-28 22:20:14 -05:00
builder := c.store.NewEntryQueryBuilder(user.ID)
builder.WithEntryID(entryID)
builder.WithoutStatus(model.EntryStatusRemoved)
entry, err := builder.GetEntry()
if err != nil {
response.JSON().ServerError(err)
return
}
if entry == nil {
response.JSON().NotFound(errors.New("Entry not found"))
return
}
2017-12-10 23:51:04 -05:00
content, err := scraper.Fetch(entry.URL, entry.Feed.ScraperRules)
if err != nil {
response.JSON().ServerError(err)
return
}
entry.Content = sanitizer.Sanitize(entry.URL, content)
2017-12-10 23:51:04 -05:00
c.store.UpdateEntryContent(entry)
response.JSON().Created(map[string]string{"content": entry.Content})
}
// SaveEntry send the link to external services.
func (c *Controller) SaveEntry(ctx *handler.Context, request *handler.Request, response *handler.Response) {
entryID, err := request.IntegerParam("entryID")
if err != nil {
response.HTML().BadRequest(err)
return
}
user := ctx.LoggedUser()
2017-12-28 22:20:14 -05:00
builder := c.store.NewEntryQueryBuilder(user.ID)
builder.WithEntryID(entryID)
builder.WithoutStatus(model.EntryStatusRemoved)
entry, err := builder.GetEntry()
if err != nil {
response.JSON().ServerError(err)
return
}
if entry == nil {
response.JSON().NotFound(errors.New("Entry not found"))
return
}
settings, err := c.store.Integration(user.ID)
if err != nil {
response.JSON().ServerError(err)
return
}
go func() {
integration.SendEntry(entry, settings)
}()
response.JSON().Created(map[string]string{"message": "saved"})
}
2017-11-21 18:46:59 -05:00
// ShowFeedEntry shows a single feed entry in "feed" mode.
func (c *Controller) ShowFeedEntry(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-21 21:37:08 -05:00
user := ctx.LoggedUser()
2017-11-20 00:10:04 -05:00
2017-11-21 21:14:45 -05:00
entryID, err := request.IntegerParam("entryID")
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: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-12-28 22:20:14 -05:00
builder := c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
builder.WithoutStatus(model.EntryStatusRemoved)
2017-11-20 00:10:04 -05:00
entry, err := builder.GetEntry()
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 entry == nil {
2017-11-21 21:30:16 -05:00
response.HTML().NotFound()
2017-11-20 00:10:04 -05:00
return
}
2017-12-02 20:04:01 -05:00
if entry.Status == model.EntryStatusUnread {
err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Error("[Controller:ShowFeedEntry] %v", err)
2017-12-02 20:04:01 -05:00
response.HTML().ServerError(nil)
return
}
2017-11-20 00:10:04 -05:00
}
2017-12-02 20:04:01 -05:00
args, err := c.getCommonTemplateArgs(ctx)
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-12-28 22:20:14 -05:00
builder = c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithFeedID(feedID)
2017-12-02 20:04:01 -05:00
prevEntry, nextEntry, err := c.getEntryPrevNext(user, builder, entry.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
}
nextEntryRoute := ""
if nextEntry != nil {
2017-11-21 21:37:08 -05:00
nextEntryRoute = ctx.Route("feedEntry", "feedID", feedID, "entryID", nextEntry.ID)
2017-11-20 00:10:04 -05:00
}
prevEntryRoute := ""
if prevEntry != nil {
2017-11-21 21:37:08 -05:00
prevEntryRoute = ctx.Route("feedEntry", "feedID", feedID, "entryID", prevEntry.ID)
2017-11-20 00:10:04 -05:00
}
2017-11-21 21:30:16 -05:00
response.HTML().Render("entry", args.Merge(tplParams{
2017-11-20 00:10:04 -05:00
"entry": entry,
"prevEntry": prevEntry,
"nextEntry": nextEntry,
"nextEntryRoute": nextEntryRoute,
"prevEntryRoute": prevEntryRoute,
"menu": "feeds",
}))
}
2017-11-21 18:46:59 -05:00
// ShowCategoryEntry shows a single feed entry in "category" mode.
func (c *Controller) ShowCategoryEntry(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-21 21:37:08 -05:00
user := ctx.LoggedUser()
2017-11-20 00:10:04 -05:00
2017-11-21 21:14:45 -05:00
categoryID, err := request.IntegerParam("categoryID")
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:14:45 -05:00
entryID, err := request.IntegerParam("entryID")
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-12-28 22:20:14 -05:00
builder := c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithCategoryID(categoryID)
builder.WithEntryID(entryID)
builder.WithoutStatus(model.EntryStatusRemoved)
2017-11-20 00:10:04 -05:00
entry, err := builder.GetEntry()
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 entry == nil {
2017-11-21 21:30:16 -05:00
response.HTML().NotFound()
2017-11-20 00:10:04 -05:00
return
}
2017-12-02 20:04:01 -05:00
if entry.Status == model.EntryStatusUnread {
err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Error("[Controller:ShowCategoryEntry] %v", err)
2017-12-02 20:04:01 -05:00
response.HTML().ServerError(nil)
return
}
2017-11-20 00:10:04 -05:00
}
2017-12-02 20:04:01 -05:00
args, err := c.getCommonTemplateArgs(ctx)
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-12-28 22:20:14 -05:00
builder = c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithCategoryID(categoryID)
2017-12-02 20:04:01 -05:00
prevEntry, nextEntry, err := c.getEntryPrevNext(user, builder, entry.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
}
nextEntryRoute := ""
if nextEntry != nil {
2017-11-21 21:37:08 -05:00
nextEntryRoute = ctx.Route("categoryEntry", "categoryID", categoryID, "entryID", nextEntry.ID)
2017-11-20 00:10:04 -05:00
}
prevEntryRoute := ""
if prevEntry != nil {
2017-11-21 21:37:08 -05:00
prevEntryRoute = ctx.Route("categoryEntry", "categoryID", categoryID, "entryID", prevEntry.ID)
2017-11-20 00:10:04 -05:00
}
2017-11-21 21:30:16 -05:00
response.HTML().Render("entry", args.Merge(tplParams{
2017-11-20 00:10:04 -05:00
"entry": entry,
"prevEntry": prevEntry,
"nextEntry": nextEntry,
"nextEntryRoute": nextEntryRoute,
"prevEntryRoute": prevEntryRoute,
"menu": "categories",
}))
}
2017-11-21 18:46:59 -05:00
// ShowUnreadEntry shows a single feed entry in "unread" mode.
func (c *Controller) ShowUnreadEntry(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-21 21:37:08 -05:00
user := ctx.LoggedUser()
2017-11-20 00:10:04 -05:00
2017-11-21 21:14:45 -05:00
entryID, err := request.IntegerParam("entryID")
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-12-28 22:20:14 -05:00
builder := c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithEntryID(entryID)
builder.WithoutStatus(model.EntryStatusRemoved)
2017-11-20 00:10:04 -05:00
entry, err := builder.GetEntry()
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 entry == nil {
2017-11-21 21:30:16 -05:00
response.HTML().NotFound()
2017-11-20 00:10:04 -05:00
return
}
2017-12-02 20:04:01 -05:00
args, err := c.getCommonTemplateArgs(ctx)
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-12-28 22:20:14 -05:00
builder = c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithStatus(model.EntryStatusUnread)
2017-12-02 20:04:01 -05:00
prevEntry, nextEntry, err := c.getEntryPrevNext(user, builder, entry.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
}
nextEntryRoute := ""
if nextEntry != nil {
2017-11-21 21:37:08 -05:00
nextEntryRoute = ctx.Route("unreadEntry", "entryID", nextEntry.ID)
2017-11-20 00:10:04 -05:00
}
prevEntryRoute := ""
if prevEntry != nil {
2017-11-21 21:37:08 -05:00
prevEntryRoute = ctx.Route("unreadEntry", "entryID", prevEntry.ID)
2017-11-20 00:10:04 -05:00
}
2017-12-04 23:40:23 -05:00
// We change the status here, otherwise we cannot get the pagination for unread items.
if entry.Status == model.EntryStatusUnread {
err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Error("[Controller:ShowUnreadEntry] %v", err)
2017-12-04 23:40:23 -05:00
response.HTML().ServerError(nil)
return
}
}
2017-11-21 21:30:16 -05:00
response.HTML().Render("entry", args.Merge(tplParams{
2017-11-20 00:10:04 -05:00
"entry": entry,
"prevEntry": prevEntry,
"nextEntry": nextEntry,
"nextEntryRoute": nextEntryRoute,
"prevEntryRoute": prevEntryRoute,
"menu": "unread",
}))
}
2017-11-21 18:46:59 -05:00
// ShowReadEntry shows a single feed entry in "history" mode.
func (c *Controller) ShowReadEntry(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-21 21:37:08 -05:00
user := ctx.LoggedUser()
2017-11-20 00:10:04 -05:00
2017-11-21 21:14:45 -05:00
entryID, err := request.IntegerParam("entryID")
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-12-28 22:20:14 -05:00
builder := c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithEntryID(entryID)
builder.WithoutStatus(model.EntryStatusRemoved)
2017-11-20 00:10:04 -05:00
entry, err := builder.GetEntry()
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 entry == nil {
2017-11-21 21:30:16 -05:00
response.HTML().NotFound()
2017-11-20 00:10:04 -05:00
return
}
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-12-28 22:20:14 -05:00
builder = c.store.NewEntryQueryBuilder(user.ID)
2017-11-20 00:10:04 -05:00
builder.WithStatus(model.EntryStatusRead)
2017-12-02 20:04:01 -05:00
prevEntry, nextEntry, err := c.getEntryPrevNext(user, builder, entry.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
}
nextEntryRoute := ""
if nextEntry != nil {
2017-11-21 21:37:08 -05:00
nextEntryRoute = ctx.Route("readEntry", "entryID", nextEntry.ID)
2017-11-20 00:10:04 -05:00
}
prevEntryRoute := ""
if prevEntry != nil {
2017-11-21 21:37:08 -05:00
prevEntryRoute = ctx.Route("readEntry", "entryID", prevEntry.ID)
2017-11-20 00:10:04 -05:00
}
2017-11-21 21:30:16 -05:00
response.HTML().Render("entry", args.Merge(tplParams{
2017-11-20 00:10:04 -05:00
"entry": entry,
"prevEntry": prevEntry,
"nextEntry": nextEntry,
"nextEntryRoute": nextEntryRoute,
"prevEntryRoute": prevEntryRoute,
"menu": "history",
}))
}
2017-12-22 14:33:01 -05:00
// ShowStarredEntry shows a single feed entry in "starred" mode.
func (c *Controller) ShowStarredEntry(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-12-22 14:33:01 -05:00
user := ctx.LoggedUser()
entryID, err := request.IntegerParam("entryID")
if err != nil {
response.HTML().BadRequest(err)
return
}
2017-12-28 22:20:14 -05:00
builder := c.store.NewEntryQueryBuilder(user.ID)
2017-12-22 14:33:01 -05:00
builder.WithEntryID(entryID)
builder.WithoutStatus(model.EntryStatusRemoved)
entry, err := builder.GetEntry()
if err != nil {
response.HTML().ServerError(err)
return
}
if entry == nil {
response.HTML().NotFound()
return
}
if entry.Status == model.EntryStatusUnread {
err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
if err != nil {
logger.Error("[Controller:ShowReadEntry] %v", err)
response.HTML().ServerError(nil)
return
}
}
args, err := c.getCommonTemplateArgs(ctx)
if err != nil {
response.HTML().ServerError(err)
return
}
2017-12-28 22:20:14 -05:00
builder = c.store.NewEntryQueryBuilder(user.ID)
2017-12-22 14:33:01 -05:00
builder.WithStarred()
prevEntry, nextEntry, err := c.getEntryPrevNext(user, builder, entry.ID)
if err != nil {
response.HTML().ServerError(err)
return
}
nextEntryRoute := ""
if nextEntry != nil {
nextEntryRoute = ctx.Route("starredEntry", "entryID", nextEntry.ID)
}
prevEntryRoute := ""
if prevEntry != nil {
prevEntryRoute = ctx.Route("starredEntry", "entryID", prevEntry.ID)
}
response.HTML().Render("entry", args.Merge(tplParams{
"entry": entry,
"prevEntry": prevEntry,
"nextEntry": nextEntry,
"nextEntryRoute": nextEntryRoute,
"prevEntryRoute": prevEntryRoute,
"menu": "starred",
}))
}
// UpdateEntriesStatus handles Ajax request to update the status for a list of entries.
func (c *Controller) UpdateEntriesStatus(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-21 21:37:08 -05:00
user := ctx.LoggedUser()
2017-11-20 00:10:04 -05:00
entryIDs, status, err := decodeEntryStatusPayload(request.Body())
2017-11-20 00:10:04 -05:00
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Error("[Controller:UpdateEntryStatus] %v", err)
2017-11-21 21:30:16 -05:00
response.JSON().BadRequest(nil)
2017-11-20 00:10:04 -05:00
return
}
if len(entryIDs) == 0 {
2017-11-21 21:30:16 -05:00
response.JSON().BadRequest(errors.New("The list of entryID is empty"))
2017-11-20 00:10:04 -05:00
return
}
err = c.store.SetEntriesStatus(user.ID, entryIDs, status)
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Error("[Controller:UpdateEntryStatus] %v", err)
2017-11-21 21:30:16 -05:00
response.JSON().ServerError(nil)
2017-11-20 00:10:04 -05:00
return
}
2017-11-21 21:30:16 -05:00
response.JSON().Standard("OK")
2017-11-20 00:10:04 -05:00
}
2017-12-02 20:04:01 -05:00
func (c *Controller) getEntryPrevNext(user *model.User, builder *storage.EntryQueryBuilder, entryID int64) (prev *model.Entry, next *model.Entry, err error) {
builder.WithoutStatus(model.EntryStatusRemoved)
builder.WithOrder(model.DefaultSortingOrder)
builder.WithDirection(user.EntryDirection)
entries, err := builder.GetEntries()
if err != nil {
return nil, nil, err
}
n := len(entries)
for i := 0; i < n; i++ {
if entries[i].ID == entryID {
2017-12-22 14:33:01 -05:00
if i-1 >= 0 {
2017-12-02 20:04:01 -05:00
prev = entries[i-1]
}
if i+1 < n {
next = entries[i+1]
}
}
}
return prev, next, nil
}