Skip to content

Commit

Permalink
Ensure we don't accidentally send out invites when testing (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Shay authored Jan 16, 2024
1 parent 0b54630 commit 7189eb1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
5 changes: 4 additions & 1 deletion config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,7 @@ runMode: normal
metrics:
enabled: false
port: 8081
address: 127.0.0.1
address: 127.0.0.1

# if set to `true` will prevent the conf-bot from sending live invites to email/matrix_ids
dry_run_enabled: false
1 change: 1 addition & 0 deletions spec/util/e2e-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export class E2ETestEnv {
address: '0.0.0.0',
port: 0,
},
dry_run_enabled: false,
...providedConfig,
};
const conferenceBot = await ConferenceBot.start(config);
Expand Down
5 changes: 3 additions & 2 deletions src/IRCBridge.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 { MatrixClient, MatrixEvent } from "matrix-bot-sdk";
import {LogService, MatrixClient, MatrixEvent} from "matrix-bot-sdk";
import * as irc from "irc-upd";
import { Auditorium } from "./models/Auditorium";
import { InterestRoom } from "./models/InterestRoom";
Expand Down Expand Up @@ -128,7 +128,8 @@ export class IRCBridge {

public async plumbChannelToRoom(channel: string, roomId: string) {
if (await this.shouldInviteBot(roomId)) {
await this.mxClient.inviteUser(this.config.botUserId, roomId);
//await this.mxClient.inviteUser(this.config.botUserId, roomId);
LogService.info("IRCBridge", `Inviting ${this.config.botUserId}`)
}
await this.ircClient.join(channel);
const result = await this.executeCommand(`plumb ${roomId} ${this.config.serverName} ${channel}`);
Expand Down
9 changes: 5 additions & 4 deletions src/commands/FDMCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import { invitePersonToRoom, resolveIdentifiers } from "../invites";
import { logMessage } from "../LogProxy";
import { IPerson } from "../models/schedule";
import { ConferenceMatrixClient } from "../ConferenceMatrixClient";
import {IConfig} from "../config";

export class FDMCommand implements ICommand {
public readonly prefixes = ["fdm"];

constructor(private readonly client: ConferenceMatrixClient, private readonly conference: Conference) {}
constructor(private readonly client: ConferenceMatrixClient, private readonly conference: Conference, private readonly config: IConfig) {}

public async run(roomId: string, event: any, args: string[]) {
const spi = this.conference.getInterestRoom("I.infodesk");
Expand Down Expand Up @@ -115,7 +116,7 @@ export class FDMCommand implements ICommand {
for (const person of infodeskResolved) {
try {
if (person.mxid && inBsJoined.includes(person.mxid)) continue;
await invitePersonToRoom(this.client, person, infBackstage);
await invitePersonToRoom(this.client, person, infBackstage, this.config);
} catch (e) {
await logMessage(LogLevel.ERROR, "InviteCommand", `Error inviting ${person.mxid} / ${person.person.id} to ${infBackstage} - ignoring`, this.client);
}
Expand All @@ -124,13 +125,13 @@ export class FDMCommand implements ICommand {
for (const person of volResolved) {
try {
if (person.mxid && volJoined.includes(person.mxid)) continue;
await invitePersonToRoom(this.client, person, vol);
await invitePersonToRoom(this.client, person, vol, this.config);
} catch (e) {
await logMessage(LogLevel.ERROR, "InviteCommand", `Error inviting ${person.mxid} / ${person.person.id} to ${vol} - ignoring`, this.client);
}
try {
if (person.mxid && volBsJoined.includes(person.mxid)) continue;
await invitePersonToRoom(this.client, person, volBackstage);
await invitePersonToRoom(this.client, person, volBackstage, this.config);
} catch (e) {
await logMessage(LogLevel.ERROR, "InviteCommand", `Error inviting ${person.mxid} / ${person.person.id} to ${volBackstage} - ignoring`, this.client);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/InviteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class InviteCommand implements ICommand {
if (target.mxid && effectiveJoinedUserIds.includes(target.mxid)) continue;
if (emailInvitePersonIds.includes(target.person.id)) continue;
try {
await invitePersonToRoom(this.client, target, roomId);
await invitePersonToRoom(this.client, target, roomId, this.config);
} catch (e) {
LogService.error("InviteCommand", e);
await logMessage(LogLevel.ERROR, "InviteCommand", `Error inviting ${target.mxid}/${target.emails} / ${target.person.id} to ${roomId} - ignoring: ${e.message ?? e.statusMessage ?? '(see logs)'}`, this.client);
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface IConfig {
address: string;
port: number;
};
dry_run_enabled: boolean;
conference: {
id: string;
name: string;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class ConferenceBot {
new BuildCommand(this.client, this.conference, this.config),
new CopyModeratorsCommand(this.client),
new DevCommand(this.client, this.conference),
new FDMCommand(this.client, this.conference),
new FDMCommand(this.client, this.conference, this.config),
new HelpCommand(this.client),
new InviteCommand(this.client, this.conference, this.config),
new InviteMeCommand(this.client, this.conference),
Expand Down
37 changes: 24 additions & 13 deletions src/invites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RS_3PID_PERSON_ID } from "./models/room_state";
import { logMessage } from "./LogProxy";
import { IPerson } from "./models/schedule";
import { ConferenceMatrixClient } from "./ConferenceMatrixClient";
import {IConfig} from "./config";

const MAX_EMAILS_PER_BATCH = 1000;

Expand Down Expand Up @@ -78,9 +79,14 @@ export async function resolveIdentifiers(client: ConferenceMatrixClient, people:
return resolved;
}

export async function invitePersonToRoom(client: ConferenceMatrixClient, resolvedPerson: ResolvedPersonIdentifier, roomId: string): Promise<void> {
export async function invitePersonToRoom(client: ConferenceMatrixClient, resolvedPerson: ResolvedPersonIdentifier, roomId: string, config: IConfig): Promise<void> {
if (resolvedPerson.mxid) {
return await client.inviteUser(resolvedPerson.mxid.trim(), roomId);
if (config.dry_run_enabled) {
LogService.info("invites", `Inviting ${resolvedPerson.mxid} (dry-run)`)
}
else {
return await client.inviteUser(resolvedPerson.mxid.trim(), roomId);
}
}

if (!resolvedPerson.emails) {
Expand All @@ -93,16 +99,21 @@ export async function invitePersonToRoom(client: ConferenceMatrixClient, resolve
}

for (const email of resolvedPerson.emails) {
const idInvite = await client.identityClient.makeEmailInvite(email, roomId);
const content = {
display_name: idInvite.display_name,
// XXX: https://github.com/matrix-org/matrix-doc/issues/2948
key_validity_url: `${client.identityClient.serverUrl}/_matrix/identity/v2/pubkey/ephemeral/isvalid`,
public_key: idInvite.public_key,
public_keys: idInvite.public_keys,
[RS_3PID_PERSON_ID]: resolvedPerson.person.id,
};
const stateKey = idInvite.token; // not included in the content
await client.sendStateEvent(roomId, "m.room.third_party_invite", stateKey, content);
if (config.dry_run_enabled) {
LogService.info("invites", `Third-party inviting ${email} (dry-run)`)
}
else {
const idInvite = await client.identityClient.makeEmailInvite(email, roomId);
const content = {
display_name: idInvite.display_name,
// XXX: https://github.com/matrix-org/matrix-doc/issues/2948
key_validity_url: `${client.identityClient.serverUrl}/_matrix/identity/v2/pubkey/ephemeral/isvalid`,
public_key: idInvite.public_key,
public_keys: idInvite.public_keys,
[RS_3PID_PERSON_ID]: resolvedPerson.person.id,
};
const stateKey = idInvite.token; // not included in the content
await client.sendStateEvent(roomId, "m.room.third_party_invite", stateKey, content);
}
}
}

0 comments on commit 7189eb1

Please sign in to comment.