From dd79f7d0f4b7bf7d79a81c30d03398da5a7a8d2f Mon Sep 17 00:00:00 2001 From: Valdo Romao Date: Wed, 1 Dec 2021 15:38:34 +0000 Subject: [PATCH] Added the user-room token created from admin when we access a room --- back/src/Services/SocketManager.ts | 1 + docs/maps/api-player.md | 23 +++++++++++++++++++ front/src/Api/Events/GameStateEvent.ts | 1 + front/src/Api/iframe/player.ts | 13 +++++++++++ front/src/Connexion/RoomConnection.ts | 6 +++++ front/src/Phaser/Game/GameScene.ts | 1 + front/src/iframe_api.ts | 5 ++-- maps/tests/Metadata/getCurrentRoom.js | 1 + messages/protos/messages.proto | 2 ++ pusher/src/Controller/IoSocketController.ts | 5 ++++ .../src/Model/Websocket/ExSocketInterface.ts | 1 + pusher/src/Services/AdminApi.ts | 1 + pusher/src/Services/SocketManager.ts | 5 ++++ 13 files changed, 63 insertions(+), 2 deletions(-) diff --git a/back/src/Services/SocketManager.ts b/back/src/Services/SocketManager.ts index 5efae800..8989df75 100644 --- a/back/src/Services/SocketManager.ts +++ b/back/src/Services/SocketManager.ts @@ -97,6 +97,7 @@ export class SocketManager { } const roomJoinedMessage = new RoomJoinedMessage(); roomJoinedMessage.setTagList(joinRoomMessage.getTagList()); + roomJoinedMessage.setUserroomtoken(joinRoomMessage.getUserroomtoken()); for (const [itemId, item] of room.getItemsState().entries()) { const itemStateMessage = new ItemStateMessage(); diff --git a/docs/maps/api-player.md b/docs/maps/api-player.md index 39a13d9e..cb2910c7 100644 --- a/docs/maps/api-player.md +++ b/docs/maps/api-player.md @@ -58,6 +58,29 @@ WA.onInit().then(() => { }) ``` +### Get the user-room token of the player + +``` +WA.player.userRoomToken: string; +``` + +The user-room token is available from the `WA.player.userRoomToken` property. +This token is generated in WorkAdventure and contains information such as the player's room ID and its associated membership ID. + +{.alert.alert-warn} +This token is used when you change your logo using a configured variable. +Indeed, to change your logo you need to perform an upload in order to get a file URL. This type of actions must be validated on our side. +If you are using a self-hosted version of WorkAdventure you will not have the possibility to perform actions that depends on the user-room token, unless you create an API that support it. + +{.alert.alert-info} +You need to wait for the end of the initialization before accessing `WA.player.userRoomToken` + +```typescript +WA.onInit().then(() => { + console.log('Token: ', WA.player.userRoomToken); +}) +``` + ### Listen to player movement ``` WA.player.onPlayerMove(callback: HasPlayerMovedEventCallback): void; diff --git a/front/src/Api/Events/GameStateEvent.ts b/front/src/Api/Events/GameStateEvent.ts index 112c2880..1f0f36ed 100644 --- a/front/src/Api/Events/GameStateEvent.ts +++ b/front/src/Api/Events/GameStateEvent.ts @@ -9,6 +9,7 @@ export const isGameStateEvent = new tg.IsInterface() startLayerName: tg.isUnion(tg.isString, tg.isNull), tags: tg.isArray(tg.isString), variables: tg.isObject, + userRoomToken: tg.isUnion(tg.isString, tg.isUndefined), }) .get(); /** diff --git a/front/src/Api/iframe/player.ts b/front/src/Api/iframe/player.ts index 078a1926..ed1b59d8 100644 --- a/front/src/Api/iframe/player.ts +++ b/front/src/Api/iframe/player.ts @@ -20,6 +20,12 @@ export const setTags = (_tags: string[]) => { let uuid: string | undefined; +let userRoomToken: string | undefined; + +export const setUserRoomToken = (token: string | undefined) => { + userRoomToken = token; +}; + export const setUuid = (_uuid: string | undefined) => { uuid = _uuid; }; @@ -67,6 +73,13 @@ export class WorkadventurePlayerCommands extends IframeApiContribution any) = null; // eslint-disable-line @typescript-eslint/no-explicit-any private closed: boolean = false; private tags: string[] = []; + private _userRoomToken: string | undefined; // eslint-disable-next-line @typescript-eslint/no-explicit-any public static setWebsocketFactory(websocketFactory: (url: string) => any): void { @@ -211,6 +212,7 @@ export class RoomConnection implements RoomConnection { this.userId = roomJoinedMessage.getCurrentuserid(); this.tags = roomJoinedMessage.getTagList(); + this._userRoomToken = roomJoinedMessage.getUserroomtoken(); this.dispatch(EventMessage.CONNECT, { connection: this, @@ -710,4 +712,8 @@ export class RoomConnection implements RoomConnection { public getAllTags(): string[] { return this.tags; } + + public get userRoomToken(): string | undefined { + return this._userRoomToken; + } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index d9bb8186..8a2de5cd 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1165,6 +1165,7 @@ ${escapedMessage} roomId: this.roomUrl, tags: this.connection ? this.connection.getAllTags() : [], variables: this.sharedVariablesManager.variables, + userRoomToken: this.connection ? this.connection.userRoomToken : '' }; }); this.iframeSubscriptionList.push( diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index dcd10fdc..93415b0d 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -15,11 +15,11 @@ import ui from "./Api/iframe/ui"; import sound from "./Api/iframe/sound"; import room, { setMapURL, setRoomId } from "./Api/iframe/room"; import state, { initVariables } from "./Api/iframe/state"; -import player, { setPlayerName, setTags, setUuid } from "./Api/iframe/player"; +import player, { setPlayerName, setTags, setUserRoomToken, setUuid } from "./Api/iframe/player"; import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor"; import type { Popup } from "./Api/iframe/Ui/Popup"; import type { Sound } from "./Api/iframe/Sound/Sound"; -import { answerPromises, queryWorkadventure, sendToWorkadventure } from "./Api/iframe/IframeApiContribution"; +import { answerPromises, queryWorkadventure } from "./Api/iframe/IframeApiContribution"; // Notify WorkAdventure that we are ready to receive data const initPromise = queryWorkadventure({ @@ -32,6 +32,7 @@ const initPromise = queryWorkadventure({ setTags(state.tags); setUuid(state.uuid); initVariables(state.variables as Map); + setUserRoomToken(state.userRoomToken); }); const wa = { diff --git a/maps/tests/Metadata/getCurrentRoom.js b/maps/tests/Metadata/getCurrentRoom.js index df3a995c..fa8e0226 100644 --- a/maps/tests/Metadata/getCurrentRoom.js +++ b/maps/tests/Metadata/getCurrentRoom.js @@ -4,6 +4,7 @@ WA.onInit().then(() => { console.log('Player name: ', WA.player.name); console.log('Player id: ', WA.player.id); console.log('Player tags: ', WA.player.tags); + console.log('Player token: ', WA.player.userRoomToken); }); WA.room.getTiledMap().then((data) => { diff --git a/messages/protos/messages.proto b/messages/protos/messages.proto index 76f4f5ad..38952903 100644 --- a/messages/protos/messages.proto +++ b/messages/protos/messages.proto @@ -198,6 +198,7 @@ message RoomJoinedMessage { int32 currentUserId = 4; repeated string tag = 5; repeated VariableMessage variable = 6; + string userRoomToken = 7; } message WebRtcStartMessage { @@ -297,6 +298,7 @@ message JoinRoomMessage { string IPAddress = 7; CompanionMessage companion = 8; string visitCardUrl = 9; + string userRoomToken = 10; } message UserJoinedZoneMessage { diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index 35fd08d5..3c369969 100644 --- a/pusher/src/Controller/IoSocketController.ts +++ b/pusher/src/Controller/IoSocketController.ts @@ -186,6 +186,7 @@ export class IoSocketController { let memberTags: string[] = []; let memberVisitCardUrl: string | null = null; let memberMessages: unknown; + let memberUserRoomToken: string | undefined; let memberTextures: CharacterTexture[] = []; const room = await socketManager.getOrCreateRoom(roomId); let userData: FetchMemberDataByUuidResponse = { @@ -196,6 +197,7 @@ export class IoSocketController { textures: [], messages: [], anonymous: true, + userRoomToken: undefined, }; if (ADMIN_API_URL) { try { @@ -232,6 +234,8 @@ export class IoSocketController { memberTags = userData.tags; memberVisitCardUrl = userData.visitCardUrl; memberTextures = userData.textures; + memberUserRoomToken = userData.userRoomToken; + if ( room.policyType === GameRoomPolicyTypes.USE_TAGS_POLICY && (userData.anonymous === true || !room.canAccess(memberTags)) @@ -281,6 +285,7 @@ export class IoSocketController { messages: memberMessages, tags: memberTags, visitCardUrl: memberVisitCardUrl, + userRoomToken: memberUserRoomToken, textures: memberTextures, position: { x: x, diff --git a/pusher/src/Model/Websocket/ExSocketInterface.ts b/pusher/src/Model/Websocket/ExSocketInterface.ts index ff5ed211..411d88fa 100644 --- a/pusher/src/Model/Websocket/ExSocketInterface.ts +++ b/pusher/src/Model/Websocket/ExSocketInterface.ts @@ -44,4 +44,5 @@ export interface ExSocketInterface extends WebSocket, Identificable { textures: CharacterTexture[]; backConnection: BackConnection; listenedZones: Set; + userRoomToken: string | undefined; } diff --git a/pusher/src/Services/AdminApi.ts b/pusher/src/Services/AdminApi.ts index 416b9cb6..bc3b2172 100644 --- a/pusher/src/Services/AdminApi.ts +++ b/pusher/src/Services/AdminApi.ts @@ -29,6 +29,7 @@ export interface FetchMemberDataByUuidResponse { textures: CharacterTexture[]; messages: unknown[]; anonymous?: boolean; + userRoomToken: string | undefined; } class AdminApi { diff --git a/pusher/src/Services/SocketManager.ts b/pusher/src/Services/SocketManager.ts index 1761f1bd..f3cfd95a 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -151,6 +151,11 @@ export class SocketManager implements ZoneEventListener { joinRoomMessage.setName(client.name); joinRoomMessage.setPositionmessage(ProtobufUtils.toPositionMessage(client.position)); joinRoomMessage.setTagList(client.tags); + + if (client.userRoomToken) { + joinRoomMessage.setUserroomtoken(client.userRoomToken); + } + if (client.visitCardUrl) { joinRoomMessage.setVisitcardurl(client.visitCardUrl); }