2024-04-28 00:44:29 +08:00
|
|
|
package v1
|
2023-12-23 11:17:35 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-06 09:23:20 +08:00
|
|
|
"slices"
|
|
|
|
"time"
|
2023-12-23 11:17:35 +08:00
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2024-04-27 22:02:15 +08:00
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2023-12-23 11:17:35 +08:00
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
v1pb "github.com/usememos/memos/proto/gen/api/v1"
|
2023-12-23 11:17:35 +08:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) SetMemoResources(ctx context.Context, request *v1pb.SetMemoResourcesRequest) (*emptypb.Empty, error) {
|
2024-03-20 21:17:04 +08:00
|
|
|
memoID, err := ExtractMemoIDFromName(request.Name)
|
2024-03-18 23:23:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid memo name: %v", err)
|
|
|
|
}
|
2024-01-06 09:23:20 +08:00
|
|
|
resources, err := s.Store.ListResources(ctx, &store.FindResource{
|
2024-03-20 21:17:04 +08:00
|
|
|
MemoID: &memoID,
|
2024-01-06 09:23:20 +08:00
|
|
|
})
|
2023-12-23 11:17:35 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to list resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete resources that are not in the request.
|
|
|
|
for _, resource := range resources {
|
|
|
|
found := false
|
|
|
|
for _, requestResource := range request.Resources {
|
2024-03-20 21:17:04 +08:00
|
|
|
if resource.UID == requestResource.Uid {
|
2023-12-23 11:17:35 +08:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
if err = s.Store.DeleteResource(ctx, &store.DeleteResource{
|
|
|
|
ID: int32(resource.ID),
|
2024-03-20 21:17:04 +08:00
|
|
|
MemoID: &memoID,
|
2023-12-23 11:17:35 +08:00
|
|
|
}); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to delete resource")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 09:23:20 +08:00
|
|
|
slices.Reverse(request.Resources)
|
2023-12-23 11:17:35 +08:00
|
|
|
// Update resources' memo_id in the request.
|
2024-01-06 09:23:20 +08:00
|
|
|
for index, resource := range request.Resources {
|
2024-03-24 22:35:10 +08:00
|
|
|
id, err := ExtractResourceIDFromName(resource.Name)
|
2024-03-20 21:17:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid resource name: %v", err)
|
|
|
|
}
|
2024-01-06 09:23:20 +08:00
|
|
|
updatedTs := time.Now().Unix() + int64(index)
|
2024-05-02 21:28:06 +08:00
|
|
|
if err := s.Store.UpdateResource(ctx, &store.UpdateResource{
|
2024-03-20 21:17:04 +08:00
|
|
|
ID: id,
|
|
|
|
MemoID: &memoID,
|
2024-01-06 09:23:20 +08:00
|
|
|
UpdatedTs: &updatedTs,
|
2023-12-23 11:17:35 +08:00
|
|
|
}); err != nil {
|
2024-01-15 08:13:06 +08:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to update resource: %v", err)
|
2023-12-23 11:17:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:02:15 +08:00
|
|
|
return &emptypb.Empty{}, nil
|
2023-12-23 11:17:35 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) ListMemoResources(ctx context.Context, request *v1pb.ListMemoResourcesRequest) (*v1pb.ListMemoResourcesResponse, error) {
|
2024-03-18 23:23:53 +08:00
|
|
|
id, err := ExtractMemoIDFromName(request.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid memo name: %v", err)
|
|
|
|
}
|
2023-12-23 11:17:35 +08:00
|
|
|
resources, err := s.Store.ListResources(ctx, &store.FindResource{
|
2024-03-18 23:23:53 +08:00
|
|
|
MemoID: &id,
|
2023-12-23 11:17:35 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-05-07 22:07:44 +08:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to list resources: %v", err)
|
2023-12-23 11:17:35 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
response := &v1pb.ListMemoResourcesResponse{
|
|
|
|
Resources: []*v1pb.Resource{},
|
2023-12-23 11:17:35 +08:00
|
|
|
}
|
|
|
|
for _, resource := range resources {
|
|
|
|
response.Resources = append(response.Resources, s.convertResourceFromStore(ctx, resource))
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|