main

mattermost/focalboard

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

insights.go

TLDR

This file is a part of the Demo Projects project. It contains several methods related to insights in the application.

Methods

GetTeamBoardsInsights

This method retrieves insights for boards in a team. It checks if the server is properly licensed and if the user is not a guest. It then fetches the board IDs accessible by the user and calls the GetTeamBoardsInsights method of the store to get the insights data.

GetUserBoardsInsights

This method retrieves insights for boards of a specific user in a team. Similar to the GetTeamBoardsInsights method, it checks the server license and user permissions. It then fetches the board IDs accessible by the user and calls the GetUserBoardsInsights method of the store to get the insights data.

insightPermissionGate

This method checks if the server has a valid license and the user has the necessary permissions to access insights. It retrieves the license information from the store, checks if the user is a guest, and validates the license SKU. It returns a boolean indicating whether the user is permitted to access insights.

GetUserTimezone

This method retrieves the timezone of a specific user.

getUserBoards

This method retrieves the boards accessible by a user in a team and filters the board IDs.

Classes

None

package app

import (
	"github.com/mattermost/focalboard/server/model"
	mmModel "github.com/mattermost/mattermost-server/v6/model"
	"github.com/pkg/errors"
)

func (a *App) GetTeamBoardsInsights(userID string, teamID string, opts *mmModel.InsightsOpts) (*model.BoardInsightsList, error) {
	// check if server is properly licensed, and user is not a guest
	userPermitted, err := insightPermissionGate(a, userID, false)
	if err != nil {
		return nil, err
	}
	if !userPermitted {
		return nil, errors.New("User isn't authorized to access insights.")
	}
	boardIDs, err := getUserBoards(userID, teamID, a)
	if err != nil {
		return nil, err
	}
	return a.store.GetTeamBoardsInsights(teamID, opts.StartUnixMilli, opts.Page*opts.PerPage, opts.PerPage, boardIDs)
}

func (a *App) GetUserBoardsInsights(userID string, teamID string, opts *mmModel.InsightsOpts) (*model.BoardInsightsList, error) {
	// check if server is properly licensed, and user is not a guest
	userPermitted, err := insightPermissionGate(a, userID, true)
	if err != nil {
		return nil, err
	}
	if !userPermitted {
		return nil, errors.New("User isn't authorized to access insights.")
	}
	boardIDs, err := getUserBoards(userID, teamID, a)
	if err != nil {
		return nil, err
	}
	return a.store.GetUserBoardsInsights(teamID, userID, opts.StartUnixMilli, opts.Page*opts.PerPage, opts.PerPage, boardIDs)
}

func insightPermissionGate(a *App, userID string, isMyInsights bool) (bool, error) {
	licenseError := errors.New("invalid license/authorization to use insights API")
	guestError := errors.New("guests aren't authorized to use insights API")
	lic := a.store.GetLicense()

	user, err := a.store.GetUserByID(userID)
	if err != nil {
		return false, err
	}

	if user.IsGuest {
		return false, guestError
	}

	if lic == nil && !isMyInsights {
		a.logger.Debug("Deployment doesn't have a license")
		return false, licenseError
	}

	if !isMyInsights && (lic.SkuShortName != mmModel.LicenseShortSkuProfessional && lic.SkuShortName != mmModel.LicenseShortSkuEnterprise) {
		return false, licenseError
	}

	return true, nil
}

func (a *App) GetUserTimezone(userID string) (string, error) {
	return a.store.GetUserTimezone(userID)
}

func getUserBoards(userID string, teamID string, a *App) ([]string, error) {
	// get boards accessible by user and filter boardIDs
	boards, err := a.store.GetBoardsForUserAndTeam(userID, teamID, true)
	if err != nil {
		return nil, errors.New("error getting boards for user")
	}
	boardIDs := make([]string, 0, len(boards))

	for _, board := range boards {
		boardIDs = append(boardIDs, board.ID)
	}
	return boardIDs, nil
}