(req: IncomingMessage)
| 97 | } |
| 98 | |
| 99 | async setup(req: IncomingMessage) { |
| 100 | if (this._setupPromise != null) { |
| 101 | return await this._setupPromise; |
| 102 | } |
| 103 | |
| 104 | this._setupPromise = (async () => { |
| 105 | // Check user permissions |
| 106 | |
| 107 | const roomName = (req.url ?? '').slice(1); |
| 108 | const pageId = splitStr(roomName, ':', 2)[1]; |
| 109 | |
| 110 | const groupId = await dataAbstraction().hget('page', pageId, 'group-id'); |
| 111 | |
| 112 | const groupIsPublic = await dataAbstraction().hget( |
| 113 | 'group', |
| 114 | groupId!, |
| 115 | 'is-public', |
| 116 | ); |
| 117 | |
| 118 | if (this.sessionId != null) { |
| 119 | let sessionInvalidated; |
| 120 | |
| 121 | [this.userId, sessionInvalidated] = await Promise.all([ |
| 122 | dataAbstraction().hget('session', this.sessionId, 'user-id'), |
| 123 | |
| 124 | this._checkSessionInvalidated(), |
| 125 | ]); |
| 126 | |
| 127 | if (this.userId == null || sessionInvalidated) { |
| 128 | this.destroySocket(); |
| 129 | throw new Error('Invalid session.'); |
| 130 | } |
| 131 | |
| 132 | if (!groupIsPublic) { |
| 133 | const groupMemberRole = await dataAbstraction().hget( |
| 134 | 'group-member', |
| 135 | `${groupId}:${this.userId}`, |
| 136 | 'role', |
| 137 | ); |
| 138 | |
| 139 | if (groupMemberRole == null) { |
| 140 | throw new Error('User is not a member of the group.'); |
| 141 | } |
| 142 | } |
| 143 | } else { |
| 144 | if (!groupIsPublic) { |
| 145 | this.destroySocket(); |
| 146 | |
| 147 | throw new Error( |
| 148 | 'Unauthenticated users can only access public groups.', |
| 149 | ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Setup room |
| 154 | |
| 155 | this.room = getRoom(roomName); |
| 156 | this.room.addSocket(this.socket); |
nothing calls this directly
no test coverage detected