diff --git a/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/posts.ts b/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/posts.ts index 35d3c99ee6..94be7a741d 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/posts.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/posts.ts @@ -336,7 +336,7 @@ export function makeGetPostsAroundPost(): (state: GlobalState, postId: Post['id' for (let i = 0; i < postIds.length; i++) { const post = allPosts[postIds[i]]; - if (shouldFilterJoinLeavePost(post, showJoinLeave, currentUser.username)) { + if (!post || shouldFilterJoinLeavePost(post, showJoinLeave, currentUser.username)) { continue; } @@ -370,15 +370,19 @@ export function makeGetPostsForThread(): (state: GlobalState, rootId: string) => thread.push(rootPost); } - postsForThread?.forEach((id) => { - const post = posts[id]; + if (postsForThread && Array.isArray(postsForThread) && postsForThread.length > 0) { + for (const postId of postsForThread) { + const post = posts[postId]; + if (!post) { + continue; + } - const skip = shouldFilterJoinLeavePost(post, showJoinLeave, currentUser ? currentUser.username : ''); - - if (post && !skip) { - thread.push(post); + const skip = shouldFilterJoinLeavePost(post, showJoinLeave, currentUser ? currentUser.username : ''); + if (!skip) { + thread.push(post); + } } - }); + } thread.sort(comparePosts); return thread; diff --git a/webapp/channels/src/packages/mattermost-redux/src/utils/post_utils.ts b/webapp/channels/src/packages/mattermost-redux/src/utils/post_utils.ts index b6a7161ec3..77fea035cf 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/utils/post_utils.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/utils/post_utils.ts @@ -106,7 +106,11 @@ const joinLeavePostTypes = [ Posts.POST_TYPES.COMBINED_USER_ACTIVITY, ]; -// Returns true if a post should be hidden when the user has Show Join/Leave Messages disabled +/** + * If the user has "Show Join/Leave Messages" disabled, this function will return true if the post should be hidden if it's of type join/leave. + * The post object passed in must be not null/undefined. + * @returns Returns true if a post should be hidden + */ export function shouldFilterJoinLeavePost(post: Post, showJoinLeave: boolean, currentUsername: string): boolean { if (showJoinLeave) { return false;