fcr-core
Version:
Core APIs for building online scenes
264 lines (260 loc) • 16.8 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");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FcrApplicationControlImpl = void 0;
require("core-js/modules/es.array.includes.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.iterator.find.js");
require("core-js/modules/esnext.iterator.map.js");
var _imports = require("../../imports");
var _error = require("../../utilities/error");
var _type = require("../privilege-control/type");
var _type2 = require("./type");
var _logger = require("../../utilities/logger");
let _initProto, _startApplicatioByRoleDecs, _cancelApplicationDecs, _acceptApplicationByUserIdDecs, _acceptAllApplicationDecs, _rejectApplicationByUserIdDecs, _rejectAllApplicationDecs, _getApplicationUserListDecs;
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; }
class FcrApplicationControlImpl {
static {
[_initProto] = _applyDecs(this, [[_startApplicatioByRoleDecs, 2, "startApplicatioByRole"], [_cancelApplicationDecs, 2, "cancelApplication"], [_acceptApplicationByUserIdDecs, 2, "acceptApplicationByUserId"], [_acceptAllApplicationDecs, 2, "acceptAllApplication"], [_rejectApplicationByUserIdDecs, 2, "rejectApplicationByUserId"], [_rejectAllApplicationDecs, 2, "rejectAllApplication"], [_getApplicationUserListDecs, 2, "getApplicationUserList"]], []).e;
}
//@internal
[(_startApplicatioByRoleDecs = (0, _imports.trace)(['params', 'targetRoles']), _cancelApplicationDecs = (0, _imports.trace)(['applicationKey', 'cause']), _acceptApplicationByUserIdDecs = (0, _imports.trace)(['applicationKey', 'targetUserIds', 'cause']), _acceptAllApplicationDecs = (0, _imports.trace)(['applicationKey', 'cause']), _rejectApplicationByUserIdDecs = (0, _imports.trace)(['applicationKey', 'targetUserIds', 'cause']), _rejectAllApplicationDecs = (0, _imports.trace)(['applicationKey', 'cause']), _getApplicationUserListDecs = (0, _imports.trace)(['applicationKey']), "logger")] = (_initProto(this), (0, _logger.createLogger)({
prefix: 'FcrApplicationControlImpl'
}));
//@internal
_observable = new _imports.AgoraObservable();
//@internal
//@internal
_sceneObserver = {
onScenePropertiesUpdated: (roomId, events) => {
if (!events?.cause?.data) {
return;
}
const data = events.cause.data;
if (events.cause.cmd === 12) {
this._handleApplicationUserListAddedEvent(roomId, data);
if ([1, 2, 3, 4, 5].includes(data.reason)) {
const localUserId = this._scene.localUser.getLocalUserId();
const localUser = this._scene.getUser(localUserId);
if (!localUser) {
this.logger.error('onScenePropertiesUpdated local user is not found');
return;
}
if (data.reason === 1) {
this._handleApplicationReceivedEvent(roomId, events, data, localUser);
}
const responderId = events.operatorUser.userId;
const isResponder = responderId == localUser.userId;
if (!isResponder) {
const applicationResponse = {
applicationKey: data.applicationKey,
responderId: events.operatorUser.userId,
cause: data.payload,
senderIds: data.removeApplicationUsers?.map(user => user.userUuid) || []
};
if (data.reason === 2) {
this._handleApplicationAcceptedEvent(roomId, applicationResponse, data, localUser);
}
if (data.reason === 4) {
this._handleApplicationRejectedEvent(roomId, applicationResponse, data, localUser);
}
if ([3, 5].includes(data.reason)) {
this._handleApplicationCancelledEvent(roomId, applicationResponse, data, localUser);
}
}
}
this._handleApplicationUserListRemovedEvent(roomId, data);
}
}
};
constructor(_scene, _api, _sharedCache) {
this._scene = _scene;
this._api = _api;
this._sharedCache = _sharedCache;
this._roomCache = _sharedCache.getRoomCache(this._scene.sceneId);
this._scene.addObserver(this._sceneObserver);
this._addLogObserver();
}
startApplicatioByRole(params, targetRoles) {
return (0, _error.handleRequestError)(() => this._api.startApplicatioByRole({
...this._getApiCommonParams(),
applicationKey: params.applicationKey,
targetRoles: targetRoles.map(role => _type.FcrTargetUserTypeToStringMap[role]),
cause: params.body
}), _imports.ErrorModuleCode.FCR_ROOM_APPLICATION, 'start application failed');
}
cancelApplication(applicationKey, cause) {
return (0, _error.handleRequestError)(() => this._api.cancelApplication({
...this._getApiCommonParams(),
applicationKey: applicationKey,
cause
}), _imports.ErrorModuleCode.FCR_ROOM_APPLICATION, 'cancel application failed');
}
acceptApplicationByUserId(applicationKey, targetUserIds, cause) {
return (0, _error.handleRequestError)(() => this._api.acceptApplicationByUserId({
...this._getApiCommonParams(),
applicationKey: applicationKey,
targetUserIds,
cause
}), _imports.ErrorModuleCode.FCR_ROOM_APPLICATION, 'accept application by user id failed');
}
acceptAllApplication(applicationKey, cause) {
return (0, _error.handleRequestError)(() => this._api.acceptAllApplication({
...this._getApiCommonParams(),
applicationKey: applicationKey,
cause
}), _imports.ErrorModuleCode.FCR_ROOM_APPLICATION, 'accept all application failed');
}
rejectApplicationByUserId(applicationKey, targetUserIds, cause) {
return (0, _error.handleRequestError)(() => this._api.rejectApplicationByUserId({
...this._getApiCommonParams(),
applicationKey: applicationKey,
targetUserIds,
cause
}), _imports.ErrorModuleCode.FCR_ROOM_APPLICATION, 'reject application by user id failed');
}
rejectAllApplication(applicationKey, cause) {
return (0, _error.handleRequestError)(() => this._api.rejectAllApplication({
...this._getApiCommonParams(),
applicationKey: applicationKey,
cause
}), _imports.ErrorModuleCode.FCR_ROOM_APPLICATION, 'accept all application failed');
}
getApplicationUserList(applicationKey) {
const users = this._scene.getScenePropertiesByKeyPath(`applications.${applicationKey}.users`);
if (!users) {
return undefined;
}
const activeUsers = users.filter(user => user.state == 1);
return activeUsers.map(user => {
const userInfo = this._roomCache.userMapByUserId.get(user.userUuid);
if (userInfo) {
return userInfo;
} else {
this.logger.error(` application user not found, userId: ${user.userUuid}`);
return {
userId: user.userUuid
};
}
});
}
addObserver(observer) {
this._observable.addObserver(observer);
}
removeObserver(observer) {
this._observable.removeObserver(observer);
}
_getApiCommonParams() {
return {
userUuid: this._scene.localUser.getLocalUserId(),
roomUuid: this._scene.sceneId
};
}
_getApproveTargetCondition(data, localUser) {
const approveTargetUserIdCondition = data.approveTarget === 2 && data.approveTargetUuids.includes(localUser.userId);
const approveTargetUserRoleCondition = data.approveTarget === 3 && data.approveTargetRoles.includes(localUser.userRole);
return approveTargetUserIdCondition || approveTargetUserRoleCondition;
}
_getRemoveApplicationUserIdCondition(data, localUser) {
const removeApplicationUserIdCondition = data.removeApplicationUsers?.find(user => user.userUuid === localUser.userId);
return removeApplicationUserIdCondition;
}
_getApplicationCancelledReason(data) {
let reason;
switch (data.reason) {
case 3:
reason = _type2.FcrApplicationCancelledReason.Requester;
break;
case 5:
reason = _type2.FcrApplicationCancelledReason.Timeout;
break;
case 8:
// todo: 需要确认是否是系统取消
reason = _type2.FcrApplicationCancelledReason.System;
break;
default:
reason = undefined;
break;
}
return reason;
}
_handleApplicationReceivedEvent(roomId, events, data, localUser) {
const approveTargetCondition = this._getApproveTargetCondition(data, localUser);
if (!approveTargetCondition) {
return;
}
const application = {
applicationKey: data.applicationKey,
// todo: 需要确认是否是applicationKey
body: data.payload,
senderId: events.operatorUser.userId // todo: 需要确认是否是申请人
};
this._observable.notifyObservers('onApplicationReceived', roomId, application);
}
_handleApplicationCancelledEvent(roomId, applicationResponse, data, localUser) {
const approveTargetCondition = this._getApproveTargetCondition(data, localUser);
const removeApplicationUserIdCondition = this._getRemoveApplicationUserIdCondition(data, localUser);
if (!approveTargetCondition && !removeApplicationUserIdCondition) {
return;
}
const reason = this._getApplicationCancelledReason(data);
if (!reason) {
this.logger.error('onUserPropertiesUpdated reason is not found');
return;
}
this._observable.notifyObservers('onApplicationCancelled', roomId, applicationResponse, reason);
}
_handleApplicationAcceptedEvent(roomId, applicationResponse, data, localUser) {
const approveTargetCondition = this._getApproveTargetCondition(data, localUser);
const addApplicationUsersCondition = data.addApplicationUsers?.find(user => user.userUuid === localUser.userId);
if (!approveTargetCondition && !addApplicationUsersCondition) {
return;
}
this._observable.notifyObservers('onApplicationAccepted', roomId, applicationResponse);
}
_handleApplicationRejectedEvent(roomId, applicationResponse, data, localUser) {
const approveTargetCondition = this._getApproveTargetCondition(data, localUser);
const removeApplicationUserIdCondition = this._getRemoveApplicationUserIdCondition(data, localUser);
if (!approveTargetCondition && !removeApplicationUserIdCondition) {
return;
}
this._observable.notifyObservers('onApplicationRejected', roomId, applicationResponse);
}
_handleApplicationUserListAddedEvent(roomId, data) {
if (data.addApplicationUsers && data.addApplicationUsers.length > 0) {
this._observable.notifyObservers('onApplicationUserListAdded', roomId, data.applicationKey, data.addApplicationUsers.map(user => user.userUuid));
}
}
_handleApplicationUserListRemovedEvent(roomId, data) {
if (data.removeApplicationUsers && data.removeApplicationUsers.length > 0) {
this._observable.notifyObservers('onApplicationUserListRemoved', roomId, data.applicationKey, data.removeApplicationUsers.map(user => user.userUuid));
}
}
_addLogObserver() {
this.addObserver((0, _logger.generateLogObserver)(this.logger, [['onApplicationReceived', ['roomId', 'application']], ['onApplicationCancelled', ['roomId', 'response', 'reason']], ['onApplicationAccepted', ['roomId', 'response']], ['onApplicationRejected', ['roomId', 'response']]]));
}
}
exports.FcrApplicationControlImpl = FcrApplicationControlImpl;