Files
mattermost-mobile/app/init/launch.ts
Miguel Alatzar 134c4a49c5 Integrate react-native-network-client (#5499)
* fix: handle NSMutableData

* feat: integrate react-native-network-client

* fix: typos

* fix: semicolon

* fix: rename to urlVersion

* fix: add returnDataOnly arg

* fix: configure network client

* fix: headers

* fix: handling of serverVersion

* fix: rename requests to actions

* fix: action imports

* fix: no need to stringify body

* fix: sso flow

* fix: address PR feedback

* fix: invalidate client on logout

* fix: address PR feedback take 2

* fix: address PR feedback take 3

* fix: tsc issues

* fix: get csrf token during client creation

* fix: linter

* fix: invalidate client onLogout

* fix: event emitter

* fix: unit tests

* fix: apply linter fixes

* fix lint

* Modify actions to add / update database values

* Rename clien4.d.ts to client.d.ts

* fix empty & missing translations

* cleanup api client

* Cleanup init & squash some TODO's

* Emit certificate errors in NetworkManager

* cleanup user actions

* Fix NetworkManager invalidate client

* Invalidate client when server screen appears

* Update kotlin to 1.4.30 required by network-client

* patch react-native-keychain to remove cached credential

* update react-native-network-client

* Use app.db instead of default.db in native code

* fix use of rnnc on Android

* Init PushNotifications

* No need to reset serverVersion on logout

* fix logout action

* fix deleteServerDatabase

* fix schedule expired session notification

* use safeParseJSON for db json fields

* unsubscribe when database component unmounts

* cleanup init

* session type

* pass launchprops to entire login flow

* Properly remove third party cookies after SSO login

* recreate network client if sso with redirect fails

* add missing launch props from server screen

* use query prefix for database queries

* Add temporary logout function to channel screen

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2021-07-06 11:16:35 -04:00

150 lines
4.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Linking} from 'react-native';
import {Notifications} from 'react-native-notifications';
import {Screens} from '@constants';
import {getActiveServerUrl, getServerCredentials} from '@init/credentials';
import {goToScreen, resetToChannel, resetToSelectServer} from '@screens/navigation';
import {DeepLinkChannel, DeepLinkDM, DeepLinkGM, DeepLinkPermalink, DeepLinkType, DeepLinkWithData, LaunchProps, LaunchType} from '@typings/launch';
import {parseDeepLink} from '@utils/url';
export const initialLaunch = async () => {
const deepLinkUrl = await Linking.getInitialURL();
if (deepLinkUrl) {
launchAppFromDeepLink(deepLinkUrl);
return;
}
const notification = await Notifications.getInitialNotification();
if (notification && notification.payload?.type === 'message') {
launchAppFromNotification(notification);
return;
}
launchApp({launchType: LaunchType.Normal});
};
const launchAppFromDeepLink = (deepLinkUrl: string) => {
const props = getLaunchPropsFromDeepLink(deepLinkUrl);
launchApp(props);
};
const launchAppFromNotification = (notification: NotificationWithData) => {
const props = getLaunchPropsFromNotification(notification);
launchApp(props);
};
const launchApp = async (props: LaunchProps, resetNavigation = true) => {
let serverUrl;
switch (props?.launchType) {
case LaunchType.DeepLink:
if (props.extra?.type !== DeepLinkType.Invalid) {
const extra = props.extra as DeepLinkWithData;
serverUrl = extra.data?.serverUrl;
}
break;
case LaunchType.Notification: {
const extra = props.extra as NotificationWithData;
serverUrl = extra.payload?.server_url;
break;
}
}
serverUrl = await getActiveServerUrl();
if (serverUrl) {
const credentials = await getServerCredentials(serverUrl);
if (credentials) {
launchToChannel({...props, serverUrl}, resetNavigation);
return;
}
}
launchToServer(props, resetNavigation);
};
const launchToChannel = (props: LaunchProps, resetNavigation: Boolean) => {
// TODO: Use LaunchProps to fetch posts for channel and then load user profile, etc...
const passProps = {
skipMetrics: true,
...props,
};
if (resetNavigation) {
// eslint-disable-next-line no-console
console.log('Launch app in Channel screen');
resetToChannel(passProps);
return;
}
const title = '';
goToScreen(Screens.CHANNEL, title, passProps);
};
const launchToServer = (props: LaunchProps, resetNavigation: Boolean) => {
if (resetNavigation) {
resetToSelectServer(props);
return;
}
const title = '';
goToScreen(Screens.SERVER, title, {...props});
};
export const relaunchApp = (props: LaunchProps, resetNavigation = false) => {
launchApp(props, resetNavigation);
};
export const getLaunchPropsFromDeepLink = (deepLinkUrl: string): LaunchProps => {
const parsed = parseDeepLink(deepLinkUrl);
const launchProps: LaunchProps = {
launchType: LaunchType.DeepLink,
};
switch (parsed.type) {
case DeepLinkType.Invalid:
launchProps.launchError = true;
break;
case DeepLinkType.Channel: {
const parsedData = parsed.data as DeepLinkChannel;
(launchProps.extra as DeepLinkWithData).data = parsedData;
break;
}
case DeepLinkType.DirectMessage: {
const parsedData = parsed.data as DeepLinkDM;
(launchProps.extra as DeepLinkWithData).data = parsedData;
break;
}
case DeepLinkType.GroupMessage: {
const parsedData = parsed.data as DeepLinkGM;
(launchProps.extra as DeepLinkWithData).data = parsedData;
break;
}
case DeepLinkType.Permalink: {
const parsedData = parsed.data as DeepLinkPermalink;
(launchProps.extra as DeepLinkWithData).data = parsedData;
break;
}
}
return launchProps;
};
export const getLaunchPropsFromNotification = (notification: NotificationWithData): LaunchProps => {
const {payload} = notification;
const launchProps: LaunchProps = {
launchType: LaunchType.Notification,
};
if (payload?.server_url) {
(launchProps.extra as NotificationWithData) = notification;
} else {
launchProps.launchError = true;
}
return launchProps;
};