shortcut.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  33. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil {
  34. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err)
  35. }
  36. return nil
  37. })
  38. g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error {
  39. ctx := c.Request().Context()
  40. userID, ok := c.Get(getUserIDContextKey()).(int)
  41. if !ok {
  42. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  43. }
  44. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  45. if err != nil {
  46. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  47. }
  48. shortcutFind := &api.ShortcutFind{
  49. ID: &shortcutID,
  50. }
  51. shortcut, err := s.Store.FindShortcut(ctx, shortcutFind)
  52. if err != nil {
  53. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
  54. }
  55. if shortcut.CreatorID != userID {
  56. return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
  57. }
  58. currentTs := time.Now().Unix()
  59. shortcutPatch := &api.ShortcutPatch{
  60. UpdatedTs: &currentTs,
  61. }
  62. if err := json.NewDecoder(c.Request().Body).Decode(shortcutPatch); err != nil {
  63. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
  64. }
  65. shortcutPatch.ID = shortcutID
  66. shortcut, err = s.Store.PatchShortcut(ctx, shortcutPatch)
  67. if err != nil {
  68. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err)
  69. }
  70. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  71. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil {
  72. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err)
  73. }
  74. return nil
  75. })
  76. g.GET("/shortcut", func(c echo.Context) error {
  77. ctx := c.Request().Context()
  78. userID, ok := c.Get(getUserIDContextKey()).(int)
  79. if !ok {
  80. return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find shortcut")
  81. }
  82. shortcutFind := &api.ShortcutFind{
  83. CreatorID: &userID,
  84. }
  85. list, err := s.Store.FindShortcutList(ctx, shortcutFind)
  86. if err != nil {
  87. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err)
  88. }
  89. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  90. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
  91. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut list response").SetInternal(err)
  92. }
  93. return nil
  94. })
  95. g.GET("/shortcut/:shortcutId", func(c echo.Context) error {
  96. ctx := c.Request().Context()
  97. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  98. if err != nil {
  99. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  100. }
  101. shortcutFind := &api.ShortcutFind{
  102. ID: &shortcutID,
  103. }
  104. shortcut, err := s.Store.FindShortcut(ctx, shortcutFind)
  105. if err != nil {
  106. return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", *shortcutFind.ID)).SetInternal(err)
  107. }
  108. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  109. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil {
  110. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err)
  111. }
  112. return nil
  113. })
  114. g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error {
  115. ctx := c.Request().Context()
  116. userID, ok := c.Get(getUserIDContextKey()).(int)
  117. if !ok {
  118. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  119. }
  120. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  121. if err != nil {
  122. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  123. }
  124. shortcutFind := &api.ShortcutFind{
  125. ID: &shortcutID,
  126. }
  127. shortcut, err := s.Store.FindShortcut(ctx, shortcutFind)
  128. if err != nil {
  129. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
  130. }
  131. if shortcut.CreatorID != userID {
  132. return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
  133. }
  134. shortcutDelete := &api.ShortcutDelete{
  135. ID: &shortcutID,
  136. }
  137. if err := s.Store.DeleteShortcut(ctx, shortcutDelete); err != nil {
  138. if common.ErrorCode(err) == common.NotFound {
  139. return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Shortcut ID not found: %d", shortcutID))
  140. }
  141. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
  142. }
  143. return c.JSON(http.StatusOK, true)
  144. })
  145. }
  146. func (s *Server) createShortcutCreateActivity(c echo.Context, shortcut *api.Shortcut) error {
  147. ctx := c.Request().Context()
  148. payload := api.ActivityShortcutCreatePayload{
  149. Title: shortcut.Title,
  150. Payload: shortcut.Payload,
  151. }
  152. payloadStr, err := json.Marshal(payload)
  153. if err != nil {
  154. return errors.Wrap(err, "failed to marshal activity payload")
  155. }
  156. activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
  157. CreatorID: shortcut.CreatorID,
  158. Type: api.ActivityShortcutCreate,
  159. Level: api.ActivityInfo,
  160. Payload: string(payloadStr),
  161. })
  162. if err != nil || activity == nil {
  163. return errors.Wrap(err, "failed to create activity")
  164. }
  165. return err
  166. }