main

mattermost/focalboard

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

subscriptions.go

TLDR

This file contains functions related to creating, deleting, and retrieving subscriptions. It also includes a function for notifying when a subscription has changed.

Methods

CreateSubscription

Creates a new subscription and stores it in the database. It also sends a notification to the subscribers of the subscription.

DeleteSubscription

Deletes a subscription from the database. It also sends a notification to the subscribers of the subscription.

GetSubscriptions

Retrieves a list of subscriptions for a given subscriber.

notifySubscriptionChanged

Notifies subscribers when a subscription has changed by sending a notification to the appropriate subscribers.

package app

import (
	"github.com/mattermost/focalboard/server/model"
	"github.com/mattermost/focalboard/server/utils"

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

func (a *App) CreateSubscription(sub *model.Subscription) (*model.Subscription, error) {
	sub, err := a.store.CreateSubscription(sub)
	if err != nil {
		return nil, err
	}
	a.notifySubscriptionChanged(sub)

	return sub, nil
}

func (a *App) DeleteSubscription(blockID string, subscriberID string) (*model.Subscription, error) {
	sub, err := a.store.GetSubscription(blockID, subscriberID)
	if err != nil {
		return nil, err
	}
	if err := a.store.DeleteSubscription(blockID, subscriberID); err != nil {
		return nil, err
	}
	sub.DeleteAt = utils.GetMillis()
	a.notifySubscriptionChanged(sub)

	return sub, nil
}

func (a *App) GetSubscriptions(subscriberID string) ([]*model.Subscription, error) {
	return a.store.GetSubscriptions(subscriberID)
}

func (a *App) notifySubscriptionChanged(subscription *model.Subscription) {
	if a.notifications == nil {
		return
	}

	board, err := a.getBoardForBlock(subscription.BlockID)
	if err != nil {
		a.logger.Error("Error notifying subscription change",
			mlog.String("subscriber_id", subscription.SubscriberID),
			mlog.String("block_id", subscription.BlockID),
			mlog.Err(err),
		)
	}
	a.wsAdapter.BroadcastSubscriptionChange(board.TeamID, subscription)
}