@sendbird/calls-react-native
Version:
Sendbird Calls SDK for React Native: Empower React Native apps with seamless audio, video, and group calling. Build interactive communication easily.
426 lines (415 loc) • 13.2 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Platform } from 'react-native';
import { NativeQueryType, RoomState, RoomType } from '../types';
import { Logger } from '../utils/logger';
import Version from '../version';
import { DirectCallLogListQuery, RoomListQuery } from './BridgedQuery';
import { DirectCall } from './DirectCall';
import { CallsEvent, DefaultEventType } from './NativeBinder';
import { Room } from './Room';
/**
* SendbirdCallsModule class for SendbirdCalls
*/
export default class SendbirdCallsModule {
constructor(binder) {
this.binder = binder;
_defineProperty(this, "_applicationId", '');
_defineProperty(this, "_initialized", false);
_defineProperty(this, "_currentUser", null);
_defineProperty(this, "_sendbirdCallListener", null);
/**
* Set logger level
*
* @since 1.1.0
* */
_defineProperty(this, "setLoggerLevel", level => {
Logger.setLogLevel(level);
this.binder.nativeModule.setLoggerLevel(level);
});
/**
* Gets the constants from React-Native Native module
* Returns the object
*
* @since 1.0.0
*/
_defineProperty(this, "getConstants", () => {
var _this$binder$nativeMo, _this$binder$nativeMo2;
// @ts-ignore
return ((_this$binder$nativeMo = (_this$binder$nativeMo2 = this.binder.nativeModule).getConstants) === null || _this$binder$nativeMo === void 0 ? void 0 : _this$binder$nativeMo.call(_this$binder$nativeMo2)) ?? {
NATIVE_SDK_VERSION: ''
};
});
/**
* Adds sound used in DirectCall such as ringtone and some sound effects with its file name with extension
*
* @iOS bundle file name
* @Android res/raw file name
*
* @since 1.0.0
*/
_defineProperty(this, "addDirectCallSound", (type, fileName) => {
let name = fileName;
if (Platform.OS === 'android') {
const idx = fileName.lastIndexOf('.');
if (idx) name = fileName.slice(0, idx);
}
this.binder.nativeModule.addDirectCallSound(type, name);
});
/**
* Removes sound used in {@link DirectCall} with {@link SoundType} value.
*
* @since 1.0.0
*/
_defineProperty(this, "removeDirectCallSound", type => {
this.binder.nativeModule.removeDirectCallSound(type);
});
/**
* Enables / disables dial sound used in {@link DirectCall} even when the device is in silent mode.
* Call this method right after {@link addDirectCallSound}.
*
* @since 1.0.0
*/
_defineProperty(this, "setDirectCallDialingSoundOnWhenSilentOrVibrateMode", enabled => {
this.binder.nativeModule.setDirectCallDialingSoundOnWhenSilentOrVibrateMode(enabled);
});
/**
* Gets the current `User` from native
* Returns the current `User`. If SendbirdCalls is not authenticated, `null` will be returned.
*
* @since 1.0.0
*/
_defineProperty(this, "getCurrentUser", async () => {
this._currentUser = await this.binder.nativeModule.getCurrentUser();
return this.currentUser;
});
/**
* Gets call from call ID or call UUID
*
* @since 1.0.0
*/
_defineProperty(this, "getDirectCall", async callId => {
const callProps = await this.binder.nativeModule.getDirectCall(callId);
return DirectCall.get(this.binder, callProps);
});
/**
* Initializes SendbirdCalls.
*
* @since 1.0.0
*/
_defineProperty(this, "initialize", appId => {
if (this.initialized) {
if (this.applicationId !== appId) {
return this._init(appId);
} else {
return this.initialized;
}
} else {
return this._init(appId);
}
});
_defineProperty(this, "_init", appId => {
this.Logger.info('[SendbirdCalls]', 'initialize()');
DirectCall.poolRelease();
Room.poolRelease();
if (!this.initialized) {
this.binder.addListener(CallsEvent.DEFAULT, ({
type,
data
}) => {
if (type === DefaultEventType.ON_RINGING) {
var _this$_sendbirdCallLi;
this.Logger.info('[SendbirdCalls]', 'onRinging', data.callId);
(_this$_sendbirdCallLi = this._sendbirdCallListener) === null || _this$_sendbirdCallLi === void 0 || _this$_sendbirdCallLi.onRinging(data);
}
});
}
this.binder.nativeModule.initialize(appId);
this._applicationId = appId;
this._initialized = true;
return this.initialized;
});
/**
* Authenticates.
*
* @since 1.0.0
*/
_defineProperty(this, "authenticate", async authParams => {
this._currentUser = await this.binder.nativeModule.authenticate(authParams);
return this.currentUser;
});
/**
* Deauthenticates.
*
* @since 1.0.0
*/
_defineProperty(this, "deauthenticate", async () => {
await this.binder.nativeModule.deauthenticate();
this._currentUser = null;
});
/**
* Registers push token for current user.
*
* on iOS, push token means APNS token.
* on Android, push token means FCM token.
*
* ```ts
* if (Platform.OS === 'android') {
* const fcmToken = await messaging().getToken();
* await SendbirdCalls.registerPushToken(fcmToken);
* }
* if (Platform.OS === 'ios') {
* const apnsToken = await messaging().getAPNSToken();
* await SendbirdCalls.registerPushToken(apnsToken);
* }
* ```
*
* @since 1.0.0
*/
_defineProperty(this, "registerPushToken", async (token, unique = true) => {
await this.binder.nativeModule.registerPushToken(token, unique);
});
/**
* Unregisters push token for current user.
*
* @since 1.0.0
*/
_defineProperty(this, "unregisterPushToken", async token => {
await this.binder.nativeModule.unregisterPushToken(token);
});
/**
* To receive native-like calls while an app is in the background or closed, a device registration token must be registered to the server.
* Register a device push token after authentication has completed using the `SendbirdCalls.ios_registerVoIPPushToken()` method.
*
* @platform iOS
* @since 1.0.0
*/
_defineProperty(this, "ios_registerVoIPPushToken", async (token, unique = true) => {
if (Platform.OS !== 'ios') return;
await this.binder.nativeModule.registerVoIPPushToken(token, unique);
});
/**
* Unregisters a VoIP push token of specific device.
* You will not receive VoIP push notification for a call anymore.
*
* @platform iOS
* @since 1.0.0
*/
_defineProperty(this, "ios_unregisterVoIPPushToken", async token => {
if (Platform.OS !== 'ios') return;
await this.binder.nativeModule.unregisterVoIPPushToken(token);
});
/**
* Show-up a view that allows user to change the system audio route.
* [AVRoutePickerView](https://developer.apple.com/documentation/avkit/avroutepickerview) in iOS 11 or later
*
* @platform iOS
* @since 1.0.0
*/
_defineProperty(this, "ios_routePickerView", () => {
if (Platform.OS !== 'ios') return;
this.binder.nativeModule.routePickerView();
});
/**
* Handles Firebase message data.
* Returns true if Sendbird call message. Otherwise false.
*
* @platform Android
* @since 1.0.0
*/
_defineProperty(this, "android_handleFirebaseMessageData", data => {
if (Platform.OS !== 'android' || !(data !== null && data !== void 0 && data['sendbird_call'])) {
return false;
} else {
//@ts-ignore
this.binder.nativeModule.handleFirebaseMessageData(data);
return true;
}
});
/**
* Creates direct call log list query.
*
* @since 1.0.0
*/
_defineProperty(this, "createDirectCallLogListQuery", async (params = {}) => {
const queryKey = await this.binder.nativeModule.createDirectCallLogListQuery(params);
return new DirectCallLogListQuery(queryKey, NativeQueryType.DIRECT_CALL_LOG, this.binder);
});
/**
* Creates a query for room list with specified parameters.
*
* @since 1.0.0
*/
_defineProperty(this, "createRoomListQuery", async (params = {}) => {
const queryKey = await this.binder.nativeModule.createRoomListQuery(params);
return new RoomListQuery(queryKey, NativeQueryType.ROOM_LIST, this.binder);
});
/**
* Updates custom items for a given call ID.
*
* @since 1.1.9
*/
_defineProperty(this, "updateCustomItems", async (callId, customItems) => {
const result = await this.binder.nativeModule.updateCustomItems(callId, customItems);
if (result && result.updatedItems) {
DirectCall.updateCustomItems(callId, result.updatedItems);
}
return result;
});
/**
* Deletes custom items for a given call ID.
*
* @since 1.1.9
*/
_defineProperty(this, "deleteCustomItems", async (callId, customItemKeys) => {
const result = await this.binder.nativeModule.deleteCustomItems(callId, customItemKeys);
if (result && result.updatedItems) {
DirectCall.updateCustomItems(callId, result.updatedItems);
}
return result;
});
/**
* Deletes all custom items for a given call ID.
*
* @since 1.1.9
*/
_defineProperty(this, "deleteAllCustomItems", async callId => {
const result = await this.binder.nativeModule.deleteAllCustomItems(callId);
if (result && result.updatedItems) {
DirectCall.updateCustomItems(callId, result.updatedItems);
}
return result;
});
}
/**
* Returns current React-Native SDK version.
*
* @since 1.0.0
*/
get VERSION() {
return Version;
}
/**
* Returns current iOS/Android SDK version.
*
* @since 1.0.0
*/
get NATIVE_VERSION() {
return this.getConstants()['NATIVE_SDK_VERSION'];
}
/**
* Returns the SDK Logger
*
* @since 1.0.0
*/
get Logger() {
return {
setLogLevel: level => {
this.setLoggerLevel(level);
},
getLogLevel: Logger.getLogLevel,
info: Logger.info,
warn: Logger.warn,
error: Logger.error
};
}
/**
* Returns current application ID.
*
* @since 1.0.0
*/
get applicationId() {
return this._applicationId;
}
/**
* Returns is SDK initialized.
*
* @since 1.0.0
*/
get initialized() {
return this._initialized;
}
/**
* Gets the current `User`.
* Returns the current `User`. If SendbirdCalls is not authenticated, `null` will be returned.
*
* @since 1.0.0
*/
get currentUser() {
return this._currentUser;
}
/**
* An enum that represents different types of a room.
* Returns {@link RoomType}
*
* @since 1.0.0
*/
get RoomType() {
return RoomType;
}
/**
* An enum that represents state of a room.
* Returns {@link RoomState}
*
* @since 1.0.0
*/
get RoomState() {
return RoomState;
}
/**
* Returns all ongoing calls, including the active call and all calls on hold.
*
* @since 1.0.0
*/
getOngoingCalls() {
return this.binder.nativeModule.getOngoingCalls();
}
/**
* Makes a call to user(callee) directly. (1:1 Call).
* Use the {@link CallOptions} object to choose initial call configuration (e.g. muted/unmuted)
*
* @since 1.0.0
*/
dial(calleeUserId, isVideoCall, options = {
audioEnabled: true,
frontCamera: true,
videoEnabled: true
}) {
return this.binder.nativeModule.dial(calleeUserId, isVideoCall, options);
}
/**
* Creates a {@link Room} for group calls.
*
* @since 1.0.0
*/
createRoom(roomParams) {
return this.binder.nativeModule.createRoom(roomParams).then(props => Room.get(this.binder, props));
}
/**
* Fetches a room instance from Sendbird server.
*
* @since 1.0.0
*/
fetchRoomById(roomId) {
return this.binder.nativeModule.fetchRoomById(roomId).then(props => Room.get(this.binder, props));
}
/**
* Gets a locally-cached room instance by room ID.
*
* @since 1.0.0
*/
getCachedRoomById(roomId) {
return this.binder.nativeModule.getCachedRoomById(roomId).then(props => props ? Room.get(this.binder, props) : null);
}
/**
* Set SendbirdCall listener
*
* @since 1.0.0
*/
setListener(listener) {
this.Logger.info('[SendbirdCalls]', 'setListener');
this._sendbirdCallListener = listener;
}
}
//# sourceMappingURL=SendbirdCallsModule.js.map