Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

feat: open api for get memos

emailc77996a8914d
diff --git a/server/auth.go b/server/auth.go
index 085346adc..bd42ce3bc 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -30,8 +30,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
// Compare the stored password
if login.Password != user.Password {
- // If the two passwords don't match, return a 401 status.
- return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password").SetInternal(err)
+ return echo.NewHTTPError(http.StatusBadRequest, "Incorrect password").SetInternal(err)
}
err = setUserSession(c, user)
diff --git a/server/basic_auth.go b/server/basic_auth.go
index 1e84addc2..9bae4da83 100644
--- a/server/basic_auth.go
+++ b/server/basic_auth.go
@@ -72,14 +72,14 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
userId, err := strconv.Atoi(fmt.Sprintf("%v", userIdValue))
if err != nil {
- return echo.NewHTTPError(http.StatusUnauthorized, "Failed to malformatted user id in the session.")
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to malformatted user id in the session.").SetInternal(err)
}
// Even if there is no error, we still need to make sure the user still exists.
- principalFind := &api.UserFind{
+ userFind := &api.UserFind{
Id: &userId,
}
- user, err := us.FindUser(principalFind)
+ user, err := us.FindUser(userFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userId)).SetInternal(err)
}
@@ -89,6 +89,7 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
// Stores userId into context.
c.Set(getUserIdContextKey(), userId)
+
return next(c)
}
}
diff --git a/server/memo.go b/server/memo.go
index b54aa7f1d..0f18e3841 100644
--- a/server/memo.go
+++ b/server/memo.go
@@ -98,8 +98,9 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
memo, err := s.MemoService.FindMemo(memoFind)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoId))
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoId)).SetInternal(err)
}
+
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoId)).SetInternal(err)
}
diff --git a/server/webhook.go b/server/webhook.go
index 017e7efb1..5318668f9 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -47,6 +47,47 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
return nil
})
+ 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,
+ }
+ showHiddenMemo, err := strconv.ParseBool(c.QueryParam("hidden"))
+ if err != nil {
+ showHiddenMemo = false
+ }
+
+ rowStatus := "NORMAL"
+ if showHiddenMemo {
+ rowStatus = "HIDDEN"
+ }
+ memoFind.RowStatus = &rowStatus
+
+ 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
+ })
+
g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
resourceId, err := strconv.Atoi(c.Param("resourceId"))
if err != nil {