2024-04-28 00:44:29 +08:00
|
|
|
package v1
|
2024-02-08 11:54:59 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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"
|
2024-02-08 11:54:59 +08:00
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
v1pb "github.com/usememos/memos/proto/gen/api/v1"
|
2024-02-08 11:54:59 +08:00
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) ListMemoReactions(ctx context.Context, request *v1pb.ListMemoReactionsRequest) (*v1pb.ListMemoReactionsResponse, error) {
|
2024-02-08 11:54:59 +08:00
|
|
|
reactions, err := s.Store.ListReactions(ctx, &store.FindReaction{
|
2024-03-18 23:23:53 +08:00
|
|
|
ContentID: &request.Name,
|
2024-02-08 11:54:59 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to list reactions")
|
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
response := &v1pb.ListMemoReactionsResponse{
|
|
|
|
Reactions: []*v1pb.Reaction{},
|
2024-02-08 11:54:59 +08:00
|
|
|
}
|
|
|
|
for _, reaction := range reactions {
|
|
|
|
reactionMessage, err := s.convertReactionFromStore(ctx, reaction)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to convert reaction")
|
|
|
|
}
|
|
|
|
response.Reactions = append(response.Reactions, reactionMessage)
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) UpsertMemoReaction(ctx context.Context, request *v1pb.UpsertMemoReactionRequest) (*v1pb.Reaction, error) {
|
2024-05-26 11:02:23 +08:00
|
|
|
user, err := s.GetCurrentUser(ctx)
|
2024-02-08 11:54:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get current user")
|
|
|
|
}
|
2024-04-13 12:07:53 +08:00
|
|
|
reaction, err := s.Store.UpsertReaction(ctx, &store.Reaction{
|
|
|
|
CreatorID: user.ID,
|
|
|
|
ContentID: request.Reaction.ContentId,
|
|
|
|
ReactionType: storepb.ReactionType(request.Reaction.ReactionType),
|
2024-02-08 11:54:59 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to upsert reaction")
|
|
|
|
}
|
|
|
|
|
|
|
|
reactionMessage, err := s.convertReactionFromStore(ctx, reaction)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to convert reaction")
|
|
|
|
}
|
2024-04-27 22:02:15 +08:00
|
|
|
return reactionMessage, nil
|
2024-02-08 11:54:59 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) DeleteMemoReaction(ctx context.Context, request *v1pb.DeleteMemoReactionRequest) (*emptypb.Empty, error) {
|
2024-02-08 11:54:59 +08:00
|
|
|
if err := s.Store.DeleteReaction(ctx, &store.DeleteReaction{
|
2024-04-08 20:52:46 +08:00
|
|
|
ID: request.ReactionId,
|
2024-02-08 11:54:59 +08:00
|
|
|
}); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to delete reaction")
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:02:15 +08:00
|
|
|
return &emptypb.Empty{}, nil
|
2024-02-08 11:54:59 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) convertReactionFromStore(ctx context.Context, reaction *store.Reaction) (*v1pb.Reaction, error) {
|
2024-02-08 11:54:59 +08:00
|
|
|
creator, err := s.Store.GetUser(ctx, &store.FindUser{
|
2024-04-13 12:07:53 +08:00
|
|
|
ID: &reaction.CreatorID,
|
2024-02-08 11:54:59 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-28 00:44:29 +08:00
|
|
|
return &v1pb.Reaction{
|
2024-04-13 12:07:53 +08:00
|
|
|
Id: reaction.ID,
|
2024-03-18 23:23:53 +08:00
|
|
|
Creator: fmt.Sprintf("%s%d", UserNamePrefix, creator.ID),
|
2024-04-13 12:07:53 +08:00
|
|
|
ContentId: reaction.ContentID,
|
2024-04-28 00:44:29 +08:00
|
|
|
ReactionType: v1pb.Reaction_Type(reaction.ReactionType),
|
2024-02-08 11:54:59 +08:00
|
|
|
}, nil
|
|
|
|
}
|