Skip to content

Commit

Permalink
Tighter guard around room type in PolicyList
Browse files Browse the repository at this point in the history
As we updated typescript, we need to have a better guard around the room type.
matrix-bot-sdk was also updated because of this.
  • Loading branch information
Gnuxie committed Nov 1, 2022
1 parent 53f9312 commit 3e2e505
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 146 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"humanize-duration-ts": "^2.1.1",
"js-yaml": "^4.1.0",
"jsdom": "^16.6.0",
"matrix-bot-sdk": "^0.5.19",
"matrix-bot-sdk": "^0.6.2",
"parse-duration": "^1.0.2",
"shell-quote": "^1.7.3",
"yaml": "^2.1.1"
Expand Down
9 changes: 5 additions & 4 deletions src/models/PolicyList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { extractRequestError, LogService, MatrixClient, UserID } from "matrix-bot-sdk";
import { extractRequestError, LogService, MatrixClient, RoomCreateOptions, UserID } from "matrix-bot-sdk";
import { EventEmitter } from "events";
import { ALL_RULE_TYPES, EntityType, ListRule, Recommendation, ROOM_RULE_TYPES, RULE_ROOM, RULE_SERVER, RULE_USER, SERVER_RULE_TYPES, USER_RULE_TYPES } from "./ListRule";

Expand Down Expand Up @@ -121,7 +121,7 @@ class PolicyList extends EventEmitter {
client: MatrixClient,
shortcode: string,
invite: string[],
createRoomOptions = {}
createRoomOptions: RoomCreateOptions = {}
): Promise<string /* room id */> {
const powerLevels: { [key: string]: any } = {
"ban": 50,
Expand All @@ -143,7 +143,7 @@ class PolicyList extends EventEmitter {
},
"users_default": 0,
};
const finalRoomCreateOptions = {
const finalRoomCreateOptions: RoomCreateOptions = {
// Support for MSC3784.
creation_content: {
type: PolicyList.ROOM_TYPE
Expand All @@ -161,7 +161,8 @@ class PolicyList extends EventEmitter {
...createRoomOptions
};
// Guard room type in case someone overwrites it when declaring custom creation_content in future code.
if (!PolicyList.ROOM_TYPE_VARIANTS.includes(finalRoomCreateOptions.creation_content.type)) {
const roomType = finalRoomCreateOptions.creation_content?.type;
if (typeof roomType === 'string' && !PolicyList.ROOM_TYPE_VARIANTS.includes(roomType)) {
throw new TypeError(`Creating a policy room with a type other than the policy room type is not supported, you probably don't want to do this.`);
}
const listRoomId = await client.createRoom(finalRoomCreateOptions);
Expand Down
Loading

0 comments on commit 3e2e505

Please sign in to comment.