From 63468dbaf3a741c7679efa2a0d3eb174342c68fb Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 21 Sep 2022 19:31:02 +0800 Subject: [PATCH] chore: update memo detail access handler --- server/acl.go | 2 +- server/memo.go | 61 +++++++++++++++------------ web/src/components/MemoCardDialog.tsx | 5 --- web/src/components/Toast.tsx | 2 +- web/src/pages/MemoDetail.tsx | 17 +++++--- 5 files changed, 49 insertions(+), 38 deletions(-) diff --git a/server/acl.go b/server/acl.go index b0d4a6ad..b826562e 100644 --- a/server/acl.go +++ b/server/acl.go @@ -104,7 +104,7 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc { } } - if common.HasPrefixes(path, "/api/memo/all") && c.Request().Method == http.MethodGet { + if common.HasPrefixes(path, "/api/memo/all", "/api/memo/:memoId") && c.Request().Method == http.MethodGet { return next(c) } diff --git a/server/memo.go b/server/memo.go index 0f136eaa..d8d6846d 100644 --- a/server/memo.go +++ b/server/memo.go @@ -203,6 +203,41 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { return nil }) + g.GET("/memo/:memoId", func(c echo.Context) error { + ctx := c.Request().Context() + memoID, err := strconv.Atoi(c.Param("memoId")) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err) + } + + memoFind := &api.MemoFind{ + ID: &memoID, + } + memo, err := s.Store.FindMemo(ctx, memoFind) + if err != nil { + if common.ErrorCode(err) == common.NotFound { + return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err) + } + + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err) + } + + if memo.Visibility == api.Privite { + return echo.NewHTTPError(http.StatusForbidden, "this memo is private only") + } else if memo.Visibility == api.Protected { + _, ok := c.Get(getUserIDContextKey()).(int) + if !ok { + return echo.NewHTTPError(http.StatusForbidden, "this memo is protected, missing user in session") + } + } + + 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 + }) + g.POST("/memo/:memoId/organizer", func(c echo.Context) error { ctx := c.Request().Context() memoID, err := strconv.Atoi(c.Param("memoId")) @@ -245,32 +280,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { return nil }) - g.GET("/memo/:memoId", func(c echo.Context) error { - ctx := c.Request().Context() - memoID, err := strconv.Atoi(c.Param("memoId")) - if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err) - } - - memoFind := &api.MemoFind{ - ID: &memoID, - } - memo, err := s.Store.FindMemo(ctx, memoFind) - if err != nil { - if common.ErrorCode(err) == common.NotFound { - return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err) - } - - return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).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 - }) - g.DELETE("/memo/:memoId", func(c echo.Context) error { ctx := c.Request().Context() memoID, err := strconv.Atoi(c.Param("memoId")) diff --git a/web/src/components/MemoCardDialog.tsx b/web/src/components/MemoCardDialog.tsx index 2dde33c7..36c97bf7 100644 --- a/web/src/components/MemoCardDialog.tsx +++ b/web/src/components/MemoCardDialog.tsx @@ -115,11 +115,6 @@ const MemoCardDialog: React.FC = (props: Props) => { }, []); const handleGotoMemoLinkBtnClick = () => { - if (memo.visibility === "PRIVATE") { - toastHelper.error(t("message.private-only")); - return; - } - window.open(`/m/${memo.id}`); }; diff --git a/web/src/components/Toast.tsx b/web/src/components/Toast.tsx index e440d568..622b5807 100644 --- a/web/src/components/Toast.tsx +++ b/web/src/components/Toast.tsx @@ -94,7 +94,7 @@ const initialToastHelper = () => { return showToast({ type: "success", content, duration }); }; - const error = (content: string, duration = 3000) => { + const error = (content: string, duration = -1) => { return showToast({ type: "error", content, duration }); }; diff --git a/web/src/pages/MemoDetail.tsx b/web/src/pages/MemoDetail.tsx index 115172b5..bea9bae1 100644 --- a/web/src/pages/MemoDetail.tsx +++ b/web/src/pages/MemoDetail.tsx @@ -7,6 +7,7 @@ import { UNKNOWN_ID } from "../helpers/consts"; import { isNullorUndefined } from "../helpers/utils"; import { useAppSelector } from "../store"; import useLoading from "../hooks/useLoading"; +import toastHelper from "../components/Toast"; import MemoContent from "../components/MemoContent"; import MemoResources from "../components/MemoResources"; import "../less/explore.less"; @@ -37,12 +38,18 @@ const MemoDetail = () => { const memoId = Number(params.memoId); if (memoId && !isNaN(memoId)) { - memoService.fetchMemoById(memoId).then((memo) => { - setState({ - memo, + memoService + .fetchMemoById(memoId) + .then((memo) => { + setState({ + memo, + }); + loadingState.setFinish(); + }) + .catch((error) => { + console.error(error); + toastHelper.error(error.response.data.message); }); - loadingState.setFinish(); - }); } }, [location]);