memos/server/webhook.go

268 lines
8.5 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"
"io/ioutil"
2022-02-03 10:32:03 +03:00
"net/http"
2022-06-28 17:32:10 +03:00
"regexp"
2022-02-03 10:32:03 +03:00
"strconv"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/api"
2022-02-03 10:32:03 +03:00
"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 {
2022-05-02 21:05:43 +03:00
openID := c.Param("openId")
2022-02-03 10:32:03 +03:00
userFind := &api.UserFind{
2022-05-02 21:05:43 +03:00
OpenID: &openID,
2022-02-03 10:32:03 +03:00
}
2022-05-16 02:37:23 +03:00
user, err := s.Store.FindUser(userFind)
2022-02-03 10:32:03 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
}
if user == nil {
2022-05-02 21:05:43 +03:00
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User openId not found: %s", openID))
2022-02-03 10:32:03 +03:00
}
memoCreate := &api.MemoCreate{
2022-05-02 21:05:43 +03:00
CreatorID: user.ID,
2022-02-03 10:32:03 +03:00
}
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)
}
2022-05-16 02:37:23 +03:00
memo, err := s.Store.CreateMemo(memoCreate)
2022-02-03 10:32:03 +03:00
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-05-22 17:39:24 +03:00
g.PATCH("/:openId/memo/:memoId", func(c echo.Context) error {
2022-05-02 21:05:43 +03:00
openID := c.Param("openId")
2022-05-22 17:39:24 +03:00
userFind := &api.UserFind{
OpenID: &openID,
}
user, err := s.Store.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))
}
memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("memoId is not a number: %s", c.Param("memoId"))).SetInternal(err)
}
memoPatch := &api.MemoPatch{
ID: memoID,
}
if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request by open api").SetInternal(err)
}
memo, err := s.Store.PatchMemo(memoPatch)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch memo").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
}
return nil
})
2022-02-05 19:25:41 +03:00
2022-05-22 17:39:24 +03:00
g.GET("/:openId/memo", func(c echo.Context) error {
openID := c.Param("openId")
2022-02-05 19:25:41 +03:00
userFind := &api.UserFind{
2022-05-02 21:05:43 +03:00
OpenID: &openID,
2022-02-05 19:25:41 +03:00
}
2022-05-16 02:37:23 +03:00
user, err := s.Store.FindUser(userFind)
2022-02-05 19:25:41 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
}
if user == nil {
2022-05-22 17:39:24 +03:00
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Not found user with openid: %s", openID))
2022-02-05 19:25:41 +03:00
}
memoFind := &api.MemoFind{
2022-05-02 21:05:43 +03:00
CreatorID: &user.ID,
2022-02-05 19:25:41 +03:00
}
2022-05-19 13:32:04 +03:00
rowStatus := api.RowStatus(c.QueryParam("rowStatus"))
2022-03-28 19:01:34 +03:00
if rowStatus != "" {
memoFind.RowStatus = &rowStatus
2022-02-05 19:25:41 +03:00
}
2022-06-14 18:09:03 +03:00
pinnedStr := c.QueryParam("pinned")
if pinnedStr != "" {
pinned := pinnedStr == "true"
memoFind.Pinned = &pinned
}
tag := c.QueryParam("tag")
if tag != "" {
2022-06-21 16:58:33 +03:00
contentSearch := tag + " "
memoFind.ContentSearch = &contentSearch
}
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
memoFind.Limit = limit
}
if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
memoFind.Offset = offset
2022-06-14 18:09:03 +03:00
}
2022-02-05 19:25:41 +03:00
2022-05-16 02:37:23 +03:00
list, err := s.Store.FindMemoList(memoFind)
2022-02-05 19:25:41 +03:00
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
})
g.POST("/:openId/resource", func(c echo.Context) error {
openID := c.Param("openId")
userFind := &api.UserFind{
OpenID: &openID,
}
2022-05-16 02:37:23 +03:00
user, err := s.Store.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))
}
if err := c.Request().ParseMultipartForm(64 << 20); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Upload file overload max size").SetInternal(err)
}
file, err := c.FormFile("file")
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Upload file not found").SetInternal(err)
}
filename := file.Filename
filetype := file.Header.Get("Content-Type")
size := file.Size
src, err := file.Open()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to open file").SetInternal(err)
}
defer src.Close()
fileBytes, err := ioutil.ReadAll(src)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err)
}
resourceCreate := &api.ResourceCreate{
Filename: filename,
Type: filetype,
Size: size,
Blob: fileBytes,
CreatorID: user.ID,
}
2022-05-16 02:37:23 +03:00
resource, err := s.Store.CreateResource(resourceCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
}
return nil
})
2022-06-28 17:32:10 +03:00
g.GET("/:openId/tag", func(c echo.Context) error {
openID := c.Param("openId")
userFind := &api.UserFind{
OpenID: &openID,
}
user, err := s.Store.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))
}
contentSearch := "#"
normalRowStatus := api.Normal
memoFind := api.MemoFind{
CreatorID: &user.ID,
ContentSearch: &contentSearch,
RowStatus: &normalRowStatus,
}
memoList, err := s.Store.FindMemoList(&memoFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
}
tagMapSet := make(map[string]bool)
r, err := regexp.Compile("#(.+?) ")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compile regexp").SetInternal(err)
}
for _, memo := range memoList {
for _, rawTag := range r.FindAllString(memo.Content, -1) {
tag := r.ReplaceAllString(rawTag, "$1")
tagMapSet[tag] = true
}
}
tagList := []string{}
for tag := range tagMapSet {
tagList = append(tagList, tag)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
}
return nil
})
2022-02-04 13:54:24 +03:00
g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
2022-05-02 21:05:43 +03:00
resourceID, err := strconv.Atoi(c.Param("resourceId"))
2022-02-03 10:32:03 +03:00
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{
2022-05-02 21:05:43 +03:00
ID: &resourceID,
2022-02-03 10:32:03 +03:00
Filename: &filename,
}
2022-05-16 02:37:23 +03:00
resource, err := s.Store.FindResource(resourceFind)
2022-02-03 10:32:03 +03:00
if err != nil {
2022-05-02 21:05:43 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
2022-02-03 10:32:03 +03:00
}
c.Response().Writer.WriteHeader(http.StatusOK)
2022-03-29 02:30:29 +03:00
c.Response().Writer.Header().Set("Content-Type", resource.Type)
2022-02-03 10:32:03 +03:00
c.Response().Writer.Write(resource.Blob)
return nil
})
}