2020-10-29 22:09:02 +02:00
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-09-13 12:18:40 +02:00
|
|
|
"io"
|
2020-10-29 22:09:02 +02:00
|
|
|
"net/http"
|
|
|
|
|
2021-01-27 00:13:46 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
2024-06-07 20:00:08 +02:00
|
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
2020-10-29 22:09:02 +02:00
|
|
|
)
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// NotifyUpdate calls webhooks.
|
2022-10-25 22:46:43 +02:00
|
|
|
func (wh *Client) NotifyUpdate(block *model.Block) {
|
2020-10-29 22:32:51 +02:00
|
|
|
if len(wh.config.WebhookUpdate) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-29 22:09:02 +02:00
|
|
|
json, err := json.Marshal(block)
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
wh.logger.Fatal("NotifyUpdate: json.Marshal", mlog.Err(err))
|
2020-10-29 22:09:02 +02:00
|
|
|
}
|
|
|
|
for _, url := range wh.config.WebhookUpdate {
|
2021-06-21 11:21:42 +02:00
|
|
|
resp, _ := http.Post(url, "application/json", bytes.NewBuffer(json)) //nolint:gosec
|
2022-09-13 12:18:40 +02:00
|
|
|
_, _ = io.ReadAll(resp.Body)
|
2021-06-21 11:21:42 +02:00
|
|
|
resp.Body.Close()
|
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
wh.logger.Debug("webhook.NotifyUpdate", mlog.String("url", url))
|
2020-10-29 22:09:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// Client is a webhook client.
|
2020-10-29 22:42:43 +02:00
|
|
|
type Client struct {
|
2020-10-29 22:09:02 +02:00
|
|
|
config *config.Configuration
|
2022-07-18 19:21:57 +02:00
|
|
|
logger mlog.LoggerIFace
|
2020-10-29 22:09:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// NewClient creates a new Client.
|
2022-07-18 19:21:57 +02:00
|
|
|
func NewClient(config *config.Configuration, logger mlog.LoggerIFace) *Client {
|
2020-10-29 22:42:43 +02:00
|
|
|
return &Client{
|
2020-10-29 22:09:02 +02:00
|
|
|
config: config,
|
2021-05-29 08:23:10 +02:00
|
|
|
logger: logger,
|
2020-10-29 22:09:02 +02:00
|
|
|
}
|
|
|
|
}
|