fcr-core
Version:
Core APIs for building online scenes
437 lines (430 loc) • 21.5 kB
JavaScript
"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 _imports = require("../imports");
var _observable = require("agora-foundation/lib/utilities/observable");
var _easemobWebsdk = _interopRequireDefault(require("easemob-websdk"));
var _user = require("../utilities/user");
var _error = require("../utilities/error");
var _type = require("../room-control/chatroom-control/type");
var _type2 = require("../chat-connection/type");
var _logger = require("../utilities/logger");
var _validateParams = _interopRequireDefault(require("../utilities/validate-params"));
var _schema = require("../schema");
let _initProto, _sendMessageDecs;
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; }
/**
* @internal
*/
class FcrChatRoomControlImpl {
static #_ = (() => [_initProto] = _applyDecs(this, [[_imports.trace, 2, "join"], [_imports.trace, 2, "getConnectionState"], [_imports.trace, 2, "leave"], [_sendMessageDecs, 2, "sendMessage"], [_imports.trace, 2, "addObserver"], [_imports.trace, 2, "removeObserver"]], []).e)();
[(_sendMessageDecs = [_imports.trace, (0, _validateParams.default)(_schema.messageSchema, _schema.progressSchema.optional())], "logger")] = (() => (_initProto(this), (0, _logger.createLogger)({
prefix: 'FcrChatRoomControlImpl'
})))();
_observable = (() => new _observable.AgoraObservable())();
_connectionState = (() => _type.FcrChatRoomConnectionState.Disconnected)();
_chatConnectionObserver = {
onConnectionStateUpdated: async state => {
// im reconnect
this.logger.info(`onConnectionStateUpdated, chatConnectionState: ${state}, chatControlState: ${this.getConnectionState()}`);
if (state === _type2.FcrChatConnectionState.DISCONNECTED && this.getConnectionState() === _type.FcrChatRoomConnectionState.Connected) {
this._setConnectionState(_type.FcrChatRoomConnectionState.Disconnected);
} else if (state === _type2.FcrChatConnectionState.CONNECTED && this.getConnectionState() !== _type.FcrChatRoomConnectionState.Connected) {
this._removeEventListener();
this._addEventListener();
await this._joinChatRoom(state);
}
},
onConnectionLoginSuccess: () => {
this._addEventListener();
}
};
get conn() {
if (!this._chatConnection.getConnectionInstance()) {
throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'chat room connection not initialized, please get chat room token first', new Error('chat room connection not initialized, please get chat room token first'));
}
return this._chatConnection.getConnectionInstance();
}
get chatRoomId() {
return this._scene.getScenePropertiesByKeyPath('widgets.easemobIM.extra.chatRoomId');
}
constructor(_scene, _chatConnection, _sharedCache) {
this._scene = _scene;
this._chatConnection = _chatConnection;
this._sharedCache = _sharedCache;
this._roomCache = _sharedCache.getRoomCache(_scene.sceneId);
this._addLogObserver();
this._chatConnection.addObserver(this._chatConnectionObserver);
if (this._chatConnection.getConnectionInstance()) {
this._addEventListener();
}
}
async join() {
await this._joinChatRoom(this._chatConnection.getConnectionState());
}
getConnectionState() {
return this._connectionState;
}
async leave() {
if (!this._chatConnection.getConnectionInstance()) {
return;
}
this._chatConnection.removeObserver(this._chatConnectionObserver);
await new Promise(async (resolve, reject) => {
this._removeEventListener();
try {
await this.conn.leaveChatRoom({
roomId: this.chatRoomId,
success: () => {
resolve(undefined);
},
error: e => {
reject((0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'leave chat room failed', e));
}
});
} catch (e) {
resolve(undefined);
}
});
}
_joinChatRoom = async state => {
this.logger.info(`joinChatRoom, chatConnectionState: ${state}, chatControlState: ${this.getConnectionState()}`);
if (state === _type2.FcrChatConnectionState.CONNECTED && this.getConnectionState() === _type.FcrChatRoomConnectionState.Disconnected && this.chatRoomId) {
this._setConnectionState(_type.FcrChatRoomConnectionState.Connecting);
this.logger.info(`start join chat room, room id: ${this.chatRoomId}, scene id: ${this._scene.sceneId}`);
let error = null;
[error] = await (0, _imports.to)((0, _imports.retryAttempt)(async () => {
await this.conn.joinChatRoom({
roomId: this.chatRoomId
});
}, [], {
retriesMax: 4
}).fail(async _ref => {
let {
error,
timeFn,
currentRetry
} = _ref;
this.logger.error(`retry to join chat room, ${error.message}, retry ${currentRetry} times`);
await timeFn();
return true;
}).exec());
if (error) {
this._setConnectionState(_type.FcrChatRoomConnectionState.Disconnected);
throw error;
}
this._setConnectionState(_type.FcrChatRoomConnectionState.Connected);
}
};
_removeEventListener() {
this.conn.removeEventHandler(`chatroom${this._scene.sceneId}`);
}
_addEventListener() {
this._removeEventListener();
this.conn.addEventHandler(`chatroom${this._scene.sceneId}`, {
onTextMessage: msg => {
if (msg.to !== this.chatRoomId) {
return;
}
const message = this._convertAgoraChatMessageToFcrChatRoomReceiveMessage(msg);
this._observable.notifyObservers('onTextMessageReceived', this._scene.sceneId, message);
}
});
this.conn.onPictureMessage = msg => {
if (msg.to !== this.chatRoomId) {
return;
}
// 这里的数据结构和 onTextMessage 不一样,实际上contentsType是img ,type是chatroom,
// 但是因为在onPictureMessage的回调中,所以这里直接用type: 'img',后续可以考虑优化
const message = this._convertAgoraChatMessageToFcrChatRoomReceiveMessage({
...msg,
type: 'img'
});
this._observable.notifyObservers('onImageMessageReceived', this._scene.sceneId, message);
};
// TODO: 3.x
// this.conn.onCmdMessage = (msg) => {
// if (msg.to !== this.initConfig.chatRoomId) {
// return;
// }
// const message: FcrChatRoomReceiveCustomMessage =
// this._convertAgoraChatMessageToFcrChatRoomReceiveMessage(msg);
// this._observable.notifyObservers('onCustomMessageReceived', this._scene.sceneId, message);
// };
// TODO: 3.x
// this.conn.onChatroomEvent = (eventData: EasemobChat.ChatroomEvent) => {
// if (eventData.operation === 'updateAnnouncement') {
// this._observable.notifyObservers(
// 'onAnnouncementUpdated',
// this._scene.sceneId,
// // TODO: 应该用什么字段传递?
// (eventData.attributes as { [key: string]: string })?.announcement,
// );
// } else if (eventData.operation === 'deleteAnnouncement') {
// this._observable.notifyObservers('onAnnouncementDeleted', this._scene.sceneId);
// }
// };
}
_convertAgoraChatMessageToFcrChatRoomReceiveMessage = msg => {
// TODO: 没看到有 contentsType 字段
//@ts-ignore
switch (msg.type) {
case 'txt':
{
const message = {
id: msg.id,
//@ts-ignore
from: msg.ext.sender,
type: _type.FcrChatRoomMessageType.Text,
//@ts-ignore
timestamp: parseInt(msg.time),
//@ts-ignore
content: msg.msg,
isPrivate: msg.ext?.isPrivate,
properties: msg.ext?.properties
};
return message;
}
case 'img':
{
const message = {
id: msg.id,
from: msg.ext?.sender,
type: _type.FcrChatRoomMessageType.Image,
//@ts-ignore
timestamp: parseInt(msg.time),
url: msg.url || '',
isPrivate: msg.ext?.isPrivate,
properties: msg.ext?.properties
};
return message;
}
}
throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'unknown remote message type', new Error('unknown remote message type'));
};
_convertAgoraChatHistoryMessageToFcrChatRoomReceiveMessage = msg => {
// TODO: 没看到有 contentsType 字段
//@ts-ignore
switch (msg.contentsType) {
case 'TEXT':
{
const message = {
id: msg.id,
//@ts-ignore
from: msg.ext.sender,
type: _type.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;
}
case 'IMAGE':
{
const message = {
id: msg.id,
//@ts-ignore
from: msg.ext?.sender,
type: _type.FcrChatRoomMessageType.Image,
//@ts-ignore
timestamp: parseInt(msg.time),
//@ts-ignore
url: msg.url || '',
//@ts-ignore
isPrivate: msg.ext?.isPrivate,
//@ts-ignore
properties: msg.ext?.properties
};
return message;
}
}
throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'unknown remote message type', new Error('unknown remote message type'));
};
_convertFcrChatRoomSendBaseMessageToAgoraChatMessage = (msg, progress) => {
switch (msg.type) {
case _type.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
});
}
case _type.FcrChatRoomMessageType.Image:
{
const imageMessage = msg;
const isPrivateImage = !!imageMessage.to?.length;
const localUser = this._scene.getUser(this._scene.getLocalUser().getLocalUserId());
return _easemobWebsdk.default.message.create({
to: this.chatRoomId,
type: 'img',
chatType: 'chatRoom',
url: imageMessage.file.filePath,
file: {
filename: imageMessage.file.fileName,
filetype: imageMessage.file.fileType,
data: imageMessage.file.data,
url: ''
},
ext: {
sender: (0, _user.convertRteUserToFcrUser)(localUser, this._roomCache),
roomUuid: this._scene.sceneId,
isPrivate: isPrivateImage,
properties: msg.properties
},
receiverList: imageMessage.to,
onFileUploadProgress: e => {
progress?.(Math.round(e.loaded / e.total * 100));
}
});
}
}
throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'unknown local message type', new Error('unknown local message type'));
};
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 === _type.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);
}
removeObserver(observer) {
this._observable.removeObserver(observer);
}
_setConnectionState(state) {
this._connectionState = state;
this._observable.notifyObservers('onConnectionStateUpdated', this._scene.sceneId, state);
}
_addLogObserver() {
this.addObserver((0, _logger.generateLogObserver)(this.logger, ['onConnectionStateUpdated', 'onErrorOccurred'
// 'onTextMessageReceived'
]));
}
async getHistoryMessageList(pageSize, startedMessageId) {
if (pageSize > 50) {
throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, 'pageSize should be less than 50', new Error('pageSize should be less than 50'));
}
const assembleRet = await this.conn.fetchHistoryMessages({
queue: this.chatRoomId,
count: pageSize,
start: startedMessageId,
isGroup: true,
fail(error) {
throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM_CHATROOM, _error.FcrErrorCode.UNDEFINED_ERROR, error.message, new Error('get history message list failed'));
}
});
const ret = assembleRet.map(msgBody => {
return this._convertAgoraChatHistoryMessageToFcrChatRoomReceiveMessage(msgBody);
});
return ret;
}
// TODO: 3.x
// async getAnnouncement(): Promise<string> {
// const announcement = await this.conn.fetchChatRoomAnnouncement({
// roomId: this.initConfig.chatRoomId,
// error: (e) => {
// throw generateFcrCoreClientError(
// FcrErrorModuleCode.ROOM_CHATROOM,
// FcrErrorCode.UNDEFINED_ERROR,
// e.message,
// new Error('get announcement failed'),
// );
// },
// });
// return announcement.data?.announcement || '';
// }
// TODO: 3.x
// async setAnnouncement(announcement: string) {
// await this.conn.updateChatRoomAnnouncement({
// roomId: this.initConfig.chatRoomId,
// announcement,
// success: (_) => {},
// error: (e) => {
// throw generateFcrCoreClientError(
// FcrErrorModuleCode.ROOM_CHATROOM,
// FcrErrorCode.UNDEFINED_ERROR,
// e.message,
// new Error('set announcement failed'),
// );
// },
// });
// }
// TODO: 3.x
// async deleteAnnouncement() {
// await this.conn.updateChatRoomAnnouncement({
// roomId: this.initConfig.chatRoomId,
// announcement: '',
// success: (_) => {},
// error: (e) => {
// const { message } = e;
// throw generateFcrCoreClientError(
// FcrErrorModuleCode.ROOM_CHATROOM,
// FcrErrorCode.UNDEFINED_ERROR,
// message,
// new Error('delete announcement failed'),
// );
// },
// });
// }
}
exports.FcrChatRoomControlImpl = FcrChatRoomControlImpl;