diff --git a/src/bot.ts b/src/bot.ts index 8d923e21c97e0bf3c06dbb4d5f8c14bbcd2d0f9a..7cf5ade82eb90ca54fcd4da7433b886ad2137697 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -35,7 +35,7 @@ export class DiscordBot { this.sentMessages = []; this.clientFactory = new DiscordClientFactory(store, config.auth); this.msgProcessor = new MessageProcessor( - new MessageProcessorOpts(this.config.bridge.domain) + new MessageProcessorOpts(this.config.bridge.domain), ); } @@ -505,7 +505,7 @@ export class DiscordBot { intent.sendMessage(room, { body: result.body, msgtype: "m.text", - formatted_body: result.formatted_body, + formatted_body: result.formattedBody, format: "org.matrix.custom.html", }); }); diff --git a/src/db/dbdatainterface.ts b/src/db/dbdatainterface.ts index 5a8de9b518402c1e6df38cb39fe836ae37dfe3ce..7687e18fa1e9a8d1ac7d24476d2aa082ea3f53e4 100644 --- a/src/db/dbdatainterface.ts +++ b/src/db/dbdatainterface.ts @@ -2,7 +2,7 @@ import { DiscordStore } from "../store"; export interface IDbData { Result: boolean; - RunQuery(store: DiscordStore, params: any) - Insert(store: DiscordStore) - Update(store: DiscordStore) + RunQuery(store: DiscordStore, params: any); + Insert(store: DiscordStore); + Update(store: DiscordStore); } diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts index 58bdccde356aa56202cb1eddbe670a49733020dd..5f78fe0e26ed369b2e0eb0ce2d2b91ea8326e689 100644 --- a/src/messageprocessor.ts +++ b/src/messageprocessor.ts @@ -15,31 +15,31 @@ export class MessageProcessorOpts { } export class MessageProcessorMatrixResult { - public formatted_body: string; + public formattedBody: string; public body: string; } export class MessageProcessor { - readonly opts: MessageProcessorOpts; + private readonly opts: MessageProcessorOpts; constructor (opts: MessageProcessorOpts) { this.opts = opts; } public FormatDiscordMessage(msg: Discord.Message): MessageProcessorMatrixResult { - let result = new MessageProcessorMatrixResult(); + const result = new MessageProcessorMatrixResult(); // Replace Users let content = msg.content; content = this.ReplaceMembers(content, msg); content = this.ReplaceChannels(content, msg); - //content = this.ReplaceEmoji(content, msg); + // content = this.ReplaceEmoji(content, msg); // Replace channels result.body = content; - result.formatted_body = marked(content); + result.formattedBody = marked(content); return result; } - public ReplaceMembers(content:string, msg: Discord.Message): string { + public ReplaceMembers(content: string, msg: Discord.Message): string { let results = USER_REGEX.exec(content); while (results !== null) { const id = results[1]; diff --git a/src/store.ts b/src/store.ts index d896c59b45775f263346e4a9d6ecd3556974a20a..c4c695a66ae82ac359609fcf81e98813797d737e 100644 --- a/src/store.ts +++ b/src/store.ts @@ -3,7 +3,7 @@ import * as log from "npmlog"; import * as Bluebird from "bluebird"; import * as fs from "fs"; import { IDbSchema } from "./db/schema/dbschema"; -import { IDbData} from "./db/dbdatainterface" +import { IDbData} from "./db/dbdatainterface"; const CURRENT_SCHEMA = 4; /** * Stores data for specific users and data not specific to rooms. @@ -103,17 +103,17 @@ export class DiscordStore { INSERT INTO user_id_discord_id (discord_id,user_id) VALUES ($userId,$discordId); ` , { - $userId: userId, - $discordId: discordId, + $userId: userId, + $discordId: discordId, }), this.db.runAsync( ` INSERT INTO discord_id_token (discord_id,token) VALUES ($discordId,$token); ` , { - $discordId: discordId, - $token: token, - }) + $discordId: discordId, + $token: token, + }), ]).catch( (err) => { log.error("DiscordStore", "Error storing user token %s", err); throw err; @@ -227,15 +227,6 @@ export class DiscordStore { }); } - private getSchemaVersion ( ): Promise<number> { - log.silly("DiscordStore", "_get_schema_version"); - return this.db.getAsync(`SELECT version FROM schema`).then((row) => { - return row === undefined ? 0 : row.version; - }).catch( () => { - return 0; - }); - } - public Get<T extends IDbData>(dbType: {new(): T; }, params: any): T { const dType = new dbType(); log.silly("DiscordStore", `get <${dType.constructor.name}>`); @@ -253,6 +244,15 @@ export class DiscordStore { data.Update(this); } + private getSchemaVersion ( ): Promise<number> { + log.silly("DiscordStore", "_get_schema_version"); + return this.db.getAsync(`SELECT version FROM schema`).then((row) => { + return row === undefined ? 0 : row.version; + }).catch( () => { + return 0; + }); + } + private setSchemaVersion (ver: number): Promise<any> { log.silly("DiscordStore", "_set_schema_version => %s", ver); return this.db.getAsync( diff --git a/test/mocks/discordclient.ts b/test/mocks/discordclient.ts index c65eb7d3cb8397cca5d01bcb68c43b2f9f1e6c41..71ffef63a1f37141798c36b13941f90a4225c797 100644 --- a/test/mocks/discordclient.ts +++ b/test/mocks/discordclient.ts @@ -2,7 +2,6 @@ import {MockCollection} from "./collection"; import {MockGuild} from "./guild"; import {MockUser} from "./user"; - export class MockDiscordClient { public guilds = new MockCollection<string, MockGuild>(); public user: MockUser; diff --git a/test/test_discordbot.ts b/test/test_discordbot.ts index 8cb5061c15edad1ffc1131333f58e88a1b05c2b7..c11d8a5e9e1e8fd86acfe93112805684347cd4a7 100644 --- a/test/test_discordbot.ts +++ b/test/test_discordbot.ts @@ -44,7 +44,7 @@ describe("DiscordBot", () => { }, bridge: { domain: "localhost", - } + }, }; describe("run()", () => { it("should resolve when ready.", () => { diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts index fa3120486d12276f51d3a736581e009f2f58d43a..d3689b1dfd549fa2722bec2052db5a1c9c2e71c6 100644 --- a/test/test_messageprocessor.ts +++ b/test/test_messageprocessor.ts @@ -16,43 +16,43 @@ log.level = "silly"; describe("MessageProcessor", () => { describe("init", () => { it("constructor", () => { - new MessageProcessor(new MessageProcessorOpts("localhost")); + const mp = new MessageProcessor(new MessageProcessorOpts("localhost")); }); }); describe("FormatDiscordMessage", () => { it("processes plain text messages correctly", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); - const msg = new Discord.Message(null,null,null); + const msg = new Discord.Message(null, null, null); msg.content = "Hello World!"; const result = processor.FormatDiscordMessage(msg); Chai.assert(result.body, "Hello World!"); - Chai.assert(result.formatted_body, "Hello World!"); + Chai.assert(result.formattedBody, "Hello World!"); }); it("processes markdown messages correctly.", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); - const msg = new Discord.Message(null,null,null); + const msg = new Discord.Message(null, null, null); msg.content = "Hello *World*!"; const result = processor.FormatDiscordMessage(msg); Chai.assert.equal(result.body, "Hello *World*!"); - Chai.assert.equal(result.formatted_body, "<p>Hello <em>World</em>!</p>\n"); + Chai.assert.equal(result.formattedBody, "<p>Hello <em>World</em>!</p>\n"); }); }); describe("ReplaceMembers", () => { it("processes members missing from the guild correctly", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); - const guild :any = new MockGuild("123", []); - const channel = new Discord.TextChannel(guild,null); - const msg = new Discord.Message(channel,null,null); + const guild: any = new MockGuild("123", []); + const channel = new Discord.TextChannel(guild, null); + const msg = new Discord.Message(channel, null, null); let content = "Hello <@!12345>"; content = processor.ReplaceMembers(content, msg); Chai.assert.equal(content, "Hello @_discord_12345:localhost"); }); it("processes members with usernames correctly", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); - const guild :any = new MockGuild("123", []); + const guild: any = new MockGuild("123", []); guild._mockAddMember(new MockMember("12345", "TestUsername")); - const channel = new Discord.TextChannel(guild,null); - const msg = new Discord.Message(channel,null,null); + const channel = new Discord.TextChannel(guild, null); + const msg = new Discord.Message(channel, null, null); let content = "Hello <@!12345>"; content = processor.ReplaceMembers(content, msg); Chai.assert.equal(content, "Hello TestUsername"); @@ -61,19 +61,19 @@ describe("MessageProcessor", () => { describe("ReplaceChannels", () => { it("processes unknown channel correctly", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); - const guild :any = new MockGuild("123", []); - const channel = new Discord.TextChannel(guild,{id:"456", name:"TestChannel"}); - const msg = new Discord.Message(channel,null,null); + const guild: any = new MockGuild("123", []); + const channel = new Discord.TextChannel(guild, {id: "456", name: "TestChannel"}); + const msg = new Discord.Message(channel, null, null); let content = "Hello <#123456789>"; content = processor.ReplaceChannels(content, msg); Chai.assert.equal(content, "Hello [#123456789](https://matrix.to/#/#_discord_123_123456789:localhost)"); }); it("processes channels correctly", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); - const guild :any = new MockGuild("123", []); - const channel = new Discord.TextChannel(guild,{id:"456", name:"TestChannel"}); + const guild: any = new MockGuild("123", []); + const channel = new Discord.TextChannel(guild, {id: "456", name: "TestChannel"}); guild.channels.set("456", channel); - const msg = new Discord.Message(channel,null,null); + const msg = new Discord.Message(channel, null, null); let content = "Hello <#456>"; content = processor.ReplaceChannels(content, msg); Chai.assert.equal(content, "Hello [#TestChannel](https://matrix.to/#/#_discord_123_456:localhost)"); diff --git a/test/test_store.ts b/test/test_store.ts index 3aba138c7bd01d13c6be70c47192a92af50aa13b..f3e82c092a10ec941a254c893b4a30b146640c13 100644 --- a/test/test_store.ts +++ b/test/test_store.ts @@ -19,7 +19,7 @@ describe("DiscordStore", () => { return store.init(-1); }); for (let i = 1; i < TEST_SCHEMA; i++) { - it("update schema to v"+i, () => { + it("update schema to v" + i, () => { const store = new DiscordStore(":memory:"); return store.init(i); }); @@ -33,4 +33,12 @@ describe("DiscordStore", () => { })).to.eventually.be.fulfilled; }); }); + describe("add_user_token", () => { + it("should not throw when adding an entry", () => { + const store = new DiscordStore(":memory:"); + return expect(store.init().then(() => { + return store.add_user_token("userid", "token", "discordid"); + })).to.eventually.be.fulfilled; + }); + }); });