From 14fcc8a22e05efdb9e535e49257da80a9228507e Mon Sep 17 00:00:00 2001 From: Kyriakos Z <3829551+koox00@users.noreply.github.com> Date: Tue, 30 May 2023 22:33:19 +0300 Subject: [PATCH] MM-52513: fixes deleting a reply (#23177) * MM-52513: fixes deleting a reply Currently when we receive a WS event for a reply being deleted we might accidentally push it to a wrong team's store. This might happen if the thread is already loaded in store and we are viewing another team. In that case we were fetching the thread from the API using the team id of the current team. The API returns the thread, even though the team id is not the one which the thread belongs to. This commit is fixing the above issue by getting the team id in which the thread belongs to, or current team id in the case of DM/GM messages, and using that to fetch the thread from the API. PS: the fetching is needed since we don't send a thread_update WS event upon deleting a reply, and we need to get the new participants list. * Fixes team id on another occasion * Refactors a bit * Reverts returning empty string as team id * Refactor a bit to pass the post as argument --------- Co-authored-by: Kyriakos Ziakoulis Co-authored-by: Kyriakos Ziakoulis Co-authored-by: Harrison Healey --- .../src/actions/websocket_actions.jsx | 10 ++++++---- .../src/selectors/entities/posts.ts | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/webapp/channels/src/actions/websocket_actions.jsx b/webapp/channels/src/actions/websocket_actions.jsx index 03ce908cab..79388b0a49 100644 --- a/webapp/channels/src/actions/websocket_actions.jsx +++ b/webapp/channels/src/actions/websocket_actions.jsx @@ -82,7 +82,7 @@ import { getCurrentChannelId, getRedirectChannelNameForTeam, } from 'mattermost-redux/selectors/entities/channels'; -import {getPost, getMostRecentPostIdInChannel} from 'mattermost-redux/selectors/entities/posts'; +import {getPost, getMostRecentPostIdInChannel, getTeamIdFromPost} from 'mattermost-redux/selectors/entities/posts'; import {haveISystemPermission, haveITeamPermission} from 'mattermost-redux/selectors/entities/roles'; import {appsFeatureFlagEnabled} from 'mattermost-redux/selectors/entities/apps'; import {getStandardAnalytics} from 'mattermost-redux/actions/admin'; @@ -774,8 +774,10 @@ async function handlePostDeleteEvent(msg) { const thread = getThread(state, post.root_id); if (thread) { const userId = getCurrentUserId(state); - const teamId = getCurrentTeamId(state); - dispatch(fetchThread(userId, teamId, post.root_id, true)); + const teamId = getTeamIdFromPost(state, post); + if (teamId) { + dispatch(fetchThread(userId, teamId, post.root_id, true)); + } } else { const res = await dispatch(getPostThread(post.root_id)); const {order, posts} = res.data; @@ -1679,7 +1681,7 @@ function handleThreadFollowChanged(msg) { const state = doGetState(); const thread = getThread(state, msg.data.thread_id); if (!thread && msg.data.state && msg.data.reply_count) { - await doDispatch(fetchThread(getCurrentUserId(state), getCurrentTeamId(state), msg.data.thread_id, true)); + await doDispatch(fetchThread(getCurrentUserId(state), msg.broadcast.team_id, msg.data.thread_id, true)); } handleFollowChanged(doDispatch, msg.data.thread_id, msg.broadcast.team_id, msg.data.state); }; 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 ce50bba0eb..b898cc9019 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 @@ -1,13 +1,15 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import {Posts, Preferences} from 'mattermost-redux/constants'; +import {General, Posts, Preferences} from 'mattermost-redux/constants'; import {createSelector} from 'mattermost-redux/selectors/create_selector'; import {getCurrentUser} from 'mattermost-redux/selectors/entities/common'; import {getMyPreferences} from 'mattermost-redux/selectors/entities/preferences'; import {getUsers, getCurrentUserId, getUserStatuses} from 'mattermost-redux/selectors/entities/users'; import {getConfig, getFeatureFlagValue} from 'mattermost-redux/selectors/entities/general'; +import {getChannel} from 'mattermost-redux/selectors/entities/channels'; +import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams'; import {createIdsSelector} from 'mattermost-redux/utils/helpers'; @@ -36,6 +38,7 @@ import { import {Reaction} from '@mattermost/types/reactions'; import {GlobalState} from '@mattermost/types/store'; import {UserProfile} from '@mattermost/types/users'; +import type {Team} from '@mattermost/types/teams'; import { IDMappedObjects, RelationOneToOne, @@ -830,3 +833,17 @@ export function makeGetPostAcknowledgementsWithProfiles(): (state: GlobalState, }, ); } + +export function getTeamIdFromPost(state: GlobalState, post: Post): Team['id'] | undefined { + const channel = getChannel(state, post.channel_id); + + if (!channel) { + return undefined; + } + + if (channel.type === General.DM_CHANNEL || channel.type === General.GM_CHANNEL) { + return getCurrentTeamId(state); + } + + return channel.team_id; +}