Skip to content

Commit

Permalink
Test that multiple modes are bridged as one PL
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Nov 12, 2020
1 parent 3ffc02b commit a66d30d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
49 changes: 48 additions & 1 deletion spec/integ/irc-to-matrix.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ describe("IRC-to-Matrix operator modes bridging", function() {

const tRealMatrixUserNick = "M-alice";
const tRealUserId = "@alice:anotherhomeserver";
const tRealMatrixUserNick2 = "M-bob";
const tRealUserId2 = "@bob:anotherhomeserver";

beforeEach(test.coroutine(function*() {
yield test.beforeEach(env);
Expand All @@ -382,6 +384,14 @@ describe("IRC-to-Matrix operator modes bridging", function() {
roomMapping.server, tRealMatrixUserNick, roomMapping.channel
);

env.ircMock._autoConnectNetworks(
roomMapping.server, tRealMatrixUserNick2, roomMapping.server
);

env.ircMock._autoJoinChannels(
roomMapping.server, tRealMatrixUserNick2, roomMapping.channel
);

// do the init
yield test.initEnv(env).then(() => {
return env.mockAppService._trigger("type:m.room.message", {
Expand Down Expand Up @@ -427,7 +437,44 @@ describe("IRC-to-Matrix operator modes bridging", function() {
expect(setPowerLevelResult.content.users[tRealUserId]).toBe(50);
});

it("should bridge the highest power of multiple modes", async function() {
it("should bridge multiple mode changes as a single power level event", async () => {
// Set IRC user prefix, which in reality is assumed to have happened
const client = await env.ircMock._findClientAsync(roomMapping.server, tRealMatrixUserNick);
const client2 = await env.ircMock._findClientAsync(roomMapping.server, tRealMatrixUserNick2);
client.chans[roomMapping.channel] = {
users: {
[tRealMatrixUserNick]: "@",
[tRealMatrixUserNick2]: "@"
}
};

client2.chans[roomMapping.channel] = {
users: {
[tRealMatrixUserNick2]: "@"
}
};

const promise = new Promise((resolve) => {
botMatrixClient.sendStateEvent.and.callFake(async (roomId, eventType, content, key) => {
resolve({roomId, eventType, content, key});
});
});
const cli = await env.ircMock._findClientAsync(roomMapping.server, roomMapping.botNick);
cli.emit(
"+mode", roomMapping.channel, "op-er", "o", tRealMatrixUserNick, "here you go"
);
cli.emit(
"+mode", roomMapping.channel, "op-er", "o", tRealMatrixUserNick2, "here you go"
);

const setPowerLevelResult = await promise;
expect(setPowerLevelResult.roomId).toBe(roomMapping.roomId);
expect(setPowerLevelResult.eventType).toBe("m.room.power_levels");
expect(setPowerLevelResult.key).toBe("");
expect(setPowerLevelResult.content.users[tRealUserId]).toBe(50);
expect(setPowerLevelResult.content.users[tRealUserId2]).toBe(50);
});

// Set IRC user prefix, which in reality is assumed to have happened
const client = await env.ircMock._findClientAsync(roomMapping.server, tRealMatrixUserNick);

Expand Down
2 changes: 1 addition & 1 deletion spec/util/test-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"toConsole": true
},
"ircHandler": {
"powerLevelGracePeriodMs": 50
"powerLevelGracePeriodMs": 500
},
"servers": {
"irc.example": {
Expand Down
15 changes: 8 additions & 7 deletions src/bridge/RoomAccessSyncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,23 @@ export class RoomAccessSyncer {
}

private async setPowerLevel(roomId: string, userId: string, level: number|null, req: BridgeRequest) {
if (this.powerLevelGracePeriod === 0) {
// If there is no grace period, just change them now.
await this.changePowerLevels(roomId, {[userId]: level}, req);
return;
}

const existingPl: RequestedPLChange = this.pendingPLChanges.get(roomId) || { users: {} };
existingPl.users[userId] = level;
if (existingPl.timeout) {
// If we had a pending request, clear it and wait again.
clearTimeout(existingPl.timeout);
}

if (this.powerLevelGracePeriod === 0) {
// If there is no grace period, just change them now.
await this.changePowerLevels(roomId, existingPl.users, req);
return;
}

existingPl.timeout = setTimeout(async () => {
this.changePowerLevels(roomId, existingPl.users, req);
}, this.powerLevelGracePeriod);
this.pendingPLChanges.set(roomId, existingPl);
}

/**
Expand Down Expand Up @@ -238,7 +239,7 @@ export class RoomAccessSyncer {
return;
}
const chanData = bridgedClient.chanData(channel);
if (!(chanData && chanData.users)) {
if (!chanData?.users) {
req.log.error(`No channel data for ${channel}`);
return;
}
Expand Down

0 comments on commit a66d30d

Please sign in to comment.