entry_search.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // ShowSearchEntry shows a single entry in "search" mode.
  18. func (c *Controller) ShowSearchEntry(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. searchQuery := request.QueryParam(r, "q", "")
  31. builder := c.store.NewEntryQueryBuilder(user.ID)
  32. builder.WithSearchQuery(searchQuery)
  33. builder.WithEntryID(entryID)
  34. builder.WithoutStatus(model.EntryStatusRemoved)
  35. entry, err := builder.GetEntry()
  36. if err != nil {
  37. html.ServerError(w, err)
  38. return
  39. }
  40. if entry == nil {
  41. html.NotFound(w)
  42. return
  43. }
  44. if entry.Status == model.EntryStatusUnread {
  45. err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
  46. if err != nil {
  47. logger.Error("[Controller:ShowSearchEntry] %v", err)
  48. html.ServerError(w, nil)
  49. return
  50. }
  51. }
  52. entryPaginationBuilder := storage.NewEntryPaginationBuilder(c.store, user.ID, entry.ID, user.EntryDirection)
  53. entryPaginationBuilder.WithSearchQuery(searchQuery)
  54. prevEntry, nextEntry, err := entryPaginationBuilder.Entries()
  55. if err != nil {
  56. html.ServerError(w, err)
  57. return
  58. }
  59. nextEntryRoute := ""
  60. if nextEntry != nil {
  61. nextEntryRoute = route.Path(c.router, "searchEntry", "entryID", nextEntry.ID)
  62. }
  63. prevEntryRoute := ""
  64. if prevEntry != nil {
  65. prevEntryRoute = route.Path(c.router, "searchEntry", "entryID", prevEntry.ID)
  66. }
  67. sess := session.New(c.store, ctx)
  68. view := view.New(c.tpl, ctx, sess)
  69. view.Set("searchQuery", searchQuery)
  70. view.Set("entry", entry)
  71. view.Set("prevEntry", prevEntry)
  72. view.Set("nextEntry", nextEntry)
  73. view.Set("nextEntryRoute", nextEntryRoute)
  74. view.Set("prevEntryRoute", prevEntryRoute)
  75. view.Set("menu", "search")
  76. view.Set("user", user)
  77. view.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  78. view.Set("hasSaveEntry", c.store.HasSaveEntry(user.ID))
  79. html.OK(w, r, view.Render("entry"))
  80. }