mattermost/server/channels/app/cloud.go
Nick Misasi e402db875c
Add support for dynamic fetching of preview modal content from S3 bucket (#33380)
* Add support for dynamic fetching of preview modal content from S3 bucket

* Update server/channels/api4/cloud.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update webapp/channels/src/components/cloud_preview_modal/cloud_preview_modal_controller.test.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fixes for CI pipelines

* Add definitions for openapi spec

* Use any instead of interface{}

* Update translations

* Add the translations

* Hook should only run fetch when in cloud preview

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-07-15 12:58:18 -04:00

70 lines
2.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"encoding/json"
"net/http"
"github.com/mattermost/mattermost/server/public/model"
)
func (a *App) AdjustInProductLimits(limits *model.ProductLimits, subscription *model.Subscription) *model.AppError {
if limits.Teams != nil && limits.Teams.Active != nil && *limits.Teams.Active > 0 {
err := a.AdjustTeamsFromProductLimits(limits.Teams)
if err != nil {
return err
}
}
return nil
}
// Create/ Update a subscription history event
// This function is run daily to record the number of activated users in the system for Cloud workspaces
func (a *App) SendSubscriptionHistoryEvent(userID string) (*model.SubscriptionHistory, error) {
license := a.Srv().License()
// No need to create a Subscription History Event if the license isn't cloud
if !license.IsCloud() {
return nil, nil
}
userCount, err := a.Srv().Store().User().Count(model.UserCountOptions{})
if err != nil {
return nil, err
}
return a.Cloud().CreateOrUpdateSubscriptionHistoryEvent(userID, int(userCount))
}
// GetPreviewModalData fetches modal content data from the configured S3 bucket
func (a *App) GetPreviewModalData() ([]model.PreviewModalContentData, *model.AppError) {
bucketURL := a.Config().CloudSettings.PreviewModalBucketURL
if bucketURL == nil || *bucketURL == "" {
return nil, model.NewAppError("GetPreviewModalData", "app.cloud.preview_modal_bucket_url_not_configured", nil, "", http.StatusNotFound)
}
// Construct the full URL to the modal_content.json file
fileURL := *bucketURL + "/modal_content.json"
// Make HTTP request to S3
resp, err := http.Get(fileURL)
if err != nil {
return nil, model.NewAppError("GetPreviewModalData", "app.cloud.preview_modal_data_fetch_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, model.NewAppError("GetPreviewModalData", "app.cloud.preview_modal_data_fetch_error", nil, "", resp.StatusCode)
}
// Parse the JSON response
var modalData []model.PreviewModalContentData
if err := json.NewDecoder(resp.Body).Decode(&modalData); err != nil {
return nil, model.NewAppError("GetPreviewModalData", "app.cloud.preview_modal_data_parse_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
return modalData, nil
}