From 1d144cf19c617ad168d3ec605c6653d37a6ed2b3 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 8 Mar 2023 17:01:04 -0300 Subject: [PATCH] change videoConf saga to TS --- app/actions/actionsTypes.ts | 9 ++- app/actions/videoConf.ts | 40 ++++++++++- app/lib/hooks/useAppSelector.ts | 5 ++ app/sagas/videoConf.js | 38 ---------- app/sagas/videoConf.ts | 120 ++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 43 deletions(-) delete mode 100644 app/sagas/videoConf.js create mode 100644 app/sagas/videoConf.ts diff --git a/app/actions/actionsTypes.ts b/app/actions/actionsTypes.ts index f80ccdff6..328ba4913 100644 --- a/app/actions/actionsTypes.ts +++ b/app/actions/actionsTypes.ts @@ -84,5 +84,10 @@ export const ENCRYPTION = createRequestTypes('ENCRYPTION', ['INIT', 'STOP', 'DEC export const PERMISSIONS = createRequestTypes('PERMISSIONS', ['SET', 'UPDATE']); export const ROLES = createRequestTypes('ROLES', ['SET', 'UPDATE', 'REMOVE']); -export const VIDEO_CONF = createRequestTypes('VIDEO_CONF', ['HANDLE_INCOMING_WEBSOCKET_MESSAGES']); - +export const VIDEO_CONF = createRequestTypes('VIDEO_CONF', [ + 'HANDLE_INCOMING_WEBSOCKET_MESSAGES', + 'SET', + 'REMOVE', + 'CLEAR', + 'INIT_CALL' +]); diff --git a/app/actions/videoConf.ts b/app/actions/videoConf.ts index f11bcd052..ef9773db1 100644 --- a/app/actions/videoConf.ts +++ b/app/actions/videoConf.ts @@ -1,16 +1,50 @@ import { Action } from 'redux'; +import { ICallInfo } from '../reducers/videoConf'; import { VIDEO_CONF } from './actionsTypes'; -export interface IVideoConfGenericAction extends Action { +interface IHandleVideoConfIncomingWebsocketMessages extends Action { data: any; } -export type TActionUserTyping = IVideoConfGenericAction & Action; +export type TCallProps = { mic: boolean; cam: boolean; direct: boolean; roomId: string }; -export function handleVideoConfIncomingWebsocketMessages(data: any): IVideoConfGenericAction { +export interface IVideoConfGenericAction extends Action { + payload: ICallInfo; +} + +export type TActionVideoConf = IHandleVideoConfIncomingWebsocketMessages & IVideoConfGenericAction & Action; + +export function handleVideoConfIncomingWebsocketMessages(data: any): IHandleVideoConfIncomingWebsocketMessages { return { type: VIDEO_CONF.HANDLE_INCOMING_WEBSOCKET_MESSAGES, data }; } + +export function setVideoConfCall(payload: ICallInfo): IVideoConfGenericAction { + return { + type: VIDEO_CONF.SET, + payload + }; +} + +export function removeVideoConfCall(payload: ICallInfo): IVideoConfGenericAction { + return { + type: VIDEO_CONF.REMOVE, + payload + }; +} + +export function clearVideoConfCalls(): Action { + return { + type: VIDEO_CONF.CLEAR + }; +} + +export function initVideoCall(payload: TCallProps): Action & { payload: TCallProps } { + return { + type: VIDEO_CONF.INIT_CALL, + payload + }; +} diff --git a/app/lib/hooks/useAppSelector.ts b/app/lib/hooks/useAppSelector.ts index 58c524b6b..98231ab67 100644 --- a/app/lib/hooks/useAppSelector.ts +++ b/app/lib/hooks/useAppSelector.ts @@ -1,5 +1,10 @@ import { TypedUseSelectorHook, useSelector } from 'react-redux'; +import { select } from 'redux-saga/effects'; import { IApplicationState } from '../../definitions'; export const useAppSelector: TypedUseSelectorHook = useSelector; + +export function* appSelector(selector: (state: IApplicationState) => TSelected): Generator { + return yield select(selector); +} diff --git a/app/sagas/videoConf.js b/app/sagas/videoConf.js deleted file mode 100644 index c9f9a7b3a..000000000 --- a/app/sagas/videoConf.js +++ /dev/null @@ -1,38 +0,0 @@ -import { takeLatest } from 'redux-saga/effects'; - -import { VIDEO_CONF } from '../actions/actionsTypes'; - -const handleVideoConfIncomingWebsocketMessages = function* handleVideoConfIncomingWebsocketMessages({ data }) { - const { action, params } = data.action; - - if (!action || typeof action !== 'string') { - return; - } - if (!params || typeof params !== 'object' || !params.callId || !params.uid || !params.rid) { - return; - } - - console.log(action, params); - // switch (action) { - // case 'call': - // return this.onDirectCall(params); - // case 'canceled': - // return this.onDirectCallCanceled(params); - // case 'accepted': - // return this.onDirectCallAccepted(params); - // case 'rejected': - // return this.onDirectCallRejected(params); - // case 'confirmed': - // return this.onDirectCallConfirmed(params); - // case 'join': - // return this.onDirectCallJoined(params); - // case 'end': - // return this.onDirectCallEnded(params); - // } -}; - -const root = function* root() { - yield takeLatest(VIDEO_CONF.HANDLE_INCOMING_WEBSOCKET_MESSAGES, handleVideoConfIncomingWebsocketMessages); -}; - -export default root; diff --git a/app/sagas/videoConf.ts b/app/sagas/videoConf.ts new file mode 100644 index 000000000..2d9ce4479 --- /dev/null +++ b/app/sagas/videoConf.ts @@ -0,0 +1,120 @@ +import { Action } from 'redux'; +import { call, takeLatest } from 'typed-redux-saga'; + +import { VIDEO_CONF } from '../actions/actionsTypes'; +import { IVideoConfGenericAction, TCallProps } from '../actions/videoConf'; +import i18n from '../i18n'; +import { getSubscriptionByRoomId } from '../lib/database/services/Subscription'; +import { appSelector } from '../lib/hooks'; +import { callJitsi } from '../lib/methods'; +import { compareServerVersion, showErrorAlert } from '../lib/methods/helpers'; +import log from '../lib/methods/helpers/log'; +import { videoConfJoin } from '../lib/methods/videoConf'; +import { Services } from '../lib/services'; +import { ICallInfo } from '../reducers/videoConf'; + +type TGenerator = Generator; + +function* onDirectCall(payload: ICallInfo): TGenerator { + return null; +} + +function* onDirectCallCanceled(payload: ICallInfo): TGenerator { + return null; +} + +function* onDirectCallAccepted(payload: ICallInfo): TGenerator { + return null; +} + +function* onDirectCallRejected(payload: ICallInfo): TGenerator { + return null; +} + +function* onDirectCallConfirmed(payload: ICallInfo): TGenerator { + return null; +} + +function* onDirectCallJoined(payload: ICallInfo): TGenerator { + return null; +} + +function* onDirectCallEnded(payload: ICallInfo): TGenerator { + return null; +} + +function* handleVideoConfIncomingWebsocketMessages({ data }: { data: any }) { + const { action, params } = data.action; + + if (!action || typeof action !== 'string') { + return; + } + if (!params || typeof params !== 'object' || !params.callId || !params.uid || !params.rid) { + return; + } + const prop = { ...params, action }; + switch (action) { + case 'call': + yield call(onDirectCall, prop); + break; + case 'canceled': + yield call(onDirectCallCanceled, prop); + break; + case 'accepted': + yield call(onDirectCallAccepted, prop); + break; + case 'rejected': + yield call(onDirectCallRejected, prop); + break; + case 'confirmed': + yield call(onDirectCallConfirmed, prop); + break; + case 'join': + yield call(onDirectCallJoined, prop); + break; + case 'end': + yield call(onDirectCallEnded, prop); + break; + } +} + +function* initCall({ payload: { mic, cam, direct, roomId } }: { payload: TCallProps }) { + const serverVersion = yield* appSelector(state => state.server.version); + const isServer5OrNewer = compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '5.0.0'); + if (isServer5OrNewer) { + try { + const videoConfResponse = yield* call(Services.videoConferenceStart, roomId); + if (videoConfResponse.success) { + if (direct) { + // callUser({ uid: data.calleeId, rid: roomId, callId: data.callId }); + } else { + videoConfJoin(videoConfResponse.data.callId, cam, mic); + } + // setCalling(false); + } + } catch (e) { + // setCalling(false); + showErrorAlert(i18n.t('error-init-video-conf')); + log(e); + } + } else { + const sub = yield* call(getSubscriptionByRoomId, roomId); + if (sub) { + callJitsi({ room: sub, cam }); + // setCalling(false); + } + } +} + +interface IGenericAction extends Action { + type: string; +} + +export default function* root(): Generator { + yield takeLatest< + IGenericAction & { + data: any; + } + >(VIDEO_CONF.HANDLE_INCOMING_WEBSOCKET_MESSAGES, handleVideoConfIncomingWebsocketMessages); + yield takeLatest(VIDEO_CONF.INIT_CALL, initCall); +}