webhook_service.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package v1
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "google.golang.org/grpc/codes"
  7. "google.golang.org/grpc/status"
  8. "google.golang.org/protobuf/types/known/emptypb"
  9. "google.golang.org/protobuf/types/known/timestamppb"
  10. v1pb "github.com/usememos/memos/proto/gen/api/v1"
  11. "github.com/usememos/memos/store"
  12. )
  13. func (s *APIV1Service) CreateWebhook(ctx context.Context, request *v1pb.CreateWebhookRequest) (*v1pb.Webhook, error) {
  14. currentUser, err := s.GetCurrentUser(ctx)
  15. if err != nil {
  16. return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
  17. }
  18. webhook, err := s.Store.CreateWebhook(ctx, &store.Webhook{
  19. CreatorID: currentUser.ID,
  20. Name: request.Name,
  21. URL: request.Url,
  22. })
  23. if err != nil {
  24. return nil, status.Errorf(codes.Internal, "failed to create webhook, error: %+v", err)
  25. }
  26. return convertWebhookFromStore(webhook), nil
  27. }
  28. func (s *APIV1Service) ListWebhooks(ctx context.Context, request *v1pb.ListWebhooksRequest) (*v1pb.ListWebhooksResponse, error) {
  29. creatorID, err := ExtractUserIDFromName(request.Creator)
  30. if err != nil {
  31. return nil, status.Errorf(codes.InvalidArgument, "invalid creator name: %v", err)
  32. }
  33. webhooks, err := s.Store.ListWebhooks(ctx, &store.FindWebhook{
  34. CreatorID: &creatorID,
  35. })
  36. if err != nil {
  37. return nil, status.Errorf(codes.Internal, "failed to list webhooks, error: %+v", err)
  38. }
  39. response := &v1pb.ListWebhooksResponse{
  40. Webhooks: []*v1pb.Webhook{},
  41. }
  42. for _, webhook := range webhooks {
  43. response.Webhooks = append(response.Webhooks, convertWebhookFromStore(webhook))
  44. }
  45. return response, nil
  46. }
  47. func (s *APIV1Service) GetWebhook(ctx context.Context, request *v1pb.GetWebhookRequest) (*v1pb.Webhook, error) {
  48. currentUser, err := s.GetCurrentUser(ctx)
  49. if err != nil {
  50. return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
  51. }
  52. webhook, err := s.Store.GetWebhook(ctx, &store.FindWebhook{
  53. ID: &request.Id,
  54. CreatorID: &currentUser.ID,
  55. })
  56. if err != nil {
  57. return nil, status.Errorf(codes.Internal, "failed to get webhook, error: %+v", err)
  58. }
  59. if webhook == nil {
  60. return nil, status.Errorf(codes.NotFound, "webhook not found")
  61. }
  62. return convertWebhookFromStore(webhook), nil
  63. }
  64. func (s *APIV1Service) UpdateWebhook(ctx context.Context, request *v1pb.UpdateWebhookRequest) (*v1pb.Webhook, error) {
  65. if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
  66. return nil, status.Errorf(codes.InvalidArgument, "update_mask is required")
  67. }
  68. update := &store.UpdateWebhook{}
  69. for _, field := range request.UpdateMask.Paths {
  70. switch field {
  71. case "name":
  72. update.Name = &request.Webhook.Name
  73. case "url":
  74. update.URL = &request.Webhook.Url
  75. }
  76. }
  77. webhook, err := s.Store.UpdateWebhook(ctx, update)
  78. if err != nil {
  79. return nil, status.Errorf(codes.Internal, "failed to update webhook, error: %+v", err)
  80. }
  81. return convertWebhookFromStore(webhook), nil
  82. }
  83. func (s *APIV1Service) DeleteWebhook(ctx context.Context, request *v1pb.DeleteWebhookRequest) (*emptypb.Empty, error) {
  84. err := s.Store.DeleteWebhook(ctx, &store.DeleteWebhook{
  85. ID: request.Id,
  86. })
  87. if err != nil {
  88. return nil, status.Errorf(codes.Internal, "failed to delete webhook, error: %+v", err)
  89. }
  90. return &emptypb.Empty{}, nil
  91. }
  92. func convertWebhookFromStore(webhook *store.Webhook) *v1pb.Webhook {
  93. return &v1pb.Webhook{
  94. Id: webhook.ID,
  95. CreateTime: timestamppb.New(time.Unix(webhook.CreatedTs, 0)),
  96. UpdateTime: timestamppb.New(time.Unix(webhook.UpdatedTs, 0)),
  97. Creator: fmt.Sprintf("%s%d", UserNamePrefix, webhook.CreatorID),
  98. Name: webhook.Name,
  99. Url: webhook.URL,
  100. }
  101. }