shortcut.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package v1
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/labstack/echo/v4"
  9. "github.com/pkg/errors"
  10. "github.com/usememos/memos/store"
  11. )
  12. type Shortcut struct {
  13. ID int `json:"id"`
  14. // Standard fields
  15. RowStatus RowStatus `json:"rowStatus"`
  16. CreatorID int `json:"creatorId"`
  17. CreatedTs int64 `json:"createdTs"`
  18. UpdatedTs int64 `json:"updatedTs"`
  19. // Domain specific fields
  20. Title string `json:"title"`
  21. Payload string `json:"payload"`
  22. }
  23. type CreateShortcutRequest struct {
  24. Title string `json:"title"`
  25. Payload string `json:"payload"`
  26. }
  27. type UpdateShortcutRequest struct {
  28. RowStatus *RowStatus `json:"rowStatus"`
  29. Title *string `json:"title"`
  30. Payload *string `json:"payload"`
  31. }
  32. type ShortcutFind struct {
  33. ID *int
  34. // Standard fields
  35. CreatorID *int
  36. // Domain specific fields
  37. Title *string `json:"title"`
  38. }
  39. type ShortcutDelete struct {
  40. ID *int
  41. // Standard fields
  42. CreatorID *int
  43. }
  44. func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
  45. g.POST("/shortcut", func(c echo.Context) error {
  46. ctx := c.Request().Context()
  47. userID, ok := c.Get(getUserIDContextKey()).(int)
  48. if !ok {
  49. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  50. }
  51. shortcutCreate := &CreateShortcutRequest{}
  52. if err := json.NewDecoder(c.Request().Body).Decode(shortcutCreate); err != nil {
  53. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err)
  54. }
  55. shortcut, err := s.Store.CreateShortcut(ctx, &store.Shortcut{
  56. CreatorID: userID,
  57. Title: shortcutCreate.Title,
  58. Payload: shortcutCreate.Payload,
  59. })
  60. if err != nil {
  61. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err)
  62. }
  63. shortcutMessage := convertShortcutFromStore(shortcut)
  64. if err := s.createShortcutCreateActivity(c, shortcutMessage); err != nil {
  65. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
  66. }
  67. return c.JSON(http.StatusOK, shortcutMessage)
  68. })
  69. g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error {
  70. ctx := c.Request().Context()
  71. userID, ok := c.Get(getUserIDContextKey()).(int)
  72. if !ok {
  73. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  74. }
  75. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  76. if err != nil {
  77. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  78. }
  79. shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{
  80. ID: &shortcutID,
  81. })
  82. if err != nil {
  83. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
  84. }
  85. if shortcut == nil {
  86. return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Shortcut not found: %d", shortcutID))
  87. }
  88. if shortcut.CreatorID != userID {
  89. return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
  90. }
  91. request := &UpdateShortcutRequest{}
  92. if err := json.NewDecoder(c.Request().Body).Decode(request); err != nil {
  93. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
  94. }
  95. currentTs := time.Now().Unix()
  96. shortcutUpdate := &store.UpdateShortcut{
  97. ID: shortcutID,
  98. UpdatedTs: &currentTs,
  99. }
  100. if request.RowStatus != nil {
  101. rowStatus := store.RowStatus(*request.RowStatus)
  102. shortcutUpdate.RowStatus = &rowStatus
  103. }
  104. if request.Title != nil {
  105. shortcutUpdate.Title = request.Title
  106. }
  107. if request.Payload != nil {
  108. shortcutUpdate.Payload = request.Payload
  109. }
  110. shortcut, err = s.Store.UpdateShortcut(ctx, shortcutUpdate)
  111. if err != nil {
  112. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err)
  113. }
  114. return c.JSON(http.StatusOK, convertShortcutFromStore(shortcut))
  115. })
  116. g.GET("/shortcut", func(c echo.Context) error {
  117. ctx := c.Request().Context()
  118. userID, ok := c.Get(getUserIDContextKey()).(int)
  119. if !ok {
  120. return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find shortcut")
  121. }
  122. list, err := s.Store.ListShortcuts(ctx, &store.FindShortcut{
  123. CreatorID: &userID,
  124. })
  125. if err != nil {
  126. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get shortcut list").SetInternal(err)
  127. }
  128. shortcutMessageList := make([]*Shortcut, 0, len(list))
  129. for _, shortcut := range list {
  130. shortcutMessageList = append(shortcutMessageList, convertShortcutFromStore(shortcut))
  131. }
  132. return c.JSON(http.StatusOK, shortcutMessageList)
  133. })
  134. g.GET("/shortcut/:shortcutId", func(c echo.Context) error {
  135. ctx := c.Request().Context()
  136. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  137. if err != nil {
  138. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  139. }
  140. shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{
  141. ID: &shortcutID,
  142. })
  143. if err != nil {
  144. return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", shortcutID)).SetInternal(err)
  145. }
  146. if shortcut == nil {
  147. return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Shortcut not found: %d", shortcutID))
  148. }
  149. return c.JSON(http.StatusOK, convertShortcutFromStore(shortcut))
  150. })
  151. g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error {
  152. ctx := c.Request().Context()
  153. userID, ok := c.Get(getUserIDContextKey()).(int)
  154. if !ok {
  155. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  156. }
  157. shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
  158. if err != nil {
  159. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
  160. }
  161. shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{
  162. ID: &shortcutID,
  163. })
  164. if err != nil {
  165. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find shortcut").SetInternal(err)
  166. }
  167. if shortcut == nil {
  168. return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Shortcut not found: %d", shortcutID))
  169. }
  170. if shortcut.CreatorID != userID {
  171. return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
  172. }
  173. if err := s.Store.DeleteShortcut(ctx, &store.DeleteShortcut{
  174. ID: &shortcutID,
  175. }); err != nil {
  176. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
  177. }
  178. return c.JSON(http.StatusOK, true)
  179. })
  180. }
  181. func (s *APIV1Service) createShortcutCreateActivity(c echo.Context, shortcut *Shortcut) error {
  182. ctx := c.Request().Context()
  183. payload := ActivityShortcutCreatePayload{
  184. Title: shortcut.Title,
  185. Payload: shortcut.Payload,
  186. }
  187. payloadBytes, err := json.Marshal(payload)
  188. if err != nil {
  189. return errors.Wrap(err, "failed to marshal activity payload")
  190. }
  191. activity, err := s.Store.CreateActivity(ctx, &store.Activity{
  192. CreatorID: shortcut.CreatorID,
  193. Type: ActivityShortcutCreate.String(),
  194. Level: ActivityInfo.String(),
  195. Payload: string(payloadBytes),
  196. })
  197. if err != nil || activity == nil {
  198. return errors.Wrap(err, "failed to create activity")
  199. }
  200. return err
  201. }
  202. func convertShortcutFromStore(shortcut *store.Shortcut) *Shortcut {
  203. return &Shortcut{
  204. ID: shortcut.ID,
  205. RowStatus: RowStatus(shortcut.RowStatus),
  206. CreatorID: shortcut.CreatorID,
  207. Title: shortcut.Title,
  208. Payload: shortcut.Payload,
  209. CreatedTs: shortcut.CreatedTs,
  210. UpdatedTs: shortcut.UpdatedTs,
  211. }
  212. }