fix: rename roles to attributes to avoid confusion

This commit is contained in:
hippoz 2021-09-15 17:11:33 +03:00
parent b2a725d084
commit 7db4c5f2c2
Signed by untrusted user who does not match committer: hippoz
GPG key ID: 7C52899193467641
2 changed files with 17 additions and 17 deletions

View file

@ -19,11 +19,11 @@ const wsCloseCodes = {
NO_PING: [4008, "No ping"], NO_PING: [4008, "No ping"],
}; };
const roles = { const attributes = {
PRESENCE_UPDATES: "PRESENCE_UPDATES" PRESENCE_UPDATES: "PRESENCE_UPDATES"
}; };
const supportedRoles = [roles.PRESENCE_UPDATES]; const supportedAttributes = [attributes.PRESENCE_UPDATES];
class GatewaySession { class GatewaySession {
constructor() { constructor() {
@ -31,7 +31,7 @@ class GatewaySession {
this.user = null; this.user = null;
this.token = null; this.token = null;
this.sessionId = uuid.v4(); this.sessionId = uuid.v4();
this.roles = []; this.attributes = [];
// Specific to websocket sessions // Specific to websocket sessions
this.isWebsocketConnection = false; this.isWebsocketConnection = false;
@ -40,9 +40,9 @@ class GatewaySession {
this.channels = []; this.channels = [];
} }
hasRole(roleName) { hasAttribute(roleName) {
if (roleName.length < 1) return true; // TODO: HACK if (roleName.length < 1) return true; // TODO: HACK
return this.roles.includes(roleName); return this.attributes.includes(roleName);
} }
setWebsocketClient(ws) { setWebsocketClient(ws) {
@ -115,7 +115,7 @@ class GatewayHandler {
handleConnectionClose(ws) { handleConnectionClose(ws) {
if (ws.session && ws.session.user && ws.session.channels) { if (ws.session && ws.session.user && ws.session.channels) {
if (this.sessionCounters[ws.session.user._id] <= 1) { 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()) { if (client.session && client.session.isReady()) {
client.session.send("EVENT_CHANNEL_MEMBERS", { client.session.send("EVENT_CHANNEL_MEMBERS", {
[ws.session.user._id]: { [ws.session.user._id]: {
@ -155,7 +155,7 @@ class GatewayHandler {
eachInChannel({channelId, role=""}, callback) { eachInChannel({channelId, role=""}, callback) {
const clients = this.getClients(); const clients = this.getClients();
clients.forEach((client) => { 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); 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))) || []; 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()); session.channels = channels.map(x => x._id.toString());
if (data.roles) { if (data.attributes) {
if (!Array.isArray(data.roles) || data.roles.length > 8) return {error: wsCloseCodes.PAYLOAD_ERROR}; if (!Array.isArray(data.attributes) || data.attributes.length > 8) return {error: wsCloseCodes.PAYLOAD_ERROR};
for (let i = 0; i < data.roles; i++) { for (let i = 0; i < data.attributes; i++) {
if (!supportedRoles.includes(data[i])) return {error: wsCloseCodes.PAYLOAD_ERROR}; 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 }); 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: 1,
status_text: "Online" 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", { remoteSession.send("EVENT_CHANNEL_MEMBERS", {
[session.user._id]: { [session.user._id]: {
_id: 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);
} }
} }

View file

@ -43,11 +43,11 @@ JSON data format:
| Field | Description | | Field | Description |
| - | - | | - | - |
| token | The authentication token | | 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: Example:
```json ```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. 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). 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: Example:
```json ```json