From 640c6359c92b1c9d6bbdbb4a1eb086c93c82124f Mon Sep 17 00:00:00 2001 From: Aleksey Konstantinov Date: Thu, 25 Jul 2024 16:09:08 +0300 Subject: [PATCH] ULMS-3167 Refactored api routes, removed deprecated code --- src/conference.js | 55 ------------- src/dispatcher.js | 196 ---------------------------------------------- src/error.js | 26 ++++++ src/event.js | 44 ----------- src/index.js | 3 - src/service.js | 82 ------------------- src/ulms.js | 66 +++++----------- 7 files changed, 45 insertions(+), 427 deletions(-) delete mode 100644 src/conference.js delete mode 100644 src/dispatcher.js delete mode 100644 src/event.js delete mode 100644 src/service.js diff --git a/src/conference.js b/src/conference.js deleted file mode 100644 index c14dc6e..0000000 --- a/src/conference.js +++ /dev/null @@ -1,55 +0,0 @@ -import Service from './service' - -/** - * Agent reader configuration - * @name AgentReaderConfig - * @type {object} - * @property {string} agent_id - * @property {boolean} receive_audio - * @property {boolean} receive_video - */ - -/** - * Agent writer configuration - * @name AgentWriterConfig - * @type {object} - * @property {string} agent_id - * @property {boolean} send_audio - * @property {boolean} send_video - * @property {number} video_remb - */ - -/** - * @deprecated Use Broker class instead of Conference class - */ -class Conference extends Service { - /** - * Conference events enum - * @returns {{ - * AGENT_WRITER_CONFIG_UPDATE: string, - * GROUP_UPDATE: string, - * ROOM_CLOSE: string, - * ROOM_ENTER: string, - * ROOM_LEAVE: string, - * RTC_STREAM_AGENT_SPEAKING: string - * RTC_STREAM_UPDATE: string - * }} - */ - static get events() { - return { - AGENT_WRITER_CONFIG_UPDATE: 'agent_writer_config.update', - GROUP_UPDATE: 'video_group.update', - ROOM_CLOSE: 'room.close', - ROOM_ENTER: 'room.enter', - ROOM_LEAVE: 'room.leave', - RTC_STREAM_AGENT_SPEAKING: 'rtc_stream.agent_speaking', - RTC_STREAM_UPDATE: 'rtc_stream.update', - } - } - - constructor(mqttClient, agentId) { - super(mqttClient, agentId, 'conference.svc.netology-group.services') - } -} - -export default Conference diff --git a/src/dispatcher.js b/src/dispatcher.js deleted file mode 100644 index ec664ac..0000000 --- a/src/dispatcher.js +++ /dev/null @@ -1,196 +0,0 @@ -import BasicClient from './basic-client' - -/** - * @deprecated Use ULMS client instead of Dispatcher client - */ -class Dispatcher extends BasicClient { - /** - * Scope kind enum - * @returns {{CHAT: string, MINIGROUP: string, P2P: string, WEBINAR: string}} - */ - static get kind() { - return { - CHAT: 'chats', - MINIGROUP: 'minigroups', - P2P: 'p2p', - WEBINAR: 'webinars', - } - } - - /** - * Scope status enum - * @returns {{REAL_TIME: string, CLOSED: string, FINISHED: string, ADJUSTED: string, TRANSCODED: string}} - */ - static get scopeStatus() { - return { - REAL_TIME: 'real-time', - CLOSED: 'closed', - FINISHED: 'finished', - ADJUSTED: 'adjusted', - TRANSCODED: 'transcoded', - } - } - - /** - * Class properties enum - * @returns {{IS_ADULT: string, HAS_USER_ACCESS_TO_BOARD: string}} - */ - static get classKeys() { - return { - IS_ADULT: 'is_adult', - HAS_USER_ACCESS_TO_BOARD: 'has_user_access_to_board', - EMOTIONS: 'emotions', - } - } - - /** - * Account properties enum - * @returns {{ONBOARDING: string}} - */ - static get accountKeys() { - return { - LAST_SEEN_MESSAGE_ID_BY_ROOMS: 'last_seen_message_id_by_rooms', - ONBOARDING: 'onboarding', - } - } - - /** - * Bans media stream and collaboration for user - * @param {{ accountId: string, ban: boolean, classId: string }} - * @returns {Promise} - */ - banUser({ accountId, ban, classId }) { - return this.get( - `${this.baseUrl}/account/${accountId}/ban/${classId}`, // get last ban operation id for user - // eslint-disable-next-line camelcase - ).then(({ last_seen_op_id }) => - this.post(`${this.baseUrl}/account/${accountId}/ban`, { - ban, - class_id: classId, - // eslint-disable-next-line camelcase - last_seen_op_id, - }), - ) - } - - /** - * Commit edition by scope - * @param {string} audience - * @param {string} scope - * @param {string} editionId - * @returns {Promise} - */ - commitEdition(audience, scope, editionId) { - return this.post( - `${this.baseUrl}/audiences/${audience}/classes/${scope}/editions/${editionId}`, - ) - } - - /** - * Fetch token data for NATS - * @param {string} classroomId - * @returns {Promise} - */ - fetchTokenData(classroomId) { - return this.post(`${this.baseUrl}/classrooms/${classroomId}/tokens`) - } - - /** - * Read dispatcher scope - * @param {string} kind - * @param {string} audience - * @param {string} scope - * @param {object} options - * @returns {Promise} - */ - readScope(kind, audience, scope, options) { - return this.get( - this.url(`/audiences/${audience}/${kind}/${scope}`, options), - ) - } - - /** - * Read class property - * @param {string} kind - * @param {string} classId - * @param {string} propertyId - * @returns {Promise} - */ - readClassProperty(kind, classId, propertyId) { - return this.get( - `${this.baseUrl}/${kind}/${classId}/properties/${propertyId}`, - ) - } - - /** - * Update class property - * @param {string} kind - * @param {string} classId - * @param {string} propertyId - * @param {object} data - * @returns {Promise} - */ - updateClassProperty(kind, classId, propertyId, data) { - return this.put( - `${this.baseUrl}/${kind}/${classId}/properties/${propertyId}`, - data, - ) - } - - /** - * Read account property - * @param {string} propertyId - * @returns {Promise} - */ - readAccountProperty(propertyId) { - return this.get(`${this.baseUrl}/account/properties/${propertyId}`) - } - - /** - * Update account property - * @param {string} propertyId - * @param {object} data - * @returns {Promise} - */ - updateAccountProperty(propertyId, data) { - return this.put(`${this.baseUrl}/account/properties/${propertyId}`, data) - } - - /** - * Update dispatcher scope - * @param {string} kind - * @param {string} audience - * @param {string} scope - * @param {object} data - * @returns {Promise} - */ - updateScope(kind, audience, scope, data) { - return this.put( - `${this.baseUrl}/audiences/${audience}/${kind}/${scope}`, - data, - ) - } - - /** - * Update position timestamp - * @param {string} kind - * @param {string} classId - * @param {number} position - * @returns {Promise} - */ - updatePosition(kind, classId, position) { - const controller = new AbortController() - const { signal } = controller - const timeoutId = setTimeout(() => controller.abort(), 10 * 1000) - - return this.post( - `${this.baseUrl}/${kind}/${classId}/timestamps`, - { position }, - { signal }, - ).finally(() => { - clearTimeout(timeoutId) - }) - } -} - -export default Dispatcher diff --git a/src/error.js b/src/error.js index 6b35956..06f77f6 100644 --- a/src/error.js +++ b/src/error.js @@ -1,4 +1,30 @@ /* eslint-disable max-classes-per-file, unicorn/prevent-abbreviations */ +export class ApiError extends Error { + constructor(message, options) { + super(message, options) + + const { is_transient: isTransient } = options || {} + + this.name = 'ApiError' + this.isTransient = isTransient + } + + /** + * Factory method for creating error instance + * @param payload {{ kind?: string, is_transient?: boolean }} + * @returns {ApiError} + */ + static fromPayload(payload) { + const { kind, is_transient: isTransient } = payload + + return new ApiError(kind, { isTransient }) + } + + get kind() { + return this.message + } +} + export class MQTTClientError extends Error { constructor(...args) { super(...args) diff --git a/src/event.js b/src/event.js deleted file mode 100644 index f42d92b..0000000 --- a/src/event.js +++ /dev/null @@ -1,44 +0,0 @@ -import Service from './service' - -/** - * @deprecated Use Broker class instead of Event class - */ -class Event extends Service { - /** - * Change type enum - * @returns {{ADDITION: string, MODIFICATION: string, REMOVAL: string}} - */ - static get changeTypes() { - return { - ADDITION: 'addition', - MODIFICATION: 'modification', - REMOVAL: 'removal', - } - } - - /** - * Events enum - * @returns {{ - * AGENT_UPDATE: string, - * EVENT_CREATE: string, - * ROOM_ENTER: string, - * ROOM_LEAVE: string, - * ROOM_UPDATE: string - * }} - */ - static get events() { - return { - AGENT_UPDATE: 'agent.update', - EVENT_CREATE: 'event.create', - ROOM_ENTER: 'room.enter', - ROOM_LEAVE: 'room.leave', - ROOM_UPDATE: 'room.update', - } - } - - constructor(mqttClient, agentId) { - super(mqttClient, agentId, 'event.svc.netology-group.services') - } -} - -export default Event diff --git a/src/index.js b/src/index.js index 3090d47..ea3a2b1 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,5 @@ export { default as Backoff } from './backoff' export { default as Broker } from './broker' -export { default as Conference } from './conference' -export { default as Dispatcher } from './dispatcher' -export { default as Event } from './event' export { default as FVS } from './fvs' export { default as FetchHttpClient } from './http-client' export { MQTTClient, ReconnectingMQTTClient, defaultOptions } from './mqtt' diff --git a/src/service.js b/src/service.js deleted file mode 100644 index e8f38b1..0000000 --- a/src/service.js +++ /dev/null @@ -1,82 +0,0 @@ -/* eslint-disable unicorn/prevent-abbreviations */ -// eslint-disable-next-line unicorn/prefer-node-protocol -import EventEmitter from 'events' - -import Codec from './codec' - -/** - * @deprecated Use Broker class instead of Service base class - */ -class Service { - constructor(mqttClient, agentId, appName) { - this.agentId = agentId - this.appName = appName - this.topicPatternNotifications = `apps/${this.appName}/api/v1/rooms/+roomId/events` - this.mqtt = mqttClient - - this.codec = new Codec( - (data) => JSON.stringify(data), - (data) => { - let payload - - try { - payload = JSON.parse(data.toString()) - } catch { - payload = {} - } - - return payload - }, - ) - // eslint-disable-next-line unicorn/prefer-event-target - this.ee = new EventEmitter() - - this.attachRoutes() - } - - attachRoutes() { - this.mqtt.attachRoute( - this.topicPatternNotifications, - this.subMessageHandler.bind(this), - ) - } - - detachRoutes() { - this.mqtt.detachRoute(this.topicPatternNotifications) - } - - on(eventName, eventHandler) { - this.ee.addListener(eventName, eventHandler) - } - - off(eventName, eventHandler) { - this.ee.removeListener(eventName, eventHandler) - } - - subMessageHandler(topicParams, topic, message, packet) { - const payload = this.codec.decode(message) - const { properties } = packet - const { - userProperties: { label, type }, - } = properties - let event - - if (type === 'event' && payload !== undefined) { - event = { - type: label, - data: payload, - } - - this.ee.emit(event.type, event) - } else { - // do nothing - } - } - - destroy() { - this.detachRoutes() - this.ee.removeAllListeners() - } -} - -export default Service diff --git a/src/ulms.js b/src/ulms.js index 743488d..22f7e8c 100644 --- a/src/ulms.js +++ b/src/ulms.js @@ -1,23 +1,5 @@ import BasicClient from './basic-client' -const eventEndpoints = { - agentsList: (id) => `/event_rooms/${id}/agents`, - agentsUpdate: (id) => `/event_rooms/${id}/agents`, - banList: (id) => `/event_rooms/${id}/bans`, - changesCreate: (id) => `/editions/${id}/changes`, - changesDelete: (id) => `/changes/${id}`, - changesList: (id) => `/editions/${id}/changes`, - editionsCreate: (id) => `/event_rooms/${id}/editions`, - editionsDelete: (id) => `/editions/${id}`, - editionsList: (id) => `/event_rooms/${id}/editions`, - eventsCreate: (id) => `/event_rooms/${id}/events`, - eventsList: (id) => `/event_rooms/${id}/events`, - roomRead: (id) => `/event_rooms/${id}`, - roomState: (id) => `/event_rooms/${id}/state`, - roomUpdateLockedTypes: (id) => `/event_rooms/${id}/locked_types`, - roomUpdateWhiteboardAccess: (id) => `/event_rooms/${id}/whiteboard_access`, -} - /** * Agent reader configuration * @name AgentReaderConfig @@ -114,10 +96,10 @@ class ULMS extends BasicClient { */ static get classKeys() { return { - IS_ADULT: 'is_adult', + EMOTIONS: 'emotions', HAS_USER_ACCESS_TO_BOARD: 'has_user_access_to_board', + IS_ADULT: 'is_adult', TOXIC_COMMENT_CLASSIFIER_ENABLED: 'toxic_comment_classifier_enabled', - EMOTIONS: 'emotions', } } @@ -172,7 +154,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ createEdition(roomId) { - return this.post(this.url(eventEndpoints.editionsCreate(roomId))) + return this.post(this.url(`/event_rooms/${roomId}/editions`)) } /** @@ -192,7 +174,7 @@ class ULMS extends BasicClient { type, } - return this.post(this.url(eventEndpoints.eventsCreate(roomId)), parameters) + return this.post(this.url(`/event_rooms/${roomId}/events`), parameters) } /** @@ -208,10 +190,7 @@ class ULMS extends BasicClient { type, } - return this.post( - this.url(eventEndpoints.changesCreate(editionId)), - parameters, - ) + return this.post(this.url(`/editions/${editionId}/changes`), parameters) } /** @@ -284,7 +263,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ deleteEdition(id) { - return this.delete(this.url(eventEndpoints.editionsDelete(id))) + return this.delete(this.url(`/editions/${id}`)) } /** @@ -293,7 +272,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ deleteChange(id) { - return this.delete(this.url(eventEndpoints.changesDelete(id))) + return this.delete(this.url(`/changes/${id}`)) } /** @@ -324,9 +303,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ listAgent(roomId, filterParameters = {}) { - return this.get( - this.url(eventEndpoints.agentsList(roomId), filterParameters), - ) + return this.get(this.url(`/event_rooms/${roomId}/agents`, filterParameters)) } /** @@ -348,7 +325,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ listBans(roomId, filterParameters = {}) { - return this.get(this.url(eventEndpoints.banList(roomId), filterParameters)) + return this.get(this.url(`/event_rooms/${roomId}/bans`, filterParameters)) } /** @@ -358,7 +335,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ listChange(id, filterParameters = {}) { - return this.get(this.url(eventEndpoints.changesList(id), filterParameters)) + return this.get(this.url(`/editions/${id}/changes`, filterParameters)) } /** @@ -369,7 +346,7 @@ class ULMS extends BasicClient { */ listEdition(roomId, filterParameters = {}) { return this.get( - this.url(eventEndpoints.editionsList(roomId), filterParameters), + this.url(`/event_rooms/${roomId}/editions`, filterParameters), ) } @@ -380,9 +357,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ listEvent(roomId, filterParameters = {}) { - return this.get( - this.url(eventEndpoints.eventsList(roomId), filterParameters), - ) + return this.get(this.url(`/event_rooms/${roomId}/events`, filterParameters)) } /** @@ -442,7 +417,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ readEventRoom(roomId) { - return this.get(this.url(eventEndpoints.roomRead(roomId))) + return this.get(this.url(`/event_rooms/${roomId}`)) } /** @@ -479,7 +454,7 @@ class ULMS extends BasicClient { sets, } - return this.get(this.url(eventEndpoints.roomState(roomId), parameters)) + return this.get(this.url(`/event_rooms/${roomId}/state`, parameters)) } /** @@ -510,7 +485,7 @@ class ULMS extends BasicClient { value, } - return this.patch(this.url(eventEndpoints.agentsUpdate(roomId)), parameters) + return this.patch(this.url(`/event_rooms/${roomId}/agents`), parameters) } /** @@ -606,7 +581,7 @@ class ULMS extends BasicClient { * @returns {Promise} */ updateLockedTypes(roomId, lockedTypes) { - return this.post(this.url(eventEndpoints.roomUpdateLockedTypes(roomId)), { + return this.post(this.url(`/event_rooms/${roomId}/locked_types`), { locked_types: lockedTypes, }) } @@ -654,12 +629,9 @@ class ULMS extends BasicClient { * @returns {Promise} */ updateWhiteboardAccess(roomId, payload) { - return this.post( - this.url(eventEndpoints.roomUpdateWhiteboardAccess(roomId)), - { - whiteboard_access: payload, - }, - ) + return this.post(this.url(`/event_rooms/${roomId}/whiteboard_access`), { + whiteboard_access: payload, + }) } }