entry_unread.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/logger"
  12. "github.com/miniflux/miniflux/model"
  13. "github.com/miniflux/miniflux/storage"
  14. "github.com/miniflux/miniflux/ui/session"
  15. "github.com/miniflux/miniflux/ui/view"
  16. )
  17. // ShowUnreadEntry shows a single feed entry in "unread" mode.
  18. func (c *Controller) ShowUnreadEntry(w http.ResponseWriter, r *http.Request) {
  19. ctx := context.New(r)
  20. user, err := c.store.UserByID(ctx.UserID())
  21. if err != nil {
  22. html.ServerError(w, err)
  23. return
  24. }
  25. entryID, err := request.IntParam(r, "entryID")
  26. if err != nil {
  27. html.BadRequest(w, err)
  28. return
  29. }
  30. builder := c.store.NewEntryQueryBuilder(user.ID)
  31. builder.WithEntryID(entryID)
  32. builder.WithoutStatus(model.EntryStatusRemoved)
  33. entry, err := builder.GetEntry()
  34. if err != nil {
  35. html.ServerError(w, err)
  36. return
  37. }
  38. if entry == nil {
  39. html.NotFound(w)
  40. return
  41. }
  42. entryPaginationBuilder := storage.NewEntryPaginationBuilder(c.store, user.ID, entry.ID, user.EntryDirection)
  43. entryPaginationBuilder.WithStatus(model.EntryStatusUnread)
  44. prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
  45. if err != nil {
  46. html.ServerError(w, err)
  47. return
  48. }
  49. nextEntryRoute := ""
  50. if nextEntry != nil {
  51. nextEntryRoute = route.Path(c.router, "unreadEntry", "entryID", nextEntry.ID)
  52. }
  53. prevEntryRoute := ""
  54. if prevEntry != nil {
  55. prevEntryRoute = route.Path(c.router, "unreadEntry", "entryID", prevEntry.ID)
  56. }
  57. // We change the status here, otherwise we cannot get the pagination for unread items.
  58. if entry.Status == model.EntryStatusUnread {
  59. err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
  60. if err != nil {
  61. logger.Error("[Controller:ShowUnreadEntry] %v", err)
  62. html.ServerError(w, nil)
  63. return
  64. }
  65. }
  66. sess := session.New(c.store, ctx)
  67. view := view.New(c.tpl, ctx, sess)
  68. view.Set("entry", entry)
  69. view.Set("prevEntry", prevEntry)
  70. view.Set("nextEntry", nextEntry)
  71. view.Set("nextEntryRoute", nextEntryRoute)
  72. view.Set("prevEntryRoute", prevEntryRoute)
  73. view.Set("menu", "unread")
  74. view.Set("user", user)
  75. view.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  76. view.Set("hasSaveEntry", c.store.HasSaveEntry(user.ID))
  77. html.OK(w, view.Render("entry"))
  78. }