Invites.js

import SmartShare from './managers/share.js';
import InviteContent from './models/invites/InviteContent.js';
import ReferralUser from './models/invites/ReferralUser.js';
import ReferralUsersQuery from './models/invites/ReferralUsersQuery.js';
import UserId from './models/UserId.js';
import InviteChannel from './models/invites/InviteChannel';
import SDK from './sdk';

import './thrift/smartinvites_types';

/**
 * @class Invites
 * @hideconstructor
 */
export default class Invites {
    /**
     * Creates a Smart Link with user referral data attached used for Smart Invites.
     *
     * @memberof Invites
     * @param {Object<string, string>} [linkParams] - Link customization parameters. More info @see {@link https://docs.getsocial.im/guides/smart-links/parameters/|here}
     * @return {Promise<string>} Promise with Smart invite URL.
     */
    static createURL(linkParams) {
        const request = new THCreateTokenRequest({
            providerId: 'web',
            linkParams
        });

        return SDK.request('createInviteUrl', request)
            .then((res) => res.url);
    }

    /**
     * Get the list of available invite channels. Contains only channels enabled on the Dashboard and available on the device.
     *
     * @memberof Invites
     * @return {InviteChannel[]} Promise with list of available invite channels.
     */
     static getAvailableChannels() {
        const lang = SDK.getLanguage();
        return SDK._inviteProviders
            .filter((provider) => provider.enabled)
            .map((provider) => {
                return new InviteChannel({
                    ...provider,
                    name: typeof provider.name === 'object'
                        ? provider.name[lang]
                        : provider.name
                })
            });
    }

    /**
     * Invite friends via a specified invite channel.
     *
     * @memberof Invites
     * @param {InviteContent} inviteContent - Custom content to override the default content provided from the Dashboard.
     * @param {string} channel - The channel through which the invite will be sent, one of the constants defined in {@link SupportedChannel}.
     * @return {Promise<string>} Promise with Smart Invite URL.
     */
    static send(inviteContent, channel) {
        inviteContent = inviteContent || {};
        const defaultContent = Invites.getDefaultContent(channel);

        return SmartShare
            .shareLinkViaChannel(channel, {
                inviteText: inviteContent.text || defaultContent.text,
                emailSubject: inviteContent.subject || defaultContent.subject,
                twitterVia: inviteContent.twitterVia,
                customData: inviteContent.linkParams
            });
    }

    /**
     * Returns referral data.
     *
     * @memberof Invites
     * @return {Object} Referral data.
     */
    static getReferralData() {
        return SDK._referral || new THTokenInfo();
    }

    /**
     * Returns list of users who were referred by this user for a specific event.
     *
     * @memberof Invites
     * @param {ReferralUsersQuery} query - Users query.
     * @return {Promise<ReferralUser[]>} Promise with list of referred users.
     */
     static getReferredUsers(query) {
        return SDK.request(
            'getReferredUsersV2',
            query.event,
            query.offset,
            query.limit
        )
            .then((users) => {
                return users && users.length
                    ? users.map((user) => new ReferralUser({
                        ...user,
                        eventData: user.customData
                    }))
                    : [];
            });
    }

    /**
     * Returns list of users who are referrers for this user for a specific event.
     *
     * @memberof Invites
     * @param {ReferralUsersQuery} query - Users query.
     * @return {Promise<ReferralUser[]>} Promise with list of referrer users.
     */
    static getReferrerUsers(query) {
        return SDK.request(
            'getReferrerUsers',
            query.event,
            query.offset,
            query.limit
        )
            .then((users) => {
                return users && users.length
                    ? users.map((user) => new ReferralUser({
                        ...user,
                        eventData: user.customData
                    }))
                    : [];
            });
    }

    /**
     * Sets referrer id for current user.
     *
     * @memberof Invites
     * @param {UserId} userId Id of referrer user.
     * @param {string} event Referrer event.
     * @param {Object<string, string>} eventData Custom key-value pairs.
     * @return {Promise<boolean>} Promise with true if successful.
     */
    static setReferrer(userId, event, eventData) {
        return SDK.request('setReferrer', userId.id, event, eventData);
    }

    /**
     * @ignore
     */
    static getDefaultContent(channel) {
        const lang = SDK.getLanguage();
        const provider = channel && SDK._inviteProviders
            ? SDK._inviteProviders
                .find((provider) => provider.providerId === channel)
            : null;
        const defaultContent = SDK._defaultInviteContent;
        let inviteContent = {};

        if (provider && provider.inviteContent &&
            provider.inviteContent.text &&
            provider.inviteContent.text[lang]) {
            inviteContent.text = provider.inviteContent.text[lang];
        } else if (defaultContent &&
            defaultContent.text &&
            defaultContent.text[lang]) {
            inviteContent.text = defaultContent.text[lang];
        }

        if (channel === 'email') {
            if (provider && provider.inviteContent &&
                provider.inviteContent.subject &&
                provider.inviteContent.subject[lang]) {
                inviteContent.subject = provider.inviteContent.subject[lang];
            } else if (defaultContent &&
                defaultContent.subject &&
                defaultContent.subject[lang]) {
                inviteContent.subject = defaultContent.subject[lang];
            }
        }

        return inviteContent;
    }
}