From 6a5190b0a8728d3ee5c405b2b1302ee72f5b163c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Mon, 26 Apr 2021 11:27:21 +0200 Subject: [PATCH] Expose the id of the user that sent a socket via the API --- CHANGELOG.md | 4 +++- src/socketlib.js | 32 ++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb3d717..7f63b9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ ## In development +### New features +- The `this` value of functions now contains the id of the user that triggered the function execution. + ### Bugfixes - When an invalid user id is specified socketlib will now throw the correct error message. - ## 1.0.3 ### Bugfixes - `executeFor` functions will no longer fail with an exception if a function scheduled to be called by the local user throws. diff --git a/src/socketlib.js b/src/socketlib.js index 544813e..48dcc9a 100644 --- a/src/socketlib.js +++ b/src/socketlib.js @@ -84,7 +84,7 @@ class SocketlibSocket { async executeAsGM(handler, ...args) { const [name, func] = this._resolveFunction(handler); if (game.user.isGM) { - return func(...args); + return this._executeLocal(func, ...args); } else { if (!game.users.find(user => user.isGM && user.active)) { @@ -97,7 +97,7 @@ class SocketlibSocket { async executeAsUser(handler, userId, ...args) { const [name, func] = this._resolveFunction(handler); if (userId === game.userId) - return func(...args); + return this._executeLocal(func, ...args); const user = game.users.get(userId); if (!user) throw new errors.SocketlibInvalidUserError(`No user with id '${userId}' exists.`); @@ -111,7 +111,7 @@ class SocketlibSocket { this._sendCommand(name, args, RECIPIENT_TYPES.ALL_GMS); if (game.user.isGM) { try { - func(...args); + this._executeLocal(func, ...args); } catch (e) { console.error(e); @@ -128,7 +128,7 @@ class SocketlibSocket { const [name, func] = this._resolveFunction(handler); this._sendCommand(name, args, RECIPIENT_TYPES.EVERYONE); try { - func(...args); + this._executeLocal(func, ...args); } catch (e) { console.error(e); } @@ -149,7 +149,7 @@ class SocketlibSocket { this._sendCommand(name, args, recipients); if (currentUserIndex >= 0) { try { - func(...args); + this._executeLocal(func, ...args); } catch (e) { console.error(e); @@ -185,6 +185,11 @@ class SocketlibSocket { game.socket.emit(this.socketName, message); } + _executeLocal(func, ...args) { + const socketdata = {userId: game.userId}; + return func.call({socketdata}, ...args); + } + _resolveFunction(func) { if (func instanceof Function) { const entry = Array.from(this.functions.entries()).find(([key, val]) => val === func); @@ -200,14 +205,14 @@ class SocketlibSocket { } } - _onSocketReceived(message) { + _onSocketReceived(message, senderId) { if (message.type === MESSAGE_TYPES.COMMAND || message.type === MESSAGE_TYPES.REQUEST) - this._handleRequest(message); + this._handleRequest(message, senderId); else - this._handleResponse(message); + this._handleResponse(message, senderId); } - async _handleRequest(message) { + async _handleRequest(message, senderId) { const {handlerName, args, recipient, id, type} = message; // Check if we're the recipient of the received message. If not, return early. if (recipient instanceof Array) { @@ -241,13 +246,15 @@ class SocketlibSocket { } throw e; } + const socketdata = {userId: senderId}; + const _this = {socketdata}; if (type === MESSAGE_TYPES.COMMAND) { - func(...args); + func.call(_this, ...args); } else { let result; try { - result = await func(...args); + result = await func.call(_this, ...args); } catch (e) { console.error(`An exception occured while executing handler '${name}'.`); @@ -258,11 +265,12 @@ class SocketlibSocket { } } - _handleResponse(message) { + _handleResponse(message, senderId) { const {id, result, type} = message; const request = this.pendingRequests.get(id); if (!request) return; + // TODO Verify if the response comes from the correct sender, discard otherwise switch (type) { case MESSAGE_TYPES.RESULT: request.resolve(result);