From 67f6740be444db7b7f8716e4524e2c8ebdcb7116 Mon Sep 17 00:00:00 2001 From: harshil Sharma Date: Mon, 30 Oct 2023 18:33:19 +0530 Subject: [PATCH] Making conversion API call --- app/actions/remote/channel.ts | 30 ++++++++++ app/client/rest/channels.ts | 15 +++++ .../convert_private/convert_private.tsx | 1 + .../channel_name_input.tsx | 4 +- .../convert_gm_to_channel.tsx | 2 + .../convert_gm_to_channel_form.tsx | 60 ++++++++++++++----- .../convert_gm_to_channel/team_selector.tsx | 12 +++- 7 files changed, 106 insertions(+), 18 deletions(-) diff --git a/app/actions/remote/channel.ts b/app/actions/remote/channel.ts index 1e2436642..c8cfed5f5 100644 --- a/app/actions/remote/channel.ts +++ b/app/actions/remote/channel.ts @@ -1274,3 +1274,33 @@ export const getGroupMessageMembersCommonTeams = async (serverUrl: string, chann return {error}; } }; + +export const convertGroupMessageToPrivateChannel = async (serverUrl: string, channelId: string, teamId: string, displayName: string) => { + try { + const client = NetworkManager.getClient(serverUrl); + const name = generateChannelNameFromDisplayName(displayName); + + const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); + const existingChannel = await getChannelById(database, channelId); + + // addConvertingChannel ??? do we need to do this? + + const updatedChannel = await client.convertGroupMessageToPrivateChannel(channelId, teamId, displayName, name); + + if (existingChannel) { + existingChannel.prepareUpdate((channel) => { + channel.type = General.PRIVATE_CHANNEL; + channel.displayName = displayName; + channel.name = name; + }); + + await operator.batchRecords([existingChannel], 'convertGroupMessageToPrivateChannel'); + } + + return {updatedChannel}; + } catch (error) { + logDebug('error on convertGroupMessageToPrivateChannel', getFullErrorMessage(error)); + forceLogoutIfNecessary(serverUrl, error); + return {error}; + } +}; diff --git a/app/client/rest/channels.ts b/app/client/rest/channels.ts index 4bfbafa3e..80577f973 100644 --- a/app/client/rest/channels.ts +++ b/app/client/rest/channels.ts @@ -45,6 +45,7 @@ export interface ClientChannelsMix { updateChannelMemberSchemeRoles: (channelId: string, userId: string, isSchemeUser: boolean, isSchemeAdmin: boolean) => Promise; getMemberInChannel: (channelId: string, userId: string) => Promise; getGroupMessageMembersCommonTeams: (channelId: string) => Promise; + convertGroupMessageToPrivateChannel: (channelId: string, teamId: string, displayName: string, name: string) => Promise; } const ClientChannels = >(superclass: TBase) => class extends superclass { @@ -358,6 +359,20 @@ const ClientChannels = >(superclass: TBase {method: 'get'}, ); }; + + convertGroupMessageToPrivateChannel = (channelId: string, teamId: string, displayName: string, name: string) => { + const body = { + channel_id: channelId, + team_id: teamId, + display_name: displayName, + name, + }; + + return this.doFetch( + `${this.getChannelRoute(channelId)}/convert_to_channel?team-id=${teamId}`, + {method: 'post', body}, + ); + }; }; export default ClientChannels; diff --git a/app/screens/channel_info/destructive_options/convert_private/convert_private.tsx b/app/screens/channel_info/destructive_options/convert_private/convert_private.tsx index 87b4d737c..b89b810af 100644 --- a/app/screens/channel_info/destructive_options/convert_private/convert_private.tsx +++ b/app/screens/channel_info/destructive_options/convert_private/convert_private.tsx @@ -42,6 +42,7 @@ const ConvertPrivate = ({canConvert, channelId, displayName}: Props) => { ); }; + // LOL LOL const convertToPrivate = preventDoubleTap(async () => { const result = await convertChannelToPrivate(serverUrl, channelId); const {formatMessage} = intl; diff --git a/app/screens/convert_gm_to_channel/channel_name_input.tsx b/app/screens/convert_gm_to_channel/channel_name_input.tsx index 8d4a04bd4..538cfcd46 100644 --- a/app/screens/convert_gm_to_channel/channel_name_input.tsx +++ b/app/screens/convert_gm_to_channel/channel_name_input.tsx @@ -12,9 +12,10 @@ import {getKeyboardAppearanceFromTheme} from '@app/utils/theme'; type Props = { error?: string; + onChange: (text: string) => void; } -export const ChannelNameInput = ({error}: Props) => { +export const ChannelNameInput = ({error, onChange}: Props) => { const {formatMessage} = useIntl(); const theme = useTheme(); @@ -38,6 +39,7 @@ export const ChannelNameInput = ({error}: Props) => { testID='gonvert_gm_to_channel.channel_display_name.input' theme={theme} error={error} + onChangeText={onChange} /> ); }; diff --git a/app/screens/convert_gm_to_channel/convert_gm_to_channel.tsx b/app/screens/convert_gm_to_channel/convert_gm_to_channel.tsx index 12629544e..f02b6f873 100644 --- a/app/screens/convert_gm_to_channel/convert_gm_to_channel.tsx +++ b/app/screens/convert_gm_to_channel/convert_gm_to_channel.tsx @@ -9,6 +9,7 @@ import {useServerUrl} from '@app/context/server'; import {ConvertGMToChannelForm} from './convert_gm_to_channel_form/convert_gm_to_channel_form'; import {Loader} from './loader'; +import channel from '@app/constants/channel'; type Props = { channelId: string; @@ -98,6 +99,7 @@ const ConvertGMToChannel = ({ commonTeams={commonTeams} profiles={profiles} teammateNameDisplay={teammateNameDisplay} + channelId={channelId} /> ); }; diff --git a/app/screens/convert_gm_to_channel/convert_gm_to_channel_form/convert_gm_to_channel_form.tsx b/app/screens/convert_gm_to_channel/convert_gm_to_channel_form/convert_gm_to_channel_form.tsx index 6f476bc42..a35732707 100644 --- a/app/screens/convert_gm_to_channel/convert_gm_to_channel_form/convert_gm_to_channel_form.tsx +++ b/app/screens/convert_gm_to_channel/convert_gm_to_channel_form/convert_gm_to_channel_form.tsx @@ -1,12 +1,14 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React, {useCallback, useEffect, useRef} from 'react'; +import React, {useCallback, useEffect, useRef, useState} from 'react'; import {useIntl} from 'react-intl'; import {View} from 'react-native'; +import {convertGroupMessageToPrivateChannel, switchToChannelById} from '@actions/remote/channel'; +import {useServerUrl} from '@app/context/server'; import {useTheme} from '@app/context/theme'; -import {logDebug} from '@app/utils/log'; +import {logDebug, logError} from '@app/utils/log'; import {makeStyleSheetFromTheme} from '@app/utils/theme'; import {displayUsername} from '@app/utils/user'; import Button from '@components/button'; @@ -16,6 +18,9 @@ import MessageBox from '../message_box/message_box'; import {TeamSelector} from '../team_selector'; import {NoCommonTeamForm} from './no_common_teams_form'; +import { dismissAllModalsAndPopToRoot, popToRoot, popTopScreen } from '@app/screens/navigation'; +import { switchToChannel } from '@actions/local/channel'; +import { update } from 'lodash'; const getStyleFromTheme = makeStyleSheetFromTheme((theme: Theme) => { return { @@ -30,6 +35,7 @@ const getStyleFromTheme = makeStyleSheetFromTheme((theme: Theme) => { }); type Props = { + channelId: string; commonTeams: Team[]; profiles: UserProfile[]; locale?: string; @@ -37,6 +43,7 @@ type Props = { } export const ConvertGMToChannelForm = ({ + channelId, commonTeams, profiles, locale, @@ -44,8 +51,11 @@ export const ConvertGMToChannelForm = ({ }: Props) => { const theme = useTheme(); const styles = getStyleFromTheme(theme); + const serverUrl = useServerUrl(); + const intl = useIntl(); - const team = useRef(); + const [selectedTeam, setSelectedTeam] = useState(); + const newChannelName = useRef(); const {formatMessage} = useIntl(); const confirmButtonText = formatMessage({ @@ -54,20 +64,38 @@ export const ConvertGMToChannelForm = ({ }); useEffect(() => { + logDebug(`BBB commonTeams.length: ${commonTeams.length}`); if (commonTeams.length > 0) { - team.current = commonTeams[0]; + logDebug(`BBB commonTeams[0]: ${commonTeams[0].display_name}`); + setSelectedTeam(commonTeams[0]); } }, [commonTeams]); - const handleOnSelectTeam = useCallback((selectedTeam: Team) => { - team.current = selectedTeam; + const handleOnPress = useCallback(async () => { + if (!selectedTeam || !newChannelName.current) { + logDebug(`!selectedTeam: ${selectedTeam} || !newChannelName.current: ${newChannelName.current}`); + return; + } + + const {updatedChannel, error} = await convertGroupMessageToPrivateChannel(serverUrl, channelId, selectedTeam.id, newChannelName.current); + logDebug(updatedChannel); + + if (error) { + logError(error); + return; + } + + if (!updatedChannel) { + return; + } + + switchToChannelById(serverUrl, updatedChannel.id, selectedTeam.id); + }, [selectedTeam]); + + const handleOnChannelNameChange = useCallback((newName: string) => { + newChannelName.current = newName; }, []); - const handleOnPress = useCallback(() => { - logDebug(1); - }, []); - - const intl = useIntl(); const messageBoxHeader = intl.formatMessage({ id: 'channel_info.convert_gm_to_channel.warning.header', defaultMessage: 'Conversation history will be visible to any channel members', @@ -75,9 +103,7 @@ export const ConvertGMToChannelForm = ({ const userDisplayNames = profiles.map((profile) => displayUsername(profile, locale, teammateNameDisplay)); const defaultUserDisplayNames = intl.formatMessage({id: 'channel_info.convert_gm_to_channel.warning.body.yourself', defaultMessage: 'yourself'}); - const memberNames = profiles.length > 0 ? intl.formatList(userDisplayNames) : defaultUserDisplayNames; - const messageBoxBody = intl.formatMessage({ id: 'channel_info.convert_gm_to_channel.warning.bodyXXXX', defaultMessage: 'You are about to convert the Group Message with {memberNames} to a Channel. This cannot be undone.', @@ -101,11 +127,13 @@ export const ConvertGMToChannelForm = ({ commonTeams.length > 1 && } - +