Commit e876ed37 authored by Steven's avatar Steven

feat: impl part of inbox service

parent 67d2e4eb
package v2
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/store"
)
func (s *APIV2Service) ListInbox(ctx context.Context, _ *apiv2pb.ListInboxRequest) (*apiv2pb.ListInboxResponse, error) {
user, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user")
}
inboxList, err := s.Store.ListInbox(ctx, &store.FindInbox{
ReceiverID: &user.ID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list inbox: %v", err)
}
response := &apiv2pb.ListInboxResponse{
Inbox: []*apiv2pb.Inbox{},
}
for _, inbox := range inboxList {
inboxMessage, err := s.convertInboxFromStore(ctx, inbox)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to convert inbox from store: %v", err)
}
response.Inbox = append(response.Inbox, inboxMessage)
}
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")
}
inboxID, err := GetInboxID(request.Inbox.Name)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid inbox name: %v", err)
}
update := &store.UpdateInbox{
ID: inboxID,
}
for _, path := range request.UpdateMask.Paths {
if path == "status" {
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) {
inboxID, err := GetInboxID(request.Name)
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 (*APIV2Service) convertInboxFromStore(_ context.Context, inbox *store.Inbox) (*apiv2pb.Inbox, error) {
// TODO: convert sender and receiver.
return &apiv2pb.Inbox{
Name: fmt.Sprintf("inbox/%d", inbox.ID),
Status: convertInboxStatusFromStore(inbox.Status),
Title: inbox.Message.Title,
Content: inbox.Message.Content,
Link: inbox.Message.Link,
}, nil
}
func convertInboxStatusFromStore(status store.InboxStatus) apiv2pb.Inbox_Status {
switch status {
case store.UNREAD:
return apiv2pb.Inbox_UNREAD
case store.READ:
return apiv2pb.Inbox_READ
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_READ:
return store.READ
case apiv2pb.Inbox_ARCHIVED:
return store.ARCHIVED
default:
return store.UNREAD
}
}
package v2
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
const (
InboxNamePrefix = "inbox/"
)
// GetNameParentTokens returns the tokens from a resource name.
func GetNameParentTokens(name string, tokenPrefixes ...string) ([]string, error) {
parts := strings.Split(name, "/")
if len(parts) != 2*len(tokenPrefixes) {
return nil, errors.Errorf("invalid request %q", name)
}
var tokens []string
for i, tokenPrefix := range tokenPrefixes {
if fmt.Sprintf("%s/", parts[2*i]) != tokenPrefix {
return nil, errors.Errorf("invalid prefix %q in request %q", tokenPrefix, name)
}
if parts[2*i+1] == "" {
return nil, errors.Errorf("invalid request %q with empty prefix %q", name, tokenPrefix)
}
tokens = append(tokens, parts[2*i+1])
}
return tokens, nil
}
// GetInboxID returns the inbox ID from a resource name.
func GetInboxID(name string) (int32, error) {
tokens, err := GetNameParentTokens(name, InboxNamePrefix)
if err != nil {
return 0, err
}
id, err := strconv.Atoi(tokens[0])
if err != nil {
return 0, errors.Errorf("invalid inbox ID %q", tokens[0])
}
return int32(id), nil
}
......@@ -17,6 +17,8 @@ import (
)
type APIV2Service struct {
apiv2pb.UnimplementedInboxServiceServer
Secret string
Profile *profile.Profile
Store *store.Store
......@@ -33,20 +35,23 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
authProvider.AuthenticationInterceptor,
),
)
apiv2Service := &APIV2Service{
Secret: secret,
Profile: profile,
Store: store,
grpcServer: grpcServer,
grpcServerPort: grpcServerPort,
}
apiv2pb.RegisterSystemServiceServer(grpcServer, NewSystemService(profile, store))
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(store, secret))
apiv2pb.RegisterMemoServiceServer(grpcServer, NewMemoService(store))
apiv2pb.RegisterTagServiceServer(grpcServer, NewTagService(store))
apiv2pb.RegisterResourceServiceServer(grpcServer, NewResourceService(profile, store))
apiv2pb.RegisterInboxServiceServer(grpcServer, apiv2Service)
reflection.Register(grpcServer)
return &APIV2Service{
Secret: secret,
Profile: profile,
Store: store,
grpcServer: grpcServer,
grpcServerPort: grpcServerPort,
}
return apiv2Service
}
func (s *APIV2Service) GetGRPCServer() *grpc.Server {
......@@ -82,6 +87,9 @@ func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error
if err := apiv2pb.RegisterResourceServiceHandler(context.Background(), gwMux, conn); err != nil {
return err
}
if err := apiv2pb.RegisterInboxServiceHandler(context.Background(), gwMux, conn); err != nil {
return err
}
e.Any("/api/v2/*", echo.WrapHandler(gwMux))
// GRPC web proxy.
......
syntax = "proto3";
package memos.api.v2;
import "google/api/annotations.proto";
import "google/api/client.proto";
import "google/protobuf/field_mask.proto";
option go_package = "gen/api/v2";
service InboxService {
rpc ListInbox(ListInboxRequest) returns (ListInboxResponse) {
option (google.api.http) = {get: "/api/v2/inbox"};
}
rpc UpdateInbox(UpdateInboxRequest) returns (UpdateInboxResponse) {
option (google.api.http) = {
patch: "/v2/inbox"
body: "inbox"
};
option (google.api.method_signature) = "inbox,update_mask";
}
rpc DeleteInbox(DeleteInboxRequest) returns (DeleteInboxResponse) {
option (google.api.http) = {delete: "/v2/{name=inbox/*}"};
option (google.api.method_signature) = "name";
}
}
message Inbox {
// The name of the inbox.
// Format: inbox/{id}
string name = 1;
// Format: users/{username}
string sender = 2;
// Format: users/{username}
string receiver = 3;
enum Status {
STATUS_UNSPECIFIED = 0;
UNREAD = 1;
READ = 2;
ARCHIVED = 3;
}
Status status = 4;
string title = 5;
string content = 6;
string link = 7;
}
message ListInboxRequest {
// Format: /users/{username}
string user = 1;
}
message ListInboxResponse {
repeated Inbox inbox = 1;
}
message UpdateInboxRequest {
Inbox inbox = 1;
google.protobuf.FieldMask update_mask = 2;
}
message UpdateInboxResponse {
Inbox inbox = 1;
}
message DeleteInboxRequest {
// The name of the inbox to delete.
// Format: inbox/{inbox}
string name = 1;
}
message DeleteInboxResponse {}
......@@ -6,6 +6,19 @@
- [api/v2/common.proto](#api_v2_common-proto)
- [RowStatus](#memos-api-v2-RowStatus)
- [api/v2/inbox_service.proto](#api_v2_inbox_service-proto)
- [DeleteInboxRequest](#memos-api-v2-DeleteInboxRequest)
- [DeleteInboxResponse](#memos-api-v2-DeleteInboxResponse)
- [Inbox](#memos-api-v2-Inbox)
- [ListInboxRequest](#memos-api-v2-ListInboxRequest)
- [ListInboxResponse](#memos-api-v2-ListInboxResponse)
- [UpdateInboxRequest](#memos-api-v2-UpdateInboxRequest)
- [UpdateInboxResponse](#memos-api-v2-UpdateInboxResponse)
- [Inbox.Status](#memos-api-v2-Inbox-Status)
- [InboxService](#memos-api-v2-InboxService)
- [api/v2/memo_service.proto](#api_v2_memo_service-proto)
- [CreateMemoCommentRequest](#memos-api-v2-CreateMemoCommentRequest)
- [CreateMemoCommentResponse](#memos-api-v2-CreateMemoCommentResponse)
......@@ -109,6 +122,155 @@
<a name="api_v2_inbox_service-proto"></a>
<p align="right"><a href="#top">Top</a></p>
## api/v2/inbox_service.proto
<a name="memos-api-v2-DeleteInboxRequest"></a>
### DeleteInboxRequest
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| name | [string](#string) | | The name of the inbox to delete. Format: inbox/{inbox} |
<a name="memos-api-v2-DeleteInboxResponse"></a>
### DeleteInboxResponse
<a name="memos-api-v2-Inbox"></a>
### Inbox
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| name | [string](#string) | | The name of the inbox. Format: inbox/{id} |
| sender | [string](#string) | | Format: users/{username} |
| receiver | [string](#string) | | Format: users/{username} |
| status | [Inbox.Status](#memos-api-v2-Inbox-Status) | | |
| title | [string](#string) | | |
| content | [string](#string) | | |
| link | [string](#string) | | |
<a name="memos-api-v2-ListInboxRequest"></a>
### ListInboxRequest
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| user | [string](#string) | | Format: /users/{username} |
<a name="memos-api-v2-ListInboxResponse"></a>
### ListInboxResponse
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| inbox | [Inbox](#memos-api-v2-Inbox) | repeated | |
<a name="memos-api-v2-UpdateInboxRequest"></a>
### UpdateInboxRequest
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| inbox | [Inbox](#memos-api-v2-Inbox) | | |
| update_mask | [google.protobuf.FieldMask](#google-protobuf-FieldMask) | | |
<a name="memos-api-v2-UpdateInboxResponse"></a>
### UpdateInboxResponse
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| inbox | [Inbox](#memos-api-v2-Inbox) | | |
<a name="memos-api-v2-Inbox-Status"></a>
### Inbox.Status
| Name | Number | Description |
| ---- | ------ | ----------- |
| STATUS_UNSPECIFIED | 0 | |
| UNREAD | 1 | |
| READ | 2 | |
| ARCHIVED | 3 | |
<a name="memos-api-v2-InboxService"></a>
### InboxService
| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| ListInbox | [ListInboxRequest](#memos-api-v2-ListInboxRequest) | [ListInboxResponse](#memos-api-v2-ListInboxResponse) | |
| UpdateInbox | [UpdateInboxRequest](#memos-api-v2-UpdateInboxRequest) | [UpdateInboxResponse](#memos-api-v2-UpdateInboxResponse) | |
| DeleteInbox | [DeleteInboxRequest](#memos-api-v2-DeleteInboxRequest) | [DeleteInboxResponse](#memos-api-v2-DeleteInboxResponse) | |
<a name="api_v2_memo_service-proto"></a>
<p align="right"><a href="#top">Top</a></p>
......
This diff is collapsed.
This diff is collapsed.
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc (unknown)
// source: api/v2/inbox_service.proto
package apiv2
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
InboxService_ListInbox_FullMethodName = "/memos.api.v2.InboxService/ListInbox"
InboxService_UpdateInbox_FullMethodName = "/memos.api.v2.InboxService/UpdateInbox"
InboxService_DeleteInbox_FullMethodName = "/memos.api.v2.InboxService/DeleteInbox"
)
// InboxServiceClient is the client API for InboxService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type InboxServiceClient interface {
ListInbox(ctx context.Context, in *ListInboxRequest, opts ...grpc.CallOption) (*ListInboxResponse, error)
UpdateInbox(ctx context.Context, in *UpdateInboxRequest, opts ...grpc.CallOption) (*UpdateInboxResponse, error)
DeleteInbox(ctx context.Context, in *DeleteInboxRequest, opts ...grpc.CallOption) (*DeleteInboxResponse, error)
}
type inboxServiceClient struct {
cc grpc.ClientConnInterface
}
func NewInboxServiceClient(cc grpc.ClientConnInterface) InboxServiceClient {
return &inboxServiceClient{cc}
}
func (c *inboxServiceClient) ListInbox(ctx context.Context, in *ListInboxRequest, opts ...grpc.CallOption) (*ListInboxResponse, error) {
out := new(ListInboxResponse)
err := c.cc.Invoke(ctx, InboxService_ListInbox_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *inboxServiceClient) UpdateInbox(ctx context.Context, in *UpdateInboxRequest, opts ...grpc.CallOption) (*UpdateInboxResponse, error) {
out := new(UpdateInboxResponse)
err := c.cc.Invoke(ctx, InboxService_UpdateInbox_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *inboxServiceClient) DeleteInbox(ctx context.Context, in *DeleteInboxRequest, opts ...grpc.CallOption) (*DeleteInboxResponse, error) {
out := new(DeleteInboxResponse)
err := c.cc.Invoke(ctx, InboxService_DeleteInbox_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// InboxServiceServer is the server API for InboxService service.
// All implementations must embed UnimplementedInboxServiceServer
// for forward compatibility
type InboxServiceServer interface {
ListInbox(context.Context, *ListInboxRequest) (*ListInboxResponse, error)
UpdateInbox(context.Context, *UpdateInboxRequest) (*UpdateInboxResponse, error)
DeleteInbox(context.Context, *DeleteInboxRequest) (*DeleteInboxResponse, error)
mustEmbedUnimplementedInboxServiceServer()
}
// UnimplementedInboxServiceServer must be embedded to have forward compatible implementations.
type UnimplementedInboxServiceServer struct {
}
func (UnimplementedInboxServiceServer) ListInbox(context.Context, *ListInboxRequest) (*ListInboxResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ListInbox not implemented")
}
func (UnimplementedInboxServiceServer) UpdateInbox(context.Context, *UpdateInboxRequest) (*UpdateInboxResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateInbox not implemented")
}
func (UnimplementedInboxServiceServer) DeleteInbox(context.Context, *DeleteInboxRequest) (*DeleteInboxResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeleteInbox not implemented")
}
func (UnimplementedInboxServiceServer) mustEmbedUnimplementedInboxServiceServer() {}
// UnsafeInboxServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to InboxServiceServer will
// result in compilation errors.
type UnsafeInboxServiceServer interface {
mustEmbedUnimplementedInboxServiceServer()
}
func RegisterInboxServiceServer(s grpc.ServiceRegistrar, srv InboxServiceServer) {
s.RegisterService(&InboxService_ServiceDesc, srv)
}
func _InboxService_ListInbox_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ListInboxRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(InboxServiceServer).ListInbox(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: InboxService_ListInbox_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(InboxServiceServer).ListInbox(ctx, req.(*ListInboxRequest))
}
return interceptor(ctx, in, info, handler)
}
func _InboxService_UpdateInbox_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UpdateInboxRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(InboxServiceServer).UpdateInbox(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: InboxService_UpdateInbox_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(InboxServiceServer).UpdateInbox(ctx, req.(*UpdateInboxRequest))
}
return interceptor(ctx, in, info, handler)
}
func _InboxService_DeleteInbox_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DeleteInboxRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(InboxServiceServer).DeleteInbox(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: InboxService_DeleteInbox_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(InboxServiceServer).DeleteInbox(ctx, req.(*DeleteInboxRequest))
}
return interceptor(ctx, in, info, handler)
}
// InboxService_ServiceDesc is the grpc.ServiceDesc for InboxService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var InboxService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "memos.api.v2.InboxService",
HandlerType: (*InboxServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "ListInbox",
Handler: _InboxService_ListInbox_Handler,
},
{
MethodName: "UpdateInbox",
Handler: _InboxService_UpdateInbox_Handler,
},
{
MethodName: "DeleteInbox",
Handler: _InboxService_DeleteInbox_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "api/v2/inbox_service.proto",
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment