mattermost-mobile/app/products/calls/utils.ts
Elias Nahum b8c088cc70
Upgrade RN as well as update or replace other dependencies (#8011)
* start upgrade to RN 74

* migrate react-native-fs to expo-file-system

* exclude expo-asset module

* fix database manager remove directory

* fix: android network helper

* include expo on android

* temporarily disable android dep lock

* replace react-native-create-thumbnail with expo-video-thumnails

* update patches file version

* fix android build on 74

* create local library to replace MattermostManaged, Notifications and SplitView modules with new arch support

* migrate app to use new mattermost-rnutils library

* remove unused flipper class for android unsigned

* fix mattermost-rnutils android foldedObserver lifecycle

* use mattermost-rnutils on Android

* use mattermost-rnutils on iOS

* path react-native-navigation to not crash when activity is not NavigationActivity

* create local library for android share extension with new arch support

* Replace ShareModule with @mattermost/rnshare library

* remove ShareModule from android native code

* update react-intl

* update nodejs to 20.x.x npm to 10.x.x and dev deps

* update @gorhom/bottom-sheet

* use MattermostShare conditionally based on the platform

* update @react-native-camera-roll/camera-roll

* remove unused react-native-calendars

* fix metro config

* fix terminate session race condition

* remove unused analytics

* replace react-native-device-info with expo-application and expo-device

* update @react-native-clipboard/clipboard

* update @react-native-community/datetimepicker

* update @react-native-community/netinfo

* update @sentry/react-native

* update react-native-document-picker

* update react-native-gesture-handler

* update react-native-share

* update react-native-svg and react-native-svg-transformer

* update react-native-vector-icons

* update babel

* update react-native-shadow-2

* update semver

* remove react-native-svg-transformer and convert svg files to svg components

* fix @mattermost/rnshare new-arch build on android

* remove react-native-create-thumbnail resolution in build.gradle

* create @mattermost/hardware-keyboard library to replace hw-keyboard-events

* fix hardware-keyboard library

* fix rnutils library

* create @mattermost/keyboard-tracker library

* replace react-native-keyboard-tracking-view with @mattermost/keyboard-tracker

* fix: rnutils to not crash on lateinit context

* fix: rnutils delete database

* revert changes to session_manager

* Removed react-native-webview and added expo-web-browser instead

With expo-web-browser we no longer need the webview for SSO login
the SSO login is now done by using "custom Chrome tabs" on Android
and ASWebAuthenticationSession on iOS

* remove patch for react-native-keyboard-tracking-view

* replace react-native-linear-gradient with expo-linear-gradient

* replace react-native-in-app-review with expo-store-review

* fix: shared group database directory on ios

* replace react-native-fast-image with expo-image

* remove unusued type def for react-native-keyboard-tracking-view

* replace react-native-elements and react-native-button with @rneui

* node version

* update sizzling methods

* fix tests using jest-expo

* replace jail-monkey with expo-device

* update babel deps

* update typescript eslint

* update rn and expo

* react-native-document-picker @react-native-camera-roll/camera-roll @react-native-community/datetimepicker react-native-reanimated react-native-safe-area-context

* update patches

* update @sentry/react-native

* upgrade react-native-navigation

* update expo & expo-image

* upgrade to working version of @sentry/react-native

* update node, cocoapods, bundler, fastlane versions

* @testing-library/react-native and eslint-plugin-jest

* fix: FloatingTextInput causing a crash with reanimated

* update sentry, localize, @types/lodash and uuid

* fix floating text input label

* update react-native-video

* fix: cannot calculate shadow efficiently on some components

* fix: reduce motion warning for bottomSheet

* fix: shadow on YouTube component

* update react-native-webrtc expo and @typescript-eslint

* audit fix

* fix swizzling bad merge

* temp use of github dependency for @mattermost libraries

* feedback review

* feedback review

* npm audit fix

* update bundle deps

* update @mattermost/react-native-turbo-log

* update deps
2024-06-19 09:33:45 +08:00

244 lines
7.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {makeCallsBaseAndBadgeRGB, rgbToCSS} from '@mattermost/calls';
import {Alert} from 'react-native';
import {SelectedTrackType, TextTrackType, type ISO639_1, type SelectedTrack, type TextTracks} from 'react-native-video';
import {buildFileUrl} from '@actions/remote/file';
import {Calls, Post} from '@constants';
import {NOTIFICATION_SUB_TYPE} from '@constants/push_notification';
import {isMinimumServerVersion} from '@utils/helpers';
import {displayUsername} from '@utils/user';
import type {
CallsConfigState,
CallSession,
CallsTheme,
CallsVersion,
} from '@calls/types/calls';
import type {CallsConfig, Caption} from '@mattermost/calls/lib/types';
import type PostModel from '@typings/database/models/servers/post';
import type UserModel from '@typings/database/models/servers/user';
import type {IntlShape} from 'react-intl';
import type {RTCIceServer} from 'react-native-webrtc';
const callsMessageRegex = /^\u200b.* is inviting you to a call$/;
export function sortSessions(locale: string, teammateNameDisplay: string, sessions?: Dictionary<CallSession>, presenterID?: string): CallSession[] {
if (!sessions) {
return [];
}
const sessns = Object.values(sessions);
return sessns.sort(sortByName(locale, teammateNameDisplay)).sort(sortByState(presenterID));
}
const sortByName = (locale: string, teammateNameDisplay: string) => {
return (a: CallSession, b: CallSession) => {
const nameA = displayUsername(a.userModel, locale, teammateNameDisplay);
const nameB = displayUsername(b.userModel, locale, teammateNameDisplay);
return nameA.localeCompare(nameB);
};
};
const sortByState = (presenterID?: string) => {
return (a: CallSession, b: CallSession) => {
if (a.sessionId === presenterID) {
return -1;
} else if (b.sessionId === presenterID) {
return 1;
}
if (a.raisedHand && !b.raisedHand) {
return -1;
} else if (b.raisedHand && !a.raisedHand) {
return 1;
} else if (a.raisedHand && b.raisedHand) {
return a.raisedHand - b.raisedHand;
}
if (!a.muted && b.muted) {
return -1;
} else if (!b.muted && a.muted) {
return 1;
}
return 0;
};
};
export function getHandsRaised(sessions: Dictionary<CallSession>) {
return Object.values(sessions).filter((s) => s.raisedHand);
}
export function getHandsRaisedNames(sessions: CallSession[], sessionId: string, locale: string, teammateNameDisplay: string, intl: IntlShape) {
return sessions.sort((a, b) => a.raisedHand - b.raisedHand).map((p) => {
if (p.sessionId === sessionId) {
return intl.formatMessage({id: 'mobile.calls_you_2', defaultMessage: 'You'});
}
return displayUsername(p.userModel, locale, teammateNameDisplay);
});
}
export function isSupportedServerCalls(serverVersion?: string) {
if (serverVersion) {
return isMinimumServerVersion(
serverVersion,
Calls.RequiredServer.MAJOR_VERSION,
Calls.RequiredServer.MIN_VERSION,
Calls.RequiredServer.PATCH_VERSION,
);
}
return false;
}
export function isMultiSessionSupported(callsVersion: CallsVersion) {
return isMinimumServerVersion(
callsVersion.version,
Calls.MultiSessionCallsVersion.MAJOR_VERSION,
Calls.MultiSessionCallsVersion.MIN_VERSION,
Calls.MultiSessionCallsVersion.PATCH_VERSION,
);
}
export function isHostControlsAllowed(config: CallsConfigState) {
return Boolean(config.HostControlsAllowed);
}
export function isCallsCustomMessage(post: PostModel | Post): boolean {
return Boolean(post.type && post.type === Post.POST_TYPES.CUSTOM_CALLS);
}
export function idsAreEqual(a: string[], b: string[]) {
if (a.length !== b.length) {
return false;
}
// We can assume ids are unique
// Doing a quick search indicated objects are tuned better than Map or Set
const obj = a.reduce((prev, cur) => {
prev[cur] = true;
return prev;
}, {} as Record<string, boolean>);
for (let i = 0; i < b.length; i++) {
if (!obj.hasOwnProperty(b[i])) {
return false;
}
}
return true;
}
export function errorAlert(error: string, intl: IntlShape) {
Alert.alert(
intl.formatMessage({
id: 'mobile.calls_error_title',
defaultMessage: 'Error',
}),
intl.formatMessage({
id: 'mobile.calls_error_message',
defaultMessage: 'Error: {error}',
}, {error}),
);
}
export function getICEServersConfigs(config: CallsConfig): RTCIceServer[] {
// if ICEServersConfigs is set, we can trust this to be complete and
// coming from an updated API.
if (config.ICEServersConfigs && config.ICEServersConfigs.length > 0) {
return config.ICEServersConfigs;
}
// otherwise we revert to using the now deprecated field.
if (config.ICEServers && config.ICEServers.length > 0) {
return [
{
urls: config.ICEServers,
},
];
}
return [];
}
export function makeCallsTheme(theme: Theme): CallsTheme {
const {baseColorRGB, badgeBgRGB} = makeCallsBaseAndBadgeRGB(theme.sidebarBg);
const newTheme = {...theme} as CallsTheme;
newTheme.callsBg = rgbToCSS(baseColorRGB);
newTheme.callsBgRgb = `${baseColorRGB.r},${baseColorRGB.g},${baseColorRGB.b}`;
newTheme.callsBadgeBg = rgbToCSS(badgeBgRGB);
return newTheme;
}
interface HasUserId {
userId: string;
}
export function userIds<T extends HasUserId>(hasUserId: T[]): string[] {
const ids: string[] = [];
const seen: Record<string, boolean> = {};
for (const p of hasUserId) {
if (!seen[p.userId]) {
ids.push(p.userId);
seen[p.userId] = true;
}
}
return ids;
}
export function fillUserModels(sessions: Dictionary<CallSession>, models: UserModel[]) {
const idToModel = models.reduce((accum, cur) => {
accum[cur.id] = cur;
return accum;
}, {} as Dictionary<UserModel>);
const next = {...sessions};
for (const participant of Object.values(next)) {
participant.userModel = idToModel[participant.userId];
}
return sessions;
}
export function isCallsStartedMessage(payload?: NotificationData) {
if (payload?.sub_type === NOTIFICATION_SUB_TYPE.CALLS) {
return true;
}
// MM-55506 - Remove once we can assume MM servers will be >= 9.3.0, mobile will be >= 2.11.0,
// calls will be >= 0.21.0, and push proxy will be >= 5.27.0
return (payload?.message === 'You\'ve been invited to a call' || callsMessageRegex.test(payload?.message || ''));
}
export const hasCaptions = (postProps?: Record<string, any> & { captions?: Caption[] }): boolean => {
return !(!postProps || !postProps.captions?.[0]);
};
export const getTranscriptionUri = (serverUrl: string, postProps?: Record<string, any> & { captions?: Caption[] }): {
tracks?: TextTracks;
selected: SelectedTrack;
} => {
// Note: We're not using hasCaptions above because this tells typescript that the caption exists later.
// We could use some fancy typescript to do the same, but it's not worth the complexity.
if (!postProps || !postProps.captions?.[0]) {
return {
tracks: undefined,
selected: {type: SelectedTrackType.DISABLED, value: ''},
};
}
const tracks: TextTracks = postProps.captions.map((t) => ({
title: t.title,
language: t.language as ISO639_1,
type: TextTrackType.VTT,
uri: buildFileUrl(serverUrl, t.file_id),
}));
return {
tracks,
selected: {type: SelectedTrackType.INDEX, value: 0},
};
};