UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

133 lines (129 loc) • 4.6 kB
/** * @package @bitrix24/b24jssdk * @version 3.0.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ 'use strict'; const sdkError = require('../core/sdk-error.cjs'); const loggerFactory = require('../logger/logger-factory.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const PUBLIC_IDS_UNAVAILABLE = "JSSDK_PULL_PUBLIC_IDS_UNAVAILABLE"; class ChannelManager { static { __name(this, "ChannelManager"); } _logger; _publicIds; _restClient; _getPublicListMethod; constructor(params) { this._logger = loggerFactory.LoggerFactory.createNullLogger(); this._publicIds = /* @__PURE__ */ new Map(); this._restClient = params.b24; this._getPublicListMethod = params.getPublicListMethod || "pull.channel.public.list"; } setLogger(logger) { this._logger = logger; } getLogger() { return this._logger; } /** * @param {Array} users Array of user ids. * @return {Promise} */ async getPublicIds(users) { const now = /* @__PURE__ */ new Date(); const result = {}; const unknownUsers = []; for (const userId of users) { const chanel = this._publicIds.get(userId); if (chanel && chanel.end > now) { result[chanel.userId] = chanel; } else { unknownUsers.push(userId); } } if (unknownUsers.length === 0) { return Promise.resolve(result); } return new Promise((resolve, reject) => { this._restClient.actions.v2.call.make({ method: this._getPublicListMethod, params: { users: unknownUsers } }).then((response) => { if (!response.isSuccess) { return reject(this.publicIdsUnavailable( new Error(response.getErrorMessages().join("; ")) )); } const payload = response.getData(); if (payload?.result) { this.setPublicIds(Object.values(payload.result)); } for (const userId of unknownUsers) { const chanel = this._publicIds.get(userId); if (chanel) { result[chanel.userId] = chanel; } } if (Object.keys(result).length === 0) { return reject(this.publicIdsUnavailable( new Error(`\`${this._getPublicListMethod}\` answered without a channel for any requested user`) )); } resolve(result); }).catch((error) => { if (error instanceof sdkError.SdkError && error.code === PUBLIC_IDS_UNAVAILABLE) { return reject(error); } return reject(this.publicIdsUnavailable(error)); }); }); } /** * The one failure this class reports, built in one place. * * `originalError` carries the cause. It is deliberately non-enumerable on * `SdkError`, so it does not reach a serializer, but it is what lets a caller * tell a permanent refusal (the method is not in the application surface — * retrying is pointless) from a transient one (a 503, a rate limit — retrying * is right). The description alone asserts the former and cannot distinguish * them. * * No caller value is interpolated into the description: `SdkError` does not * run it through the log redaction, so only the method name — a constant — * goes in. */ publicIdsUnavailable(cause) { this.getLogger().error("some error in getPublicIds", { error: cause }).catch(() => { }); return new sdkError.SdkError({ code: PUBLIC_IDS_UNAVAILABLE, description: `Pull: could not resolve channel ids through \`${this._getPublicListMethod}\`, so there is nobody to send to. Publishing from the client needs that method; that method is not part of the application REST surface \u2014 put messages into the channel with \`pull.application.event.add\` from your back end instead.`, status: 0, ...cause instanceof Error ? { originalError: cause } : {} }); } /** * @param {TypePublicIdDescriptor[]} publicIds */ setPublicIds(publicIds) { publicIds.forEach((publicIdDescriptor) => { const userId = Number(publicIdDescriptor.user_id); this._publicIds.set(userId, { userId, publicId: publicIdDescriptor.public_id, signature: publicIdDescriptor.signature, start: new Date(publicIdDescriptor.start), end: new Date(publicIdDescriptor.end) }); }); } } exports.ChannelManager = ChannelManager; //# sourceMappingURL=channel-manager.cjs.map