pubnub
Version:
Publish & Subscribe Real-time Messaging with PubNub
123 lines (122 loc) • 6 kB
JavaScript
;
/**
* Channels / channel groups presence REST API module.
*
* @internal
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HereNowRequest = void 0;
const request_1 = require("../../components/request");
const operations_1 = __importDefault(require("../../constants/operations"));
const utils_1 = require("../../utils");
// --------------------------------------------------------
// ----------------------- Defaults -----------------------
// --------------------------------------------------------
// region Defaults
/**
* Whether `uuid` should be included in response or not.
*/
const INCLUDE_UUID = true;
/**
* Whether state associated with `uuid` should be included in response or not.
*/
const INCLUDE_STATE = false;
/**
* Maximum number of participants which can be returned with single response.
*/
const MAXIMUM_COUNT = 1000;
// endregion
/**
* Channel presence request.
*
* @internal
*/
class HereNowRequest extends request_1.AbstractRequest {
constructor(parameters) {
var _a, _b, _c, _d;
var _e, _f, _g, _h;
super();
this.parameters = parameters;
// Apply defaults.
(_a = (_e = this.parameters).queryParameters) !== null && _a !== void 0 ? _a : (_e.queryParameters = {});
(_b = (_f = this.parameters).includeUUIDs) !== null && _b !== void 0 ? _b : (_f.includeUUIDs = INCLUDE_UUID);
(_c = (_g = this.parameters).includeState) !== null && _c !== void 0 ? _c : (_g.includeState = INCLUDE_STATE);
if (this.parameters.limit)
this.parameters.limit = Math.min(this.parameters.limit, MAXIMUM_COUNT);
else
this.parameters.limit = MAXIMUM_COUNT;
(_d = (_h = this.parameters).offset) !== null && _d !== void 0 ? _d : (_h.offset = 0);
}
operation() {
const { channels = [], channelGroups = [] } = this.parameters;
return channels.length === 0 && channelGroups.length === 0
? operations_1.default.PNGlobalHereNowOperation
: operations_1.default.PNHereNowOperation;
}
validate() {
if (!this.parameters.keySet.subscribeKey)
return 'Missing Subscribe Key';
}
parse(response) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const serviceResponse = this.deserializeResponse(response);
// Extract general presence information.
const totalChannels = 'occupancy' in serviceResponse ? 1 : serviceResponse.payload.total_channels;
const totalOccupancy = 'occupancy' in serviceResponse ? serviceResponse.occupancy : serviceResponse.payload.total_occupancy;
const channelsPresence = {};
let channels = {};
// Remap single channel presence to multiple channels presence response.
if ('occupancy' in serviceResponse) {
const channel = this.parameters.channels[0];
channels[channel] = { uuids: (_a = serviceResponse.uuids) !== null && _a !== void 0 ? _a : [], occupancy: totalOccupancy };
}
else
channels = (_b = serviceResponse.payload.channels) !== null && _b !== void 0 ? _b : {};
Object.keys(channels).forEach((channel) => {
const channelEntry = channels[channel];
channelsPresence[channel] = {
occupants: this.parameters.includeUUIDs
? channelEntry.uuids.map((uuid) => {
if (typeof uuid === 'string')
return { uuid, state: null };
return uuid;
})
: [],
name: channel,
occupancy: channelEntry.occupancy,
};
});
return {
totalChannels,
totalOccupancy,
next: 0,
channels: channelsPresence,
};
});
}
get path() {
const { keySet: { subscribeKey }, channels, channelGroups, } = this.parameters;
let path = `/v2/presence/sub-key/${subscribeKey}`;
if ((channels && channels.length > 0) || (channelGroups && channelGroups.length > 0))
path += `/channel/${(0, utils_1.encodeNames)(channels !== null && channels !== void 0 ? channels : [], ',')}`;
return path;
}
get queryParameters() {
const { channelGroups, includeUUIDs, includeState, limit, offset, queryParameters } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (this.operation() === operations_1.default.PNHereNowOperation ? { limit } : {})), (this.operation() === operations_1.default.PNHereNowOperation && offset > 0 ? { offset } : {})), (!includeUUIDs ? { disable_uuids: '1' } : {})), ((includeState !== null && includeState !== void 0 ? includeState : false) ? { state: '1' } : {})), (channelGroups && channelGroups.length > 0 ? { 'channel-group': channelGroups.join(',') } : {})), queryParameters);
}
}
exports.HereNowRequest = HereNowRequest;