[MM-54994] Use local requests instead of HTTP requests in the flow library (#26471)

This commit is contained in:
Ben Schumacher 2024-04-04 10:28:31 +02:00 committed by GitHub
parent 3efb25667d
commit e3d86984cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 7 deletions

View File

@ -27,9 +27,11 @@ type Flow struct {
name Name name Name
api *pluginapi.Client api *pluginapi.Client
pluginURL string pluginID string
botUserID string botUserID string
siteURL string
steps map[Name]Step steps map[Name]Step
index []Name index []Name
done func(userID string, state State) error 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. // NewFlow creates a new flow using direct messages with the user.
// //
// name must be a unique identifier for the flow within the plugin. // 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{ return &Flow{
name: name, name: name,
api: api, api: api,
pluginURL: pluginURL, pluginID: pluginID,
botUserID: botUserID, botUserID: botUserID,
siteURL: strings.TrimRight(*config.ServiceSettings.SiteURL, "/"),
steps: map[Name]Step{}, steps: map[Name]Step{},
} }, nil
} }
func (f *Flow) WithSteps(orderedSteps ...Step) *Flow { func (f *Flow) WithSteps(orderedSteps ...Step) *Flow {

View File

@ -162,7 +162,7 @@ func (f *Flow) handle(
dialogRequest := model.OpenDialogRequest{ dialogRequest := model.OpenDialogRequest{
TriggerId: triggerID, TriggerId: triggerID,
URL: f.pluginURL + namePath(f.name) + "/dialog", URL: "/plugins/" + f.pluginID + namePath(f.name) + "/dialog",
Dialog: processDialog(b.Dialog, state.AppState), Dialog: processDialog(b.Dialog, state.AppState),
} }
dialogRequest.Dialog.State = fmt.Sprintf("%v,%v", fromName, selectedButton) dialogRequest.Dialog.State = fmt.Sprintf("%v,%v", fromName, selectedButton)
@ -205,6 +205,6 @@ func (f *Flow) processButtonPostActions(post *model.Post) {
if a.Integration == nil { if a.Integration == nil {
a.Integration = &model.PostActionIntegration{} a.Integration = &model.PostActionIntegration{}
} }
a.Integration.URL = f.pluginURL + namePath(f.name) + "/button" a.Integration.URL = "/plugins/" + f.pluginID + namePath(f.name) + "/button"
} }
} }

View File

@ -189,7 +189,7 @@ func (f *Flow) processAttachment(attachment *model.SlackAttachment) *model.Slack
if u.Host != "" && (u.Scheme == "http" || u.Scheme == "https") { if u.Host != "" && (u.Scheme == "http" || u.Scheme == "https") {
a.ImageURL = attachment.ImageURL a.ImageURL = attachment.ImageURL
} else { } 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, "/"))
} }
} }
} }