mirror of
https://github.com/usememos/memos.git
synced 2024-12-20 09:41:58 +03:00
chore: update memo detail access handler
This commit is contained in:
parent
2acd5d4af2
commit
63468dbaf3
@ -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)
|
return next(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,6 +203,41 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|||||||
return nil
|
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 {
|
g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
|
||||||
ctx := c.Request().Context()
|
ctx := c.Request().Context()
|
||||||
memoID, err := strconv.Atoi(c.Param("memoId"))
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
||||||
@ -245,32 +280,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|||||||
return nil
|
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 {
|
g.DELETE("/memo/:memoId", func(c echo.Context) error {
|
||||||
ctx := c.Request().Context()
|
ctx := c.Request().Context()
|
||||||
memoID, err := strconv.Atoi(c.Param("memoId"))
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
||||||
|
@ -115,11 +115,6 @@ const MemoCardDialog: React.FC<Props> = (props: Props) => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleGotoMemoLinkBtnClick = () => {
|
const handleGotoMemoLinkBtnClick = () => {
|
||||||
if (memo.visibility === "PRIVATE") {
|
|
||||||
toastHelper.error(t("message.private-only"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window.open(`/m/${memo.id}`);
|
window.open(`/m/${memo.id}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ const initialToastHelper = () => {
|
|||||||
return showToast({ type: "success", content, duration });
|
return showToast({ type: "success", content, duration });
|
||||||
};
|
};
|
||||||
|
|
||||||
const error = (content: string, duration = 3000) => {
|
const error = (content: string, duration = -1) => {
|
||||||
return showToast({ type: "error", content, duration });
|
return showToast({ type: "error", content, duration });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import { UNKNOWN_ID } from "../helpers/consts";
|
|||||||
import { isNullorUndefined } from "../helpers/utils";
|
import { isNullorUndefined } from "../helpers/utils";
|
||||||
import { useAppSelector } from "../store";
|
import { useAppSelector } from "../store";
|
||||||
import useLoading from "../hooks/useLoading";
|
import useLoading from "../hooks/useLoading";
|
||||||
|
import toastHelper from "../components/Toast";
|
||||||
import MemoContent from "../components/MemoContent";
|
import MemoContent from "../components/MemoContent";
|
||||||
import MemoResources from "../components/MemoResources";
|
import MemoResources from "../components/MemoResources";
|
||||||
import "../less/explore.less";
|
import "../less/explore.less";
|
||||||
@ -37,12 +38,18 @@ const MemoDetail = () => {
|
|||||||
|
|
||||||
const memoId = Number(params.memoId);
|
const memoId = Number(params.memoId);
|
||||||
if (memoId && !isNaN(memoId)) {
|
if (memoId && !isNaN(memoId)) {
|
||||||
memoService.fetchMemoById(memoId).then((memo) => {
|
memoService
|
||||||
setState({
|
.fetchMemoById(memoId)
|
||||||
memo,
|
.then((memo) => {
|
||||||
|
setState({
|
||||||
|
memo,
|
||||||
|
});
|
||||||
|
loadingState.setFinish();
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
toastHelper.error(error.response.data.message);
|
||||||
});
|
});
|
||||||
loadingState.setFinish();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}, [location]);
|
}, [location]);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user