memo_relation_service.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package v2
  2. import (
  3. "context"
  4. "google.golang.org/grpc/codes"
  5. "google.golang.org/grpc/status"
  6. apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
  7. "github.com/usememos/memos/store"
  8. )
  9. func (s *APIV2Service) SetMemoRelations(ctx context.Context, request *apiv2pb.SetMemoRelationsRequest) (*apiv2pb.SetMemoRelationsResponse, error) {
  10. referenceType := store.MemoRelationReference
  11. // Delete all reference relations first.
  12. if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelation{
  13. MemoID: &request.Id,
  14. Type: &referenceType,
  15. }); err != nil {
  16. return nil, status.Errorf(codes.Internal, "failed to delete memo relation")
  17. }
  18. for _, relation := range request.Relations {
  19. // Ignore reflexive relations.
  20. if request.Id == relation.RelatedMemoId {
  21. continue
  22. }
  23. // Ignore comment relations as there's no need to update a comment's relation.
  24. // Inserting/Deleting a comment is handled elsewhere.
  25. if relation.Type == apiv2pb.MemoRelation_COMMENT {
  26. continue
  27. }
  28. if _, err := s.Store.UpsertMemoRelation(ctx, &store.MemoRelation{
  29. MemoID: request.Id,
  30. RelatedMemoID: relation.RelatedMemoId,
  31. Type: convertMemoRelationTypeToStore(relation.Type),
  32. }); err != nil {
  33. return nil, status.Errorf(codes.Internal, "failed to upsert memo relation")
  34. }
  35. }
  36. return &apiv2pb.SetMemoRelationsResponse{}, nil
  37. }
  38. func (s *APIV2Service) ListMemoRelations(ctx context.Context, request *apiv2pb.ListMemoRelationsRequest) (*apiv2pb.ListMemoRelationsResponse, error) {
  39. relationList := []*apiv2pb.MemoRelation{}
  40. tempList, err := s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
  41. MemoID: &request.Id,
  42. })
  43. if err != nil {
  44. return nil, err
  45. }
  46. for _, relation := range tempList {
  47. relationList = append(relationList, convertMemoRelationFromStore(relation))
  48. }
  49. tempList, err = s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
  50. RelatedMemoID: &request.Id,
  51. })
  52. if err != nil {
  53. return nil, err
  54. }
  55. for _, relation := range tempList {
  56. relationList = append(relationList, convertMemoRelationFromStore(relation))
  57. }
  58. response := &apiv2pb.ListMemoRelationsResponse{
  59. Relations: relationList,
  60. }
  61. return response, nil
  62. }
  63. func convertMemoRelationFromStore(memoRelation *store.MemoRelation) *apiv2pb.MemoRelation {
  64. return &apiv2pb.MemoRelation{
  65. MemoId: memoRelation.MemoID,
  66. RelatedMemoId: memoRelation.RelatedMemoID,
  67. Type: convertMemoRelationTypeFromStore(memoRelation.Type),
  68. }
  69. }
  70. func convertMemoRelationTypeFromStore(relationType store.MemoRelationType) apiv2pb.MemoRelation_Type {
  71. switch relationType {
  72. case store.MemoRelationReference:
  73. return apiv2pb.MemoRelation_REFERENCE
  74. case store.MemoRelationComment:
  75. return apiv2pb.MemoRelation_COMMENT
  76. default:
  77. return apiv2pb.MemoRelation_TYPE_UNSPECIFIED
  78. }
  79. }
  80. func convertMemoRelationTypeToStore(relationType apiv2pb.MemoRelation_Type) store.MemoRelationType {
  81. switch relationType {
  82. case apiv2pb.MemoRelation_REFERENCE:
  83. return store.MemoRelationReference
  84. case apiv2pb.MemoRelation_COMMENT:
  85. return store.MemoRelationComment
  86. default:
  87. return store.MemoRelationReference
  88. }
  89. }