entry_unread.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // Make sure we always get the pagination in unread mode even if the page is refreshed.
  43. if entry.Status == model.EntryStatusRead {
  44. err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusUnread)
  45. if err != nil {
  46. logger.Error("[Controller:ShowUnreadEntry] %v", err)
  47. html.ServerError(w, nil)
  48. return
  49. }
  50. }
  51. entryPaginationBuilder := storage.NewEntryPaginationBuilder(c.store, user.ID, entry.ID, user.EntryDirection)
  52. entryPaginationBuilder.WithStatus(model.EntryStatusUnread)
  53. prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
  54. if err != nil {
  55. html.ServerError(w, err)
  56. return
  57. }
  58. nextEntryRoute := ""
  59. if nextEntry != nil {
  60. nextEntryRoute = route.Path(c.router, "unreadEntry", "entryID", nextEntry.ID)
  61. }
  62. prevEntryRoute := ""
  63. if prevEntry != nil {
  64. prevEntryRoute = route.Path(c.router, "unreadEntry", "entryID", prevEntry.ID)
  65. }
  66. // Always mark the entry as read after fetching the pagination.
  67. err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
  68. if err != nil {
  69. logger.Error("[Controller:ShowUnreadEntry] %v", err)
  70. html.ServerError(w, nil)
  71. return
  72. }
  73. sess := session.New(c.store, ctx)
  74. view := view.New(c.tpl, ctx, sess)
  75. view.Set("entry", entry)
  76. view.Set("prevEntry", prevEntry)
  77. view.Set("nextEntry", nextEntry)
  78. view.Set("nextEntryRoute", nextEntryRoute)
  79. view.Set("prevEntryRoute", prevEntryRoute)
  80. view.Set("menu", "unread")
  81. view.Set("user", user)
  82. view.Set("hasSaveEntry", c.store.HasSaveEntry(user.ID))
  83. // Fetching the counter here avoid to be off by one.
  84. view.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  85. html.OK(w, r, view.Render("entry"))
  86. }