diff --git a/app/channel.go b/app/channel.go index 273555f6b8..fdc8d17da3 100644 --- a/app/channel.go +++ b/app/channel.go @@ -343,15 +343,13 @@ func (a *App) CreateChannel(c request.CTX, channel *model.Channel, addMember boo a.InvalidateCacheForUser(channel.CreatorId) } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.ChannelHasBeenCreated(pluginContext, sc) - return true - }, plugin.ChannelHasBeenCreatedID) - }) - } + a.Srv().Go(func() { + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.ChannelHasBeenCreated(pluginContext, sc) + return true + }, plugin.ChannelHasBeenCreatedID) + }) return sc, nil } @@ -429,15 +427,13 @@ func (a *App) handleCreationEvent(c request.CTX, userID, otherUserID string, cha a.InvalidateCacheForUser(userID) a.InvalidateCacheForUser(otherUserID) - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.ChannelHasBeenCreated(pluginContext, channel) - return true - }, plugin.ChannelHasBeenCreatedID) - }) - } + a.Srv().Go(func() { + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.ChannelHasBeenCreated(pluginContext, channel) + return true + }, plugin.ChannelHasBeenCreatedID) + }) message := model.NewWebSocketEvent(model.WebsocketEventDirectAdded, "", channel.Id, "", nil, "") message.Add("creator_id", userID) @@ -1599,15 +1595,13 @@ func (a *App) AddChannelMember(c request.CTX, userID string, channel *model.Chan return nil, err } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.UserHasJoinedChannel(pluginContext, cm, userRequestor) - return true - }, plugin.UserHasJoinedChannelID) - }) - } + a.Srv().Go(func() { + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.UserHasJoinedChannel(pluginContext, cm, userRequestor) + return true + }, plugin.UserHasJoinedChannelID) + }) if opts.UserRequestorID == "" || userID == opts.UserRequestorID { if err := a.postJoinChannelMessage(c, user, channel); err != nil { @@ -2177,15 +2171,13 @@ func (a *App) JoinChannel(c request.CTX, channel *model.Channel, userID string) return err } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.UserHasJoinedChannel(pluginContext, cm, nil) - return true - }, plugin.UserHasJoinedChannelID) - }) - } + a.Srv().Go(func() { + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.UserHasJoinedChannel(pluginContext, cm, nil) + return true + }, plugin.UserHasJoinedChannelID) + }) if err := a.postJoinChannelMessage(c, user, channel); err != nil { return err @@ -2484,21 +2476,19 @@ func (a *App) removeUserFromChannel(c request.CTX, userIDToRemove string, remove a.InvalidateCacheForUser(userIDToRemove) a.invalidateCacheForChannelMembers(channel.Id) - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - var actorUser *model.User - if removerUserId != "" { - actorUser, _ = a.GetUser(removerUserId) - } - - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.UserHasLeftChannel(pluginContext, cm, actorUser) - return true - }, plugin.UserHasLeftChannelID) - }) + var actorUser *model.User + if removerUserId != "" { + actorUser, _ = a.GetUser(removerUserId) } + a.Srv().Go(func() { + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.UserHasLeftChannel(pluginContext, cm, actorUser) + return true + }, plugin.UserHasLeftChannelID) + }) + message := model.NewWebSocketEvent(model.WebsocketEventUserRemoved, "", channel.Id, "", nil, "") message.Add("user_id", userIDToRemove) message.Add("remover_id", removerUserId) diff --git a/app/channels.go b/app/channels.go index 31ef2d429c..e4a25f3749 100644 --- a/app/channels.go +++ b/app/channels.go @@ -326,7 +326,7 @@ func (s *hooksService) RegisterHooks(productID string, hooks any) error { } func (ch *Channels) RunMultiHook(hookRunnerFunc func(hooks plugin.Hooks) bool, hookId int) { - if env := ch.pluginsEnvironment; env != nil { + if env := ch.GetPluginsEnvironment(); env != nil { env.RunMultiPluginHook(hookRunnerFunc, hookId) } @@ -336,7 +336,7 @@ func (ch *Channels) RunMultiHook(hookRunnerFunc func(hooks plugin.Hooks) bool, h func (ch *Channels) HooksForPluginOrProduct(id string) (plugin.Hooks, error) { var hooks plugin.Hooks - if env := ch.pluginsEnvironment; env != nil { + if env := ch.GetPluginsEnvironment(); env != nil { // we intentionally ignore the error here, because the id can be a product id // we are going to check if we have the hooks or not hooks, _ = env.HooksForPlugin(id) diff --git a/app/file.go b/app/file.go index 67a227cab0..06a516bc86 100644 --- a/app/file.go +++ b/app/file.go @@ -895,29 +895,27 @@ func (a *App) DoUploadFileExpectModification(c request.CTX, now time.Time, rawTe info.ThumbnailPath = pathPrefix + nameWithoutExtension + "_thumb." + getFileExtFromMimeType(info.MimeType) } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - var rejectionError *model.AppError - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - var newBytes bytes.Buffer - replacementInfo, rejectionReason := hooks.FileWillBeUploaded(pluginContext, info, bytes.NewReader(data), &newBytes) - if rejectionReason != "" { - rejectionError = model.NewAppError("DoUploadFile", "File rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest) - return false - } - if replacementInfo != nil { - info = replacementInfo - } - if newBytes.Len() != 0 { - data = newBytes.Bytes() - info.Size = int64(len(data)) - } - - return true - }, plugin.FileWillBeUploadedID) - if rejectionError != nil { - return nil, data, rejectionError + var rejectionError *model.AppError + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + var newBytes bytes.Buffer + replacementInfo, rejectionReason := hooks.FileWillBeUploaded(pluginContext, info, bytes.NewReader(data), &newBytes) + if rejectionReason != "" { + rejectionError = model.NewAppError("DoUploadFile", "File rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest) + return false } + if replacementInfo != nil { + info = replacementInfo + } + if newBytes.Len() != 0 { + data = newBytes.Bytes() + info.Size = int64(len(data)) + } + + return true + }, plugin.FileWillBeUploadedID) + if rejectionError != nil { + return nil, data, rejectionError } if _, err := a.WriteFile(bytes.NewReader(data), info.Path); err != nil { diff --git a/app/login.go b/app/login.go index 98279d7f2f..e0854bef37 100644 --- a/app/login.go +++ b/app/login.go @@ -157,17 +157,15 @@ func (a *App) GetUserForLogin(id, loginId string) (*model.User, *model.AppError) } func (a *App) DoLogin(c *request.Context, w http.ResponseWriter, r *http.Request, user *model.User, deviceID string, isMobile, isOAuthUser, isSaml bool) *model.AppError { - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - var rejectionReason string - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - rejectionReason = hooks.UserWillLogIn(pluginContext, user) - return rejectionReason == "" - }, plugin.UserWillLogInID) + var rejectionReason string + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + rejectionReason = hooks.UserWillLogIn(pluginContext, user) + return rejectionReason == "" + }, plugin.UserWillLogInID) - if rejectionReason != "" { - return model.NewAppError("DoLogin", "Login rejected by plugin: "+rejectionReason, nil, "", http.StatusBadRequest) - } + if rejectionReason != "" { + return model.NewAppError("DoLogin", "Login rejected by plugin: "+rejectionReason, nil, "", http.StatusBadRequest) } session := &model.Session{UserId: user.Id, Roles: user.GetRawRoles(), DeviceId: deviceID, IsOAuth: false, Props: map[string]string{ @@ -226,15 +224,12 @@ func (a *App) DoLogin(c *request.Context, w http.ResponseWriter, r *http.Request }) } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.UserHasLoggedIn(pluginContext, user) - return true - }, plugin.UserHasLoggedInID) - }) - } + a.Srv().Go(func() { + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.UserHasLoggedIn(pluginContext, user) + return true + }, plugin.UserHasLoggedInID) + }) return nil } diff --git a/app/plugin.go b/app/plugin.go index 7ce6c47fe6..679576e723 100644 --- a/app/plugin.go +++ b/app/plugin.go @@ -286,14 +286,13 @@ func (ch *Channels) initPlugins(c *request.Context, pluginDir, webappPluginDir s ch.installFeatureFlagPlugins() ch.syncPluginsActiveState() } - if pluginsEnvironment := ch.GetPluginsEnvironment(); pluginsEnvironment != nil { - ch.RunMultiHook(func(hooks plugin.Hooks) bool { - if err := hooks.OnConfigurationChange(); err != nil { - ch.srv.Log().Error("Plugin OnConfigurationChange hook failed", mlog.Err(err)) - } - return true - }, plugin.OnConfigurationChangeID) - } + + ch.RunMultiHook(func(hooks plugin.Hooks) bool { + if err := hooks.OnConfigurationChange(); err != nil { + ch.srv.Log().Error("Plugin OnConfigurationChange hook failed", mlog.Err(err)) + } + return true + }, plugin.OnConfigurationChangeID) }) ch.pluginsLock.Unlock() diff --git a/app/post.go b/app/post.go index ace950239e..a0daea37c6 100644 --- a/app/post.go +++ b/app/post.go @@ -263,38 +263,36 @@ func (a *App) CreatePost(c request.CTX, post *model.Post, channel *model.Channel post.Metadata.Priority = nil } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - var metadata *model.PostMetadata - if post.Metadata != nil { - metadata = post.Metadata.Copy() - } - var rejectionError *model.AppError - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - replacementPost, rejectionReason := hooks.MessageWillBePosted(pluginContext, post.ForPlugin()) - if rejectionReason != "" { - id := "Post rejected by plugin. " + rejectionReason - if rejectionReason == plugin.DismissPostError { - id = plugin.DismissPostError - } - rejectionError = model.NewAppError("createPost", id, nil, "", http.StatusBadRequest) - return false + var metadata *model.PostMetadata + if post.Metadata != nil { + metadata = post.Metadata.Copy() + } + var rejectionError *model.AppError + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + replacementPost, rejectionReason := hooks.MessageWillBePosted(pluginContext, post.ForPlugin()) + if rejectionReason != "" { + id := "Post rejected by plugin. " + rejectionReason + if rejectionReason == plugin.DismissPostError { + id = plugin.DismissPostError } - if replacementPost != nil { - post = replacementPost - if post.Metadata != nil && metadata != nil { - post.Metadata.Priority = metadata.Priority - } else { - post.Metadata = metadata - } - } - - return true - }, plugin.MessageWillBePostedID) - - if rejectionError != nil { - return nil, rejectionError + rejectionError = model.NewAppError("createPost", id, nil, "", http.StatusBadRequest) + return false } + if replacementPost != nil { + post = replacementPost + if post.Metadata != nil && metadata != nil { + post.Metadata.Priority = metadata.Priority + } else { + post.Metadata = metadata + } + } + + return true + }, plugin.MessageWillBePostedID) + + if rejectionError != nil { + return nil, rejectionError } // Pre-fill the CreateAt field for link previews to get the correct timestamp. @@ -328,16 +326,13 @@ func (a *App) CreatePost(c request.CTX, post *model.Post, channel *model.Channel // We make a copy of the post for the plugin hook to avoid a race condition, // and to remove the non-GOB-encodable Metadata from it. - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - pluginPost := rpost.ForPlugin() - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.MessageHasBeenPosted(pluginContext, pluginPost) - return true - }, plugin.MessageHasBeenPostedID) - }) - } + pluginPost := rpost.ForPlugin() + a.Srv().Go(func() { + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.MessageHasBeenPosted(pluginContext, pluginPost) + return true + }, plugin.MessageHasBeenPostedID) + }) if a.Metrics() != nil { a.Metrics().IncrementPostCreate() @@ -658,20 +653,18 @@ func (a *App) UpdatePost(c *request.Context, post *model.Post, safeUpdate bool) oldPost.RemoteId = model.NewString(*post.RemoteId) } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - var rejectionReason string - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - newPost, rejectionReason = hooks.MessageWillBeUpdated(pluginContext, newPost.ForPlugin(), oldPost.ForPlugin()) - return post != nil - }, plugin.MessageWillBeUpdatedID) - if newPost == nil { - return nil, model.NewAppError("UpdatePost", "Post rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest) - } - // Restore the post metadata that was stripped by the plugin. Set it to - // the last known good. - newPost.Metadata = oldPost.Metadata + var rejectionReason string + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + newPost, rejectionReason = hooks.MessageWillBeUpdated(pluginContext, newPost.ForPlugin(), oldPost.ForPlugin()) + return post != nil + }, plugin.MessageWillBeUpdatedID) + if newPost == nil { + return nil, model.NewAppError("UpdatePost", "Post rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest) } + // Restore the post metadata that was stripped by the plugin. Set it to + // the last known good. + newPost.Metadata = oldPost.Metadata rpost, nErr := a.Srv().Store().Post().Update(newPost, oldPost) if nErr != nil { @@ -684,17 +677,14 @@ func (a *App) UpdatePost(c *request.Context, post *model.Post, safeUpdate bool) } } - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - pluginOldPost := oldPost.ForPlugin() - pluginNewPost := newPost.ForPlugin() - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.MessageHasBeenUpdated(pluginContext, pluginNewPost, pluginOldPost) - return true - }, plugin.MessageHasBeenUpdatedID) - }) - } + pluginOldPost := oldPost.ForPlugin() + pluginNewPost := newPost.ForPlugin() + a.Srv().Go(func() { + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.MessageHasBeenUpdated(pluginContext, pluginNewPost, pluginOldPost) + return true + }, plugin.MessageHasBeenUpdatedID) + }) rpost = a.PreparePostForClientWithEmbedsAndImages(c, rpost, false, true, true) diff --git a/app/reaction.go b/app/reaction.go index 80bc24b4b4..fc6d54699f 100644 --- a/app/reaction.go +++ b/app/reaction.go @@ -43,15 +43,13 @@ func (a *App) SaveReactionForPost(c *request.Context, reaction *model.Reaction) // The post is always modified since the UpdateAt always changes a.invalidateCacheForChannelPosts(post.ChannelId) - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.ReactionHasBeenAdded(pluginContext, reaction) - return true - }, plugin.ReactionHasBeenAddedID) - }) - } + pluginContext := pluginContext(c) + a.Srv().Go(func() { + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.ReactionHasBeenAdded(pluginContext, reaction) + return true + }, plugin.ReactionHasBeenAddedID) + }) a.Srv().Go(func() { a.sendReactionEvent(model.WebsocketEventReactionAdded, reaction, post) @@ -142,15 +140,13 @@ func (a *App) DeleteReactionForPost(c *request.Context, reaction *model.Reaction // The post is always modified since the UpdateAt always changes a.invalidateCacheForChannelPosts(post.ChannelId) - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.ReactionHasBeenRemoved(pluginContext, reaction) - return true - }, plugin.ReactionHasBeenRemovedID) - }) - } + pluginContext := pluginContext(c) + a.Srv().Go(func() { + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.ReactionHasBeenRemoved(pluginContext, reaction) + return true + }, plugin.ReactionHasBeenRemovedID) + }) a.Srv().Go(func() { a.sendReactionEvent(model.WebsocketEventReactionRemoved, reaction, post) diff --git a/app/team.go b/app/team.go index 4376e2d77a..222d10e442 100644 --- a/app/team.go +++ b/app/team.go @@ -846,21 +846,19 @@ func (a *App) JoinUserToTeam(c request.CTX, team *model.Team, user *model.User, a.InvalidateCacheForUser(user.Id) a.invalidateCacheForUserTeams(user.Id) - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - var actor *model.User - if userRequestorId != "" { - actor, _ = a.GetUser(userRequestorId) - } - - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.UserHasJoinedTeam(pluginContext, teamMember, actor) - return true - }, plugin.UserHasJoinedTeamID) - }) + var actor *model.User + if userRequestorId != "" { + actor, _ = a.GetUser(userRequestorId) } + a.Srv().Go(func() { + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.UserHasJoinedTeam(pluginContext, teamMember, actor) + return true + }, plugin.UserHasJoinedTeamID) + }) + message := model.NewWebSocketEvent(model.WebsocketEventAddedToTeam, "", "", user.Id, nil, "") message.Add("team_id", team.Id) message.Add("user_id", user.Id) @@ -1220,21 +1218,19 @@ func (a *App) RemoveUserFromTeam(c request.CTX, teamID string, userID string, re } func (a *App) postProcessTeamMemberLeave(c request.CTX, teamMember *model.TeamMember, requestorId string) *model.AppError { - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - var actor *model.User - if requestorId != "" { - actor, _ = a.GetUser(requestorId) - } - - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.UserHasLeftTeam(pluginContext, teamMember, actor) - return true - }, plugin.UserHasLeftTeamID) - }) + var actor *model.User + if requestorId != "" { + actor, _ = a.GetUser(requestorId) } + a.Srv().Go(func() { + pluginContext := pluginContext(c) + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.UserHasLeftTeam(pluginContext, teamMember, actor) + return true + }, plugin.UserHasLeftTeamID) + }) + user, nErr := a.Srv().Store().User().Get(context.Background(), teamMember.UserId) if nErr != nil { var nfErr *store.ErrNotFound diff --git a/app/upload.go b/app/upload.go index b63725b135..4da0b853b6 100644 --- a/app/upload.go +++ b/app/upload.go @@ -49,11 +49,6 @@ func (a *App) genFileInfoFromReader(name string, file io.ReadSeeker, size int64) } func (a *App) runPluginsHook(c *request.Context, info *model.FileInfo, file io.Reader) *model.AppError { - pluginsEnvironment := a.GetPluginsEnvironment() - if pluginsEnvironment == nil { - return nil - } - filePath := info.Path // using a pipe to avoid loading the whole file content in memory. r, w := io.Pipe() diff --git a/app/user.go b/app/user.go index 953446071d..a2f72eddb5 100644 --- a/app/user.go +++ b/app/user.go @@ -308,15 +308,13 @@ func (a *App) createUserOrGuest(c request.CTX, user *model.User, guest bool) (*m message.Add("user_id", ruser.Id) a.Publish(message) - if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { - a.Srv().Go(func() { - pluginContext := pluginContext(c) - a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { - hooks.UserHasBeenCreated(pluginContext, ruser) - return true - }, plugin.UserHasBeenCreatedID) - }) - } + pluginContext := pluginContext(c) + a.Srv().Go(func() { + a.ch.RunMultiHook(func(hooks plugin.Hooks) bool { + hooks.UserHasBeenCreated(pluginContext, ruser) + return true + }, plugin.UserHasBeenCreatedID) + }) _, cwsErr := a.SendSubscriptionHistoryEvent(ruser.Id) if cwsErr != nil {