mirror of
https://github.com/usememos/memos.git
synced 2024-12-11 12:25:12 +03:00
chore: update get memo api (#2079)
This commit is contained in:
parent
57dd1fc49f
commit
3df9da91b4
@ -49,17 +49,43 @@ func (s *MemoService) ListMemos(ctx context.Context, request *apiv2pb.ListMemosR
|
||||
memoMessages[i] = convertMemoFromStore(memo)
|
||||
}
|
||||
|
||||
// TODO(steven): Add privalige checks.
|
||||
response := &apiv2pb.ListMemosResponse{
|
||||
Memos: memoMessages,
|
||||
Memos: 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" .
|
||||
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))
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -30,6 +30,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
|
||||
),
|
||||
)
|
||||
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(store))
|
||||
apiv2pb.RegisterMemoServiceServer(grpcServer, NewMemoService(store))
|
||||
apiv2pb.RegisterTagServiceServer(grpcServer, NewTagService(store))
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
if err := apiv2pb.RegisterMemoServiceHandler(context.Background(), gwMux, conn); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := apiv2pb.RegisterTagServiceHandler(context.Background(), gwMux, conn); err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user