Skip to content

Commit

Permalink
chore: tweak webhook payload
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Jun 1, 2024
1 parent 8c0bee3 commit 2e0d541
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
28 changes: 19 additions & 9 deletions plugin/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ import (
"time"

"github.com/pkg/errors"

v1pb "github.com/usememos/memos/proto/gen/api/v1"
)

var (
// timeout is the timeout for webhook request. Default to 30 seconds.
timeout = 30 * time.Second
)

type Memo struct {
// The name of the memo.
// Format: memos/{id}
// id is the system generated id.
Name string
// The name of the creator.
// Format: users/{id}
Creator string
// The raw content.
Content string
}

// WebhookPayload is the payload of webhook request.
// nolint
type WebhookPayload struct {
URL string `json:"url"`
ActivityType string `json:"activityType"`
CreatorID int32 `json:"creatorId"`
CreatedTs int64 `json:"createdTs"`
Memo *v1pb.Memo `json:"memo"`
URL string `json:"url"`
ActivityType string `json:"activityType"`
CreatorID int32 `json:"creatorId"`
CreatedTs int64 `json:"createdTs"`
Memo *Memo `json:"memo"`
}

// WebhookResponse is the response of webhook request.
Expand All @@ -40,8 +50,8 @@ func Post(payload WebhookPayload) error {
if err != nil {
return errors.Wrapf(err, "failed to marshal webhook request to %s", payload.URL)
}
req, err := http.NewRequest("POST",
payload.URL, bytes.NewBuffer(body))

req, err := http.NewRequest("POST", payload.URL, bytes.NewBuffer(body))
if err != nil {
return errors.Wrapf(err, "failed to construct webhook request to %s", payload.URL)
}
Expand Down
6 changes: 5 additions & 1 deletion server/router/api/v1/memo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,10 @@ func convertMemoToWebhookPayload(memo *v1pb.Memo) (*webhook.WebhookPayload, erro
return &webhook.WebhookPayload{
CreatorID: creatorID,
CreatedTs: time.Now().Unix(),
Memo: memo,
Memo: &webhook.Memo{
Name: memo.Name,
Creator: memo.Creator,
Content: memo.Content,
},
}, nil
}

0 comments on commit 2e0d541

Please sign in to comment.