diff --git a/app/actions/remote/channel.ts b/app/actions/remote/channel.ts index af36ba38b1..e9c43e18d1 100644 --- a/app/actions/remote/channel.ts +++ b/app/actions/remote/channel.ts @@ -16,15 +16,17 @@ import {privateChannelJoinPrompt} from '@helpers/api/channel'; import {getTeammateNameDisplaySetting} from '@helpers/api/preference'; import AppsManager from '@managers/apps_manager'; import NetworkManager from '@managers/network_manager'; +import {getActiveServer} from '@queries/app/servers'; import {prepareMyChannelsForTeam, getChannelById, getChannelByName, getMyChannel, getChannelInfo, queryMyChannelSettingsByIds, getMembersCountByChannelsId} from '@queries/servers/channel'; import {queryPreferencesByCategoryAndName} from '@queries/servers/preference'; -import {getCommonSystemValues, getConfig, getCurrentTeamId, getCurrentUserId, getLicense, setCurrentChannelId} from '@queries/servers/system'; +import {getCommonSystemValues, getConfig, getCurrentChannelId, getCurrentTeamId, getCurrentUserId, getLicense, setCurrentChannelId, setCurrentTeamAndChannelId} from '@queries/servers/system'; import {getNthLastChannelFromTeam, getMyTeamById, getTeamByName, queryMyTeams} from '@queries/servers/team'; import {getCurrentUser} from '@queries/servers/user'; +import {dismissAllModals, popToRoot} from '@screens/navigation'; import EphemeralStore from '@store/ephemeral_store'; import {generateChannelNameFromDisplayName, getDirectChannelName, isDMorGM} from '@utils/channel'; import {isTablet} from '@utils/helpers'; -import {logError, logInfo} from '@utils/log'; +import {logDebug, logError, logInfo} from '@utils/log'; import {showMuteChannelSnackbar} from '@utils/snack_bar'; import {PERMALINK_GENERIC_TEAM_NAME_REDIRECT} from '@utils/url'; import {displayGroupMessageName, displayUsername} from '@utils/user'; @@ -1299,3 +1301,44 @@ export const convertChannelToPrivate = async (serverUrl: string, channelId: stri EphemeralStore.removeConvertingChannel(channelId); } }; + +export const handleKickFromChannel = async (serverUrl: string, channelId: string) => { + try { + const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); + + const currentChannelId = await getCurrentChannelId(database); + if (currentChannelId !== channelId) { + return; + } + + const currentServer = await getActiveServer(); + if (currentServer?.url === serverUrl) { + const channel = await getChannelById(database, channelId); + DeviceEventEmitter.emit(Events.LEAVE_CHANNEL, channel?.displayName); + await dismissAllModals(); + await popToRoot(); + } + + const tabletDevice = await isTablet(); + + if (tabletDevice) { + const teamId = await getCurrentTeamId(database); + const newChannelId = await getNthLastChannelFromTeam(database, teamId); + if (newChannelId) { + if (currentServer?.url === serverUrl) { + if (newChannelId === Screens.GLOBAL_THREADS) { + await switchToGlobalThreads(serverUrl, teamId, false); + } else { + await switchToChannelById(serverUrl, newChannelId, teamId, true); + } + } else { + await setCurrentTeamAndChannelId(operator, teamId, channelId); + } + } // TODO else jump to "join a channel" screen https://mattermost.atlassian.net/browse/MM-41051 + } else { + await setCurrentChannelId(operator, ''); + } + } catch (error) { + logDebug('cannot kick user from channel', error); + } +}; diff --git a/app/actions/remote/entry/app.ts b/app/actions/remote/entry/app.ts index 6ab47c4334..1339bdaaf2 100644 --- a/app/actions/remote/entry/app.ts +++ b/app/actions/remote/entry/app.ts @@ -2,16 +2,14 @@ // See LICENSE.txt for license information. import {setLastServerVersionCheck} from '@actions/local/systems'; -import {switchToChannelById} from '@actions/remote/channel'; import {fetchConfigAndLicense} from '@actions/remote/systems'; import DatabaseManager from '@database/manager'; -import {prepareCommonSystemValues, getCurrentTeamId, getWebSocketLastDisconnected, setCurrentTeamAndChannelId, getCurrentChannelId, getConfig, getLicense} from '@queries/servers/system'; +import {prepareCommonSystemValues, getCurrentTeamId, getWebSocketLastDisconnected, getCurrentChannelId, getConfig, getLicense} from '@queries/servers/system'; import {getCurrentUser} from '@queries/servers/user'; import {deleteV1Data} from '@utils/file'; -import {isTablet} from '@utils/helpers'; import {logInfo} from '@utils/log'; -import {registerDeviceToken, syncOtherServers, verifyPushProxy} from './common'; +import {handleEntryAfterLoadNavigation, registerDeviceToken, syncOtherServers, verifyPushProxy} from './common'; import {deferredAppEntryActions, entry} from './gql_common'; export async function appEntry(serverUrl: string, since = 0, isUpgrade = false) { @@ -35,7 +33,6 @@ export async function appEntry(serverUrl: string, since = 0, isUpgrade = false) const {database} = operator; - const tabletDevice = await isTablet(); const currentTeamId = await getCurrentTeamId(database); const currentChannelId = await getCurrentChannelId(database); const lastDisconnectedAt = (await getWebSocketLastDisconnected(database)) || since; @@ -53,16 +50,7 @@ export async function appEntry(serverUrl: string, since = 0, isUpgrade = false) } } - let switchToChannel = false; - - // Immediately set the new team as the current team in the database so that the UI - // renders the correct team. - if (tabletDevice && initialChannelId) { - switchToChannel = true; - switchToChannelById(serverUrl, initialChannelId, initialTeamId); - } else { - setCurrentTeamAndChannelId(operator, initialTeamId, initialChannelId); - } + await handleEntryAfterLoadNavigation(serverUrl, teamData.memberships || [], chData?.memberships || [], currentTeamId, currentChannelId, initialTeamId, initialChannelId); const dt = Date.now(); await operator.batchRecords(models); @@ -71,7 +59,7 @@ export async function appEntry(serverUrl: string, since = 0, isUpgrade = false) const {id: currentUserId, locale: currentUserLocale} = meData?.user || (await getCurrentUser(database))!; const config = await getConfig(database); const license = await getLicense(database); - await deferredAppEntryActions(serverUrl, lastDisconnectedAt, currentUserId, currentUserLocale, prefData.preferences, config, license, teamData, chData, initialTeamId, switchToChannel ? initialChannelId : undefined); + await deferredAppEntryActions(serverUrl, lastDisconnectedAt, currentUserId, currentUserLocale, prefData.preferences, config, license, teamData, chData, initialTeamId); if (!since) { // Load data from other servers diff --git a/app/actions/remote/entry/common.ts b/app/actions/remote/entry/common.ts index a82fe6d09b..6827cc1bbb 100644 --- a/app/actions/remote/entry/common.ts +++ b/app/actions/remote/entry/common.ts @@ -4,13 +4,13 @@ import {Database, Model} from '@nozbe/watermelondb'; import {DeviceEventEmitter} from 'react-native'; -import {fetchMissingDirectChannelsInfo, fetchMyChannelsForTeam, MyChannelsRequest} from '@actions/remote/channel'; +import {fetchMissingDirectChannelsInfo, fetchMyChannelsForTeam, handleKickFromChannel, MyChannelsRequest} from '@actions/remote/channel'; import {fetchGroupsForMember} from '@actions/remote/groups'; import {fetchPostsForUnreadChannels} from '@actions/remote/post'; import {MyPreferencesRequest, fetchMyPreferences} from '@actions/remote/preference'; import {fetchRoles} from '@actions/remote/role'; import {fetchConfigAndLicense} from '@actions/remote/systems'; -import {fetchAllTeams, fetchMyTeams, fetchTeamsChannelsAndUnreadPosts, MyTeamsRequest} from '@actions/remote/team'; +import {fetchAllTeams, fetchMyTeams, fetchTeamsChannelsAndUnreadPosts, handleKickFromTeam, MyTeamsRequest} from '@actions/remote/team'; import {syncTeamThreads} from '@actions/remote/thread'; import {autoUpdateTimezone, fetchMe, MyUserRequest, updateAllUsersSince} from '@actions/remote/user'; import {gqlAllChannels} from '@client/graphQL/entry'; @@ -27,11 +27,13 @@ import {getAllServers} from '@queries/app/servers'; import {prepareMyChannelsForTeam, queryAllChannelsForTeam, queryChannelsById} from '@queries/servers/channel'; import {prepareModels, truncateCrtRelatedTables} from '@queries/servers/entry'; import {getHasCRTChanged} from '@queries/servers/preference'; -import {getConfig, getCurrentUserId, getPushVerificationStatus, getWebSocketLastDisconnected} from '@queries/servers/system'; +import {getConfig, getCurrentChannelId, getCurrentTeamId, getCurrentUserId, getPushVerificationStatus, getWebSocketLastDisconnected, setCurrentTeamAndChannelId} from '@queries/servers/system'; import {deleteMyTeams, getAvailableTeamIds, getTeamChannelHistory, queryMyTeams, queryMyTeamsByIds, queryTeamsById} from '@queries/servers/team'; import {getIsCRTEnabled} from '@queries/servers/thread'; +import NavigationStore from '@store/navigation_store'; import {isDMorGM, sortChannelsByDisplayName} from '@utils/channel'; import {getMemberChannelsFromGQLQuery, gqlToClientChannelMembership} from '@utils/graphql'; +import {isTablet} from '@utils/helpers'; import {logDebug} from '@utils/log'; import {processIsCRTEnabled} from '@utils/thread'; @@ -517,3 +519,50 @@ export async function verifyPushProxy(serverUrl: string) { // Do nothing } } + +export async function handleEntryAfterLoadNavigation( + serverUrl: string, + teamMembers: TeamMembership[], + channelMembers: ChannelMember[], + currentTeamId: string, + currentChannelId: string, + initialTeamId: string, + initialChannelId: string, +) { + try { + const {operator, database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); + + const currentTeamIdAfterLoad = await getCurrentTeamId(database); + const currentChannelIdAfterLoad = await getCurrentChannelId(database); + + if (currentTeamIdAfterLoad !== currentTeamId) { + // Switched teams while loading + if (!teamMembers.find((t) => t.team_id === currentTeamIdAfterLoad && t.delete_at === 0)) { + await handleKickFromTeam(serverUrl, currentTeamIdAfterLoad); + } + } else if (currentTeamIdAfterLoad !== initialTeamId) { + await handleKickFromTeam(serverUrl, currentTeamIdAfterLoad); + } else if (currentChannelIdAfterLoad !== currentChannelId) { + // Switched channels while loading + if (!channelMembers.find((m) => m.channel_id === currentChannelIdAfterLoad)) { + const tabletDevice = await isTablet(); + const navComponents = NavigationStore.getNavigationComponents(); + if (tabletDevice || navComponents.includes(Screens.CHANNEL) || navComponents.includes(Screens.THREAD)) { + await handleKickFromChannel(serverUrl, currentChannelIdAfterLoad); + } else { + await setCurrentTeamAndChannelId(operator, initialTeamId, initialChannelId); + } + } + } else if (currentChannelIdAfterLoad !== initialChannelId) { + const tabletDevice = await isTablet(); + const navComponents = NavigationStore.getNavigationComponents(); + if (tabletDevice || navComponents.includes(Screens.CHANNEL) || navComponents.includes(Screens.THREAD)) { + await handleKickFromChannel(serverUrl, currentChannelIdAfterLoad); + } else { + await setCurrentTeamAndChannelId(operator, initialTeamId, initialChannelId); + } + } + } catch (error) { + logDebug('could not manage the entry after load navigation', error); + } +} diff --git a/app/actions/remote/team.ts b/app/actions/remote/team.ts index 7bd9616e2f..e633cedd46 100644 --- a/app/actions/remote/team.ts +++ b/app/actions/remote/team.ts @@ -8,12 +8,15 @@ import {removeUserFromTeam as localRemoveUserFromTeam} from '@actions/local/team import {Events} from '@constants'; import DatabaseManager from '@database/manager'; import NetworkManager from '@managers/network_manager'; +import {getActiveServerUrl} from '@queries/app/servers'; import {prepareCategoriesAndCategoriesChannels} from '@queries/servers/categories'; import {prepareMyChannelsForTeam, getDefaultChannelForTeam} from '@queries/servers/channel'; import {prepareCommonSystemValues, getCurrentTeamId, getCurrentUserId} from '@queries/servers/system'; -import {addTeamToTeamHistory, prepareDeleteTeam, prepareMyTeams, getNthLastChannelFromTeam, queryTeamsById, syncTeamTable} from '@queries/servers/team'; +import {addTeamToTeamHistory, prepareDeleteTeam, prepareMyTeams, getNthLastChannelFromTeam, queryTeamsById, syncTeamTable, getLastTeam, getTeamById} from '@queries/servers/team'; +import {dismissAllModals, popToRoot, resetToTeams} from '@screens/navigation'; import EphemeralStore from '@store/ephemeral_store'; import {isTablet} from '@utils/helpers'; +import {logDebug} from '@utils/log'; import {fetchMyChannelsForTeam, switchToChannelById} from './channel'; import {fetchGroupsForTeamIfConstrained} from './groups'; @@ -324,3 +327,30 @@ export async function handleTeamChange(serverUrl: string, teamId: string) { // Fetch Groups + GroupTeams fetchGroupsForTeamIfConstrained(serverUrl, teamId); } + +export async function handleKickFromTeam(serverUrl: string, teamId: string) { + try { + const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); + const currentTeamId = await getCurrentTeamId(database); + if (currentTeamId !== teamId) { + return; + } + + const currentServer = await getActiveServerUrl(); + if (currentServer === serverUrl) { + const team = await getTeamById(database, teamId); + DeviceEventEmitter.emit(Events.LEAVE_TEAM, team?.displayName); + await dismissAllModals(); + await popToRoot(); + } + + const teamToJumpTo = await getLastTeam(database); + if (teamToJumpTo) { + await handleTeamChange(serverUrl, teamToJumpTo); + } else if (currentServer === serverUrl) { + await resetToTeams(); + } + } catch (error) { + logDebug('Failed to kick user from team', error); + } +} diff --git a/app/actions/websocket/channel.ts b/app/actions/websocket/channel.ts index 12040ee3f1..5b883ecbc5 100644 --- a/app/actions/websocket/channel.ts +++ b/app/actions/websocket/channel.ts @@ -11,7 +11,7 @@ import { } from '@actions/local/channel'; import {storePostsForChannel} from '@actions/local/post'; import {switchToGlobalThreads} from '@actions/local/thread'; -import {fetchMissingDirectChannelsInfo, fetchMyChannel, fetchChannelStats, fetchChannelById, switchToChannelById} from '@actions/remote/channel'; +import {fetchMissingDirectChannelsInfo, fetchMyChannel, fetchChannelStats, fetchChannelById, switchToChannelById, handleKickFromChannel} from '@actions/remote/channel'; import {fetchPostsForChannel} from '@actions/remote/post'; import {fetchRolesIfNeeded} from '@actions/remote/role'; import {fetchUsersByIds, updateUsersNoLongerVisible} from '@actions/remote/user'; @@ -20,12 +20,13 @@ import {Events, Screens} from '@constants'; import DatabaseManager from '@database/manager'; import {getActiveServer} from '@queries/app/servers'; import {deleteChannelMembership, getChannelById, prepareMyChannelsForTeam, getCurrentChannel} from '@queries/servers/channel'; -import {prepareCommonSystemValues, getConfig, setCurrentChannelId, getCurrentChannelId, getCurrentTeamId} from '@queries/servers/system'; +import {getConfig, setCurrentChannelId, getCurrentChannelId, getCurrentTeamId} from '@queries/servers/system'; import {getNthLastChannelFromTeam} from '@queries/servers/team'; import {getCurrentUser, getTeammateNameDisplay, getUserById} from '@queries/servers/user'; import {dismissAllModals, popToRoot} from '@screens/navigation'; import EphemeralStore from '@store/ephemeral_store'; import {isTablet} from '@utils/helpers'; +import {logDebug} from '@utils/log'; // Received when current user created a channel in a different client export async function handleChannelCreatedEvent(serverUrl: string, msg: any) { @@ -323,12 +324,9 @@ export async function handleUserAddedToChannelEvent(serverUrl: string, msg: any) } export async function handleUserRemovedFromChannelEvent(serverUrl: string, msg: any) { - const operator = DatabaseManager.serverDatabases[serverUrl]?.operator; - if (!operator) { - return; - } - try { + const {operator, database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); + // Depending on who was removed, the ids may come from one place dataset or the other. const userId = msg.data.user_id || msg.broadcast.user_id; const channelId = msg.data.channel_id || msg.broadcast.channel_id; @@ -337,8 +335,6 @@ export async function handleUserRemovedFromChannelEvent(serverUrl: string, msg: return; } - const {database} = operator; - const channel = await getCurrentChannel(database); const user = await getCurrentUser(database); if (!user) { return; @@ -354,39 +350,11 @@ export async function handleUserRemovedFromChannelEvent(serverUrl: string, msg: } if (user.id === userId) { - await removeCurrentUserFromChannel(serverUrl, channelId); - if (channel && channel.id === channelId) { - const currentServer = await getActiveServer(); - - if (currentServer?.url === serverUrl) { - DeviceEventEmitter.emit(Events.LEAVE_CHANNEL, channel.displayName); - await dismissAllModals(); - await popToRoot(); - - if (await isTablet()) { - let tId = channel.teamId; - if (!tId) { - tId = await getCurrentTeamId(database); - } - const channelToJumpTo = await getNthLastChannelFromTeam(database, tId); - if (channelToJumpTo) { - if (channelToJumpTo === Screens.GLOBAL_THREADS) { - const {models: switchToGlobalThreadsModels} = await switchToGlobalThreads(serverUrl, tId, true); - if (switchToGlobalThreadsModels) { - models.push(...switchToGlobalThreadsModels); - } - } else { - switchToChannelById(serverUrl, channelToJumpTo, tId, true); - } - } // TODO else jump to "join a channel" screen https://mattermost.atlassian.net/browse/MM-41051 - } else { - const currentChannelModels = await prepareCommonSystemValues(operator, {currentChannelId: ''}); - if (currentChannelModels?.length) { - models.push(...currentChannelModels); - } - } - } + const currentChannelId = await getCurrentChannelId(database); + if (currentChannelId && currentChannelId === channelId) { + await handleKickFromChannel(serverUrl, currentChannelId); } + await removeCurrentUserFromChannel(serverUrl, channelId); } else { const {models: deleteMemberModels} = await deleteChannelMembership(operator, userId, channelId, true); if (deleteMemberModels) { @@ -395,8 +363,8 @@ export async function handleUserRemovedFromChannelEvent(serverUrl: string, msg: } operator.batchRecords(models); - } catch { - // Do nothing + } catch (error) { + logDebug('cannot handle user removed from channel websocket event', error); } } diff --git a/app/actions/websocket/index.ts b/app/actions/websocket/index.ts index d0eb6d799b..652a2c0eee 100644 --- a/app/actions/websocket/index.ts +++ b/app/actions/websocket/index.ts @@ -3,7 +3,7 @@ import {DeviceEventEmitter} from 'react-native'; -import {switchToChannelById} from '@actions/remote/channel'; +import {handleEntryAfterLoadNavigation} from '@actions/remote/entry/common'; import {deferredAppEntryActions, entry} from '@actions/remote/entry/gql_common'; import {fetchStatusByIds} from '@actions/remote/user'; import {loadConfigAndCalls} from '@calls/actions/calls'; @@ -27,11 +27,11 @@ import { handleCallUserVoiceOn, } from '@calls/connection/websocket_event_handlers'; import {isSupportedServerCalls} from '@calls/utils'; -import {Events, Screens, WebsocketEvents} from '@constants'; +import {Events, WebsocketEvents} from '@constants'; import {SYSTEM_IDENTIFIERS} from '@constants/database'; import DatabaseManager from '@database/manager'; import AppsManager from '@managers/apps_manager'; -import {getActiveServerUrl, getActiveServer} from '@queries/app/servers'; +import {getActiveServerUrl} from '@queries/app/servers'; import {getCurrentChannel} from '@queries/servers/channel'; import { getConfig, @@ -39,13 +39,9 @@ import { getLicense, getWebSocketLastDisconnected, resetWebSocketLastDisconnected, - setCurrentTeamAndChannelId, } from '@queries/servers/system'; import {getCurrentTeam} from '@queries/servers/team'; import {getCurrentUser} from '@queries/servers/user'; -import {dismissAllModals, popToRoot} from '@screens/navigation'; -import NavigationStore from '@store/navigation_store'; -import {isTablet} from '@utils/helpers'; import {logInfo} from '@utils/log'; import {handleCategoryCreatedEvent, handleCategoryDeletedEvent, handleCategoryOrderUpdatedEvent, handleCategoryUpdatedEvent} from './category'; @@ -135,8 +131,8 @@ async function doReconnect(serverUrl: string) { const currentTeam = await getCurrentTeam(database); const currentChannel = await getCurrentChannel(database); - const currentActiveServerUrl = await getActiveServerUrl(); + const currentActiveServerUrl = await getActiveServerUrl(); if (serverUrl === currentActiveServerUrl) { DeviceEventEmitter.emit(Events.FETCHING_POSTS, true); } @@ -149,35 +145,7 @@ async function doReconnect(serverUrl: string) { } const {models, initialTeamId, initialChannelId, prefData, teamData, chData} = entryData; - let switchedToChannel = false; - - // if no longer a member of the current team or the current channel - if (initialTeamId !== currentTeam?.id || initialChannelId !== currentChannel?.id) { - const currentServer = await getActiveServer(); - const isChannelScreenMounted = NavigationStore.getNavigationComponents().includes(Screens.CHANNEL); - if (serverUrl === currentServer?.url) { - if (currentTeam && initialTeamId !== currentTeam.id) { - DeviceEventEmitter.emit(Events.LEAVE_TEAM, {displayName: currentTeam.displayName}); - await dismissAllModals(); - await popToRoot(); - } else if (currentChannel && initialChannelId !== currentChannel.id && isChannelScreenMounted) { - DeviceEventEmitter.emit(Events.LEAVE_CHANNEL, {displayName: currentChannel?.displayName}); - await dismissAllModals(); - await popToRoot(); - } - - const tabletDevice = await isTablet(); - - if (tabletDevice && initialChannelId) { - switchedToChannel = true; - switchToChannelById(serverUrl, initialChannelId, initialTeamId); - } else { - setCurrentTeamAndChannelId(operator, initialTeamId, initialChannelId); - } - } else { - setCurrentTeamAndChannelId(operator, initialTeamId, initialChannelId); - } - } + await handleEntryAfterLoadNavigation(serverUrl, teamData.memberships || [], chData?.memberships || [], currentTeam?.id || '', currentChannel?.id || '', initialTeamId, initialChannelId); const dt = Date.now(); await operator.batchRecords(models); @@ -191,7 +159,7 @@ async function doReconnect(serverUrl: string) { loadConfigAndCalls(serverUrl, currentUserId); } - await deferredAppEntryActions(serverUrl, lastDisconnectedAt, currentUserId, currentUserLocale, prefData.preferences, config, license, teamData, chData, initialTeamId, switchedToChannel ? initialChannelId : undefined); + await deferredAppEntryActions(serverUrl, lastDisconnectedAt, currentUserId, currentUserLocale, prefData.preferences, config, license, teamData, chData, initialTeamId); AppsManager.refreshAppBindings(serverUrl); } diff --git a/app/actions/websocket/teams.ts b/app/actions/websocket/teams.ts index c469b8b24f..cf5b501839 100644 --- a/app/actions/websocket/teams.ts +++ b/app/actions/websocket/teams.ts @@ -2,60 +2,45 @@ // See LICENSE.txt for license information. import {Model} from '@nozbe/watermelondb'; -import {DeviceEventEmitter} from 'react-native'; import {removeUserFromTeam} from '@actions/local/team'; import {fetchMyChannelsForTeam} from '@actions/remote/channel'; import {fetchRoles} from '@actions/remote/role'; -import {fetchAllTeams, handleTeamChange, fetchMyTeam} from '@actions/remote/team'; +import {fetchAllTeams, fetchMyTeam, handleKickFromTeam} from '@actions/remote/team'; import {updateUsersNoLongerVisible} from '@actions/remote/user'; -import Events from '@constants/events'; import DatabaseManager from '@database/manager'; -import {getActiveServerUrl} from '@queries/app/servers'; import {prepareCategoriesAndCategoriesChannels} from '@queries/servers/categories'; import {prepareMyChannelsForTeam} from '@queries/servers/channel'; -import {getCurrentTeam, getLastTeam, prepareMyTeams} from '@queries/servers/team'; +import {getCurrentTeam, prepareMyTeams} from '@queries/servers/team'; import {getCurrentUser} from '@queries/servers/user'; -import {dismissAllModals, popToRoot, resetToTeams} from '@screens/navigation'; import EphemeralStore from '@store/ephemeral_store'; +import {logDebug} from '@utils/log'; export async function handleLeaveTeamEvent(serverUrl: string, msg: WebSocketMessage) { - const database = DatabaseManager.serverDatabases[serverUrl]; - if (!database) { - return; - } + try { + const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); - const currentTeam = await getCurrentTeam(database.database); - const user = await getCurrentUser(database.database); - if (!user) { - return; - } - - const {user_id: userId, team_id: teamId} = msg.data; - if (user.id === userId) { - await removeUserFromTeam(serverUrl, teamId); - fetchAllTeams(serverUrl); - - if (user.isGuest) { - updateUsersNoLongerVisible(serverUrl); + const user = await getCurrentUser(database); + if (!user) { + return; } - if (currentTeam?.id === teamId) { - const currentServer = await getActiveServerUrl(); - - if (currentServer === serverUrl) { - DeviceEventEmitter.emit(Events.LEAVE_TEAM, currentTeam?.displayName); - await dismissAllModals(); - await popToRoot(); + const {user_id: userId, team_id: teamId} = msg.data; + if (user.id === userId) { + const currentTeam = await getCurrentTeam(database); + if (currentTeam?.id === teamId) { + await handleKickFromTeam(serverUrl, teamId); } - const teamToJumpTo = await getLastTeam(database.database); - if (teamToJumpTo) { - handleTeamChange(serverUrl, teamToJumpTo); - } else if (currentServer === serverUrl) { - resetToTeams(); + await removeUserFromTeam(serverUrl, teamId); + fetchAllTeams(serverUrl); + + if (user.isGuest) { + updateUsersNoLongerVisible(serverUrl); } } + } catch (error) { + logDebug('cannot handle leave team websocket event', error); } }