webhook_service.go 3.8 KB

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