inbox_service.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package v2
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/pkg/errors"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/status"
  9. "google.golang.org/protobuf/types/known/timestamppb"
  10. apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
  11. "github.com/usememos/memos/store"
  12. )
  13. func (s *APIV2Service) ListInboxes(ctx context.Context, _ *apiv2pb.ListInboxesRequest) (*apiv2pb.ListInboxesResponse, error) {
  14. user, err := getCurrentUser(ctx, s.Store)
  15. if err != nil {
  16. return nil, status.Errorf(codes.Internal, "failed to get user")
  17. }
  18. inboxes, err := s.Store.ListInboxes(ctx, &store.FindInbox{
  19. ReceiverID: &user.ID,
  20. })
  21. if err != nil {
  22. return nil, status.Errorf(codes.Internal, "failed to list inbox: %v", err)
  23. }
  24. response := &apiv2pb.ListInboxesResponse{
  25. Inboxes: []*apiv2pb.Inbox{},
  26. }
  27. for _, inbox := range inboxes {
  28. inboxMessage, err := s.convertInboxFromStore(ctx, inbox)
  29. if err != nil {
  30. return nil, status.Errorf(codes.Internal, "failed to convert inbox from store: %v", err)
  31. }
  32. response.Inboxes = append(response.Inboxes, inboxMessage)
  33. }
  34. return response, nil
  35. }
  36. func (s *APIV2Service) UpdateInbox(ctx context.Context, request *apiv2pb.UpdateInboxRequest) (*apiv2pb.UpdateInboxResponse, error) {
  37. if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
  38. return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
  39. }
  40. inboxID, err := ExtractInboxIDFromName(request.Inbox.Name)
  41. if err != nil {
  42. return nil, status.Errorf(codes.InvalidArgument, "invalid inbox name: %v", err)
  43. }
  44. update := &store.UpdateInbox{
  45. ID: inboxID,
  46. }
  47. for _, field := range request.UpdateMask.Paths {
  48. if field == "status" {
  49. if request.Inbox.Status == apiv2pb.Inbox_STATUS_UNSPECIFIED {
  50. return nil, status.Errorf(codes.InvalidArgument, "status is required")
  51. }
  52. update.Status = convertInboxStatusToStore(request.Inbox.Status)
  53. }
  54. }
  55. inbox, err := s.Store.UpdateInbox(ctx, update)
  56. if err != nil {
  57. return nil, status.Errorf(codes.Internal, "failed to update inbox: %v", err)
  58. }
  59. inboxMessage, err := s.convertInboxFromStore(ctx, inbox)
  60. if err != nil {
  61. return nil, status.Errorf(codes.Internal, "failed to convert inbox from store: %v", err)
  62. }
  63. return &apiv2pb.UpdateInboxResponse{
  64. Inbox: inboxMessage,
  65. }, nil
  66. }
  67. func (s *APIV2Service) DeleteInbox(ctx context.Context, request *apiv2pb.DeleteInboxRequest) (*apiv2pb.DeleteInboxResponse, error) {
  68. inboxID, err := ExtractInboxIDFromName(request.Name)
  69. if err != nil {
  70. return nil, status.Errorf(codes.InvalidArgument, "invalid inbox name: %v", err)
  71. }
  72. if err := s.Store.DeleteInbox(ctx, &store.DeleteInbox{
  73. ID: inboxID,
  74. }); err != nil {
  75. return nil, status.Errorf(codes.Internal, "failed to update inbox: %v", err)
  76. }
  77. return &apiv2pb.DeleteInboxResponse{}, nil
  78. }
  79. func (s *APIV2Service) convertInboxFromStore(ctx context.Context, inbox *store.Inbox) (*apiv2pb.Inbox, error) {
  80. sender, err := s.Store.GetUser(ctx, &store.FindUser{
  81. ID: &inbox.SenderID,
  82. })
  83. if err != nil {
  84. return nil, errors.Wrap(err, "failed to get sender")
  85. }
  86. receiver, err := s.Store.GetUser(ctx, &store.FindUser{
  87. ID: &inbox.ReceiverID,
  88. })
  89. if err != nil {
  90. return nil, errors.Wrap(err, "failed to get receiver")
  91. }
  92. return &apiv2pb.Inbox{
  93. Name: fmt.Sprintf("inboxes/%d", inbox.ID),
  94. Sender: fmt.Sprintf("users/%s", sender.Username),
  95. Receiver: fmt.Sprintf("users/%s", receiver.Username),
  96. Status: convertInboxStatusFromStore(inbox.Status),
  97. CreateTime: timestamppb.New(time.Unix(inbox.CreatedTs, 0)),
  98. Type: apiv2pb.Inbox_Type(inbox.Message.Type),
  99. ActivityId: inbox.Message.ActivityId,
  100. }, nil
  101. }
  102. func convertInboxStatusFromStore(status store.InboxStatus) apiv2pb.Inbox_Status {
  103. switch status {
  104. case store.UNREAD:
  105. return apiv2pb.Inbox_UNREAD
  106. case store.ARCHIVED:
  107. return apiv2pb.Inbox_ARCHIVED
  108. default:
  109. return apiv2pb.Inbox_STATUS_UNSPECIFIED
  110. }
  111. }
  112. func convertInboxStatusToStore(status apiv2pb.Inbox_Status) store.InboxStatus {
  113. switch status {
  114. case apiv2pb.Inbox_UNREAD:
  115. return store.UNREAD
  116. case apiv2pb.Inbox_ARCHIVED:
  117. return store.ARCHIVED
  118. default:
  119. return store.UNREAD
  120. }
  121. }