entry_read.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package ui
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/http/context"
  8. "github.com/miniflux/miniflux/http/request"
  9. "github.com/miniflux/miniflux/http/response/html"
  10. "github.com/miniflux/miniflux/http/route"
  11. "github.com/miniflux/miniflux/model"
  12. "github.com/miniflux/miniflux/storage"
  13. "github.com/miniflux/miniflux/ui/session"
  14. "github.com/miniflux/miniflux/ui/view"
  15. )
  16. // ShowReadEntry shows a single feed entry in "history" mode.
  17. func (c *Controller) ShowReadEntry(w http.ResponseWriter, r *http.Request) {
  18. ctx := context.New(r)
  19. user, err := c.store.UserByID(ctx.UserID())
  20. if err != nil {
  21. html.ServerError(w, err)
  22. return
  23. }
  24. entryID, err := request.IntParam(r, "entryID")
  25. if err != nil {
  26. html.BadRequest(w, err)
  27. return
  28. }
  29. builder := c.store.NewEntryQueryBuilder(user.ID)
  30. builder.WithEntryID(entryID)
  31. builder.WithoutStatus(model.EntryStatusRemoved)
  32. entry, err := builder.GetEntry()
  33. if err != nil {
  34. html.ServerError(w, err)
  35. return
  36. }
  37. if entry == nil {
  38. html.NotFound(w)
  39. return
  40. }
  41. entryPaginationBuilder := storage.NewEntryPaginationBuilder(c.store, user.ID, entry.ID, user.EntryDirection)
  42. entryPaginationBuilder.WithStatus(model.EntryStatusRead)
  43. prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
  44. if err != nil {
  45. html.ServerError(w, err)
  46. return
  47. }
  48. nextEntryRoute := ""
  49. if nextEntry != nil {
  50. nextEntryRoute = route.Path(c.router, "readEntry", "entryID", nextEntry.ID)
  51. }
  52. prevEntryRoute := ""
  53. if prevEntry != nil {
  54. prevEntryRoute = route.Path(c.router, "readEntry", "entryID", prevEntry.ID)
  55. }
  56. sess := session.New(c.store, ctx)
  57. view := view.New(c.tpl, ctx, sess)
  58. view.Set("entry", entry)
  59. view.Set("prevEntry", prevEntry)
  60. view.Set("nextEntry", nextEntry)
  61. view.Set("nextEntryRoute", nextEntryRoute)
  62. view.Set("prevEntryRoute", prevEntryRoute)
  63. view.Set("menu", "history")
  64. view.Set("user", user)
  65. view.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  66. view.Set("hasSaveEntry", c.store.HasSaveEntry(user.ID))
  67. html.OK(w, r, view.Render("entry"))
  68. }