[MM-33603] Fix nil dereference panic in (*App).CreatePost() (#17124)
* Fix possible nil dereference * fixed nil dereference for unfollowed thread Co-authored-by: Eli Yukelzon <reflog@gmail.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
cee7f8ba4e
Коммит
f0ccaa89bf
@@ -437,11 +437,16 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
|
|||||||
}
|
}
|
||||||
if sendEvent {
|
if sendEvent {
|
||||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_THREAD_UPDATED, team.Id, "", uid, nil)
|
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_THREAD_UPDATED, team.Id, "", uid, nil)
|
||||||
userThread, _ := a.Srv().Store.Thread().GetThreadForUser(uid, channel.TeamId, thread.PostId, true)
|
userThread, err := a.Srv().Store.Thread().GetThreadForUser(uid, channel.TeamId, thread.PostId, true)
|
||||||
a.sanitizeProfiles(userThread.Participants, false)
|
if err != nil {
|
||||||
userThread.Post.SanitizeProps()
|
return nil, errors.Wrapf(err, "cannot get thread %q for user %q", thread.PostId, uid)
|
||||||
message.Add("thread", userThread.ToJson())
|
}
|
||||||
a.Publish(message)
|
if userThread != nil {
|
||||||
|
a.sanitizeProfiles(userThread.Participants, false)
|
||||||
|
userThread.Post.SanitizeProps()
|
||||||
|
message.Add("thread", userThread.ToJson())
|
||||||
|
a.Publish(message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -1925,6 +1926,7 @@ func TestThreadMembership(t *testing.T) {
|
|||||||
func TestCollapsedThreadFetch(t *testing.T) {
|
func TestCollapsedThreadFetch(t *testing.T) {
|
||||||
th := Setup(t).InitBasic()
|
th := Setup(t).InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|
||||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||||
*cfg.ServiceSettings.ThreadAutoFollow = true
|
*cfg.ServiceSettings.ThreadAutoFollow = true
|
||||||
*cfg.ServiceSettings.CollapsedThreads = model.COLLAPSED_THREADS_DEFAULT_ON
|
*cfg.ServiceSettings.CollapsedThreads = model.COLLAPSED_THREADS_DEFAULT_ON
|
||||||
@@ -1969,6 +1971,45 @@ func TestCollapsedThreadFetch(t *testing.T) {
|
|||||||
require.Len(t, l.Order, 1)
|
require.Len(t, l.Order, 1)
|
||||||
require.NotEmpty(t, l.Posts[postRoot.Id].Participants[0].Email)
|
require.NotEmpty(t, l.Posts[postRoot.Id].Participants[0].Email)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("Should not panic on unexpected db error", func(t *testing.T) {
|
||||||
|
os.Setenv("MM_FEATUREFLAGS_COLLAPSEDTHREADS", "true")
|
||||||
|
defer os.Unsetenv("MM_FEATUREFLAGS_COLLAPSEDTHREADS")
|
||||||
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||||
|
cfg.FeatureFlags.CollapsedThreads = true
|
||||||
|
})
|
||||||
|
|
||||||
|
channel := th.CreateChannel(th.BasicTeam)
|
||||||
|
th.AddUserToChannel(user2, channel)
|
||||||
|
defer th.App.DeleteChannel(channel, user1.Id)
|
||||||
|
|
||||||
|
postRoot, err := th.App.CreatePost(&model.Post{
|
||||||
|
UserId: user1.Id,
|
||||||
|
ChannelId: channel.Id,
|
||||||
|
Message: "root post",
|
||||||
|
}, channel, false, true)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
// we introduce a race to trigger an unexpected error from the db side.
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
th.Server.Store.Post().PermanentDeleteByUser(user1.Id)
|
||||||
|
}()
|
||||||
|
|
||||||
|
require.NotPanics(t, func() {
|
||||||
|
_, err = th.App.CreatePost(&model.Post{
|
||||||
|
UserId: user1.Id,
|
||||||
|
ChannelId: channel.Id,
|
||||||
|
RootId: postRoot.Id,
|
||||||
|
Message: fmt.Sprintf("@%s", user2.Username),
|
||||||
|
}, channel, false, true)
|
||||||
|
require.Nil(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReplyToPostWithLag(t *testing.T) {
|
func TestReplyToPostWithLag(t *testing.T) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user