From 82c514013c3097b4002d9c70a9af24cfd9cc4403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Thu, 30 Jun 2022 17:13:28 +0200 Subject: [PATCH] Fix links to channels closing the channel (#6446) --- app/actions/remote/channel.ts | 32 +++++++++++-------- .../app_command_parser/app_command_parser.ts | 2 +- .../markdown/markdown_link/markdown_link.tsx | 8 ++--- app/queries/servers/channel.ts | 6 ++-- app/utils/channel/index.ts | 5 +++ index.ts | 4 --- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/actions/remote/channel.ts b/app/actions/remote/channel.ts index b0a2d51fee..f43e4dce68 100644 --- a/app/actions/remote/channel.ts +++ b/app/actions/remote/channel.ts @@ -19,7 +19,7 @@ import {getCommonSystemValues, getConfig, getCurrentTeamId, getCurrentUserId, ge import {prepareMyTeams, getNthLastChannelFromTeam, getMyTeamById, getTeamById, getTeamByName, queryMyTeams} from '@queries/servers/team'; import {getCurrentUser} from '@queries/servers/user'; import EphemeralStore from '@store/ephemeral_store'; -import {generateChannelNameFromDisplayName, getDirectChannelName, isDMorGM} from '@utils/channel'; +import {generateChannelNameFromDisplayName, getDirectChannelName, isArchived, isDMorGM} from '@utils/channel'; import {isTablet} from '@utils/helpers'; import {logError, logInfo} from '@utils/log'; import {showMuteChannelSnackbar} from '@utils/snack_bar'; @@ -684,20 +684,22 @@ export async function switchToChannelByName(serverUrl: string, channelName: stri return {error: 'Could not fetch team member'}; } - let isArchived = false; - const chReq = await fetchChannelByName(serverUrl, team.id, channelName, true); - if (chReq.error) { - errorHandler(intl); - return {error: chReq.error}; + let channel: Channel | ChannelModel | undefined = await getChannelByName(database, team.id, channelName); + if (!channel) { + const chReq = await fetchChannelByName(serverUrl, team.id, channelName, true); + if (chReq.error) { + errorHandler(intl); + return {error: chReq.error}; + } + channel = chReq.channel; } - const channel = chReq.channel; + if (!channel) { errorHandler(intl); return {error: 'Could not fetch channel'}; } - isArchived = channel.delete_at > 0; - if (isArchived && system.config.ExperimentalViewArchivedChannels !== 'true') { + if (isArchived(channel) && system.config.ExperimentalViewArchivedChannels !== 'true') { errorHandler(intl); return {error: 'Channel is archived'}; } @@ -705,13 +707,14 @@ export async function switchToChannelByName(serverUrl: string, channelName: stri myChannel = await getMyChannel(database, channel.id); if (!myChannel) { - const req = await fetchMyChannel(serverUrl, channel.team_id || team.id, channel.id, true); + const channelTeamId = 'team_id' in channel ? channel.team_id : channel.teamId; + const req = await fetchMyChannel(serverUrl, channelTeamId || team.id, channel.id, true); myChannel = req.memberships?.[0]; } if (!myChannel) { if (channel.type === General.PRIVATE_CHANNEL) { - const displayName = channel.display_name; + const displayName = 'display_name' in channel ? channel.display_name : channel.displayName; const {join} = await privateChannelJoinPrompt(displayName, intl); if (!join) { if (joinedNewTeam) { @@ -755,7 +758,8 @@ export async function switchToChannelByName(serverUrl: string, channelName: stri ); } - if (!(myChannel instanceof Model)) { + // We are checking both, so this may become an issue + if (!(myChannel instanceof Model) && !(channel instanceof Model)) { modelPromises.push(...await prepareMyChannelsForTeam(operator, team.id, [channel], [myChannel])); } @@ -814,7 +818,7 @@ export async function createDirectChannel(serverUrl: string, userId: string, dis } const channelName = getDirectChannelName(currentUser.id, userId); - const channel = await getChannelByName(database, channelName); + const channel = await getChannelByName(database, '', channelName); if (channel) { return {data: channel.toApi()}; } @@ -906,7 +910,7 @@ export async function makeDirectChannel(serverUrl: string, userId: string, displ try { const currentUserId = await getCurrentUserId(operator.database); const channelName = getDirectChannelName(userId, currentUserId); - let channel: Channel|ChannelModel|undefined = await getChannelByName(operator.database, channelName); + let channel: Channel|ChannelModel|undefined = await getChannelByName(operator.database, '', channelName); let result: {data?: Channel|ChannelModel; error?: any}; if (channel) { result = {data: channel}; diff --git a/app/components/autocomplete/slash_suggestion/app_command_parser/app_command_parser.ts b/app/components/autocomplete/slash_suggestion/app_command_parser/app_command_parser.ts index 264d7feec0..c44caa52e7 100644 --- a/app/components/autocomplete/slash_suggestion/app_command_parser/app_command_parser.ts +++ b/app/components/autocomplete/slash_suggestion/app_command_parser/app_command_parser.ts @@ -1320,7 +1320,7 @@ export class AppCommandParser { } case AppFieldTypes.CHANNEL: { const getFieldChannel = async (channelName: string) => { - let channel: ChannelModel | Channel | undefined = await getChannelByName(this.database, channelName); + let channel: ChannelModel | Channel | undefined = await getChannelByName(this.database, this.teamID, channelName); if (!channel) { const res = await fetchChannelByName(this.serverUrl, this.teamID, channelName); if ('error' in res) { diff --git a/app/components/markdown/markdown_link/markdown_link.tsx b/app/components/markdown/markdown_link/markdown_link.tsx index 774436819b..5ed2e0c7cc 100644 --- a/app/components/markdown/markdown_link/markdown_link.tsx +++ b/app/components/markdown/markdown_link/markdown_link.tsx @@ -15,7 +15,7 @@ import SlideUpPanelItem, {ITEM_HEIGHT} from '@components/slide_up_panel_item'; import DeepLinkTypes from '@constants/deep_linking'; import {useServerUrl} from '@context/server'; import {useTheme} from '@context/theme'; -import {bottomSheet, dismissAllModals, dismissBottomSheet, popToRoot} from '@screens/navigation'; +import {bottomSheet, dismissBottomSheet} from '@screens/navigation'; import {errorBadChannel} from '@utils/draft'; import {bottomSheetSnapPoint} from '@utils/helpers'; import {preventDoubleTap} from '@utils/tap'; @@ -69,11 +69,7 @@ const MarkdownLink = ({children, experimentalNormalizeMarkdownLinks, href, siteU if (match && match.data?.teamName) { if (match.type === DeepLinkTypes.CHANNEL) { - const result = await switchToChannelByName(serverUrl, (match?.data as DeepLinkChannel).channelName, match.data?.teamName, errorBadChannel, intl); - if (!result.error) { - await dismissAllModals(); - await popToRoot(); - } + await switchToChannelByName(serverUrl, (match?.data as DeepLinkChannel).channelName, match.data?.teamName, errorBadChannel, intl); } else if (match.type === DeepLinkTypes.PERMALINK) { showPermalink(serverUrl, match.data.teamName, (match.data as DeepLinkPermalink).postId, intl); } diff --git a/app/queries/servers/channel.ts b/app/queries/servers/channel.ts index 246d2adf4b..394360a05a 100644 --- a/app/queries/servers/channel.ts +++ b/app/queries/servers/channel.ts @@ -22,7 +22,7 @@ import type MyChannelModel from '@typings/database/models/servers/my_channel'; import type MyChannelSettingsModel from '@typings/database/models/servers/my_channel_settings'; import type UserModel from '@typings/database/models/servers/user'; -const {SERVER: {CHANNEL, MY_CHANNEL, CHANNEL_MEMBERSHIP, MY_CHANNEL_SETTINGS, CHANNEL_INFO, USER}} = MM_TABLES; +const {SERVER: {CHANNEL, MY_CHANNEL, CHANNEL_MEMBERSHIP, MY_CHANNEL_SETTINGS, CHANNEL_INFO, USER, TEAM}} = MM_TABLES; export function prepareMissingChannelsForAllTeams(operator: ServerDataOperator, channels: Channel[], channelMembers: ChannelMembership[], isCRTEnabled?: boolean): Array> { const channelInfos: ChannelInfo[] = []; @@ -211,8 +211,8 @@ export const observeChannel = (database: Database, channelId: string) => { ); }; -export const getChannelByName = async (database: Database, channelName: string) => { - const channels = await database.get(CHANNEL).query(Q.where('name', channelName)).fetch(); +export const getChannelByName = async (database: Database, teamId: string, channelName: string) => { + const channels = await database.get(CHANNEL).query(Q.on(TEAM, 'id', teamId), Q.where('name', channelName)).fetch(); // Check done to force types if (channels.length) { diff --git a/app/utils/channel/index.ts b/app/utils/channel/index.ts index 06e8a4b141..50887d00f6 100644 --- a/app/utils/channel/index.ts +++ b/app/utils/channel/index.ts @@ -28,6 +28,11 @@ export function isDMorGM(channel: Channel | ChannelModel): boolean { return directTypes.includes(channel.type); } +export function isArchived(channel: Channel | ChannelModel): boolean { + const deleteAt = 'delete_at' in channel ? channel.delete_at : channel.deleteAt; + return deleteAt > 0; +} + export function selectDefaultChannelForTeam(channels: T[], memberships: ChannelMembership[], teamId: string, roles?: Role[], locale = DEFAULT_LOCALE) { let channel: T|undefined; let canIJoinPublicChannelsInTeam = false; diff --git a/index.ts b/index.ts index 0747af2dae..17f4f8d79d 100644 --- a/index.ts +++ b/index.ts @@ -102,10 +102,6 @@ function screenDidDisappearListener({componentId}: ComponentDidDisappearEvent) { DeviceEventEmitter.emit(Events.PAUSE_KEYBOARD_TRACKING_VIEW, false); } - if (NavigationStore.getNavigationTopComponentId() === componentId) { - NavigationStore.removeNavigationComponentId(componentId); - } - if (NavigationStore.getNavigationTopComponentId() === Screens.HOME) { DeviceEventEmitter.emit(Events.TAB_BAR_VISIBLE, true); }