Reduce number of calls to IsCRTEnabled (#23963)

In a single createPost flow, there were
3 separate calls to App.IsCRTEnabled. This
showed up very slightly in the CPU profiles.

Not a big deal, but good to get it out of
the way.

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2023-07-10 10:18:55 +05:30
коммит произвёл GitHub
родитель b5a3eee739
Коммит 49e1d039f5
6 изменённых файлов: 15 добавлений и 14 удалений

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

@@ -921,7 +921,7 @@ type AppIface interface {
Log() *mlog.Logger Log() *mlog.Logger
LoginByOAuth(c *request.Context, service string, userData io.Reader, teamID string, tokenUser *model.User) (*model.User, *model.AppError) LoginByOAuth(c *request.Context, service string, userData io.Reader, teamID string, tokenUser *model.User) (*model.User, *model.AppError)
MakePermissionError(s *model.Session, permissions []*model.Permission) *model.AppError MakePermissionError(s *model.Session, permissions []*model.Permission) *model.AppError
MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID string, currentSessionId string, collapsedThreadsSupported bool) (map[string]int64, *model.AppError) MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID string, currentSessionId string, collapsedThreadsSupported, isCRTEnabled bool) (map[string]int64, *model.AppError)
MaxPostSize() int MaxPostSize() int
MessageExport() einterfaces.MessageExportInterface MessageExport() einterfaces.MessageExportInterface
Metrics() einterfaces.MetricsInterface Metrics() einterfaces.MetricsInterface

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

@@ -2953,7 +2953,7 @@ func (a *App) SearchChannelsUserNotIn(c request.CTX, teamID string, userID strin
return channelList, nil return channelList, nil
} }
func (a *App) MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID string, currentSessionId string, collapsedThreadsSupported bool) (map[string]int64, *model.AppError) { func (a *App) MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID string, currentSessionId string, collapsedThreadsSupported, isCRTEnabled bool) (map[string]int64, *model.AppError) {
// I start looking for channels with notifications before I mark it as read, to clear the push notifications if needed // I start looking for channels with notifications before I mark it as read, to clear the push notifications if needed
channelsToClearPushNotifications := []string{} channelsToClearPushNotifications := []string{}
if a.canSendPushNotifications() { if a.canSendPushNotifications() {
@@ -2996,7 +2996,7 @@ func (a *App) MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID st
} }
var err error var err error
updateThreads := *a.Config().ServiceSettings.ThreadAutoFollow && (!collapsedThreadsSupported || !a.IsCRTEnabledForUser(c, userID)) updateThreads := *a.Config().ServiceSettings.ThreadAutoFollow && (!collapsedThreadsSupported || !isCRTEnabled)
if updateThreads { if updateThreads {
err = a.Srv().Store().Thread().MarkAllAsReadByChannels(userID, channelIDs) err = a.Srv().Store().Thread().MarkAllAsReadByChannels(userID, channelIDs)
if err != nil { if err != nil {
@@ -3026,7 +3026,7 @@ func (a *App) MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID st
a.clearPushNotification(currentSessionId, userID, channelID, "") a.clearPushNotification(currentSessionId, userID, channelID, "")
} }
if updateThreads && a.IsCRTEnabledForUser(c, userID) { if updateThreads && isCRTEnabled {
timestamp := model.GetMillis() timestamp := model.GetMillis()
for _, channelID := range channelIDs { for _, channelID := range channelIDs {
message := model.NewWebSocketEvent(model.WebsocketEventThreadReadChanged, "", channelID, userID, nil, "") message := model.NewWebSocketEvent(model.WebsocketEventThreadReadChanged, "", channelID, userID, nil, "")
@@ -3057,7 +3057,7 @@ func (a *App) ViewChannel(c request.CTX, view *model.ChannelView, userID string,
return map[string]int64{}, nil return map[string]int64{}, nil
} }
return a.MarkChannelsAsViewed(c, channelIDs, userID, currentSessionId, collapsedThreadsSupported) return a.MarkChannelsAsViewed(c, channelIDs, userID, currentSessionId, collapsedThreadsSupported, a.IsCRTEnabledForUser(c, userID))
} }
func (a *App) PermanentDeleteChannel(c request.CTX, channel *model.Channel) *model.AppError { func (a *App) PermanentDeleteChannel(c request.CTX, channel *model.Channel) *model.AppError {

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

@@ -1422,9 +1422,9 @@ func TestMarkChannelAsUnreadFromPost(t *testing.T) {
unread, err = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) unread, err = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id)
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, int64(4), unread.MsgCount) require.Equal(t, int64(4), unread.MsgCount)
_, err = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u1.Id, "", false) _, err = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u1.Id, "", false, false)
require.Nil(t, err) require.Nil(t, err)
_, err = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u2.Id, "", false) _, err = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u2.Id, "", false, false)
require.Nil(t, err) require.Nil(t, err)
unread, err = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) unread, err = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id)
require.Nil(t, err) require.Nil(t, err)
@@ -2146,7 +2146,7 @@ func TestMarkChannelsAsViewedPanic(t *testing.T) {
mockThreadStore.On("MarkAllAsReadByChannels", "userID", []string{"channelID"}).Return(nil) mockThreadStore.On("MarkAllAsReadByChannels", "userID", []string{"channelID"}).Return(nil)
mockStore.On("Thread").Return(&mockThreadStore) mockStore.On("Thread").Return(&mockThreadStore)
_, appErr := th.App.MarkChannelsAsViewed(th.Context, []string{"channelID"}, "userID", th.Context.Session().Id, false) _, appErr := th.App.MarkChannelsAsViewed(th.Context, []string{"channelID"}, "userID", th.Context.Session().Id, false, false)
require.Nil(t, appErr) require.Nil(t, appErr)
} }
@@ -2256,7 +2256,7 @@ func TestViewChannelCollapsedThreadsTurnedOff(t *testing.T) {
require.Truef(t, found, "did not find created thread in user's threads") require.Truef(t, found, "did not find created thread in user's threads")
// Mark channel as read from a client that supports CRT // Mark channel as read from a client that supports CRT
_, appErr = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id}, u1.Id, th.Context.Session().Id, true) _, appErr = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id}, u1.Id, th.Context.Session().Id, true, th.App.IsCRTEnabledForUser(th.Context, u1.Id))
require.Nil(t, appErr) require.Nil(t, appErr)
// Thread should be marked as read because CRT has been turned off by user // Thread should be marked as read because CRT has been turned off by user

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

