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 <koox00@Kyriakoss-MacBook-Pro.local>
Co-authored-by: Kyriakos Ziakoulis <koox00@192.168.2.3>
Co-authored-by: Harrison Healey <harrisonmhealey@gmail.com>
This commit is contained in:
Kyriakos Z 2023-05-30 22:33:19 +03:00 committed by GitHub
parent 281f8cf9ea
commit 14fcc8a22e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -82,7 +82,7 @@ import {
getCurrentChannelId, getCurrentChannelId,
getRedirectChannelNameForTeam, getRedirectChannelNameForTeam,
} from 'mattermost-redux/selectors/entities/channels'; } 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 {haveISystemPermission, haveITeamPermission} from 'mattermost-redux/selectors/entities/roles';
import {appsFeatureFlagEnabled} from 'mattermost-redux/selectors/entities/apps'; import {appsFeatureFlagEnabled} from 'mattermost-redux/selectors/entities/apps';
import {getStandardAnalytics} from 'mattermost-redux/actions/admin'; import {getStandardAnalytics} from 'mattermost-redux/actions/admin';
@ -774,8 +774,10 @@ async function handlePostDeleteEvent(msg) {
const thread = getThread(state, post.root_id); const thread = getThread(state, post.root_id);
if (thread) { if (thread) {
const userId = getCurrentUserId(state); const userId = getCurrentUserId(state);
const teamId = getCurrentTeamId(state); const teamId = getTeamIdFromPost(state, post);
dispatch(fetchThread(userId, teamId, post.root_id, true)); if (teamId) {
dispatch(fetchThread(userId, teamId, post.root_id, true));
}
} else { } else {
const res = await dispatch(getPostThread(post.root_id)); const res = await dispatch(getPostThread(post.root_id));
const {order, posts} = res.data; const {order, posts} = res.data;
@ -1679,7 +1681,7 @@ function handleThreadFollowChanged(msg) {
const state = doGetState(); const state = doGetState();
const thread = getThread(state, msg.data.thread_id); const thread = getThread(state, msg.data.thread_id);
if (!thread && msg.data.state && msg.data.reply_count) { 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); handleFollowChanged(doDispatch, msg.data.thread_id, msg.broadcast.team_id, msg.data.state);
}; };

View File

@ -1,13 +1,15 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information. // 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 {createSelector} from 'mattermost-redux/selectors/create_selector';
import {getCurrentUser} from 'mattermost-redux/selectors/entities/common'; import {getCurrentUser} from 'mattermost-redux/selectors/entities/common';
import {getMyPreferences} from 'mattermost-redux/selectors/entities/preferences'; import {getMyPreferences} from 'mattermost-redux/selectors/entities/preferences';
import {getUsers, getCurrentUserId, getUserStatuses} from 'mattermost-redux/selectors/entities/users'; import {getUsers, getCurrentUserId, getUserStatuses} from 'mattermost-redux/selectors/entities/users';
import {getConfig, getFeatureFlagValue} from 'mattermost-redux/selectors/entities/general'; 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'; import {createIdsSelector} from 'mattermost-redux/utils/helpers';
@ -36,6 +38,7 @@ import {
import {Reaction} from '@mattermost/types/reactions'; import {Reaction} from '@mattermost/types/reactions';
import {GlobalState} from '@mattermost/types/store'; import {GlobalState} from '@mattermost/types/store';
import {UserProfile} from '@mattermost/types/users'; import {UserProfile} from '@mattermost/types/users';
import type {Team} from '@mattermost/types/teams';
import { import {
IDMappedObjects, IDMappedObjects,
RelationOneToOne, 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;
}