mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update get memo api (#2079)
This commit is contained in:
@ -49,17 +49,43 @@ func (s *MemoService) ListMemos(ctx context.Context, request *apiv2pb.ListMemosR
|
|||||||
memoMessages[i] = convertMemoFromStore(memo)
|
memoMessages[i] = convertMemoFromStore(memo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(steven): Add privalige checks.
|
||||||
response := &apiv2pb.ListMemosResponse{
|
response := &apiv2pb.ListMemosResponse{
|
||||||
Memos: memoMessages,
|
Memos: nil,
|
||||||
}
|
}
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const visibilityFilterExample = `visibility == "PRIVATE"`
|
func (s *MemoService) GetMemo(ctx context.Context, request *apiv2pb.GetMemoRequest) (*apiv2pb.GetMemoResponse, error) {
|
||||||
|
memo, err := s.Store.GetMemo(ctx, &store.FindMemo{
|
||||||
|
ID: &request.Id,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if memo == nil {
|
||||||
|
return nil, status.Errorf(codes.NotFound, "memo not found")
|
||||||
|
}
|
||||||
|
if memo.Visibility != store.Public {
|
||||||
|
userIDPtr := ctx.Value(UserIDContextKey)
|
||||||
|
if userIDPtr == nil {
|
||||||
|
return nil, status.Errorf(codes.Unauthenticated, "unauthenticated")
|
||||||
|
}
|
||||||
|
userID := userIDPtr.(int32)
|
||||||
|
if memo.Visibility == store.Private && memo.CreatorID != userID {
|
||||||
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &apiv2pb.GetMemoResponse{
|
||||||
|
Memo: convertMemoFromStore(memo),
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
// getVisibilityFilter will parse the simple filter such as `visibility = "PRIVATE"` to "PRIVATE" .
|
// getVisibilityFilter will parse the simple filter such as `visibility = "PRIVATE"` to "PRIVATE" .
|
||||||
func getVisibilityFilter(filter string) (string, error) {
|
func getVisibilityFilter(filter string) (string, error) {
|
||||||
formatInvalidErr := errors.Errorf("invalid filter %q, example %q", filter, visibilityFilterExample)
|
formatInvalidErr := errors.Errorf("invalid filter %q", filter)
|
||||||
e, err := cel.NewEnv(cel.Variable("visibility", cel.StringType))
|
e, err := cel.NewEnv(cel.Variable("visibility", cel.StringType))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -30,6 +30,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(store))
|
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(store))
|
||||||
|
apiv2pb.RegisterMemoServiceServer(grpcServer, NewMemoService(store))
|
||||||
apiv2pb.RegisterTagServiceServer(grpcServer, NewTagService(store))
|
apiv2pb.RegisterTagServiceServer(grpcServer, NewTagService(store))
|
||||||
|
|
||||||
return &APIV2Service{
|
return &APIV2Service{
|
||||||
@ -62,6 +63,9 @@ func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error
|
|||||||
if err := apiv2pb.RegisterUserServiceHandler(context.Background(), gwMux, conn); err != nil {
|
if err := apiv2pb.RegisterUserServiceHandler(context.Background(), gwMux, conn); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := apiv2pb.RegisterMemoServiceHandler(context.Background(), gwMux, conn); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := apiv2pb.RegisterTagServiceHandler(context.Background(), gwMux, conn); err != nil {
|
if err := apiv2pb.RegisterTagServiceHandler(context.Background(), gwMux, conn); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user