mattermost/server/channels/app/context.go
Felipe Martin 0afef7760c
Include connection ID in plugin context (#36074)
* feat: include connection id in the plugin context

* refactor: group ConnectionId next to SessionId in plugin Context

Addresses review feedback to keep related identifier fields adjacent.

* fix(files): forward Connection-Id on file uploads to plugin hooks

The webapp uploadFile XHR didn't attach the Connection-Id header, so
FileWillBeUploaded plugin hooks always received an empty ConnectionId.
Read it from the websocket selector and set it on the request, matching
how drafts and channel bookmarks already do it. Adds a server-side test
asserting the connection id propagates through pluginContext.

* fix(lint): reorder file_actions imports to satisfy import/order

* Document ConnectionId on request.Context
2026-05-11 10:09:47 +02:00

43 lines
1.5 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store/sqlstore"
)
// RequestContextWithMaster adds the context value that master DB should be selected for this request.
func RequestContextWithMaster(rctx request.CTX) request.CTX {
return sqlstore.RequestContextWithMaster(rctx)
}
// RequestContextWithCallerID adds the caller ID to a request.CTX for access control purposes.
func RequestContextWithCallerID(rctx request.CTX, callerID string) request.CTX {
ctx := model.WithCallerID(rctx.Context(), callerID)
return rctx.WithContext(ctx)
}
// CallerIDFromRequestContext extracts the caller ID from a request.CTX.
// Returns the caller ID and true if found, or empty string and false if not.
func CallerIDFromRequestContext(rctx request.CTX) (string, bool) {
if rctx == nil {
return "", false
}
return model.CallerIDFromContext(rctx.Context())
}
func pluginContext(rctx request.CTX) *plugin.Context {
context := &plugin.Context{
RequestId: rctx.RequestId(),
SessionId: rctx.Session().Id,
IPAddress: rctx.IPAddress(),
AcceptLanguage: rctx.AcceptLanguage(),
UserAgent: rctx.UserAgent(),
ConnectionId: rctx.ConnectionId(),
}
return context
}