lists.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "github.com/knadh/listmonk/models"
  7. "github.com/labstack/echo/v4"
  8. )
  9. // handleGetLists retrieves lists with additional metadata like subscriber counts. This may be slow.
  10. func handleGetLists(c echo.Context) error {
  11. var (
  12. app = c.Get("app").(*App)
  13. out models.PageResults
  14. pg = getPagination(c.QueryParams(), 20)
  15. query = strings.TrimSpace(c.FormValue("query"))
  16. orderBy = c.FormValue("order_by")
  17. order = c.FormValue("order")
  18. minimal, _ = strconv.ParseBool(c.FormValue("minimal"))
  19. listID, _ = strconv.Atoi(c.Param("id"))
  20. )
  21. // Fetch one list.
  22. single := false
  23. if listID > 0 {
  24. single = true
  25. }
  26. // Minimal query simply returns the list of all lists without JOIN subscriber counts. This is fast.
  27. if !single && minimal {
  28. res, err := app.core.GetLists("")
  29. if err != nil {
  30. return err
  31. }
  32. if len(res) == 0 {
  33. return c.JSON(http.StatusOK, okResp{[]struct{}{}})
  34. }
  35. // Meta.
  36. out.Results = res
  37. out.Total = len(res)
  38. out.Page = 1
  39. out.PerPage = out.Total
  40. return c.JSON(http.StatusOK, okResp{out})
  41. }
  42. // Full list query.
  43. res, err := app.core.QueryLists(query, orderBy, order, pg.Offset, pg.Limit)
  44. if err != nil {
  45. return err
  46. }
  47. if single && len(res) == 0 {
  48. return echo.NewHTTPError(http.StatusBadRequest,
  49. app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.list}"))
  50. }
  51. // Replace null tags.
  52. for i, v := range res {
  53. if v.Tags == nil {
  54. res[i].Tags = make([]string, 0)
  55. }
  56. // Total counts.
  57. for _, c := range v.SubscriberCounts {
  58. res[i].SubscriberCount += c
  59. }
  60. }
  61. if single {
  62. return c.JSON(http.StatusOK, okResp{res[0]})
  63. }
  64. // Meta.
  65. // TODO: add .query?
  66. out.Results = res
  67. if len(res) > 0 {
  68. out.Total = res[0].Total
  69. }
  70. out.Page = pg.Page
  71. out.PerPage = pg.PerPage
  72. if out.PerPage == 0 {
  73. out.PerPage = out.Total
  74. }
  75. return c.JSON(http.StatusOK, okResp{out})
  76. }
  77. // handleCreateList handles list creation.
  78. func handleCreateList(c echo.Context) error {
  79. var (
  80. app = c.Get("app").(*App)
  81. l = models.List{}
  82. )
  83. if err := c.Bind(&l); err != nil {
  84. return err
  85. }
  86. // Validate.
  87. if !strHasLen(l.Name, 1, stdInputMaxLen) {
  88. return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("lists.invalidName"))
  89. }
  90. out, err := app.core.CreateList(l)
  91. if err != nil {
  92. return err
  93. }
  94. return c.JSON(http.StatusOK, okResp{out})
  95. }
  96. // handleUpdateList handles list modification.
  97. func handleUpdateList(c echo.Context) error {
  98. var (
  99. app = c.Get("app").(*App)
  100. id, _ = strconv.Atoi(c.Param("id"))
  101. )
  102. if id < 1 {
  103. return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
  104. }
  105. // Incoming params.
  106. var l models.List
  107. if err := c.Bind(&l); err != nil {
  108. return err
  109. }
  110. // Validate.
  111. if !strHasLen(l.Name, 1, stdInputMaxLen) {
  112. return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("lists.invalidName"))
  113. }
  114. out, err := app.core.UpdateList(id, l)
  115. if err != nil {
  116. return err
  117. }
  118. return c.JSON(http.StatusOK, okResp{out})
  119. }
  120. // handleDeleteLists handles list deletion, either a single one (ID in the URI), or a list.
  121. func handleDeleteLists(c echo.Context) error {
  122. var (
  123. app = c.Get("app").(*App)
  124. id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
  125. ids []int
  126. )
  127. if id < 1 && len(ids) == 0 {
  128. return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
  129. }
  130. if id > 0 {
  131. ids = append(ids, int(id))
  132. }
  133. if err := app.core.DeleteLists(ids); err != nil {
  134. return err
  135. }
  136. return c.JSON(http.StatusOK, okResp{true})
  137. }