memos/api/v2/inbox_service .go

139 lines
4.0 KiB
Go
Raw Normal View History

2023-10-27 04:01:17 +03:00
package v2
import (
"context"
"fmt"
2023-10-27 19:08:42 +03:00
"time"
2023-10-27 04:01:17 +03:00
"github.com/pkg/errors"
2023-10-27 04:01:17 +03:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2023-10-27 19:08:42 +03:00
"google.golang.org/protobuf/types/known/timestamppb"
2023-10-27 04:01:17 +03:00
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/store"
)
2023-10-27 19:08:42 +03:00
func (s *APIV2Service) ListInboxes(ctx context.Context, _ *apiv2pb.ListInboxesRequest) (*apiv2pb.ListInboxesResponse, error) {
2023-10-27 04:01:17 +03:00
user, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user")
}
2023-10-27 19:08:42 +03:00
inboxes, err := s.Store.ListInboxes(ctx, &store.FindInbox{
2023-10-27 04:01:17 +03:00
ReceiverID: &user.ID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list inbox: %v", err)
}
2023-10-27 19:08:42 +03:00
response := &apiv2pb.ListInboxesResponse{
Inboxes: []*apiv2pb.Inbox{},
2023-10-27 04:01:17 +03:00
}
2023-10-27 19:08:42 +03:00
for _, inbox := range inboxes {
2023-10-27 04:01:17 +03:00
inboxMessage, err := s.convertInboxFromStore(ctx, inbox)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to convert inbox from store: %v", err)
}
2023-10-27 19:08:42 +03:00
response.Inboxes = append(response.Inboxes, inboxMessage)
2023-10-27 04:01:17 +03:00
}
return response, nil
}
func (s *APIV2Service) UpdateInbox(ctx context.Context, request *apiv2pb.UpdateInboxRequest) (*apiv2pb.UpdateInboxResponse, error) {
if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
}
2023-11-05 18:28:09 +03:00
inboxID, err := ExtractInboxIDFromName(request.Inbox.Name)
2023-10-27 04:01:17 +03:00
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid inbox name: %v", err)
}
update := &store.UpdateInbox{
ID: inboxID,
}
2023-11-30 18:08:54 +03:00
for _, field := range request.UpdateMask.Paths {
if field == "status" {
2023-10-27 04:01:17 +03:00
if request.Inbox.Status == apiv2pb.Inbox_STATUS_UNSPECIFIED {
return nil, status.Errorf(codes.InvalidArgument, "status is required")
}
update.Status = convertInboxStatusToStore(request.Inbox.Status)
}
}
inbox, err := s.Store.UpdateInbox(ctx, update)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update inbox: %v", err)
}
inboxMessage, err := s.convertInboxFromStore(ctx, inbox)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to convert inbox from store: %v", err)
}
return &apiv2pb.UpdateInboxResponse{
Inbox: inboxMessage,
}, nil
}
func (s *APIV2Service) DeleteInbox(ctx context.Context, request *apiv2pb.DeleteInboxRequest) (*apiv2pb.DeleteInboxResponse, error) {
2023-11-05 18:28:09 +03:00
inboxID, err := ExtractInboxIDFromName(request.Name)
2023-10-27 04:01:17 +03:00
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid inbox name: %v", err)
}
if err := s.Store.DeleteInbox(ctx, &store.DeleteInbox{
ID: inboxID,
}); err != nil {
return nil, status.Errorf(codes.Internal, "failed to update inbox: %v", err)
}
return &apiv2pb.DeleteInboxResponse{}, nil
}
func (s *APIV2Service) convertInboxFromStore(ctx context.Context, inbox *store.Inbox) (*apiv2pb.Inbox, error) {
sender, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &inbox.SenderID,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get sender")
}
receiver, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &inbox.ReceiverID,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get receiver")
}
2023-10-27 04:01:17 +03:00
return &apiv2pb.Inbox{
2023-10-27 19:08:42 +03:00
Name: fmt.Sprintf("inboxes/%d", inbox.ID),
Sender: fmt.Sprintf("users/%s", sender.Username),
Receiver: fmt.Sprintf("users/%s", receiver.Username),
Status: convertInboxStatusFromStore(inbox.Status),
2023-10-27 19:08:42 +03:00
CreateTime: timestamppb.New(time.Unix(inbox.CreatedTs, 0)),
Type: apiv2pb.Inbox_Type(inbox.Message.Type),
ActivityId: inbox.Message.ActivityId,
2023-10-27 04:01:17 +03:00
}, nil
}
func convertInboxStatusFromStore(status store.InboxStatus) apiv2pb.Inbox_Status {
switch status {
case store.UNREAD:
return apiv2pb.Inbox_UNREAD
case store.ARCHIVED:
return apiv2pb.Inbox_ARCHIVED
default:
return apiv2pb.Inbox_STATUS_UNSPECIFIED
}
}
func convertInboxStatusToStore(status apiv2pb.Inbox_Status) store.InboxStatus {
switch status {
case apiv2pb.Inbox_UNREAD:
return store.UNREAD
case apiv2pb.Inbox_ARCHIVED:
return store.ARCHIVED
default:
return store.UNREAD
}
}