chore: update get memo api (#2079)

This commit is contained in:
boojack 2023-08-05 19:51:32 +08:00 committed by GitHub
parent 57dd1fc49f
commit 3df9da91b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 3 deletions

View File

@ -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

View File

@ -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
}