From 2f29266767129e6c10598200c16c10f6c27b1874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Thu, 11 Aug 2022 15:22:02 +0200 Subject: [PATCH] More robust RHS handling of websocket events (#3648) * More robust RHS handling of websocket events * Fixing tests --- .../src/components/rhsChannelBoards.test.tsx | 5 +++++ .../src/components/rhsChannelBoards.tsx | 21 ++++++++++++++++--- server/ws/plugin_adapter.go | 2 +- webapp/src/store/boards.ts | 16 +++++++++++--- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/mattermost-plugin/webapp/src/components/rhsChannelBoards.test.tsx b/mattermost-plugin/webapp/src/components/rhsChannelBoards.test.tsx index 3a9362021..652ab719e 100644 --- a/mattermost-plugin/webapp/src/components/rhsChannelBoards.test.tsx +++ b/mattermost-plugin/webapp/src/components/rhsChannelBoards.test.tsx @@ -37,6 +37,11 @@ describe('components/rhsChannelBoards', () => { current: team, currentId: team.id, }, + users: { + me: { + id: 'user-id', + }, + }, language: { value: 'en', }, diff --git a/mattermost-plugin/webapp/src/components/rhsChannelBoards.tsx b/mattermost-plugin/webapp/src/components/rhsChannelBoards.tsx index 10651cc95..ba3236252 100644 --- a/mattermost-plugin/webapp/src/components/rhsChannelBoards.tsx +++ b/mattermost-plugin/webapp/src/components/rhsChannelBoards.tsx @@ -10,9 +10,17 @@ import {useWebsockets} from '../../../../webapp/src/hooks/websockets' import {Board, BoardMember} from '../../../../webapp/src/blocks/board' import {getCurrentTeamId} from '../../../../webapp/src/store/teams' +import {IUser} from '../../../../webapp/src/user' +import {getMe, fetchMe} from '../../../../webapp/src/store/users' import {loadBoards} from '../../../../webapp/src/store/initialLoad' import {getCurrentChannel} from '../../../../webapp/src/store/channels' -import {getMySortedBoards, setLinkToChannel, updateBoards, updateMembers} from '../../../../webapp/src/store/boards' +import { + getMySortedBoards, + setLinkToChannel, + updateBoards, + updateMembersEnsuringBoardsAndUsers, + addMyBoardMemberships, +} from '../../../../webapp/src/store/boards' import {useAppSelector, useAppDispatch} from '../../../../webapp/src/store/hooks' import AddIcon from '../../../../webapp/src/widgets/icons/add' import Button from '../../../../webapp/src/widgets/buttons/button' @@ -29,11 +37,13 @@ const RHSChannelBoards = () => { const boards = useAppSelector(getMySortedBoards) const teamId = useAppSelector(getCurrentTeamId) const currentChannel = useAppSelector(getCurrentChannel) + const me = useAppSelector(getMe) const dispatch = useAppDispatch() const intl = useIntl() useEffect(() => { dispatch(loadBoards()) + dispatch(fetchMe()) }, []) useWebsockets(teamId || '', (wsClient: WSClient) => { @@ -41,7 +51,12 @@ const RHSChannelBoards = () => { dispatch(updateBoards(boards)) } const onChangeMemberHandler = (_: WSClient, members: BoardMember[]): void => { - dispatch(updateMembers(members)) + dispatch(updateMembersEnsuringBoardsAndUsers(members)) + + if (me) { + const myBoardMemberships = members.filter((boardMember) => boardMember.userId === me.id) + dispatch(addMyBoardMemberships(myBoardMemberships)) + } } wsClient.addOnChange(onChangeBoardHandler, 'board') @@ -51,7 +66,7 @@ const RHSChannelBoards = () => { wsClient.removeOnChange(onChangeBoardHandler, 'board') wsClient.removeOnChange(onChangeMemberHandler, 'boardMembers') } - }, []) + }, [me]) if (!boards) { return null diff --git a/server/ws/plugin_adapter.go b/server/ws/plugin_adapter.go index bb135226d..7da35dac5 100644 --- a/server/ws/plugin_adapter.go +++ b/server/ws/plugin_adapter.go @@ -575,7 +575,7 @@ func (pa *PluginAdapter) BroadcastMemberChange(teamID, boardID string, member *m Member: member, } - pa.sendBoardMessage(teamID, boardID, utils.StructToMap(message)) + pa.sendBoardMessage(teamID, boardID, utils.StructToMap(message), member.UserID) } func (pa *PluginAdapter) BroadcastMemberDelete(teamID, boardID, userID string) { diff --git a/webapp/src/store/boards.ts b/webapp/src/store/boards.ts index f116fb9b3..14112377f 100644 --- a/webapp/src/store/boards.ts +++ b/webapp/src/store/boards.ts @@ -107,7 +107,11 @@ export const updateMembersHandler = (state: BoardsState, action: PayloadAction) => { - action.payload.forEach((boardMember) => state.myBoardMemberships[boardMember.boardId] = boardMember) - } + action.payload.forEach((member) => { + if (!member.schemeAdmin && !member.schemeEditor && !member.schemeViewer && !member.schemeCommenter) { + delete state.myBoardMemberships[member.boardId] + } else { + state.myBoardMemberships[member.boardId] = member + } + }) + }, }, extraReducers: (builder) => {