This commit is contained in:
Will Hunt 2019-08-20 20:16:34 +01:00
parent 4ffa48643c
commit dbc9885363
9 changed files with 62 additions and 44 deletions

View file

@ -128,6 +128,7 @@ export class MatrixRoomHandler {
await Promise.all(promiseList);
}
// tslint:disable-next-line no-any
public async OnAliasQuery(alias: string): Promise<any> {
const aliasLocalpart = alias.substr("#".length, alias.indexOf(":") - 1);
log.info("Got request for #", aliasLocalpart);
@ -245,7 +246,7 @@ export class MatrixRoomHandler {
}
private async createMatrixRoom(channel: Discord.TextChannel,
alias: string, aliasLocalpart: string): Promise<any> {
alias: string, aliasLocalpart: string) {
const remote = new RemoteStoreRoom(`discord_${channel.guild.id}_${channel.id}`, {
discord_channel: channel.id,
discord_guild: channel.guild.id,

View file

@ -233,7 +233,8 @@ export class DiscordStore {
throw err;
}
}
// tslint:disable-next-line no-any
// tslint:disable-next-line no-any callable-types
public async Get<T extends IDbData>(dbType: {new(): T; }, params: any): Promise<T|null> {
const dType = new dbType();
log.silly(`get <${dType.constructor.name} with params ${params}>`);

View file

@ -56,11 +56,11 @@ class Entry implements IRoomStoreEntry {
function CreateChannelSync(remoteChannels: any[] = []) {
const bridge = new AppserviceMock({
stateEventFetcher: async (roomId: string, type: string, key: string) => {
if (roomId === "!valid:localhost" && type === "m.room.canonical_alias" && key == "") {
if (roomId === "!valid:localhost" && type === "m.room.canonical_alias" && key === "") {
return { alias: "#alias:localhost"};
}
throw Error("Event not found");
}
},
});
REMOTECHANNEL_REMOVED = false;
REMOTECHANNEL_SET = false;

View file

@ -98,7 +98,7 @@ describe("DiscordCommandHandler", () => {
};
await handler.Process(message);
// tslint:disable-next-line:no-magic-numbers
expect(bridge.botIntent.underlyingClient.wasCalled("kickUser"),).to.equal(2);
expect(bridge.botIntent.underlyingClient.wasCalled("kickUser")).to.equal(2);
});
it("will deny permission", async () => {
const {handler, bridge} = createCH();

View file

@ -112,7 +112,8 @@ describe("MatrixCommandHandler", () => {
bridge.botIntent.underlyingClient.wasCalled("sendMessage", true, "!123:localhost", {
body: "**ERROR:** The owner of this bridge does not permit self-service bridging.",
format: "org.matrix.custom.html",
formatted_body: "<strong>ERROR:</strong> The owner of this bridge does not permit self-service bridging.",
formatted_body: "<strong>ERROR:</strong> The owner of this bridge" +
"does not permit self-service bridging.",
msgtype: "m.notice",
});
});
@ -121,7 +122,8 @@ describe("MatrixCommandHandler", () => {
await handler.Process(createEvent("!discord bridge"), createContext());
const expected = "**ERROR:** insufficiant permissions to use this " +
"command! Try `!discord help` to see all available commands";
const htmlExpected = "<strong>ERROR:</strong> insufficiant permissions to use this command! Try <code>!discord help</code> to see all available commands";
const htmlExpected = "<strong>ERROR:</strong> insufficiant permissions to use this command!" +
" Try <code>!discord help</code> to see all available commands";
bridge.botIntent.underlyingClient.wasCalled("sendMessage", true, "!123:localhost", {
body: expected,
format: "org.matrix.custom.html",
@ -155,7 +157,7 @@ describe("MatrixCommandHandler", () => {
});
});
it("will fail to bridge if permissions were failed", async () => {
const {handler, bridge}= createCH({
const {handler, bridge} = createCH({
failBridgeMatrix: true,
});
await handler.Process(createEvent("!discord bridge 123 456"), createContext());
@ -255,7 +257,8 @@ describe("MatrixCommandHandler", () => {
});
});
it("will show error if unbridge fails", async () => {
const expected = "There was an error unbridging this room. Please try again later or contact the bridge operator.";
const expected = "There was an error unbridging this room. Please" +
"try again later or contact the bridge operator.";
const {handler, bridge} = createCH({
failUnbridge: true,
});

View file

@ -39,7 +39,7 @@ function buildRequest(eventData): IMatrixEvent {
eventData.sender = "@foobar:localhost";
}
return eventData;
};
}
const bot = {
GetIntentFromDiscordMember: (member) => {

View file

@ -165,20 +165,19 @@ describe("MatrixRoomHandler", () => {
describe("OnAliasQuery", () => {
it("will create room", async () => {
const {handler} = createRH({});
const ret = await handler.OnAliasQuery(
"#_discord_123_456:localhost");
const ret = await handler.OnAliasQuery("#_discord_123_456:localhost");
expect(ret).to.be.deep.equal({
"initial_state": [
initial_state: [
{
"content": {
"join_rule": "public"
content: {
join_rule: "public",
},
"state_key": "",
"type": "m.room.join_rules"
}
state_key: "",
type: "m.room.join_rules",
},
],
"room_alias_name": "_discord_123_456",
"visibility": "public"
room_alias_name: "_discord_123_456",
visibility: "public",
});
});
it("will not create room if guild cannot be found", async () => {

View file

@ -43,11 +43,11 @@ const UserSync = (Proxyquire("../src/usersyncroniser", {
Util: {
ApplyPatternString: Util.ApplyPatternString,
AsyncForEach: Util.AsyncForEach,
ParseMxid: Util.ParseMxid,
DownloadFile: async () => {
UTIL_UPLOADED_AVATAR = true;
return Buffer.from([]);
},
ParseMxid: Util.ParseMxid,
},
},
})).UserSyncroniser;
@ -238,8 +238,10 @@ describe("UserSyncroniser", () => {
expect(REMOTEUSER_SET).is.not.null;
expect(REMOTEUSER_SET.displayname).equal("123456");
expect(REMOTEUSER_SET.avatarurl).is.null;
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasCalled("setDisplayName", true, "123456");
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasNotCalled("setAvatarUrl", true);
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasCalled("setDisplayName", true, "123456");
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasNotCalled("setAvatarUrl", true);
});
it("Will set an avatar", async () => {
const {userSync, bridge} = CreateUserSync();
@ -259,8 +261,10 @@ describe("UserSyncroniser", () => {
expect(REMOTEUSER_SET).is.not.null;
expect(REMOTEUSER_SET.avatarurl).equal("654321");
expect(REMOTEUSER_SET.displayname).is.null;
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasCalled("setAvatarUrl", true, "mxc://avatarurl");
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasNotCalled("setDisplayName", true);
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasCalled("setAvatarUrl", true, "mxc://avatarurl");
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasNotCalled("setDisplayName", true);
});
it("Will remove an avatar", async () => {
const {userSync, bridge} = CreateUserSync();
@ -280,8 +284,10 @@ describe("UserSyncroniser", () => {
expect(REMOTEUSER_SET).is.not.null;
expect(REMOTEUSER_SET.avatarurl).is.null;
expect(REMOTEUSER_SET.displayname).is.null;
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasNotCalled("setAvatarUrl", true);
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasNotCalled("setDisplayName", true);
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasNotCalled("setAvatarUrl", true);
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasNotCalled("setDisplayName", true);
});
it("will do nothing if nothing needs to be done", async () => {
const {userSync, bridge} = CreateUserSync([new RemoteUser("123456")]);
@ -298,8 +304,10 @@ describe("UserSyncroniser", () => {
expect(LINK_MX_USER).is.null;
expect(LINK_RM_USER).is.null;
expect(REMOTEUSER_SET).is.null;
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasNotCalled("setAvatarUrl", true);
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasNotCalled("setDisplayName", true);
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasNotCalled("setAvatarUrl", true);
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasNotCalled("setDisplayName", true);
});
});
describe("ApplyStateToRoom", () => {
@ -324,13 +332,13 @@ describe("UserSyncroniser", () => {
"displayname": "Good Boy",
"membership": "join",
"uk.half-shot.discord.member": {
"bot": false,
"displayColor": 0,
"id": "123456",
"roles": [],
"username": ""
}
}
bot: false,
displayColor: 0,
id: "123456",
roles: [],
username: "",
},
},
);
});
it("Will not apply unchanged nick", async () => {
@ -346,7 +354,8 @@ describe("UserSyncroniser", () => {
};
await userSync.ApplyStateToRoom(state, "!abc:localhost", "123456");
expect(REMOTEUSER_SET).is.null;
bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasNotCalled("sendStateEvent", true);
bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasNotCalled("sendStateEvent", true);
});
it("Will apply roles", async () => {
const {userSync, bridge} = CreateUserSync([new RemoteUser("123456")]);
@ -373,8 +382,8 @@ describe("UserSyncroniser", () => {
"!abc:localhost",
"m.room.member",
"@_discord_123456:localhost", {
"displayname": state.displayName,
"displayColor": 0,
"displayname": state.displayName,
"uk.half-shot.discord.member": {
id: "123456",
roles: state.roles,
@ -556,7 +565,8 @@ describe("UserSyncroniser", () => {
"username",
guild);
await userSync.OnAddGuildMember(member as any);
expect(bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasCalled("sendStateEvent")).to.equal(GUILD_ROOM_IDS.length);
expect(bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasCalled("sendStateEvent")).to.equal(GUILD_ROOM_IDS.length);
});
});
describe("OnRemoveGuildMember", () => {
@ -569,7 +579,8 @@ describe("UserSyncroniser", () => {
"username",
guild);
await userSync.OnRemoveGuildMember(member as any);
expect(bridge.getIntentForSuffix("123456").underlyingClient.wasCalled("leaveRoom")).to.equal(GUILD_ROOM_IDS.length);
expect(bridge.getIntentForSuffix("123456")
.underlyingClient.wasCalled("leaveRoom")).to.equal(GUILD_ROOM_IDS.length);
});
});
describe("OnUpdateGuildMember", () => {
@ -583,7 +594,8 @@ describe("UserSyncroniser", () => {
guild,
"FiddleDee");
await userSync.OnUpdateGuildMember(newMember as any);
expect(bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasCalled("sendStateEvent")).to.equal(GUILD_ROOM_IDS.length);
expect(bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasCalled("sendStateEvent")).to.equal(GUILD_ROOM_IDS.length);
});
it("will part rooms based on role removal", async () => {
const {userSync, bridge} = CreateUserSync([new RemoteUser("123456")]);
@ -597,8 +609,11 @@ describe("UserSyncroniser", () => {
"FiddleDee");
newMember.roles.set("1234", role);
await userSync.OnUpdateGuildMember(newMember as any);
expect(bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasCalled("sendStateEvent")).to.equal(GUILD_ROOM_IDS_WITH_ROLE.length);
expect(bridge.getIntentForUserId("@_discord_123456:localhost").underlyingClient.wasCalled("leaveRoom", true, "!ghi:localhost")).to.equal(GUILD_ROOM_IDS.length - GUILD_ROOM_IDS_WITH_ROLE.length);
expect(bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasCalled("sendStateEvent")).to.equal(GUILD_ROOM_IDS_WITH_ROLE.length);
expect(bridge.getIntentForUserId("@_discord_123456:localhost")
.underlyingClient.wasCalled("leaveRoom", true, "!ghi:localhost"))
.to.equal(GUILD_ROOM_IDS.length - GUILD_ROOM_IDS_WITH_ROLE.length);
});
});
});

View file

@ -9,7 +9,6 @@
"object-literal-sort-keys": "off",
"no-any": true,
"arrow-return-shorthand": true,
"no-magic-numbers": [true, -1, 0, 1, 1000],
"prefer-for-of": true,
"typedef": {
"severity": "warning"