Skip to content

Commit

Permalink
Add more specific errors to CopyModerators command (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Shay authored Nov 9, 2023
1 parent 4d8770b commit d17d32f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
42 changes: 36 additions & 6 deletions src/commands/CopyModeratorsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { ICommand } from "./ICommand";
import { MatrixClient, MembershipEvent } from "matrix-bot-sdk";
import {MatrixClient, MembershipEvent, PowerLevelsEventContent} from "matrix-bot-sdk";
import { Conference } from "../Conference";

export class CopyModeratorsCommand implements ICommand {
Expand All @@ -29,8 +29,22 @@ export class CopyModeratorsCommand implements ICommand {
}
const fromRoomId = await this.client.resolveRoom(args[0]);
const toRoomId = await this.client.resolveRoom(args[1]);
const fromPl: {"users"?: Record<string, any>} = await this.client.getRoomStateEvent(fromRoomId, "m.room.power_levels", "");
let toPl = await this.client.getRoomStateEvent(toRoomId, "m.room.power_levels", "");

let fromPl: PowerLevelsEventContent = {}
try {
fromPl = await this.client.getRoomStateEvent(fromRoomId, "m.room.power_levels", "");
}
catch (error) {
throw Error(`Error fetching or processing power level event from room ${fromRoomId}:`, {cause: error})
}

let toPl: PowerLevelsEventContent;
try {
toPl = await this.client.getRoomStateEvent(toRoomId, "m.room.power_levels", "");
}
catch (error) {
throw Error(`Error fetching or processing power level event from room ${toRoomId}`, {cause: error})
}

if (!toPl) toPl = {};
if (!toPl['users']) toPl['users'] = {};
Expand All @@ -42,14 +56,30 @@ export class CopyModeratorsCommand implements ICommand {
}
}

await this.client.sendStateEvent(toRoomId, "m.room.power_levels", "", toPl);
try {
await this.client.sendStateEvent(toRoomId, "m.room.power_levels", "", toPl);
}
catch (error) {
throw Error(`Error sending new power level event into room ${toRoomId}`, {cause: error})
}

const state = await this.client.getRoomState(toRoomId);
let state: any[] = []
try {
state = await this.client.getRoomState(toRoomId);
}
catch (error) {
throw Error(`Error getting room state from room ${toRoomId}`, {cause: error})
}
const members = state.filter(s => s.type === "m.room.member").map(s => new MembershipEvent(s));
const effectiveJoinedUserIds = members.filter(m => m.effectiveMembership === "join").map(m => m.membershipFor);
for (const userId of Object.keys(toPl['users'])) {
if (!effectiveJoinedUserIds.includes(userId)) {
await this.client.inviteUser(userId, toRoomId);
try {
await this.client.inviteUser(userId, toRoomId);
}
catch (error) {
throw Error(`Error inviting user ${userId} to room ${toRoomId}`, {cause: error})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/HelpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class HelpCommand implements ICommand {
"<pre><code>" +
"!conference inviteme &lt;room&gt; - Asks the bot to invite you to the given room.\n" +
"!conference inviteto &lt;room&gt; &lt;user&gt; - Asks the bot to invite the given user to the given room.\n" +
"!conference join &lt;room&gt; - Makes the bot join the given room.\n"
"!conference join &lt;room&gt; - Makes the bot join the given room.\n" +
"!conference copymods &lt;from&gt; &lt;to&gt; - Copies the moderators from one room to another.\n" +
"!conference widgets &lt;aud&gt; - Creates all widgets for the auditorium and its talks.\n" +
"</code></pre>" +
Expand Down

0 comments on commit d17d32f

Please sign in to comment.