diff --git a/brainlet/api/v2/gateway/index.js b/brainlet/api/v2/gateway/index.js index 8c6106d..6cb25fa 100644 --- a/brainlet/api/v2/gateway/index.js +++ b/brainlet/api/v2/gateway/index.js @@ -19,11 +19,11 @@ const wsCloseCodes = { NO_PING: [4008, "No ping"], }; -const roles = { +const attributes = { PRESENCE_UPDATES: "PRESENCE_UPDATES" }; -const supportedRoles = [roles.PRESENCE_UPDATES]; +const supportedAttributes = [attributes.PRESENCE_UPDATES]; class GatewaySession { constructor() { @@ -31,7 +31,7 @@ class GatewaySession { this.user = null; this.token = null; this.sessionId = uuid.v4(); - this.roles = []; + this.attributes = []; // Specific to websocket sessions this.isWebsocketConnection = false; @@ -40,9 +40,9 @@ class GatewaySession { this.channels = []; } - hasRole(roleName) { + hasAttribute(roleName) { if (roleName.length < 1) return true; // TODO: HACK - return this.roles.includes(roleName); + return this.attributes.includes(roleName); } setWebsocketClient(ws) { @@ -115,7 +115,7 @@ class GatewayHandler { handleConnectionClose(ws) { if (ws.session && ws.session.user && ws.session.channels) { if (this.sessionCounters[ws.session.user._id] <= 1) { - this.eachInChannel({channelId: ws.session.channels[0], role: roles.PRESENCE_UPDATES}, (client) => { + this.eachInChannel({channelId: ws.session.channels[0], role: attributes.PRESENCE_UPDATES}, (client) => { if (client.session && client.session.isReady()) { client.session.send("EVENT_CHANNEL_MEMBERS", { [ws.session.user._id]: { @@ -155,7 +155,7 @@ class GatewayHandler { eachInChannel({channelId, role=""}, callback) { const clients = this.getClients(); clients.forEach((client) => { - if (client.session && client.session.isReady() && client.session.hasRole(role) && client.session.channels.includes(channelId)) + if (client.session && client.session.isReady() && client.session.hasAttribute(role) && client.session.channels.includes(channelId)) callback(client); }); } @@ -173,12 +173,12 @@ class GatewayHandler { const channels = (await Channel.find().lean().sort({ _id: -1 }).limit(50).select("-posts -__v").populate("creator", User.getPulicFields(true))) || []; session.channels = channels.map(x => x._id.toString()); - if (data.roles) { - if (!Array.isArray(data.roles) || data.roles.length > 8) return {error: wsCloseCodes.PAYLOAD_ERROR}; - for (let i = 0; i < data.roles; i++) { - if (!supportedRoles.includes(data[i])) return {error: wsCloseCodes.PAYLOAD_ERROR}; + if (data.attributes) { + if (!Array.isArray(data.attributes) || data.attributes.length > 8) return {error: wsCloseCodes.PAYLOAD_ERROR}; + for (let i = 0; i < data.attributes; i++) { + if (!supportedAttributes.includes(data[i])) return {error: wsCloseCodes.PAYLOAD_ERROR}; } - session.roles = data.roles; + session.attributes = data.attributes; } session.send("YOO_ACK", { session_id: session.sessionId, channels, user: { username: session.user.username, _id: session.user._id }, __global_experiments: experiments }); @@ -194,7 +194,7 @@ class GatewayHandler { status: 1, status_text: "Online" }; - if (remoteSession.sessionId !== session.sessionId && remoteSession.hasRole(roles.PRESENCE_UPDATES)) { + if (remoteSession.sessionId !== session.sessionId && remoteSession.hasAttribute(attributes.PRESENCE_UPDATES)) { remoteSession.send("EVENT_CHANNEL_MEMBERS", { [session.user._id]: { _id: session.user._id, @@ -206,7 +206,7 @@ class GatewayHandler { } }); - (session.hasRole(roles.PRESENCE_UPDATES)) && session.send("EVENT_CHANNEL_MEMBERS", presence); + (session.hasAttribute(attributes.PRESENCE_UPDATES)) && session.send("EVENT_CHANNEL_MEMBERS", presence); } } diff --git a/resources/Docs/DOCS.md b/resources/Docs/DOCS.md index c2dbdb5..6b3483b 100644 --- a/resources/Docs/DOCS.md +++ b/resources/Docs/DOCS.md @@ -43,11 +43,11 @@ JSON data format: | Field | Description | | - | - | | token | The authentication token | -| roles | An array of attributes the client wants the server to enable. The current possible values are: `PRESENCE_UPDATES` (required for presence updates to be sent to the client) | +| attributes | An array of attributes the client wants the server to enable. The current possible values are: `PRESENCE_UPDATES` (required for presence updates to be sent to the client) | Example: ```json -1@{"token":"my totally real token","roles":["PRESENCE_UPDATES"]} +1@{"token":"my totally real token","attributes":["PRESENCE_UPDATES"]} ``` If the token is invalid, or the connection is otherwise rejected, the client should be disconnected as soon as possible, and no YOO\_ACK should be sent. @@ -117,7 +117,7 @@ Example: An object containing a list of user presences which were updated, where the keys are the user ids and the values are [user presence objects](#user-presence-object). -**This packet is only sent if the user has `PRESENCE_UPDATES` in their `roles`, specified in YOO.** +**This packet is only sent if the user has `PRESENCE_UPDATES` in their `attributes`, specified in YOO.** Example: ```json