diff --git a/api/v2/memo_service.go b/api/v2/memo_service.go index c54fb6bb..2b2bac5a 100644 --- a/api/v2/memo_service.go +++ b/api/v2/memo_service.go @@ -13,20 +13,7 @@ import ( "github.com/usememos/memos/store" ) -type MemoService struct { - apiv2pb.UnimplementedMemoServiceServer - - Store *store.Store -} - -// NewMemoService creates a new MemoService. -func NewMemoService(store *store.Store) *MemoService { - return &MemoService{ - Store: store, - } -} - -func (s *MemoService) CreateMemo(ctx context.Context, request *apiv2pb.CreateMemoRequest) (*apiv2pb.CreateMemoResponse, error) { +func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMemoRequest) (*apiv2pb.CreateMemoResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get user") @@ -51,7 +38,7 @@ func (s *MemoService) CreateMemo(ctx context.Context, request *apiv2pb.CreateMem return response, nil } -func (s *MemoService) ListMemos(ctx context.Context, request *apiv2pb.ListMemosRequest) (*apiv2pb.ListMemosResponse, error) { +func (s *APIV2Service) ListMemos(ctx context.Context, request *apiv2pb.ListMemosRequest) (*apiv2pb.ListMemosResponse, error) { memoFind := &store.FindMemo{} if request.Filter != "" { filter, err := parseListMemosFilter(request.Filter) @@ -111,7 +98,7 @@ func (s *MemoService) ListMemos(ctx context.Context, request *apiv2pb.ListMemosR return response, nil } -func (s *MemoService) GetMemo(ctx context.Context, request *apiv2pb.GetMemoRequest) (*apiv2pb.GetMemoResponse, error) { +func (s *APIV2Service) GetMemo(ctx context.Context, request *apiv2pb.GetMemoRequest) (*apiv2pb.GetMemoResponse, error) { memo, err := s.Store.GetMemo(ctx, &store.FindMemo{ ID: &request.Id, }) @@ -140,7 +127,7 @@ func (s *MemoService) GetMemo(ctx context.Context, request *apiv2pb.GetMemoReque return response, nil } -func (s *MemoService) CreateMemoComment(ctx context.Context, request *apiv2pb.CreateMemoCommentRequest) (*apiv2pb.CreateMemoCommentResponse, error) { +func (s *APIV2Service) CreateMemoComment(ctx context.Context, request *apiv2pb.CreateMemoCommentRequest) (*apiv2pb.CreateMemoCommentResponse, error) { // Create the comment memo first. createMemoResponse, err := s.CreateMemo(ctx, request.Create) if err != nil { @@ -164,7 +151,7 @@ func (s *MemoService) CreateMemoComment(ctx context.Context, request *apiv2pb.Cr return response, nil } -func (s *MemoService) ListMemoComments(ctx context.Context, request *apiv2pb.ListMemoCommentsRequest) (*apiv2pb.ListMemoCommentsResponse, error) { +func (s *APIV2Service) ListMemoComments(ctx context.Context, request *apiv2pb.ListMemoCommentsRequest) (*apiv2pb.ListMemoCommentsResponse, error) { memoRelationComment := store.MemoRelationComment memoRelations, err := s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{ RelatedMemoID: &request.Id, diff --git a/api/v2/resource_service.go b/api/v2/resource_service.go index 121832a4..bc490fdc 100644 --- a/api/v2/resource_service.go +++ b/api/v2/resource_service.go @@ -14,7 +14,6 @@ import ( "github.com/usememos/memos/internal/log" apiv2pb "github.com/usememos/memos/proto/gen/api/v2" - "github.com/usememos/memos/server/profile" "github.com/usememos/memos/store" ) @@ -23,22 +22,7 @@ const ( thumbnailImagePath = ".thumbnail_cache" ) -type ResourceService struct { - apiv2pb.UnimplementedResourceServiceServer - - Profile *profile.Profile - Store *store.Store -} - -// NewResourceService creates a new ResourceService. -func NewResourceService(profile *profile.Profile, store *store.Store) *ResourceService { - return &ResourceService{ - Profile: profile, - Store: store, - } -} - -func (s *ResourceService) ListResources(ctx context.Context, _ *apiv2pb.ListResourcesRequest) (*apiv2pb.ListResourcesResponse, error) { +func (s *APIV2Service) ListResources(ctx context.Context, _ *apiv2pb.ListResourcesRequest) (*apiv2pb.ListResourcesResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) @@ -57,7 +41,7 @@ func (s *ResourceService) ListResources(ctx context.Context, _ *apiv2pb.ListReso return response, nil } -func (s *ResourceService) UpdateResource(ctx context.Context, request *apiv2pb.UpdateResourceRequest) (*apiv2pb.UpdateResourceResponse, error) { +func (s *APIV2Service) UpdateResource(ctx context.Context, request *apiv2pb.UpdateResourceRequest) (*apiv2pb.UpdateResourceResponse, error) { if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 { return nil, status.Errorf(codes.InvalidArgument, "update mask is required") } @@ -84,7 +68,7 @@ func (s *ResourceService) UpdateResource(ctx context.Context, request *apiv2pb.U }, nil } -func (s *ResourceService) DeleteResource(ctx context.Context, request *apiv2pb.DeleteResourceRequest) (*apiv2pb.DeleteResourceResponse, error) { +func (s *APIV2Service) DeleteResource(ctx context.Context, request *apiv2pb.DeleteResourceRequest) (*apiv2pb.DeleteResourceResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) @@ -119,7 +103,7 @@ func (s *ResourceService) DeleteResource(ctx context.Context, request *apiv2pb.D return &apiv2pb.DeleteResourceResponse{}, nil } -func (s *ResourceService) convertResourceFromStore(ctx context.Context, resource *store.Resource) *apiv2pb.Resource { +func (s *APIV2Service) convertResourceFromStore(ctx context.Context, resource *store.Resource) *apiv2pb.Resource { var memoID *int32 if resource.MemoID != nil { memo, _ := s.Store.GetMemo(ctx, &store.FindMemo{ diff --git a/api/v2/system_service.go b/api/v2/system_service.go index 06aad488..af37a22c 100644 --- a/api/v2/system_service.go +++ b/api/v2/system_service.go @@ -8,26 +8,10 @@ import ( "google.golang.org/grpc/status" apiv2pb "github.com/usememos/memos/proto/gen/api/v2" - "github.com/usememos/memos/server/profile" "github.com/usememos/memos/store" ) -type SystemService struct { - apiv2pb.UnimplementedSystemServiceServer - - Profile *profile.Profile - Store *store.Store -} - -// NewSystemService creates a new SystemService. -func NewSystemService(profile *profile.Profile, store *store.Store) *SystemService { - return &SystemService{ - Profile: profile, - Store: store, - } -} - -func (s *SystemService) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemInfoRequest) (*apiv2pb.GetSystemInfoResponse, error) { +func (s *APIV2Service) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemInfoRequest) (*apiv2pb.GetSystemInfoResponse, error) { defaultSystemInfo := &apiv2pb.SystemInfo{} // Get the database size if the user is a host. @@ -49,7 +33,7 @@ func (s *SystemService) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemI return response, nil } -func (s *SystemService) UpdateSystemInfo(ctx context.Context, request *apiv2pb.UpdateSystemInfoRequest) (*apiv2pb.UpdateSystemInfoResponse, error) { +func (s *APIV2Service) UpdateSystemInfo(ctx context.Context, request *apiv2pb.UpdateSystemInfoRequest) (*apiv2pb.UpdateSystemInfoResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) diff --git a/api/v2/tag_service.go b/api/v2/tag_service.go index 44700be9..f1cd8561 100644 --- a/api/v2/tag_service.go +++ b/api/v2/tag_service.go @@ -10,20 +10,7 @@ import ( "github.com/usememos/memos/store" ) -type TagService struct { - apiv2pb.UnimplementedTagServiceServer - - Store *store.Store -} - -// NewTagService creates a new TagService. -func NewTagService(store *store.Store) *TagService { - return &TagService{ - Store: store, - } -} - -func (s *TagService) UpsertTag(ctx context.Context, request *apiv2pb.UpsertTagRequest) (*apiv2pb.UpsertTagResponse, error) { +func (s *APIV2Service) UpsertTag(ctx context.Context, request *apiv2pb.UpsertTagRequest) (*apiv2pb.UpsertTagResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get user") @@ -42,7 +29,7 @@ func (s *TagService) UpsertTag(ctx context.Context, request *apiv2pb.UpsertTagRe }, nil } -func (s *TagService) ListTags(ctx context.Context, request *apiv2pb.ListTagsRequest) (*apiv2pb.ListTagsResponse, error) { +func (s *APIV2Service) ListTags(ctx context.Context, request *apiv2pb.ListTagsRequest) (*apiv2pb.ListTagsResponse, error) { tags, err := s.Store.ListTags(ctx, &store.FindTag{ CreatorID: request.CreatorId, }) @@ -57,7 +44,7 @@ func (s *TagService) ListTags(ctx context.Context, request *apiv2pb.ListTagsRequ return response, nil } -func (s *TagService) DeleteTag(ctx context.Context, request *apiv2pb.DeleteTagRequest) (*apiv2pb.DeleteTagResponse, error) { +func (s *APIV2Service) DeleteTag(ctx context.Context, request *apiv2pb.DeleteTagRequest) (*apiv2pb.DeleteTagResponse, error) { err := s.Store.DeleteTag(ctx, &store.DeleteTag{ Name: request.Tag.Name, CreatorID: request.Tag.CreatorId, diff --git a/api/v2/user_service.go b/api/v2/user_service.go index a3b8ca8f..7ec9ff26 100644 --- a/api/v2/user_service.go +++ b/api/v2/user_service.go @@ -26,22 +26,7 @@ var ( usernameMatcher = regexp.MustCompile("^[a-z0-9]([a-z0-9-]{1,30}[a-z0-9])$") ) -type UserService struct { - apiv2pb.UnimplementedUserServiceServer - - Store *store.Store - Secret string -} - -// NewUserService creates a new UserService. -func NewUserService(store *store.Store, secret string) *UserService { - return &UserService{ - Store: store, - Secret: secret, - } -} - -func (s *UserService) GetUser(ctx context.Context, request *apiv2pb.GetUserRequest) (*apiv2pb.GetUserResponse, error) { +func (s *APIV2Service) GetUser(ctx context.Context, request *apiv2pb.GetUserRequest) (*apiv2pb.GetUserResponse, error) { user, err := s.Store.GetUser(ctx, &store.FindUser{ Username: &request.Username, }) @@ -59,7 +44,7 @@ func (s *UserService) GetUser(ctx context.Context, request *apiv2pb.GetUserReque return response, nil } -func (s *UserService) CreateUser(ctx context.Context, request *apiv2pb.CreateUserRequest) (*apiv2pb.CreateUserResponse, error) { +func (s *APIV2Service) CreateUser(ctx context.Context, request *apiv2pb.CreateUserRequest) (*apiv2pb.CreateUserResponse, error) { currentUser, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get user: %v", err) @@ -93,7 +78,7 @@ func (s *UserService) CreateUser(ctx context.Context, request *apiv2pb.CreateUse return response, nil } -func (s *UserService) UpdateUser(ctx context.Context, request *apiv2pb.UpdateUserRequest) (*apiv2pb.UpdateUserResponse, error) { +func (s *APIV2Service) UpdateUser(ctx context.Context, request *apiv2pb.UpdateUserRequest) (*apiv2pb.UpdateUserResponse, error) { currentUser, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get user: %v", err) @@ -151,7 +136,7 @@ func (s *UserService) UpdateUser(ctx context.Context, request *apiv2pb.UpdateUse return response, nil } -func (s *UserService) ListUserAccessTokens(ctx context.Context, request *apiv2pb.ListUserAccessTokensRequest) (*apiv2pb.ListUserAccessTokensResponse, error) { +func (s *APIV2Service) ListUserAccessTokens(ctx context.Context, request *apiv2pb.ListUserAccessTokensRequest) (*apiv2pb.ListUserAccessTokensResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) @@ -221,7 +206,7 @@ func (s *UserService) ListUserAccessTokens(ctx context.Context, request *apiv2pb return response, nil } -func (s *UserService) CreateUserAccessToken(ctx context.Context, request *apiv2pb.CreateUserAccessTokenRequest) (*apiv2pb.CreateUserAccessTokenResponse, error) { +func (s *APIV2Service) CreateUserAccessToken(ctx context.Context, request *apiv2pb.CreateUserAccessTokenRequest) (*apiv2pb.CreateUserAccessTokenResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) @@ -286,7 +271,7 @@ func (s *UserService) CreateUserAccessToken(ctx context.Context, request *apiv2p return response, nil } -func (s *UserService) DeleteUserAccessToken(ctx context.Context, request *apiv2pb.DeleteUserAccessTokenRequest) (*apiv2pb.DeleteUserAccessTokenResponse, error) { +func (s *APIV2Service) DeleteUserAccessToken(ctx context.Context, request *apiv2pb.DeleteUserAccessTokenRequest) (*apiv2pb.DeleteUserAccessTokenResponse, error) { user, err := getCurrentUser(ctx, s.Store) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) @@ -318,7 +303,7 @@ func (s *UserService) DeleteUserAccessToken(ctx context.Context, request *apiv2p return &apiv2pb.DeleteUserAccessTokenResponse{}, nil } -func (s *UserService) UpsertAccessTokenToStore(ctx context.Context, user *store.User, accessToken, description string) error { +func (s *APIV2Service) UpsertAccessTokenToStore(ctx context.Context, user *store.User, accessToken, description string) error { userAccessTokens, err := s.Store.GetUserAccessTokens(ctx, user.ID) if err != nil { return errors.Wrap(err, "failed to get user access tokens") diff --git a/api/v2/v2.go b/api/v2/v2.go index 109eb3f1..d5040bbf 100644 --- a/api/v2/v2.go +++ b/api/v2/v2.go @@ -17,6 +17,11 @@ import ( ) type APIV2Service struct { + apiv2pb.UnimplementedSystemServiceServer + apiv2pb.UnimplementedUserServiceServer + apiv2pb.UnimplementedMemoServiceServer + apiv2pb.UnimplementedResourceServiceServer + apiv2pb.UnimplementedTagServiceServer apiv2pb.UnimplementedInboxServiceServer Secret string @@ -43,11 +48,11 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store grpcServerPort: grpcServerPort, } - apiv2pb.RegisterSystemServiceServer(grpcServer, NewSystemService(profile, store)) - apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(store, secret)) - apiv2pb.RegisterMemoServiceServer(grpcServer, NewMemoService(store)) - apiv2pb.RegisterTagServiceServer(grpcServer, NewTagService(store)) - apiv2pb.RegisterResourceServiceServer(grpcServer, NewResourceService(profile, store)) + apiv2pb.RegisterSystemServiceServer(grpcServer, apiv2Service) + apiv2pb.RegisterUserServiceServer(grpcServer, apiv2Service) + apiv2pb.RegisterMemoServiceServer(grpcServer, apiv2Service) + apiv2pb.RegisterTagServiceServer(grpcServer, apiv2Service) + apiv2pb.RegisterResourceServiceServer(grpcServer, apiv2Service) apiv2pb.RegisterInboxServiceServer(grpcServer, apiv2Service) reflection.Register(grpcServer)