memos/server/webhook.go

112 lines
3.3 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
"encoding/json"
2022-02-03 10:32:03 +03:00
"fmt"
"memos/api"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
)
func (s *Server) registerWebhookRoutes(g *echo.Group) {
g.GET("/test", func(c echo.Context) error {
return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>")
})
2022-02-18 17:21:10 +03:00
2022-02-03 10:32:03 +03:00
g.POST("/:openId/memo", func(c echo.Context) error {
openId := c.Param("openId")
userFind := &api.UserFind{
OpenId: &openId,
}
user, err := s.UserService.FindUser(userFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
}
if user == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User openId not found: %s", openId))
}
memoCreate := &api.MemoCreate{
CreatorId: user.Id,
}
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
2022-02-03 10:32:03 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request by open api").SetInternal(err)
}
memo, err := s.MemoService.CreateMemo(memoCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
2022-02-04 12:06:04 +03:00
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
2022-02-05 06:43:25 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
2022-02-03 10:32:03 +03:00
}
return nil
})
2022-02-18 17:21:10 +03:00
2022-02-05 19:25:41 +03:00
g.GET("/:openId/memo", func(c echo.Context) error {
openId := c.Param("openId")
userFind := &api.UserFind{
OpenId: &openId,
}
user, err := s.UserService.FindUser(userFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
}
if user == nil {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Unauthorized: %s", openId))
}
memoFind := &api.MemoFind{
CreatorId: &user.Id,
}
2022-03-28 19:01:34 +03:00
rowStatus := c.QueryParam("rowStatus")
if rowStatus != "" {
memoFind.RowStatus = &rowStatus
2022-02-05 19:25:41 +03:00
}
list, err := s.MemoService.FindMemoList(memoFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err)
}
return nil
})
2022-02-04 13:54:24 +03:00
g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
2022-02-03 10:32:03 +03:00
resourceId, err := strconv.Atoi(c.Param("resourceId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
}
filename := c.Param("filename")
resourceFind := &api.ResourceFind{
Id: &resourceId,
Filename: &filename,
}
resource, err := s.ResourceService.FindResource(resourceFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceId)).SetInternal(err)
}
c.Response().Writer.WriteHeader(http.StatusOK)
c.Response().Writer.Header().Set("Content-Type", "application/octet-stream")
c.Response().Writer.Write(resource.Blob)
return nil
})
}