2023-07-04 00:03:39 +00:00
|
|
|
import { Camera } from 'expo-camera';
|
2023-03-01 18:26:56 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
2023-07-04 00:03:39 +00:00
|
|
|
import { useActionSheet } from '../../../containers/ActionSheet';
|
|
|
|
import { SubscriptionType } from '../../../definitions';
|
|
|
|
import i18n from '../../../i18n';
|
|
|
|
import { getUserSelector } from '../../../selectors/login';
|
|
|
|
import { getSubscriptionByRoomId } from '../../database/services/Subscription';
|
|
|
|
import { compareServerVersion, showErrorAlert } from '../../methods/helpers';
|
|
|
|
import { handleAndroidBltPermission } from '../../methods/videoConf';
|
|
|
|
import { Services } from '../../services';
|
|
|
|
import { useAppSelector } from '../useAppSelector';
|
|
|
|
import { useSnaps } from '../useSnaps';
|
|
|
|
import StartACallActionSheet from './StartACallActionSheet';
|
2023-03-01 18:26:56 +00:00
|
|
|
|
|
|
|
const availabilityErrors = {
|
|
|
|
NOT_CONFIGURED: 'video-conf-provider-not-configured',
|
|
|
|
NOT_ACTIVE: 'no-active-video-conf-provider',
|
|
|
|
NO_APP: 'no-videoconf-provider-app'
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
const handleErrors = (isAdmin: boolean, error: typeof availabilityErrors[keyof typeof availabilityErrors]) => {
|
|
|
|
if (isAdmin) return showErrorAlert(i18n.t(`admin-${error}-body`), i18n.t(`admin-${error}-header`));
|
|
|
|
return showErrorAlert(i18n.t(`${error}-body`), i18n.t(`${error}-header`));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useVideoConf = (rid: string): { showInitCallActionSheet: () => Promise<void>; showCallOption: boolean } => {
|
|
|
|
const [showCallOption, setShowCallOption] = useState(false);
|
|
|
|
|
|
|
|
const serverVersion = useAppSelector(state => state.server.version);
|
|
|
|
const jitsiEnabled = useAppSelector(state => state.settings.Jitsi_Enabled);
|
|
|
|
const jitsiEnableTeams = useAppSelector(state => state.settings.Jitsi_Enable_Teams);
|
|
|
|
const jitsiEnableChannels = useAppSelector(state => state.settings.Jitsi_Enable_Channels);
|
|
|
|
const user = useAppSelector(state => getUserSelector(state));
|
|
|
|
|
2023-07-04 00:03:39 +00:00
|
|
|
const [permission, requestPermission] = Camera.useCameraPermissions();
|
|
|
|
|
2023-03-01 18:26:56 +00:00
|
|
|
const isServer5OrNewer = compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '5.0.0');
|
|
|
|
|
|
|
|
const { showActionSheet } = useActionSheet();
|
2023-05-19 17:40:31 +00:00
|
|
|
const snaps = useSnaps(404);
|
2023-03-01 18:26:56 +00:00
|
|
|
|
2023-04-10 20:54:02 +00:00
|
|
|
const handleShowCallOption = async () => {
|
2023-03-01 18:26:56 +00:00
|
|
|
if (isServer5OrNewer) return setShowCallOption(true);
|
2023-04-10 20:54:02 +00:00
|
|
|
const room = await getSubscriptionByRoomId(rid);
|
2023-03-01 18:26:56 +00:00
|
|
|
|
2023-04-10 20:54:02 +00:00
|
|
|
if (room) {
|
|
|
|
const isJitsiDisabledForTeams = room.teamMain && !jitsiEnableTeams;
|
|
|
|
const isJitsiDisabledForChannels = !room.teamMain && (room.t === 'p' || room.t === 'c') && !jitsiEnableChannels;
|
|
|
|
|
|
|
|
if (room.t === SubscriptionType.DIRECT) return setShowCallOption(!!jitsiEnabled);
|
|
|
|
if (room.t === SubscriptionType.CHANNEL) return setShowCallOption(!isJitsiDisabledForChannels);
|
|
|
|
if (room.t === SubscriptionType.GROUP) return setShowCallOption(!isJitsiDisabledForTeams);
|
|
|
|
}
|
2023-03-01 18:26:56 +00:00
|
|
|
|
|
|
|
return setShowCallOption(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const canInitAnCall = async () => {
|
|
|
|
if (isServer5OrNewer) {
|
|
|
|
try {
|
|
|
|
await Services.videoConferenceGetCapabilities();
|
|
|
|
return true;
|
|
|
|
} catch (error: any) {
|
|
|
|
const isAdmin = !!['admin'].find(role => user.roles?.includes(role));
|
|
|
|
switch (error?.error) {
|
|
|
|
case availabilityErrors.NOT_CONFIGURED:
|
|
|
|
return handleErrors(isAdmin, availabilityErrors.NOT_CONFIGURED);
|
|
|
|
case availabilityErrors.NOT_ACTIVE:
|
|
|
|
return handleErrors(isAdmin, availabilityErrors.NOT_ACTIVE);
|
|
|
|
case availabilityErrors.NO_APP:
|
|
|
|
return handleErrors(isAdmin, availabilityErrors.NO_APP);
|
|
|
|
default:
|
|
|
|
return handleErrors(isAdmin, availabilityErrors.NOT_CONFIGURED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const showInitCallActionSheet = async () => {
|
|
|
|
const canInit = await canInitAnCall();
|
|
|
|
if (canInit) {
|
|
|
|
showActionSheet({
|
2023-07-04 00:03:39 +00:00
|
|
|
children: <StartACallActionSheet rid={rid} />,
|
2023-03-01 18:26:56 +00:00
|
|
|
snaps
|
|
|
|
});
|
2023-07-04 00:03:39 +00:00
|
|
|
if (!permission?.granted) {
|
|
|
|
requestPermission();
|
|
|
|
handleAndroidBltPermission();
|
|
|
|
}
|
2023-03-01 18:26:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-04-10 20:54:02 +00:00
|
|
|
handleShowCallOption();
|
2023-03-01 18:26:56 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return { showInitCallActionSheet, showCallOption };
|
|
|
|
};
|