* add request context

* move initialialization to server

* use app interface instead of global app functions

* remove app context from webconn

* cleanup

* remove duplicated services

* move context to separate package

* remove finalize init method and move content to NewServer function

* restart workers and schedulers after adding license for tests

* reflect review comments

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2021-05-11 13:00:44 +03:00
коммит произвёл GitHub
родитель c09369f14a
Коммит 5ea06e51d0
235 изменённых файлов: 4048 добавлений и 3819 удалений

Просмотреть файл

@@ -13,6 +13,7 @@ import (
"sync"
"time"
"github.com/mattermost/mattermost-server/v5/app/request"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/mattermost/mattermost-server/v5/services/cache"
@@ -28,7 +29,7 @@ const (
PageDefault = 0
)
func (a *App) CreatePostAsUser(post *model.Post, currentSessionId string, setOnline bool) (*model.Post, *model.AppError) {
func (a *App) CreatePostAsUser(c *request.Context, post *model.Post, currentSessionId string, setOnline bool) (*model.Post, *model.AppError) {
// Check that channel has not been deleted
channel, errCh := a.Srv().Store.Channel().Get(post.ChannelId, true)
if errCh != nil {
@@ -46,7 +47,7 @@ func (a *App) CreatePostAsUser(post *model.Post, currentSessionId string, setOnl
return nil, err
}
rp, err := a.CreatePost(post, channel, true, setOnline)
rp, err := a.CreatePost(c, post, channel, true, setOnline)
if err != nil {
if err.Id == "api.post.create_post.root_id.app_error" ||
err.Id == "api.post.create_post.channel_root_id.app_error" ||
@@ -100,7 +101,7 @@ func (a *App) CreatePostAsUser(post *model.Post, currentSessionId string, setOnl
return rp, nil
}
func (a *App) CreatePostMissingChannel(post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) {
func (a *App) CreatePostMissingChannel(c *request.Context, post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) {
channel, err := a.Srv().Store.Channel().Get(post.ChannelId, true)
if err != nil {
var nfErr *store.ErrNotFound
@@ -112,7 +113,7 @@ func (a *App) CreatePostMissingChannel(post *model.Post, triggerWebhooks bool) (
}
}
return a.CreatePost(post, channel, triggerWebhooks, true)
return a.CreatePost(c, post, channel, triggerWebhooks, true)
}
// deduplicateCreatePost attempts to make posting idempotent within a caching window.
@@ -157,7 +158,7 @@ func (a *App) deduplicateCreatePost(post *model.Post) (foundPost *model.Post, er
return actualPost, nil
}
func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhooks, setOnline bool) (savedPost *model.Post, err *model.AppError) {
func (a *App) CreatePost(c *request.Context, post *model.Post, channel *model.Channel, triggerWebhooks, setOnline bool) (savedPost *model.Post, err *model.AppError) {
foundPost, err := a.deduplicateCreatePost(post)
if err != nil {
return nil, err
@@ -281,7 +282,7 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil {
var rejectionError *model.AppError
pluginContext := a.PluginContext()
pluginContext := pluginContext(c)
pluginsEnvironment.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
replacementPost, rejectionReason := hooks.MessageWillBePosted(pluginContext, post)
if rejectionReason != "" {
@@ -326,7 +327,7 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
rPostCopy := rpost.Clone()
if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil {
a.Srv().Go(func() {
pluginContext := a.PluginContext()
pluginContext := pluginContext(c)
pluginsEnvironment.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
hooks.MessageHasBeenPosted(pluginContext, rPostCopy)
return true
@@ -352,7 +353,7 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
// to be done when we send the post over the websocket in handlePostEvents
rpost = a.PreparePostForClient(rpost, true, false)
if err := a.handlePostEvents(rpost, user, channel, triggerWebhooks, parentPostList, setOnline); err != nil {
if err := a.handlePostEvents(c, rpost, user, channel, triggerWebhooks, parentPostList, setOnline); err != nil {
mlog.Warn("Failed to handle post events", mlog.Err(err))
}
@@ -439,7 +440,7 @@ func (a *App) FillInPostProps(post *model.Post, channel *model.Channel) *model.A
return nil
}
func (a *App) handlePostEvents(post *model.Post, user *model.User, channel *model.Channel, triggerWebhooks bool, parentPostList *model.PostList, setOnline bool) error {
func (a *App) handlePostEvents(c *request.Context, post *model.Post, user *model.User, channel *model.Channel, triggerWebhooks bool, parentPostList *model.PostList, setOnline bool) error {
var team *model.Team
if channel.TeamId != "" {
t, err := a.Srv().Store.Team().Get(channel.TeamId)
@@ -461,7 +462,7 @@ func (a *App) handlePostEvents(post *model.Post, user *model.User, channel *mode
if post.Type != model.POST_AUTO_RESPONDER { // don't respond to an auto-responder
a.Srv().Go(func() {
_, err := a.SendAutoResponseIfNecessary(channel, user, post)
_, err := a.SendAutoResponseIfNecessary(c, channel, user, post)
if err != nil {
mlog.Error("Failed to send auto response", mlog.String("user_id", user.Id), mlog.String("post_id", post.Id), mlog.Err(err))
}
@@ -470,7 +471,7 @@ func (a *App) handlePostEvents(post *model.Post, user *model.User, channel *mode
if triggerWebhooks {
a.Srv().Go(func() {
if err := a.handleWebhookEvents(post, team, channel, user); err != nil {
if err := a.handleWebhookEvents(c, post, team, channel, user); err != nil {
mlog.Error(err.Error())
}
})
@@ -535,7 +536,7 @@ func (a *App) DeleteEphemeralPost(userID, postID string) {
a.Publish(message)
}
func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model.AppError) {
func (a *App) UpdatePost(c *request.Context, post *model.Post, safeUpdate bool) (*model.Post, *model.AppError) {
post.SanitizeProps()
postLists, nErr := a.Srv().Store.Post().Get(context.Background(), post.Id, false, false, false, "")
@@ -615,7 +616,7 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model
if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil {
var rejectionReason string
pluginContext := a.PluginContext()
pluginContext := pluginContext(c)
pluginsEnvironment.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
newPost, rejectionReason = hooks.MessageWillBeUpdated(pluginContext, newPost, oldPost)
return post != nil
@@ -638,7 +639,7 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model
if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil {
a.Srv().Go(func() {
pluginContext := a.PluginContext()
pluginContext := pluginContext(c)
pluginsEnvironment.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
hooks.MessageHasBeenUpdated(pluginContext, newPost, oldPost)
return true
@@ -657,7 +658,7 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model
return rpost, nil
}
func (a *App) PatchPost(postID string, patch *model.PostPatch) (*model.Post, *model.AppError) {
func (a *App) PatchPost(c *request.Context, postID string, patch *model.PostPatch) (*model.Post, *model.AppError) {
post, err := a.GetSinglePost(postID)
if err != nil {
return nil, err
@@ -679,7 +680,7 @@ func (a *App) PatchPost(postID string, patch *model.PostPatch) (*model.Post, *mo
post.Patch(patch)
updatedPost, err := a.UpdatePost(post, false)
updatedPost, err := a.UpdatePost(c, post, false)
if err != nil {
return nil, err
}
@@ -790,7 +791,7 @@ func (a *App) GetFlaggedPostsForChannel(userID, channelID string, offset int, li
return postList, nil
}
func (a *App) GetPermalinkPost(postID string, userID string) (*model.PostList, *model.AppError) {
func (a *App) GetPermalinkPost(c *request.Context, postID string, userID string) (*model.PostList, *model.AppError) {
list, nErr := a.Srv().Store.Post().Get(context.Background(), postID, false, false, false, userID)
if nErr != nil {
var nfErr *store.ErrNotFound
@@ -815,7 +816,7 @@ func (a *App) GetPermalinkPost(postID string, userID string) (*model.PostList, *
return nil, err
}
if err = a.JoinChannel(channel, userID); err != nil {
if err = a.JoinChannel(c, channel, userID); err != nil {
return nil, err
}
@@ -1082,7 +1083,7 @@ func (a *App) DeletePostFiles(post *model.Post) {
}
}
func (a *App) parseAndFetchChannelIdByNameFromInFilter(channelName, userID, teamID string, includeDeleted bool) (*model.Channel, error) {
func (a *App) parseAndFetchChannelIdByNameFromInFilter(c *request.Context, channelName, userID, teamID string, includeDeleted bool) (*model.Channel, error) {
if strings.HasPrefix(channelName, "@") && strings.Contains(channelName, ",") {
var userIDs []string
users, err := a.GetUsersByUsernames(strings.Split(channelName[1:], ","), false, nil)
@@ -1105,7 +1106,7 @@ func (a *App) parseAndFetchChannelIdByNameFromInFilter(channelName, userID, team
if err != nil {
return nil, err
}
channel, err := a.GetOrCreateDirectChannel(userID, user.Id)
channel, err := a.GetOrCreateDirectChannel(c, userID, user.Id)
if err != nil {
return nil, err
}
@@ -1156,9 +1157,9 @@ func (a *App) searchPostsInTeam(teamID string, userID string, paramsList []*mode
return posts, nil
}
func (a *App) convertChannelNamesToChannelIds(channels []string, userID string, teamID string, includeDeletedChannels bool) []string {
func (a *App) convertChannelNamesToChannelIds(c *request.Context, channels []string, userID string, teamID string, includeDeletedChannels bool) []string {
for idx, channelName := range channels {
channel, err := a.parseAndFetchChannelIdByNameFromInFilter(channelName, userID, teamID, includeDeletedChannels)
channel, err := a.parseAndFetchChannelIdByNameFromInFilter(c, channelName, userID, teamID, includeDeletedChannels)
if err != nil {
mlog.Warn("error getting channel id by name from in filter", mlog.Err(err))
continue
@@ -1189,7 +1190,7 @@ func (a *App) SearchPostsInTeam(teamID string, paramsList []*model.SearchParams)
})
}
func (a *App) SearchPostsInTeamForUser(terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) {
func (a *App) SearchPostsInTeamForUser(c *request.Context, terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) {
var postSearchResults *model.PostSearchResults
paramsList := model.ParseSearchParams(strings.TrimSpace(terms), timeZoneOffset)
includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels
@@ -1206,8 +1207,8 @@ func (a *App) SearchPostsInTeamForUser(terms string, userID string, teamID strin
// Don't allow users to search for "*"
if params.Terms != "*" {
// Convert channel names to channel IDs
params.InChannels = a.convertChannelNamesToChannelIds(params.InChannels, userID, teamID, includeDeletedChannels)
params.ExcludedChannels = a.convertChannelNamesToChannelIds(params.ExcludedChannels, userID, teamID, includeDeletedChannels)
params.InChannels = a.convertChannelNamesToChannelIds(c, params.InChannels, userID, teamID, includeDeletedChannels)
params.ExcludedChannels = a.convertChannelNamesToChannelIds(c, params.ExcludedChannels, userID, teamID, includeDeletedChannels)
// Convert usernames to user IDs
params.FromUsers = a.convertUserNameToUserIds(params.FromUsers)