main

mattermost/focalboard

Last updated at: 29/12/2023 09:48

webhook.go

TLDR

The webhook.go file in the server/services/webhook package contains the implementation of a webhook client. It provides a NotifyUpdate method to send block updates to configured webhooks.

Methods

NotifyUpdate

The NotifyUpdate method is used to call webhooks with block updates. It takes a *model.Block as input and sends the block data as a JSON payload to each configured webhook URL. It returns nothing.

Classes

Client

The Client class represents a webhook client. It has a config field of type *config.Configuration that stores the webhook configuration and a logger field of type mlog.LoggerIFace for logging purposes.

  • NewClient is a constructor function that creates a new instance of the Client class. It takes a *config.Configuration and a mlog.LoggerIFace as input and returns a pointer to the new Client instance.
package webhook

import (
	"bytes"
	"encoding/json"
	"io"
	"net/http"

	"github.com/mattermost/focalboard/server/model"
	"github.com/mattermost/focalboard/server/services/config"

	"github.com/mattermost/mattermost-server/v6/shared/mlog"
)

// NotifyUpdate calls webhooks.
func (wh *Client) NotifyUpdate(block *model.Block) {
	if len(wh.config.WebhookUpdate) < 1 {
		return
	}

	json, err := json.Marshal(block)
	if err != nil {
		wh.logger.Fatal("NotifyUpdate: json.Marshal", mlog.Err(err))
	}
	for _, url := range wh.config.WebhookUpdate {
		resp, _ := http.Post(url, "application/json", bytes.NewBuffer(json)) //nolint:gosec
		_, _ = io.ReadAll(resp.Body)
		resp.Body.Close()

		wh.logger.Debug("webhook.NotifyUpdate", mlog.String("url", url))
	}
}

// Client is a webhook client.
type Client struct {
	config *config.Configuration
	logger mlog.LoggerIFace
}

// NewClient creates a new Client.
func NewClient(config *config.Configuration, logger mlog.LoggerIFace) *Client {
	return &Client{
		config: config,
		logger: logger,
	}
}