From e3d86984cdc3079ad448afd5022d395f0323f414 Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Thu, 4 Apr 2024 10:28:31 +0200 Subject: [PATCH] [MM-54994] Use local requests instead of HTTP requests in the flow library (#26471) --- .../pluginapi/experimental/flow/flow.go | 24 +++++++++++++++---- .../pluginapi/experimental/flow/handler.go | 4 ++-- .../pluginapi/experimental/flow/step.go | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/server/public/pluginapi/experimental/flow/flow.go b/server/public/pluginapi/experimental/flow/flow.go index 036b1e6099..98fae8dcb7 100644 --- a/server/public/pluginapi/experimental/flow/flow.go +++ b/server/public/pluginapi/experimental/flow/flow.go @@ -27,9 +27,11 @@ type Flow struct { name Name api *pluginapi.Client - pluginURL string + pluginID string botUserID string + siteURL string + steps map[Name]Step index []Name done func(userID string, state State) error @@ -40,14 +42,28 @@ type Flow struct { // NewFlow creates a new flow using direct messages with the user. // // name must be a unique identifier for the flow within the plugin. -func NewFlow(name Name, api *pluginapi.Client, pluginURL, botUserID string) *Flow { +func NewFlow(name Name, api *pluginapi.Client, pluginID, botUserID string) (*Flow, error) { + if api == nil { + return nil, errors.New("API client must not be nil") + } + + config := api.Configuration.GetConfig() + if config == nil { + return nil, errors.New("failed to fetch configuration") + } + + if config.ServiceSettings.SiteURL == nil { + return nil, errors.New("please configure the Mattermost Server's SiteURL, then restart the plugin.") + } + return &Flow{ name: name, api: api, - pluginURL: pluginURL, + pluginID: pluginID, botUserID: botUserID, + siteURL: strings.TrimRight(*config.ServiceSettings.SiteURL, "/"), steps: map[Name]Step{}, - } + }, nil } func (f *Flow) WithSteps(orderedSteps ...Step) *Flow { diff --git a/server/public/pluginapi/experimental/flow/handler.go b/server/public/pluginapi/experimental/flow/handler.go index 5d5e4dac01..c3444fe754 100644 --- a/server/public/pluginapi/experimental/flow/handler.go +++ b/server/public/pluginapi/experimental/flow/handler.go @@ -162,7 +162,7 @@ func (f *Flow) handle( dialogRequest := model.OpenDialogRequest{ TriggerId: triggerID, - URL: f.pluginURL + namePath(f.name) + "/dialog", + URL: "/plugins/" + f.pluginID + namePath(f.name) + "/dialog", Dialog: processDialog(b.Dialog, state.AppState), } dialogRequest.Dialog.State = fmt.Sprintf("%v,%v", fromName, selectedButton) @@ -205,6 +205,6 @@ func (f *Flow) processButtonPostActions(post *model.Post) { if a.Integration == nil { a.Integration = &model.PostActionIntegration{} } - a.Integration.URL = f.pluginURL + namePath(f.name) + "/button" + a.Integration.URL = "/plugins/" + f.pluginID + namePath(f.name) + "/button" } } diff --git a/server/public/pluginapi/experimental/flow/step.go b/server/public/pluginapi/experimental/flow/step.go index c4b5e829c8..3f5d32e77c 100644 --- a/server/public/pluginapi/experimental/flow/step.go +++ b/server/public/pluginapi/experimental/flow/step.go @@ -189,7 +189,7 @@ func (f *Flow) processAttachment(attachment *model.SlackAttachment) *model.Slack if u.Host != "" && (u.Scheme == "http" || u.Scheme == "https") { a.ImageURL = attachment.ImageURL } else { - a.ImageURL = f.pluginURL + "/" + strings.TrimPrefix(attachment.ImageURL, "/") + a.ImageURL = fmt.Sprintf("%s/plugins/%s/%s", f.siteURL, f.pluginID, strings.TrimPrefix(attachment.ImageURL, "/")) } } }