diff --git a/src/bot.ts b/src/bot.ts index c6c05a8388fd3e5b6580fbfcf08205d0b3abc3b2..d98092219bb9efa81a1f2b32a53f803419960f0c 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -2,6 +2,7 @@ import { DiscordBridgeConfig } from "./config"; import { DiscordClientFactory } from "./clientfactory"; import { DiscordStore } from "./store"; import { DbGuildEmoji } from "./db/dbdataemoji"; +import { DbEvent } from "./db/dbdataevent"; import { MatrixUser, RemoteUser, Bridge, Entry } from "matrix-appservice-bridge"; import { Util } from "./util"; import { MessageProcessor, MessageProcessorOpts } from "./messageprocessor"; @@ -205,7 +206,13 @@ export class DiscordBot { log.error("DiscordBot", "Couldn't send message. ", err); } if (Array.isArray(msg)) { - msg.forEach((m) => { this.sentMessages.push(m.id); }); + msg.forEach((m) => { + this.sentMessages.push(m.id); + const evt = new DbEvent(); + evt.MatrixId = event.event_id; + evt.DiscordId = m.id; + this.store.Insert(evt); + }); return; } this.sentMessages.push(msg.id); @@ -528,7 +535,12 @@ export class DiscordBot { msgtype: "m.text", formatted_body: result.formattedBody, format: "org.matrix.custom.html", - }); + }).then((res) => { + const evt = new DbEvent(); + evt.MatrixId = res.event_id; + evt.DiscordId = msg.id; + this.store.Insert(evt); + }); }); }); } diff --git a/src/db/dbdataemoji.ts b/src/db/dbdataemoji.ts index 47dd4de79745a5c4fa894d701cc201ea52ebd60b..943096d9af60aa37c56496c6b6bd496a586bb597 100644 --- a/src/db/dbdataemoji.ts +++ b/src/db/dbdataemoji.ts @@ -3,7 +3,6 @@ import { IDbData } from "./dbdatainterface"; import * as log from "npmlog"; export class DbGuildEmoji implements IDbData { - public EmojiId: string; public GuildId: string; public Name: string; @@ -66,4 +65,8 @@ export class DbGuildEmoji implements IDbData { $updated_at: this.UpdatedAt, }); } + + public Delete(store: DiscordStore): Promise<null> { + throw new Error("Delete is not implemented"); + } } diff --git a/src/db/dbdatainterface.ts b/src/db/dbdatainterface.ts index 97861acb51b4c3e3b737c1c17c46e55fef395abd..5f16794336797558883dd18d98665a1e976de65b 100644 --- a/src/db/dbdatainterface.ts +++ b/src/db/dbdatainterface.ts @@ -5,4 +5,5 @@ export interface IDbData { RunQuery(store: DiscordStore, params: any): Promise<null>; Insert(store: DiscordStore): Promise<null>; Update(store: DiscordStore): Promise<null>; + Delete(store: DiscordStore): Promise<null>; } diff --git a/src/store.ts b/src/store.ts index 648928dda2a7453c9cddaad1112ccfe813397c52..a003de5eabb45cd4fb08bac577952212b02e7d19 100644 --- a/src/store.ts +++ b/src/store.ts @@ -4,7 +4,7 @@ import * as Bluebird from "bluebird"; import * as fs from "fs"; import { IDbSchema } from "./db/schema/dbschema"; import { IDbData} from "./db/dbdatainterface"; -const CURRENT_SCHEMA = 4; +const CURRENT_SCHEMA = 5; /** * Stores data for specific users and data not specific to rooms. */ @@ -245,6 +245,11 @@ export class DiscordStore { return data.Update(this); } + public Delete<T extends IDbData>(data: T): Promise<null> { + log.silly("DiscordStore", `insert <${data.constructor.name}>`); + return data.Delete(this); + } + private getSchemaVersion ( ): Promise<number> { log.silly("DiscordStore", "_get_schema_version"); return this.db.getAsync(`SELECT version FROM schema`).then((row) => { diff --git a/test/test_store.ts b/test/test_store.ts index 1a0f3aaf487326893d9c65f8da9256a0c63875e9..0b3c3f7a0389dd29bb9a705c0573e66ed0dd657c 100644 --- a/test/test_store.ts +++ b/test/test_store.ts @@ -4,12 +4,13 @@ import * as ChaiAsPromised from "chai-as-promised"; import { DiscordStore } from "../src/store"; import * as log from "npmlog"; import { DbGuildEmoji } from "../src/db/dbdataemoji"; +import { DbEvent } from "../src/db/dbdataevent"; Chai.use(ChaiAsPromised); const expect = Chai.expect; log.level = "warn"; -const TEST_SCHEMA = 4; +const TEST_SCHEMA = 5; // const assert = Chai.assert; @@ -85,4 +86,46 @@ describe("DiscordStore", () => { Chai.assert.notEqual(getEmoji.CreatedAt, getEmoji.UpdatedAt); }); }); + describe("Get|Insert|Delete<DbEvent>", () => { + it("should insert successfully", () => { + const store = new DiscordStore(":memory:"); + return expect(store.init().then(() => { + const event = new DbEvent(); + event.MatrixId = "123"; + event.DiscordId = "456"; + return store.Insert(event); + })).to.eventually.be.fulfilled; + }); + it("should get successfully", async () => { + const store = new DiscordStore(":memory:"); + await store.init(); + const event = new DbEvent(); + event.MatrixId = "123"; + event.DiscordId = "456"; + await store.Insert(event); + const getEventDiscord = await store.Get(DbEvent, {discord_id: "456"}); + Chai.assert.equal(getEventDiscord.MatrixId, "123"); + Chai.assert.equal(getEventDiscord.DiscordId, "456"); + const getEventMatrix = await store.Get(DbEvent, {matrix_id: "123"}); + Chai.assert.equal(getEventDiscord.MatrixId, "123"); + Chai.assert.equal(getEventDiscord.DiscordId, "456"); + }); + it("should not return nonexistant emoji", async () => { + const store = new DiscordStore(":memory:"); + await store.init(); + const getEmoji = await store.Get(DbEvent, {matrix_id: "123"}); + Chai.assert.isFalse(getEmoji.Result); + }); + it("should delete successfully", async () => { + const store = new DiscordStore(":memory:"); + await store.init(); + const event = new DbEvent(); + event.MatrixId = "123"; + event.DiscordId = "456"; + await store.Insert(event); + await store.Delete(event); + const getEvent = await store.Get(DbEvent, {matrix_id: "123"}); + Chai.assert.isFalse(getEvent.Result); + }); + }); });