Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more logging and errors #197

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions src/commands/FDMCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,45 @@ export class FDMCommand implements ICommand {

public async run(roomId: string, event: any, args: string[]) {
const spi = this.conference.getInterestRoom("I.infodesk");
const infBackstage = await this.client.resolveRoom("#infodesk-backstage:fosdem.org");
const vol = await this.client.resolveRoom("#volunteers:fosdem.org");
const volBackstage = await this.client.resolveRoom("#volunteers-backstage:fosdem.org");

let infBackstage;
try {
infBackstage = await this.client.resolveRoom("#infodesk-backstage:fosdem.org");
}
catch (error) {
throw Error(`Error resolving the roomID for room #infodesk-backstage:fosdem.org`, {cause: error})
}

let vol;
try {
vol = await this.client.resolveRoom("#volunteers:fosdem.org");
}
catch (error) {
throw Error(`Error resolving the roomID for room #volunteers:fosdem.org`, {cause: error})
}

let volBackstage;
try {
volBackstage = await this.client.resolveRoom("#volunteers-backstage:fosdem.org");
}
catch (error) {
throw Error(`Error resolving the roomID for room #volunteers-backstage:fosdem.org`, {cause: error})
}

const db = await this.conference.getPentaDb();
if (db === null) {
await this.client.replyNotice(roomId, event, "Command not available as PentaDb is not enabled.");
return;
}

let volunteers = await db.findAllPeopleWithRemark("volunteer");
let volunteers;
try {
volunteers = await db.findAllPeopleWithRemark("volunteer");
}
catch (error) {
throw Error('There was an error fetching volunteers from the database', {cause:error})
}

const dedupe: IPerson[] = [];
for (const volunteer of volunteers) {
if (!dedupe.some(p => p.id === volunteer.id)) {
Expand All @@ -58,9 +86,32 @@ export class FDMCommand implements ICommand {
} else if (args[0] === 'invite') {
const infodesk = await this.conference.getInviteTargetsForInterest(spi);
const infodeskResolved = await resolveIdentifiers(this.client, infodesk);
const inBsJoined = await this.client.getJoinedRoomMembers(infBackstage);
const volJoined = await this.client.getJoinedRoomMembers(vol);
const volBsJoined = await this.client.getJoinedRoomMembers(volBackstage);

let inBsJoined;
try {
inBsJoined = await this.client.getJoinedRoomMembers(infBackstage);
}
catch (error) {
throw Error(`Error fetching the members of the room #infodesk-backstage:fosdem.org`, {cause: error})
}

let volJoined;
try {
volJoined = await this.client.getJoinedRoomMembers(vol);
}
catch (error) {
throw Error(`Error fetching the members of the room #volunteers:fosdem.org`, {cause:error})
}

let volBsJoined;
try {
volBsJoined = await this.client.getJoinedRoomMembers(volBackstage);
}
catch (error) {
throw Error("Error fetching members of the room #volunteers-backstage:fosdem.org", {cause:error})
}


for (const person of infodeskResolved) {
try {
if (person.mxid && inBsJoined.includes(person.mxid)) continue;
Expand Down
16 changes: 14 additions & 2 deletions src/commands/InviteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export class InviteCommand implements ICommand {
private async createInvites(people: IPerson[], alias: string) {
const resolved = await resolveIdentifiers(this.client, people);

const targetRoomId = await this.client.resolveRoom(alias);
let targetRoomId;
try {
targetRoomId = await this.client.resolveRoom(alias);
}
catch (error) {
throw Error(`Error resolving room id for ${alias}`, {cause: error})
}
await this.ensureInvited(targetRoomId, resolved);
}

Expand Down Expand Up @@ -94,7 +100,13 @@ export class InviteCommand implements ICommand {
public async ensureInvited(roomId: string, people: ResolvedPersonIdentifier[]) {
// We don't want to invite anyone we have already invited or that has joined though, so
// avoid those people. We do this by querying the room state and filtering.
const state = await this.client.getRoomState(roomId);
let state;
try {
state = await this.client.getRoomState(roomId);
}
catch (error) {
throw Error(`Error fetching state for room ${roomId}`, {cause: error})
}
const emailInvitePersonIds = state.filter(s => s.type === "m.room.third_party_invite").map(s => s.content?.[RS_3PID_PERSON_ID]).filter(i => !!i);
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);
Expand Down
23 changes: 20 additions & 3 deletions src/commands/InviteMeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ export class InviteMeCommand implements ICommand {
public readonly prefixes = ["inviteme", "inviteto"];

private async inviteTo(invitee: string, room: string): Promise<void> {
const members = await this.client.getJoinedRoomMembers(room);
let members;
try {
members = await this.client.getJoinedRoomMembers(room);
}
catch (error) {
throw Error(`Error getting joined members from room ${room}`, {cause:error})
}
if (members.includes(invitee)) return;
await this.client.inviteUser(invitee, room);
}
Expand Down Expand Up @@ -137,8 +143,19 @@ export class InviteMeCommand implements ICommand {
await this.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
} else {
// Invite to one particular room.
const targetRoomId = await this.client.resolveRoom(args[0]);
await this.client.inviteUser(userId, targetRoomId);
let targetRoomId;
try {
targetRoomId = await this.client.resolveRoom(args[0]);
}
catch (error) {
throw Error(`Error resolving room ${args[0]}`, {cause:error})
}
try {
await this.client.inviteUser(userId, targetRoomId);
}
catch (error) {
throw Error(`Error inviting ${userId} to ${targetRoomId}`, {cause:error})
}
await this.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/commands/JoinRoomCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export class JoinCommand implements ICommand {

await this.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '⌛️');

await this.client.joinRoom(args[0], []);
try {
await this.client.joinRoom(args[0], []);
}
catch (error) {
throw Error(`Error joining room ${args[0]}`, {cause:error})
}

await this.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
}
Expand Down
17 changes: 15 additions & 2 deletions src/commands/PermissionsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,25 @@ export class PermissionsCommand implements ICommand {
}

public static async ensureModerator(client: MatrixClient, roomId: string, people: ResolvedPersonIdentifier[]) {
const powerLevels = await client.getRoomStateEvent(roomId, "m.room.power_levels", "");
let powerLevels;
try {
powerLevels = await client.getRoomStateEvent(roomId, "m.room.power_levels", "");
}
catch (error) {
throw Error(`Error fetching power levels for room ${roomId}`, {cause:error})
}

for (const person of people) {
if (!person.mxid) continue;
if (powerLevels['users'][person.mxid]) continue;
powerLevels['users'][person.mxid] = 50;
}
await client.sendStateEvent(roomId, "m.room.power_levels", "", powerLevels);

try {
await client.sendStateEvent(roomId, "m.room.power_levels", "", powerLevels);
}
catch (error) {
throw Error(`Error sending powerlevels event into room ${roomId}`, {cause:error})
}
}
}
Loading