UNPKG

fcr-core

Version:

Core APIs for building online scenes

421 lines (419 loc) 21.8 kB
"use strict"; require("core-js/modules/es.array.push.js"); require("core-js/modules/esnext.function.metadata.js"); require("core-js/modules/esnext.map.delete-all.js"); require("core-js/modules/esnext.map.emplace.js"); require("core-js/modules/esnext.map.every.js"); require("core-js/modules/esnext.map.filter.js"); require("core-js/modules/esnext.map.find.js"); require("core-js/modules/esnext.map.find-key.js"); require("core-js/modules/esnext.map.includes.js"); require("core-js/modules/esnext.map.key-of.js"); require("core-js/modules/esnext.map.map-keys.js"); require("core-js/modules/esnext.map.map-values.js"); require("core-js/modules/esnext.map.merge.js"); require("core-js/modules/esnext.map.reduce.js"); require("core-js/modules/esnext.map.some.js"); require("core-js/modules/esnext.map.update.js"); require("core-js/modules/esnext.symbol.metadata.js"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.FcrChatRoomControlImpl = void 0; require("core-js/modules/esnext.iterator.constructor.js"); require("core-js/modules/esnext.iterator.map.js"); var _decorator = require("agora-foundation/lib/decorator"); var _raceCondition = require("agora-foundation/lib/utilities/race-condition"); var _observable = require("agora-foundation/lib/utilities/observable"); var _easemobWebsdk = _interopRequireDefault(require("easemob-websdk")); var _get = _interopRequireDefault(require("lodash/get")); var _type = require("../../chat-connector/type"); var _imports = require("../../imports"); var _type2 = require("../../room-control/chatroom-control/type"); var _schema = require("../../schema"); var _type3 = require("../../type"); var _abortableRetry = require("../../utilities/abortable-retry"); var _error = require("../../utilities/error"); var _logger = require("../../utilities/logger"); var _user = require("../../utilities/user"); var _validateParams = _interopRequireDefault(require("../../utilities/validate-params")); let _initProto, _sendMessageDecs, _addObserverDecs, _removeObserverDecs; function _applyDecs(e, t, r, n, o, a) { function i(e, t, r) { return function (n, o) { return r && r(n), e[t].call(n, o); }; } function c(e, t) { for (var r = 0; r < e.length; r++) e[r].call(t); return t; } function s(e, t, r, n) { if ("function" != typeof e && (n || void 0 !== e)) throw new TypeError(t + " must " + (r || "be") + " a function" + (n ? "" : " or undefined")); return e; } function applyDec(e, t, r, n, o, a, c, u, l, f, p, d, h) { function m(e) { if (!h(e)) throw new TypeError("Attempted to access private element on non-instance"); } var y, v = t[0], g = t[3], b = !u; if (!b) { r || Array.isArray(v) || (v = [v]); var w = {}, S = [], A = 3 === o ? "get" : 4 === o || d ? "set" : "value"; f ? (p || d ? w = { get: _setFunctionName(function () { return g(this); }, n, "get"), set: function (e) { t[4](this, e); } } : w[A] = g, p || _setFunctionName(w[A], n, 2 === o ? "" : A)) : p || (w = Object.getOwnPropertyDescriptor(e, n)); } for (var P = e, j = v.length - 1; j >= 0; j -= r ? 2 : 1) { var D = v[j], E = r ? v[j - 1] : void 0, I = {}, O = { kind: ["field", "accessor", "method", "getter", "setter", "class"][o], name: n, metadata: a, addInitializer: function (e, t) { if (e.v) throw Error("attempted to call addInitializer after decoration was finished"); s(t, "An initializer", "be", !0), c.push(t); }.bind(null, I) }; try { if (b) (y = s(D.call(E, P, O), "class decorators", "return")) && (P = y);else { var k, F; O.static = l, O.private = f, f ? 2 === o ? k = function (e) { return m(e), w.value; } : (o < 4 && (k = i(w, "get", m)), 3 !== o && (F = i(w, "set", m))) : (k = function (e) { return e[n]; }, (o < 2 || 4 === o) && (F = function (e, t) { e[n] = t; })); var N = O.access = { has: f ? h.bind() : function (e) { return n in e; } }; if (k && (N.get = k), F && (N.set = F), P = D.call(E, d ? { get: w.get, set: w.set } : w[A], O), d) { if ("object" == typeof P && P) (y = s(P.get, "accessor.get")) && (w.get = y), (y = s(P.set, "accessor.set")) && (w.set = y), (y = s(P.init, "accessor.init")) && S.push(y);else if (void 0 !== P) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0"); } else s(P, (p ? "field" : "method") + " decorators", "return") && (p ? S.push(P) : w[A] = P); } } finally { I.v = !0; } } return (p || d) && u.push(function (e, t) { for (var r = S.length - 1; r >= 0; r--) t = S[r].call(e, t); return t; }), p || b || (f ? d ? u.push(i(w, "get"), i(w, "set")) : u.push(2 === o ? w[A] : i.call.bind(w[A])) : Object.defineProperty(e, n, w)), P; } function u(e, t) { return Object.defineProperty(e, Symbol.metadata || Symbol.for("Symbol.metadata"), { configurable: !0, enumerable: !0, value: t }); } if (arguments.length >= 6) var l = a[Symbol.metadata || Symbol.for("Symbol.metadata")]; var f = Object.create(null == l ? null : l), p = function (e, t, r, n) { var o, a, i = [], s = function (t) { return _checkInRHS(t) === e; }, u = new Map(); function l(e) { e && i.push(c.bind(null, e)); } for (var f = 0; f < t.length; f++) { var p = t[f]; if (Array.isArray(p)) { var d = p[1], h = p[2], m = p.length > 3, y = 16 & d, v = !!(8 & d), g = 0 == (d &= 7), b = h + "/" + v; if (!g && !m) { var w = u.get(b); if (!0 === w || 3 === w && 4 !== d || 4 === w && 3 !== d) throw Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + h); u.set(b, !(d > 2) || d); } applyDec(v ? e : e.prototype, p, y, m ? "#" + h : _toPropertyKey(h), d, n, v ? a = a || [] : o = o || [], i, v, m, g, 1 === d, v && m ? s : r); } } return l(o), l(a), i; }(e, t, o, f); return r.length || u(e, f), { e: p, get c() { var t = []; return r.length && [u(applyDec(e, [r], n, e.name, 5, f, t), f), c.bind(null, t, e)]; } }; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _setFunctionName(e, t, n) { "symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : ""); try { Object.defineProperty(e, "name", { configurable: !0, value: n ? n + " " + t : t }); } catch (e) {} return e; } function _checkInRHS(e) { if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null")); return e; } const CHATROOM_ID_KEY_PATH = 'widgets.easemobIM.extra.chatRoomId'; /** * @internal */ class FcrChatRoomControlImpl { static { [_initProto] = _applyDecs(this, [[_decorator.trace, 2, "getConnectionState"], [_sendMessageDecs, 2, "sendMessage"], [_addObserverDecs, 2, "addObserver"], [_removeObserverDecs, 2, "removeObserver"], [_decorator.bound, 2, "_handleJoinSceneSuccess"], [_decorator.bound, 2, "_handleJoinSceneFailure"], [_decorator.trace, 2, "leave"], [_decorator.trace, 2, "release"], [_decorator.trace, 2, "join"], [_decorator.bound, 2, "_handleScenePropertiesUpdated"]], []).e; } [(_sendMessageDecs = [(0, _decorator.trace)(['message', 'progress']), (0, _validateParams.default)(_schema.messageSchema, _schema.progressSchema.optional())], _addObserverDecs = (0, _decorator.trace)(['observer']), _removeObserverDecs = (0, _decorator.trace)(['observer']), "logger")] = (_initProto(this), (0, _logger.createLogger)({ prefix: 'FcrChatRoomControlImpl' })); _observable = new _observable.AgoraObservable(); _connectionState = _type2.FcrChatRoomConnectionState.Disconnected; _joinPromise = null; _joinTimeoutMs = 30000; _joinAttemptId = 0; _abortJoinChatRoom = null; _sceneObserver = { onJoinSceneSuccess: this._handleJoinSceneSuccess, onJoinSceneFailure: this._handleJoinSceneFailure, onScenePropertiesUpdated: this._handleScenePropertiesUpdated }; chatRoomConnectionHandler = { onTextMessage: msg => { if (msg.to !== this.chatRoomId) { return; } const message = this._convertAgoraChatMessageToFcrChatRoomReceiveMessage(msg); this._observable.notifyObservers('onTextMessageReceived', this._scene.sceneId, message); } }; _chatConnectionObserver = { onConnectionStateUpdated: async connectorState => { const chatRoomConnectionState = this.getConnectionState(); if (connectorState === _type.FcrChatConnectionState.DISCONNECTED) { this._setConnectionState(_type2.FcrChatRoomConnectionState.Disconnected); } else if (connectorState === _type.FcrChatConnectionState.CONNECTED && chatRoomConnectionState !== _type2.FcrChatRoomConnectionState.Connected) { this.logger.info('chat connector connected, try to join chat room'); this._tryJoinChatRoom(); } else if (connectorState === _type.FcrChatConnectionState.CONNECTED) { this.logger.info('chat connector reconnected, try to rejoin chat room'); this._tryJoinChatRoom(true); } } }; get conn() { if (!this._chatConnection.getConnectionInstance()) { throw (0, _error.generateFcrCoreClientError)(_imports.ErrorModuleCode.FCR_ROOM_CHATROOM, _imports.DetailErrorCode.UNDEFINED_ERROR, 'chat room connection not initialized, please get chat room token first'); } return this._chatConnection.getConnectionInstance(); } get chatRoomId() { if (!this._joinSceneSuccess) { return ''; } return this._scene.getScenePropertiesByKeyPath(CHATROOM_ID_KEY_PATH); } get connectionEventHandlerKey() { return `chatroom${this._scene.sceneId}`; } constructor(_scene, _chatConnection, sharedCache, joinSceneSuccess) { this._scene = _scene; this._chatConnection = _chatConnection; this._roomCache = sharedCache.getRoomCache(_scene.sceneId); this._joinSceneSuccess = joinSceneSuccess; this._addLogObserver(); this._scene.addObserver(this._sceneObserver); if (this._chatConnection.getConnectionInstance() && this._chatConnection.getConnectionState() === _type.FcrChatConnectionState.CONNECTED) { this.logger.info('chat connection already connected, try to join chat room'); this._tryJoinChatRoom(); } } getConnectionState() { return this._connectionState; } async sendMessage(message, progress) { const msg = this._convertFcrChatRoomSendBaseMessageToAgoraChatMessage(message, progress); const sendMsgRes = await this.conn.send(msg); const localUserInfo = this._scene.getUser(this._scene.getLocalUser().getLocalUserId()); const res = { ...(message.type === _type2.FcrChatRoomMessageType.Text ? { content: message.content } : // @ts-ignore { url: sendMsgRes.message.url }), from: (0, _user.convertRteUserToFcrUser)(localUserInfo, this._roomCache), id: sendMsgRes.serverMsgId, type: message.type, properties: message.properties, timestamp: Date.now(), isPrivate: !!message.to?.length }; return res; } addObserver(observer) { this._observable.addObserver(observer); return _type3.FcrReturnCode.SUCCESS; } removeObserver(observer) { this._observable.removeObserver(observer); return _type3.FcrReturnCode.SUCCESS; } _setConnectionState(state) { if (this._connectionState === state) { return; } this._connectionState = state; this._observable.notifyObservers('onConnectionStateUpdated', this._scene.sceneId, state); } _tryJoinChatRoom(force = false) { if (this._chatConnection.getConnectionState() !== _type.FcrChatConnectionState.CONNECTED) { return; } if (!this.chatRoomId) { return; } if (this._joinPromise) { return; } if (!force && (this._connectionState === _type2.FcrChatRoomConnectionState.Connected || this._connectionState === _type2.FcrChatRoomConnectionState.Connecting)) { return; } this.join(force); } _addLogObserver() { this.addObserver((0, _logger.generateLogObserver)(this.logger, [['onConnectionStateUpdated', ['roomId', 'state']], ['onErrorOccurred', ['roomId', 'error']] // 'onTextMessageReceived' ])); } async getHistoryMessageList(pageSize, startedMessageId) { if (pageSize > 50) { throw (0, _error.generateFcrCoreClientError)(_imports.ErrorModuleCode.FCR_ROOM_CHATROOM, _imports.DetailErrorCode.UNDEFINED_ERROR, 'pageSize should be less than 50'); } try { const assembleRet = await this.conn.fetchHistoryMessages({ queue: this.chatRoomId, count: pageSize, start: startedMessageId, isGroup: true }); return assembleRet.map(msgBody => this._convertAgoraChatHistoryMessageToFcrChatRoomReceiveMessage(msgBody)); } catch (error) { throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.FCR_ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, error.message); } } _handleJoinSceneSuccess() { this._joinSceneSuccess = true; this._tryJoinChatRoom(); } _handleJoinSceneFailure() { this._joinSceneSuccess = false; } leave() { this._chatConnection.removeObserver(this._chatConnectionObserver); if (this._connectionState === _type2.FcrChatRoomConnectionState.Disconnected && !this._abortJoinChatRoom) { return; } if (this._abortJoinChatRoom) { this._abortJoinChatRoom(); this._abortJoinChatRoom = null; this._joinPromise = null; } if (!this._chatConnection.getConnectionInstance()) { this._setConnectionState(_type2.FcrChatRoomConnectionState.Disconnected); return; } this._removeEventListener(); if (this.chatRoomId) { this.conn.leaveChatRoom({ roomId: this.chatRoomId, success: () => { this.logger.info(`leave chat room success, room id: ${this.chatRoomId}, scene id: ${this._scene.sceneId}`); }, error: e => { this.logger.error('leave chat room failed', e.message); } }); } this._setConnectionState(_type2.FcrChatRoomConnectionState.Disconnected); } release() { this._observable.removeAllObservers(); return this.leave(); } async join(force = false) { this._chatConnection.addObserver(this._chatConnectionObserver); const connectorState = this._chatConnection.getConnectionState(); if (connectorState !== _type.FcrChatConnectionState.CONNECTED) { return _type3.FcrReturnCode.SUCCESS; } if (!this.chatRoomId) { this.logger.warn(`chat room id is empty, scene id: ${this._scene.sceneId}`); return _type3.FcrReturnCode.SUCCESS; } if (!this._chatConnection.getConnectionInstance()) { this.logger.warn(`chat connection instance is null, scene id: ${this._scene.sceneId}`); return _type3.FcrReturnCode.SUCCESS; } const chatRoomId = this.chatRoomId; if (this._joinPromise) { return this._joinPromise; } const chatRoomConnectionState = this.getConnectionState(); if (!force && (chatRoomConnectionState === _type2.FcrChatRoomConnectionState.Connected || chatRoomConnectionState === _type2.FcrChatRoomConnectionState.Connecting)) { return _type3.FcrReturnCode.SUCCESS; } if (force || chatRoomConnectionState === _type2.FcrChatRoomConnectionState.Disconnected) { if (chatRoomConnectionState !== _type2.FcrChatRoomConnectionState.Connecting) { this._setConnectionState(_type2.FcrChatRoomConnectionState.Connecting); } this.logger.info(`start join chat room, room id: ${chatRoomId}, scene id: ${this._scene.sceneId}`); this._addEventListener(); const attemptId = ++this._joinAttemptId; this._joinPromise = (async () => { let error = null; if (force && chatRoomConnectionState === _type2.FcrChatRoomConnectionState.Connected) { await this._leaveChatRoomForRejoin(chatRoomId); } const [abort, promise] = (0, _abortableRetry.abortableRetry)(async () => { await this.conn.joinChatRoom({ roomId: chatRoomId }); this.logger.info(`join chat room success, room id: ${chatRoomId}, scene id: ${this._scene.sceneId}`); }, { retriesMax: Infinity }); this._abortJoinChatRoom = abort; try { [error] = await (0, _raceCondition.timeout)(promise, this._joinTimeoutMs); } catch (joinError) { if (attemptId !== this._joinAttemptId) { return _type3.FcrReturnCode.SUCCESS; } if (this._abortJoinChatRoom) { this._abortJoinChatRoom(); this._abortJoinChatRoom = null; } this.leave(); throw joinError; } this._abortJoinChatRoom = null; if (attemptId !== this._joinAttemptId) { if (!error && this._chatConnection.getConnectionInstance()) { this.conn.leaveChatRoom({ roomId: chatRoomId, success: () => { this.logger.info(`leave stale chat room success, room id: ${chatRoomId}, scene id: ${this._scene.sceneId}`); }, error: leaveError => { this.logger.error('leave stale chat room failed', leaveError.message); } }); } return _type3.FcrReturnCode.SUCCESS; } if (error) { this.logger.error(`join chat room failed, room id: ${chatRoomId}, scene id: ${this._scene.sceneId}`, error.message); this._setConnectionState(_type2.FcrChatRoomConnectionState.Disconnected); throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.FCR_ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, `join chat room failed: ${error.message}`); } else { this._setConnectionState(_type2.FcrChatRoomConnectionState.Connected); } return _type3.FcrReturnCode.SUCCESS; })(); try { return await this._joinPromise; } finally { this._joinPromise = null; } } return _type3.FcrReturnCode.SUCCESS; } _leaveChatRoomForRejoin(chatRoomId) { return new Promise(resolve => { if (!this._chatConnection.getConnectionInstance()) { resolve(); return; } this.conn.leaveChatRoom({ roomId: chatRoomId, success: () => resolve(), error: () => resolve() }); }); } _removeEventListener() { const connection = this._chatConnection.getConnectionInstance(); if (connection) { this.conn.removeEventHandler(this.connectionEventHandlerKey); } } _addEventListener() { this._removeEventListener(); this.conn.addEventHandler(this.connectionEventHandlerKey, this.chatRoomConnectionHandler); } _convertAgoraChatMessageToFcrChatRoomReceiveMessage = msg => { switch (msg.type) { case 'txt': { const message = { id: msg.id, from: msg.ext?.sender, type: _type2.FcrChatRoomMessageType.Text, timestamp: parseInt(`${msg.time}`), content: msg.msg, isPrivate: msg.ext?.isPrivate, properties: msg.ext?.properties }; return message; } } throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.FCR_ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'unknown remote message type'); }; _convertAgoraChatHistoryMessageToFcrChatRoomReceiveMessage(msg) { //@ts-ignore switch (msg.contentsType) { case 'TEXT': { const message = { id: msg.id, //@ts-ignore from: msg.ext?.sender, type: _type2.FcrChatRoomMessageType.Text, //@ts-ignore timestamp: parseInt(`${msg.time}`), //@ts-ignore content: msg.data, //@ts-ignore isPrivate: msg.ext?.isPrivate, //@ts-ignore properties: msg.ext?.properties }; return message; } } throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.FCR_ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'unknown remote message type'); } _convertFcrChatRoomSendBaseMessageToAgoraChatMessage(msg, progress) { switch (msg.type) { case _type2.FcrChatRoomMessageType.Text: { const message = msg; const isPrivate = !!message.to?.length; const localUserId = this._scene.getLocalUser().getLocalUserId(); const localUserInfo = this._scene.getUser(localUserId); return _easemobWebsdk.default.message.create({ to: this.chatRoomId, msg: message.content, type: 'txt', chatType: 'chatRoom', ext: { sender: (0, _user.convertRteUserToFcrUser)(localUserInfo, this._roomCache), roomUuid: this._scene.sceneId, isPrivate, properties: msg.properties }, receiverList: message.to }); } } throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.FCR_ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'unknown local message type'); } _handleScenePropertiesUpdated(sceneId, properties) { const chatRoomId = (0, _get.default)(properties.changedProperties, CHATROOM_ID_KEY_PATH); if (chatRoomId) { this.logger.info(`received chat room id change, chat room id: ${chatRoomId}, scene id: ${sceneId}`); this.join(); } } } exports.FcrChatRoomControlImpl = FcrChatRoomControlImpl;