Skip to content

Commit

Permalink
Refactor list creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie committed Sep 27, 2022
1 parent a24b689 commit 2f0a38a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 65 deletions.
46 changes: 10 additions & 36 deletions src/appservice/MjolnirManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mjolnir } from "../Mjolnir";
import { Request, WeakEvent, BridgeContext } from "matrix-appservice-bridge";
import { IConfig, read as configRead } from "../config";
import { SHORTCODE_EVENT_TYPE } from "../models/PolicyList";
import PolicyList, { SHORTCODE_EVENT_TYPE } from "../models/PolicyList";
import { Permalinks, MatrixClient } from "matrix-bot-sdk";

export class MjolnirManager {
Expand All @@ -19,11 +19,8 @@ export class MjolnirManager {
// rather than externally...
// FIXME: We need to verify that we haven't stored a mjolnir already if we aren't doing the above.

// get mjolnir list wroking by just avoiding it for now and see if protections work
// and bans.
// Find out trade offs of changing mjolnir to make it work vs making new subcomponent of mjolnir.
const managedMjolnir = new ManagedMjolnir(await Mjolnir.setupMjolnirFromConfig(client, this.getDefaultMjolnirConfig(managementRoomId)));
await managedMjolnir.moveMeSomewhereCommonAndStopImplementingFunctionalityOnACommandFirstBasis(requestingUserId, 'list')
await managedMjolnir.createFirstList(requestingUserId, 'list')
this.mjolnirs.set(await client.getUserId(), managedMjolnir);
}

Expand Down Expand Up @@ -64,37 +61,14 @@ export class ManagedMjolnir {
await this.mjolnir.addProtectedRoom(roomId);
}

public async moveMeSomewhereCommonAndStopImplementingFunctionalityOnACommandFirstBasis(mjolnirOwnerId: string, shortcode: string) {
const powerLevels: { [key: string]: any } = {
"ban": 50,
"events": {
"m.room.name": 100,
"m.room.power_levels": 100,
},
"events_default": 50, // non-default
"invite": 0,
"kick": 50,
"notifications": {
"room": 20,
},
"redact": 50,
"state_default": 50,
"users": {
[await this.mjolnir.client.getUserId()]: 100,
[mjolnirOwnerId]: 50
},
"users_default": 0,
};

const listRoomId = await this.mjolnir.client.createRoom({
preset: "public_chat",
invite: [mjolnirOwnerId],
name: `${mjolnirOwnerId}'s policy room`,
initial_state: [{type: SHORTCODE_EVENT_TYPE, state_key: "", content: {shortcode: shortcode}}],
power_level_content_override: powerLevels,
});

public async createFirstList(mjolnirOwnerId: string, shortcode: string) {
const listRoomId = await PolicyList.createList(
this.mjolnir.client,
shortcode,
[mjolnirOwnerId],
{ name: `${mjolnirOwnerId}'s policy room` }
);
const roomRef = Permalinks.forRoom(listRoomId);
await this.mjolnir.watchList(roomRef);
return await this.mjolnir.watchList(roomRef);
}
}
36 changes: 7 additions & 29 deletions src/commands/CreateBanListCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,20 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { SHORTCODE_EVENT_TYPE } from "../models/PolicyList";
import PolicyList from "../models/PolicyList";
import { Permalinks, RichReply } from "matrix-bot-sdk";

// !mjolnir list create <shortcode> <alias localpart>
export async function execCreateListCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const shortcode = parts[3];
const aliasLocalpart = parts[4];

const powerLevels: { [key: string]: any } = {
"ban": 50,
"events": {
"m.room.name": 100,
"m.room.power_levels": 100,
},
"events_default": 50, // non-default
"invite": 0,
"kick": 50,
"notifications": {
"room": 20,
},
"redact": 50,
"state_default": 50,
"users": {
[await mjolnir.client.getUserId()]: 100,
[event["sender"]]: 50
},
"users_default": 0,
};

const listRoomId = await mjolnir.client.createRoom({
preset: "public_chat",
room_alias_name: aliasLocalpart,
invite: [event['sender']],
initial_state: [{ type: SHORTCODE_EVENT_TYPE, state_key: "", content: { shortcode: shortcode } }],
power_level_content_override: powerLevels,
});
const listRoomId = await PolicyList.createList(
mjolnir.client,
shortcode,
[event['sender']],
{ room_alias_name: aliasLocalpart }
);

const roomRef = Permalinks.forRoom(listRoomId);
await mjolnir.watchList(roomRef);
Expand Down
54 changes: 54 additions & 0 deletions src/models/PolicyList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,60 @@ class PolicyList extends EventEmitter {
this.batcher = new UpdateBatcher(this);
}

/**
* Create a new policy list.
* @param client A MatrixClient that will be used to create the list.
* @param shortcode A shortcode to refer to the list with.
* @param invite A list of users to invite to the list and make moderator.
* @param createRoomOptions Additional room create options such as an alias.
* @returns The room id for the newly created policy list.
*/
public static async createList(
client: MatrixClient,
shortcode: string,
invite: string[],
createRoomOptions = {}
): Promise<string /* room id */> {
const powerLevels: { [key: string]: any } = {
"ban": 50,
"events": {
"m.room.name": 100,
"m.room.power_levels": 100,
},
"events_default": 50, // non-default
"invite": 0,
"kick": 50,
"notifications": {
"room": 20,
},
"redact": 50,
"state_default": 50,
"users": {
[await client.getUserId()]: 100,
...invite.map(mxid => ({ [mxid]: 50 })),
},
"users_default": 0,
};
const listRoomId = await client.createRoom({
// Support for MSC3784.
creation_content: {
type: 'support.feline.policy.lists.msc.v1'
},
preset: "public_chat",
invite,
initial_state: [
{
type: SHORTCODE_EVENT_TYPE,
state_key: "",
content: {shortcode: shortcode}
}
],
power_level_content_override: powerLevels,
...createRoomOptions
});
return listRoomId
}

/**
* The code that can be used to refer to this banlist in Mjolnir commands.
*/
Expand Down

0 comments on commit 2f0a38a

Please sign in to comment.