memos/server/router/api/v1/inbox_service.go

120 lines
3.4 KiB
Go
Raw Normal View History

2024-04-28 00:44:29 +08:00
package v1
2023-10-27 09:01:17 +08:00
import (
"context"
"fmt"
2023-10-28 00:08:42 +08:00
"time"
2023-10-27 09:01:17 +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-10-28 00:08:42 +08:00
"google.golang.org/protobuf/types/known/timestamppb"
2023-10-27 09:01:17 +08:00
2024-04-28 00:44:29 +08:00
v1pb "github.com/usememos/memos/proto/gen/api/v1"
2023-10-27 09:01:17 +08:00
"github.com/usememos/memos/store"
)
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) ListInboxes(ctx context.Context, _ *v1pb.ListInboxesRequest) (*v1pb.ListInboxesResponse, error) {
2024-05-26 11:02:23 +08:00
user, err := s.GetCurrentUser(ctx)
2023-10-27 09:01:17 +08:00
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user")
}
2023-10-28 00:08:42 +08:00
inboxes, err := s.Store.ListInboxes(ctx, &store.FindInbox{
2023-10-27 09:01:17 +08:00
ReceiverID: &user.ID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list inbox: %v", err)
}
2024-04-28 00:44:29 +08:00
response := &v1pb.ListInboxesResponse{
Inboxes: []*v1pb.Inbox{},
2023-10-27 09:01:17 +08:00
}
2023-10-28 00:08:42 +08:00
for _, inbox := range inboxes {
2024-05-15 23:22:23 +08:00
inboxMessage := convertInboxFromStore(inbox)
if inboxMessage.Type == v1pb.Inbox_TYPE_UNSPECIFIED {
continue
}
response.Inboxes = append(response.Inboxes, inboxMessage)
2023-10-27 09:01:17 +08:00
}
return response, nil
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) UpdateInbox(ctx context.Context, request *v1pb.UpdateInboxRequest) (*v1pb.Inbox, error) {
2023-10-27 09:01:17 +08:00
if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
}
2024-02-06 20:55:27 +08:00
inboxID, err := ExtractInboxIDFromName(request.Inbox.Name)
2023-10-27 09:01:17 +08:00
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid inbox name: %v", err)
}
update := &store.UpdateInbox{
ID: inboxID,
}
2023-11-30 23:08:54 +08:00
for _, field := range request.UpdateMask.Paths {
if field == "status" {
2024-04-28 00:44:29 +08:00
if request.Inbox.Status == v1pb.Inbox_STATUS_UNSPECIFIED {
2023-10-27 09:01:17 +08:00
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)
}
2024-04-27 22:02:15 +08:00
return convertInboxFromStore(inbox), nil
2023-10-27 09:01:17 +08:00
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) DeleteInbox(ctx context.Context, request *v1pb.DeleteInboxRequest) (*emptypb.Empty, error) {
2023-11-05 23:28:09 +08:00
inboxID, err := ExtractInboxIDFromName(request.Name)
2023-10-27 09:01:17 +08: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)
}
2024-04-27 22:02:15 +08:00
return &emptypb.Empty{}, nil
2023-10-27 09:01:17 +08:00
}
2024-04-28 00:44:29 +08:00
func convertInboxFromStore(inbox *store.Inbox) *v1pb.Inbox {
return &v1pb.Inbox{
2024-03-30 14:58:47 +08:00
Name: fmt.Sprintf("%s%d", InboxNamePrefix, inbox.ID),
2024-04-01 00:26:46 +08:00
Sender: fmt.Sprintf("%s%d", UserNamePrefix, inbox.SenderID),
Receiver: fmt.Sprintf("%s%d", UserNamePrefix, inbox.ReceiverID),
Status: convertInboxStatusFromStore(inbox.Status),
2023-10-28 00:08:42 +08:00
CreateTime: timestamppb.New(time.Unix(inbox.CreatedTs, 0)),
2024-04-28 00:44:29 +08:00
Type: v1pb.Inbox_Type(inbox.Message.Type),
ActivityId: inbox.Message.ActivityId,
2024-04-01 00:34:51 +08:00
}
2023-10-27 09:01:17 +08:00
}
2024-04-28 00:44:29 +08:00
func convertInboxStatusFromStore(status store.InboxStatus) v1pb.Inbox_Status {
2023-10-27 09:01:17 +08:00
switch status {
case store.UNREAD:
2024-04-28 00:44:29 +08:00
return v1pb.Inbox_UNREAD
2023-10-27 09:01:17 +08:00
case store.ARCHIVED:
2024-04-28 00:44:29 +08:00
return v1pb.Inbox_ARCHIVED
2023-10-27 09:01:17 +08:00
default:
2024-04-28 00:44:29 +08:00
return v1pb.Inbox_STATUS_UNSPECIFIED
2023-10-27 09:01:17 +08:00
}
}
2024-04-28 00:44:29 +08:00
func convertInboxStatusToStore(status v1pb.Inbox_Status) store.InboxStatus {
2023-10-27 09:01:17 +08:00
switch status {
2024-04-28 00:44:29 +08:00
case v1pb.Inbox_UNREAD:
2023-10-27 09:01:17 +08:00
return store.UNREAD
2024-04-28 00:44:29 +08:00
case v1pb.Inbox_ARCHIVED:
2023-10-27 09:01:17 +08:00
return store.ARCHIVED
default:
return store.UNREAD
}
}