pubnub
Version:
Publish & Subscribe Real-time Messaging with PubNub
55 lines (41 loc) • 1.74 kB
JavaScript
/* @flow */
import { GetStateArguments, GetStateResponse, ModulesInject } from '../../flow_interfaces';
import operationConstants from '../../constants/operations';
import utils from '../../utils';
export function getOperation(): string {
return operationConstants.PNGetStateOperation;
}
export function validateParams(modules: ModulesInject) {
let { config } = modules;
if (!config.subscribeKey) return 'Missing Subscribe Key';
}
export function getURL(modules: ModulesInject, incomingParams: GetStateArguments): string {
let { config } = modules;
let { uuid = config.UUID, channels = [] } = incomingParams;
let stringifiedChannels = channels.length > 0 ? channels.join(',') : ',';
return `/v2/presence/sub-key/${config.subscribeKey}/channel/${utils.encodeString(stringifiedChannels)}/uuid/${uuid}`;
}
export function getRequestTimeout({ config }: ModulesInject): number {
return config.getTransactionTimeout();
}
export function isAuthSupported(): boolean {
return true;
}
export function prepareParams(modules: ModulesInject, incomingParams: GetStateArguments): Object {
let { channelGroups = [] } = incomingParams;
const params = {};
if (channelGroups.length > 0) {
params['channel-group'] = channelGroups.join(',');
}
return params;
}
export function handleResponse(modules: ModulesInject, serverResponse: Object, incomingParams: GetStateArguments): GetStateResponse {
let { channels = [], channelGroups = [] } = incomingParams;
let channelsResponse = {};
if (channels.length === 1 && channelGroups.length === 0) {
channelsResponse[channels[0]] = serverResponse.payload;
} else {
channelsResponse = serverResponse.payload;
}
return { channels: channelsResponse };
}