Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions template/customization-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export {default as useLocalAudio} from '../src/utils/useLocalAudio';
export {default as useLocalVideo} from '../src/utils/useLocalVideo';
export type {LanguageType} from '../src/subComponents/caption/utils';
export {default as useSpeechToText} from '../src/utils/useSpeechToText';
export {default as useChatLogin} from '../src/utils/useChatLogin';
8 changes: 4 additions & 4 deletions template/src/SDKAppWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export interface AppBuilderSdkApiInterface {
joinRoom: (
roomDetails: string | meetingData,
userName?: string,
preference?: {disableShareTile: boolean},
preference?: {disableShareTile?: boolean; preventChatAutoLogin?: boolean},
) => Promise<meetingData>;
joinPrecall: (
roomDetails: string | meetingData,
userName?: string,
skipPrecall?: boolean,
preference?: {disableShareTile: boolean},
preference?: {disableShareTile?: boolean; preventChatAutoLogin?: boolean},
) => Promise<
[
meetingData,
Expand Down Expand Up @@ -73,7 +73,7 @@ export const AppBuilderSdkApi: AppBuilderSdkApiInterface = {
logger.log(LogSource.SDK, 'Event', 'emiting event for joinRoom - join', {
room: roomDetails,
userName: userName,
preference: preference
preference: preference,
});
return await SDKMethodEventsManager.emit(
'join',
Expand All @@ -87,7 +87,7 @@ export const AppBuilderSdkApi: AppBuilderSdkApiInterface = {
logger.log(LogSource.SDK, 'Event', 'emiting event for joinPrecall - join', {
room: roomDetails,
userName: userName,
preference: preference
preference: preference,
});
if (!$config.PRECALL) {
logger.error(
Expand Down
5 changes: 4 additions & 1 deletion template/src/components/SdkApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ type SdkApiContextInterface = {
meetingDetails?: Partial<RoomInfoContextInterface['data']>;
userName: string;
skipPrecall: boolean;
preference: {disableShareTile: boolean};
preference: {
disableShareTile?: boolean;
preventChatAutoLogin?: boolean;
};
promise: extractPromises<_InternalSDKMethodEventsMap['join']>;
}
| {
Expand Down
1 change: 1 addition & 0 deletions template/src/components/chat-messages/useChatMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface ChatOption {
file_name: string;
file_url: string;
from_platform?: string;
channel?: string;
};
url?: string;
}
Expand Down
102 changes: 76 additions & 26 deletions template/src/components/chat/chatConfigure.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import {timeNow} from '../../rtm/utils';
import Share from 'react-native-share';
import RNFetchBlob from 'rn-fetch-blob';
import {logger, LogSource} from '../../logger/AppBuilderLogger';
import LocalEventEmitter, {
LocalEventsEnum,
} from '../../rtm-events-api/LocalEvents';
import events from '../../rtm-events-api';
import {EventNames} from '../../rtm-events';

interface ChatMessageAttributes {
file_ext?: string;
Expand All @@ -34,6 +39,7 @@ interface ChatMessageAttributes {
interface chatConfigureContextInterface {
open: boolean;
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
allowChatLogin: boolean;
sendChatSDKMessage: (
option: ChatOption,
callback: ChatMessageStatusCallback,
Expand All @@ -51,6 +57,7 @@ export const chatConfigureContext =
createContext<chatConfigureContextInterface>({
open: false,
setOpen: () => {},
allowChatLogin: false,
sendChatSDKMessage: () => {},
deleteChatUser: () => {},
downloadAttachment: () => {},
Expand All @@ -59,16 +66,19 @@ export const chatConfigureContext =

const ChatConfigure = ({children}) => {
const [open, setOpen] = useState(false);
const {data} = useRoomInfo();
const {data, roomPreference} = useRoomInfo();
const connRef = React.useRef(null);
const {defaultContent} = useContent();
const defaultContentRef = React.useRef(defaultContent);

const chatClient = ChatClient.getInstance();
const chatManager = chatClient.chatManager;

const localUid = data?.uid?.toString();
const agoraToken = data?.chat?.user_token;
const {store} = React.useContext(StorageContext);
const [allowChatLogin, setAllowChatLogin] = useState(
() => !roomPreference.preventChatAutoLogin,
);

const {
addMessageToPrivateStore,
showMessageNotification,
Expand All @@ -77,29 +87,54 @@ const ChatConfigure = ({children}) => {
removeMessageFromPrivateStore,
} = useChatMessages();

React.useEffect(() => {
defaultContentRef.current = defaultContent;
}, [defaultContent]);
const enableChatLoginCallback = () => {
console.warn('allow chat login callback', allowChatLogin);
setAllowChatLogin(true);
};

const logout = async () => {
try {
await chatClient?.logout();
console.warn('chat logout success');
logger.log(
LogSource.Internals,
'CHAT',
`Logged out User ${localUid} from Agora Chat Server`,
);
} catch (error) {
console.warn('logout failed');
logger.log(
LogSource.Internals,
'CHAT',
`Failed Logging out User ${localUid} from Agora Chat Server`,
);
}
};

const enableChatLogoutCallback = async () => {
console.warn('allow chat logout callback', allowChatLogin);
setAllowChatLogin(false);
await logout();
};

useEffect(() => {
const logout = async () => {
try {
await chatClient.logout();
console.warn('logout success');
logger.log(
LogSource.Internals,
'CHAT',
`Logged out User ${localUid} from Agora Chat Server`,
);
} catch (error) {
console.warn('logout failed');
logger.log(
LogSource.Internals,
'CHAT',
`Failed Logging out User ${localUid} from Agora Chat Server`,
);
}
};
// Handle Chat login for self and other users
LocalEventEmitter.on(
LocalEventsEnum.ENABLE_CHAT_LOGIN,
enableChatLoginCallback,
);
events.on(EventNames.ENABLE_CHAT_LOGIN, () => {
enableChatLoginCallback();
});

LocalEventEmitter.on(
LocalEventsEnum.ENABLE_CHAT_LOGOUT,
enableChatLogoutCallback,
);
events.on(EventNames.ENABLE_CHAT_LOGOUT, () => {
enableChatLogoutCallback();
});

const setupMessageListener = () => {
const msgListerner: ChatMessageEventListener = {
onMessagesRecalled: (messages: ChatMessage[]) => {
Expand Down Expand Up @@ -322,12 +357,26 @@ const ChatConfigure = ({children}) => {
console.warn('chat sdk: init error', error);
}
};
if (allowChatLogin) {
initializeChatSDK();
} else {
console.warn('delay chat login');
}

initializeChatSDK();
return () => {
logout();
LocalEventEmitter.off(
LocalEventsEnum.ENABLE_CHAT_LOGIN,
enableChatLoginCallback,
);
events.off(EventNames.ENABLE_CHAT_LOGIN, enableChatLoginCallback);
LocalEventEmitter.off(
LocalEventsEnum.ENABLE_CHAT_LOGOUT,
enableChatLogoutCallback,
);
events.off(EventNames.ENABLE_CHAT_LOGOUT, enableChatLogoutCallback);
};
}, []);
}, [allowChatLogin]);

const sendChatSDKMessage = (
option: ChatOption,
Expand Down Expand Up @@ -489,6 +538,7 @@ const ChatConfigure = ({children}) => {
value={{
open,
setOpen,
allowChatLogin,
deleteChatUser,
sendChatSDKMessage,
downloadAttachment,
Expand Down
84 changes: 70 additions & 14 deletions template/src/components/chat/chatConfigure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {createHook} from 'customization-implementation';
import React, {createContext, useState, useEffect} from 'react';
import AgoraChat from 'agora-chat';
import {useRoomInfo} from '../room-info/useRoomInfo';
import events from '../../rtm-events-api';
import {EventNames} from '../../rtm-events';

import {UidType, useContent} from 'customization-api';
import {
Expand All @@ -18,6 +20,9 @@ import {
useChatUIControls,
} from '../../components/chat-ui/useChatUIControls';
import {logger, LogSource} from '../../logger/AppBuilderLogger';
import LocalEventEmitter, {
LocalEventsEnum,
} from '../../rtm-events-api/LocalEvents';

export interface FileObj {
url: string;
Expand All @@ -29,6 +34,7 @@ export interface FileObj {
interface chatConfigureContextInterface {
open: boolean;
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
allowChatLogin: boolean;
sendChatSDKMessage: (option: ChatOption, messageStatusCallback?: any) => void;
deleteChatUser: () => void;
downloadAttachment: (fileName: string, fileUrl: string) => void;
Expand All @@ -44,6 +50,7 @@ export const chatConfigureContext =
createContext<chatConfigureContextInterface>({
open: false,
setOpen: () => {},
allowChatLogin: false,
sendChatSDKMessage: () => {},
deleteChatUser: () => {},
downloadAttachment: () => {},
Expand All @@ -53,12 +60,20 @@ export const chatConfigureContext =

const ChatConfigure = ({children}) => {
const [open, setOpen] = useState(false);
const {data} = useRoomInfo();
const {data, roomPreference} = useRoomInfo();
const [allowChatLogin, setAllowChatLogin] = useState(
() => !roomPreference.preventChatAutoLogin,
);

// exponse onClick Chat so that this can be set truel
//const allowChatLogin = !roomPreference?.preventChatAutoLogin;
logger.debug(LogSource.Internals, 'CHAT', `Allow Chat Login`, allowChatLogin);

const connRef = React.useRef(null);
const {defaultContent} = useContent();

const {privateChatUser, setUploadStatus, setUploadedFiles, uploadedFiles} =
useChatUIControls();
const defaultContentRef = React.useRef(defaultContent);

const {
addMessageToPrivateStore,
showMessageNotification,
Expand All @@ -68,13 +83,39 @@ const ChatConfigure = ({children}) => {
} = useChatMessages();
const {store} = React.useContext(StorageContext);

React.useEffect(() => {
defaultContentRef.current = defaultContent;
}, [defaultContent]);

let newConn = null;

const enableChatLoginCallback = () => {
logger.debug(LogSource.Internals, 'CHAT', `enable chat login callback`);
setAllowChatLogin(true);
};

const enableChatLogoutCallback = () => {
logger.debug(LogSource.Internals, 'CHAT', `enable chat logout callback`);
setAllowChatLogin(false);
if (connRef.current) {
connRef.current.close();
}
};

useEffect(() => {
// Handle Chat login for self and other users
LocalEventEmitter.on(
LocalEventsEnum.ENABLE_CHAT_LOGIN,
enableChatLoginCallback,
);
events.on(EventNames.ENABLE_CHAT_LOGIN, () => {
enableChatLoginCallback();
});

LocalEventEmitter.on(
LocalEventsEnum.ENABLE_CHAT_LOGOUT,
enableChatLogoutCallback,
);
events.on(EventNames.ENABLE_CHAT_LOGOUT, () => {
enableChatLogoutCallback();
});

const initializeChatSDK = async () => {
try {
// disable Chat SDK logs
Expand Down Expand Up @@ -307,16 +348,30 @@ const ChatConfigure = ({children}) => {
};

// initializing chat sdk
initializeChatSDK();
if (allowChatLogin) {
initializeChatSDK();
}
return () => {
newConn.close();
logger.log(
LogSource.Internals,
'CHAT',
`Logging out User ${data.uid} from Agora Chat Server`,
if (newConn) {
newConn.close();
logger.log(
LogSource.Internals,
'CHAT',
`Logging out User ${data.uid} from Agora Chat Server`,
);
}
LocalEventEmitter.off(
LocalEventsEnum.ENABLE_CHAT_LOGIN,
enableChatLoginCallback,
);
events.off(EventNames.ENABLE_CHAT_LOGIN, enableChatLoginCallback);
LocalEventEmitter.off(
LocalEventsEnum.ENABLE_CHAT_LOGOUT,
enableChatLogoutCallback,
);
events.off(EventNames.ENABLE_CHAT_LOGOUT, enableChatLogoutCallback);
};
}, []);
}, [allowChatLogin]);

const sendChatSDKMessage = (
option: ChatOption,
Expand Down Expand Up @@ -492,6 +547,7 @@ const ChatConfigure = ({children}) => {
value={{
open,
setOpen,
allowChatLogin,
sendChatSDKMessage,
deleteChatUser,
downloadAttachment,
Expand Down
1 change: 1 addition & 0 deletions template/src/components/room-info/useRoomInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const RoomInfoDefaultValue: RoomInfoContextInterface = {
},
roomPreference: {
disableShareTile: false,
preventChatAutoLogin: false,
},
};

Expand Down
Loading