shortcut.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/usememos/memos/api"
  10. "github.com/usememos/memos/common"
  11. "github.com/labstack/echo/v4"
  12. )
  13. func (s *Server) registerShortcutRoutes(g *echo.Group) {
  14. g.POST("/shortcut", func(c echo.Context) error {
  15. ctx := c.Request().Context()
  16. userID, ok := c.Get(getUserIDContextKey()).(int)
  17. if !ok {
  18. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  19. }
  20. shortcutCreate := &api.ShortcutCreate{}
  21. if err := json.NewDecoder(c.Request().Body).Decode(shortcutCreate); err != nil {
  22. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err)
  23. }
  24. shortcutCreate.CreatorID = userID
  25. shortcut, err := s.Store.CreateShortcut(ctx, shortcutCreate)
  26. if err != nil {
  27. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err)
  28. }
  29. if err := s.createShortcutCreateActivity(c, shortcut); err != nil {
  30. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
  31. }
  32. return c.JSON(http.StatusOK, composeResponse(shortcut))
  33. })
  34. g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error {
  35. ctx := c.Request().Context()
  36. userID, ok := c.Get(getUserIDContextKey()).(int)
  37. if !ok {
  38. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  39. }
  40. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  41. if err != nil {
  42. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  43. }
  44. shortcutFind := &api.ShortcutFind{
  45. ID: &shortcutID,
  46. }
  47. shortcut, err := s.Store.FindShortcut(ctx, shortcutFind)
  48. if err != nil {
  49. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
  50. }
  51. if shortcut.CreatorID != userID {
  52. return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
  53. }
  54. currentTs := time.Now().Unix()
  55. shortcutPatch := &api.ShortcutPatch{
  56. UpdatedTs: &currentTs,
  57. }
  58. if err := json.NewDecoder(c.Request().Body).Decode(shortcutPatch); err != nil {
  59. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
  60. }
  61. shortcutPatch.ID = shortcutID
  62. shortcut, err = s.Store.PatchShortcut(ctx, shortcutPatch)
  63. if err != nil {
  64. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err)
  65. }
  66. return c.JSON(http.StatusOK, composeResponse(shortcut))
  67. })
  68. g.GET("/shortcut", func(c echo.Context) error {
  69. ctx := c.Request().Context()
  70. userID, ok := c.Get(getUserIDContextKey()).(int)
  71. if !ok {
  72. return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find shortcut")
  73. }
  74. shortcutFind := &api.ShortcutFind{
  75. CreatorID: &userID,
  76. }
  77. list, err := s.Store.FindShortcutList(ctx, shortcutFind)
  78. if err != nil {
  79. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err)
  80. }
  81. return c.JSON(http.StatusOK, composeResponse(list))
  82. })
  83. g.GET("/shortcut/:shortcutId", func(c echo.Context) error {
  84. ctx := c.Request().Context()
  85. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  86. if err != nil {
  87. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  88. }
  89. shortcutFind := &api.ShortcutFind{
  90. ID: &shortcutID,
  91. }
  92. shortcut, err := s.Store.FindShortcut(ctx, shortcutFind)
  93. if err != nil {
  94. return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", *shortcutFind.ID)).SetInternal(err)
  95. }
  96. return c.JSON(http.StatusOK, composeResponse(shortcut))
  97. })
  98. g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error {
  99. ctx := c.Request().Context()
  100. userID, ok := c.Get(getUserIDContextKey()).(int)
  101. if !ok {
  102. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  103. }
  104. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  105. if err != nil {
  106. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  107. }
  108. shortcutFind := &api.ShortcutFind{
  109. ID: &shortcutID,
  110. }
  111. shortcut, err := s.Store.FindShortcut(ctx, shortcutFind)
  112. if err != nil {
  113. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
  114. }
  115. if shortcut.CreatorID != userID {
  116. return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
  117. }
  118. shortcutDelete := &api.ShortcutDelete{
  119. ID: &shortcutID,
  120. }
  121. if err := s.Store.DeleteShortcut(ctx, shortcutDelete); err != nil {
  122. if common.ErrorCode(err) == common.NotFound {
  123. return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Shortcut ID not found: %d", shortcutID))
  124. }
  125. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
  126. }
  127. return c.JSON(http.StatusOK, true)
  128. })
  129. }
  130. func (s *Server) createShortcutCreateActivity(c echo.Context, shortcut *api.Shortcut) error {
  131. ctx := c.Request().Context()
  132. payload := api.ActivityShortcutCreatePayload{
  133. Title: shortcut.Title,
  134. Payload: shortcut.Payload,
  135. }
  136. payloadBytes, err := json.Marshal(payload)
  137. if err != nil {
  138. return errors.Wrap(err, "failed to marshal activity payload")
  139. }
  140. activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
  141. CreatorID: shortcut.CreatorID,
  142. Type: api.ActivityShortcutCreate,
  143. Level: api.ActivityInfo,
  144. Payload: string(payloadBytes),
  145. })
  146. if err != nil || activity == nil {
  147. return errors.Wrap(err, "failed to create activity")
  148. }
  149. return err
  150. }