@@ -93,7 +93,7 @@ func TestExportUserChannels(t *testing.T) {
Value: "true", Value: "true",
} }
_, appErr := th.App.MarkChannelsAsViewed(th.Context, []string{th.BasicPost.ChannelId}, user.Id, "", true) _, appErr := th.App.MarkChannelsAsViewed(th.Context, []string{th.BasicPost.ChannelId}, user.Id, "", true, th.App.IsCRTEnabledForUser(th.Context, user.Id))
require.Nil(t, appErr) require.Nil(t, appErr)
var preferences model.Preferences var preferences model.Preferences

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

@@ -12594,7 +12594,7 @@ func (a *OpenTracingAppLayer) MarkChannelAsUnreadFromPost(c request.CTX, postID
return resultVar0, resultVar1 return resultVar0, resultVar1
} }
func (a *OpenTracingAppLayer) MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID string, currentSessionId string, collapsedThreadsSupported bool) (map[string]int64, *model.AppError) { func (a *OpenTracingAppLayer) MarkChannelsAsViewed(c request.CTX, channelIDs []string, userID string, currentSessionId string, collapsedThreadsSupported bool, isCRTEnabled bool) (map[string]int64, *model.AppError) {
origCtx := a.ctx origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.MarkChannelsAsViewed") span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.MarkChannelsAsViewed")
@@ -12606,7 +12606,7 @@ func (a *OpenTracingAppLayer) MarkChannelsAsViewed(c request.CTX, channelIDs []s
}() }()
defer span.Finish() defer span.Finish()
resultVar0, resultVar1 := a.app.MarkChannelsAsViewed(c, channelIDs, userID, currentSessionId, collapsedThreadsSupported) resultVar0, resultVar1 := a.app.MarkChannelsAsViewed(c, channelIDs, userID, currentSessionId, collapsedThreadsSupported, isCRTEnabled)
if resultVar1 != nil { if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1)) span.LogFields(spanlog.Error(resultVar1))

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

@@ -100,9 +100,10 @@ func (a *App) CreatePostAsUser(c request.CTX, post *model.Post, currentSessionId
// the post is NOT a reply post with CRT enabled // the post is NOT a reply post with CRT enabled
_, fromWebhook := post.GetProps()["from_webhook"] _, fromWebhook := post.GetProps()["from_webhook"]
_, fromBot := post.GetProps()["from_bot"] _, fromBot := post.GetProps()["from_bot"]
isCRTReply := post.RootId != "" && a.IsCRTEnabledForUser(c, post.UserId) isCRTEnabled := a.IsCRTEnabledForUser(c, post.UserId)
isCRTReply := post.RootId != "" && isCRTEnabled
if !fromWebhook && !fromBot && !isCRTReply { if !fromWebhook && !fromBot && !isCRTReply {
if _, err := a.MarkChannelsAsViewed(c, []string{post.ChannelId}, post.UserId, currentSessionId, true); err != nil { if _, err := a.MarkChannelsAsViewed(c, []string{post.ChannelId}, post.UserId, currentSessionId, true, isCRTEnabled); err != nil {
c.Logger().Warn( c.Logger().Warn(
"Encountered error updating last viewed", "Encountered error updating last viewed",
mlog.String("channel_id", post.ChannelId), mlog.String("channel_id", post.ChannelId),