2018-03-22 12:46:37 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Invites representation wrapper
|
|
|
|
*/
|
2018-03-30 09:07:23 +00:00
|
|
|
import { Platform } from 'react-native';
|
2018-03-22 12:46:37 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
|
|
|
import { getLogger } from '../../utils/log';
|
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
|
|
|
import { getNativeModule } from '../../utils/native';
|
|
|
|
import Invitation from './Invitation';
|
|
|
|
|
|
|
|
import type App from '../core/app';
|
|
|
|
|
|
|
|
export const MODULE_NAME = 'RNFirebaseInvites';
|
|
|
|
export const NAMESPACE = 'invites';
|
|
|
|
const NATIVE_EVENTS = ['invites_invitation_received'];
|
|
|
|
|
|
|
|
type InvitationOpen = {
|
|
|
|
deepLink: string,
|
|
|
|
invitationId: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class Invites extends ModuleBase {
|
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
|
|
|
events: NATIVE_EVENTS,
|
2018-03-23 13:24:31 +00:00
|
|
|
hasShards: false,
|
2018-03-22 12:46:37 +00:00
|
|
|
moduleName: MODULE_NAME,
|
|
|
|
multiApp: false,
|
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
|
|
|
|
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onMessage
|
|
|
|
'invites_invitation_received',
|
|
|
|
(invitation: InvitationOpen) => {
|
|
|
|
SharedEventEmitter.emit('onInvitation', invitation);
|
|
|
|
}
|
|
|
|
);
|
2018-03-30 09:07:23 +00:00
|
|
|
|
|
|
|
// Tell the native module that we're ready to receive events
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
getNativeModule(this).jsInitialised();
|
|
|
|
}
|
2018-03-22 12:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the invitation that triggered application open
|
2018-03-23 17:03:53 +00:00
|
|
|
* @returns {Promise.<InvitationOpen>}
|
2018-03-22 12:46:37 +00:00
|
|
|
*/
|
2018-03-23 17:03:53 +00:00
|
|
|
getInitialInvitation(): Promise<?InvitationOpen> {
|
2018-03-22 12:46:37 +00:00
|
|
|
return getNativeModule(this).getInitialInvitation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to invites
|
|
|
|
* @param listener
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
onInvitation(listener: InvitationOpen => any) {
|
|
|
|
getLogger(this).info('Creating onInvitation listener');
|
|
|
|
|
|
|
|
SharedEventEmitter.addListener('onInvitation', listener);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
getLogger(this).info('Removing onInvitation listener');
|
|
|
|
SharedEventEmitter.removeListener('onInvitation', listener);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
sendInvitation(invitation: Invitation): Promise<string[]> {
|
|
|
|
if (!(invitation instanceof Invitation)) {
|
2018-03-23 17:03:53 +00:00
|
|
|
return Promise.reject(
|
|
|
|
new Error(
|
|
|
|
`Invites:sendInvitation expects an 'Invitation' but got type ${typeof invitation}`
|
|
|
|
)
|
2018-03-22 12:46:37 +00:00
|
|
|
);
|
|
|
|
}
|
2018-03-23 17:03:53 +00:00
|
|
|
try {
|
|
|
|
return getNativeModule(this).sendInvitation(invitation.build());
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
2018-03-22 12:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const statics = {
|
|
|
|
Invitation,
|
|
|
|
};
|