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

115 lines
3.5 KiB
Go
Raw Normal View History

2024-04-28 00:44:29 +08:00
package v1
2023-11-24 23:04:36 +08:00
import (
"context"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2024-04-27 23:14:58 +08:00
"google.golang.org/protobuf/types/known/emptypb"
2023-11-24 23:04:36 +08:00
"google.golang.org/protobuf/types/known/timestamppb"
2024-04-28 00:44:29 +08:00
v1pb "github.com/usememos/memos/proto/gen/api/v1"
2023-11-24 23:04:36 +08:00
"github.com/usememos/memos/store"
)
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) CreateWebhook(ctx context.Context, request *v1pb.CreateWebhookRequest) (*v1pb.Webhook, error) {
2023-11-24 23:04:36 +08:00
currentUser, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
}
2024-04-13 11:54:37 +08:00
webhook, err := s.Store.CreateWebhook(ctx, &store.Webhook{
CreatorID: currentUser.ID,
2023-11-24 23:04:36 +08:00
Name: request.Name,
2024-04-13 12:11:59 +08:00
URL: request.Url,
2023-11-24 23:04:36 +08:00
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create webhook, error: %+v", err)
}
2024-04-27 23:14:58 +08:00
return convertWebhookFromStore(webhook), nil
2023-11-24 23:04:36 +08:00
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) ListWebhooks(ctx context.Context, request *v1pb.ListWebhooksRequest) (*v1pb.ListWebhooksResponse, error) {
2023-11-24 23:04:36 +08:00
webhooks, err := s.Store.ListWebhooks(ctx, &store.FindWebhook{
CreatorID: &request.CreatorId,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list webhooks, error: %+v", err)
}
2024-04-28 00:44:29 +08:00
response := &v1pb.ListWebhooksResponse{
Webhooks: []*v1pb.Webhook{},
2023-11-24 23:04:36 +08:00
}
for _, webhook := range webhooks {
response.Webhooks = append(response.Webhooks, convertWebhookFromStore(webhook))
}
return response, nil
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) GetWebhook(ctx context.Context, request *v1pb.GetWebhookRequest) (*v1pb.Webhook, error) {
2023-11-24 23:04:36 +08:00
currentUser, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
}
2024-04-13 02:55:40 +08:00
webhook, err := s.Store.GetWebhook(ctx, &store.FindWebhook{
2023-11-24 23:04:36 +08:00
ID: &request.Id,
CreatorID: &currentUser.ID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get webhook, error: %+v", err)
}
if webhook == nil {
return nil, status.Errorf(codes.NotFound, "webhook not found")
}
2024-04-27 23:14:58 +08:00
return convertWebhookFromStore(webhook), nil
2023-11-24 23:04:36 +08:00
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) UpdateWebhook(ctx context.Context, request *v1pb.UpdateWebhookRequest) (*v1pb.Webhook, error) {
2023-11-24 23:04:36 +08:00
if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "update_mask is required")
}
update := &store.UpdateWebhook{}
2023-11-30 23:08:54 +08:00
for _, field := range request.UpdateMask.Paths {
switch field {
2023-11-24 23:04:36 +08:00
case "row_status":
2024-04-13 11:54:37 +08:00
rowStatus := store.RowStatus(request.Webhook.RowStatus.String())
2023-11-24 23:04:36 +08:00
update.RowStatus = &rowStatus
case "name":
update.Name = &request.Webhook.Name
case "url":
update.URL = &request.Webhook.Url
}
}
webhook, err := s.Store.UpdateWebhook(ctx, update)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update webhook, error: %+v", err)
}
2024-04-27 23:14:58 +08:00
return convertWebhookFromStore(webhook), nil
2023-11-24 23:04:36 +08:00
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) DeleteWebhook(ctx context.Context, request *v1pb.DeleteWebhookRequest) (*emptypb.Empty, error) {
2023-11-24 23:04:36 +08:00
err := s.Store.DeleteWebhook(ctx, &store.DeleteWebhook{
ID: request.Id,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete webhook, error: %+v", err)
}
2024-04-27 23:14:58 +08:00
return &emptypb.Empty{}, nil
2023-11-24 23:04:36 +08:00
}
2024-04-28 00:44:29 +08:00
func convertWebhookFromStore(webhook *store.Webhook) *v1pb.Webhook {
return &v1pb.Webhook{
2024-04-13 11:54:37 +08:00
Id: webhook.ID,
2023-11-24 23:04:36 +08:00
CreatedTime: timestamppb.New(time.Unix(webhook.CreatedTs, 0)),
UpdatedTime: timestamppb.New(time.Unix(webhook.UpdatedTs, 0)),
2024-04-13 11:54:37 +08:00
RowStatus: convertRowStatusFromStore(webhook.RowStatus),
CreatorId: webhook.CreatorID,
2023-11-24 23:04:36 +08:00
Name: webhook.Name,
2024-04-13 12:11:59 +08:00
Url: webhook.URL,
2023-11-24 23:04:36 +08:00
}
}