diff --git a/d2m/actions/remove-reaction.js b/d2m/actions/remove-reaction.js deleted file mode 100644 index 821b9d0..0000000 --- a/d2m/actions/remove-reaction.js +++ /dev/null @@ -1,36 +0,0 @@ -// @ts-check - -const Ty = require("../../types") -const assert = require("assert").strict - -const passthrough = require("../../passthrough") -const {discord, sync, db, select} = passthrough -/** @type {import("../../matrix/api")} */ -const api = sync.require("../../matrix/api") -/** @type {import("./register-user")} */ -const registerUser = sync.require("./register-user") -/** @type {import("../actions/create-room")} */ -const createRoom = sync.require("../actions/create-room") -/** @type {import("../../matrix/file")} */ -const file = sync.require("../../matrix/file") - -/** - * @param {import("discord-api-types/v10").GatewayMessageReactionRemoveDispatchData} data - */ -async function removeReaction(data) { - const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id) - if (!roomID) return - const eventIDForMessage = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id) - if (!eventIDForMessage) return - const mxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(data.user_id) - if (!mxid) return - - /** @type {Ty.Pagination>} */ - const relations = await api.getRelations(roomID, eventIDForMessage, "m.annotation") - const eventIDForReaction = relations.chunk.find(e => e.sender === mxid && e.content["m.relates_to"].key === data.emoji) // TODO: get the key from the emoji - if (!eventIDForReaction) return - - await api.redactEvent(roomID, eventIDForReaction, mxid) -} - -module.exports.removeReaction = removeReaction diff --git a/db/ooye-schema.sql b/db/ooye-schema.sql index 51d33b2..43642ee 100644 --- a/db/ooye-schema.sql +++ b/db/ooye-schema.sql @@ -70,10 +70,4 @@ CREATE TABLE IF NOT EXISTS "emoji" ( "mxc_url" TEXT NOT NULL, PRIMARY KEY("id") ) WITHOUT ROWID; -CREATE TABLE IF NOT EXISTS "reaction" ( - "hashed_event_id" INTEGER NOT NULL, - "message_id" TEXT NOT NULL, - "encoded_emoji" TEXT NOT NULL, - PRIMARY KEY ("hashed_event_id") -) WITHOUT ROWID; COMMIT; diff --git a/db/orm-utils.d.ts b/db/orm-utils.d.ts index 58eeb28..0ae0844 100644 --- a/db/orm-utils.d.ts +++ b/db/orm-utils.d.ts @@ -69,12 +69,6 @@ export type Models = { animated: number mxc_url: string } - - reaction: { - hashed_event_id: number - message_id: string - encoded_emoji: string - } } export type Prepared = { diff --git a/m2d/actions/add-reaction.js b/m2d/actions/add-reaction.js index a9e36c3..d5d5cc7 100644 --- a/m2d/actions/add-reaction.js +++ b/m2d/actions/add-reaction.js @@ -5,8 +5,6 @@ const Ty = require("../../types") const passthrough = require("../../passthrough") const {discord, sync, db, select} = passthrough -/** @type {import("../converters/utils")} */ -const utils = sync.require("../converters/utils") /** * @param {Ty.Event.Outer} event @@ -50,9 +48,7 @@ async function addReaction(event) { console.log("add reaction from matrix:", emoji, encoded, encodedTrimmed, "chosen:", discordPreferredEncoding) } - await discord.snow.channel.createReaction(channelID, messageID, discordPreferredEncoding) // acting as the discord bot itself - - db.prepare("REPLACE INTO reaction (hashed_event_id, message_id, encoded_emoji) VALUES (?, ?, ?)").run(utils.getEventIDHash(event.event_id), messageID, discordPreferredEncoding) + return discord.snow.channel.createReaction(channelID, messageID, discordPreferredEncoding) // acting as the discord bot itself } module.exports.addReaction = addReaction diff --git a/m2d/actions/redact.js b/m2d/actions/redact.js deleted file mode 100644 index 9b03abb..0000000 --- a/m2d/actions/redact.js +++ /dev/null @@ -1,39 +0,0 @@ -// @ts-check - -const assert = require("assert").strict -const Ty = require("../../types") - -const passthrough = require("../../passthrough") -const {discord, sync, db, select, from} = passthrough -/** @type {import("../converters/utils")} */ -const utils = sync.require("../converters/utils") - -/** - * @param {Ty.Event.Outer_M_Room_Redaction} event - */ -async function deleteMessage(event) { - const row = from("event_message").join("message_channel", "message_id").select("channel_id", "message_id").and("WHERE event_id = ?").get(event.event_id) - if (!row) return - return discord.snow.channel.deleteMessage(row.channel_id, row.message_id, event.content.reason) -} - -/** - * @param {Ty.Event.Outer_M_Room_Redaction} event - */ -async function removeReaction(event) { - const hash = utils.getEventIDHash(event.redacts) - const row = from("reaction").join("message_channel", "message_id").select("channel_id", "message_id", "encoded_emoji").and("WHERE hashed_event_id = ?").get(hash) - if (!row) return - return discord.snow.channel.deleteReactionSelf(row.channel_id, row.message_id, row.encoded_emoji) -} - -/** - * Try everything that could possibly be redacted. - * @param {Ty.Event.Outer_M_Room_Redaction} event - */ -async function handle(event) { - await deleteMessage(event) - await removeReaction(event) -} - -module.exports.handle = handle diff --git a/m2d/converters/utils.js b/m2d/converters/utils.js index e1579ee..4f74c6b 100644 --- a/m2d/converters/utils.js +++ b/m2d/converters/utils.js @@ -38,7 +38,7 @@ function getPublicUrlForMxc(mxc) { /** * Event IDs are really big and have more entropy than we need. * If we want to store the event ID in the database, we can store a more compact version by hashing it with this. - * I choose a 64-bit non-cryptographic hash as only a 32-bit hash will see birthday collisions unreasonably frequently: https://en.wikipedia.org/wiki/Birthday_attack#Mathematics + * Choosing a 64-bit non-cryptographic hash as only a 32-bit hash will see birthday collisions unreasonably frequently: https://en.wikipedia.org/wiki/Birthday_attack#Mathematics * xxhash outputs an unsigned 64-bit integer. * Converting to a signed 64-bit integer with no bit loss so that it can be stored in an SQLite integer field as-is: https://www.sqlite.org/fileformat2.html#record_format * This should give very efficient storage with sufficient entropy. diff --git a/m2d/event-dispatcher.js b/m2d/event-dispatcher.js index 702f59b..d81e7cf 100644 --- a/m2d/event-dispatcher.js +++ b/m2d/event-dispatcher.js @@ -12,8 +12,6 @@ const {discord, db, sync, as} = require("../passthrough") const sendEvent = sync.require("./actions/send-event") /** @type {import("./actions/add-reaction")} */ const addReaction = sync.require("./actions/add-reaction") -/** @type {import("./actions/redact")} */ -const redact = sync.require("./actions/redact") /** @type {import("./converters/utils")} */ const utils = sync.require("./converters/utils") /** @type {import("../matrix/api")}) */ @@ -103,15 +101,6 @@ async event => { } })) -sync.addTemporaryListener(as, "type:m.room.redaction", guard("m.room.redaction", -/** - * @param {Ty.Event.Outer_M_Room_Redaction} event it is a m.room.redaction because that's what this listener is filtering for - */ -async event => { - if (utils.eventSenderIsFromDiscord(event.sender)) return - await redact.handle(event) -})) - sync.addTemporaryListener(as, "type:m.room.avatar", guard("m.room.avatar", /** * @param {Ty.Event.StateOuter} event diff --git a/matrix/api.js b/matrix/api.js index c429216..fa3aad3 100644 --- a/matrix/api.js +++ b/matrix/api.js @@ -1,7 +1,7 @@ // @ts-check const Ty = require("../types") -const assert = require("assert").strict +const assert = require("assert") const passthrough = require("../passthrough") const { discord, sync, db } = passthrough @@ -109,18 +109,6 @@ function getJoinedMembers(roomID) { return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`) } -/** - * @param {string} roomID - * @param {string} eventID - * @param {string?} [relType] - * @returns {Promise>>} - */ -function getRelations(roomID, eventID, relType) { - let path = `/client/v1/rooms/${roomID}/relations/${eventID}` - if (relType) path += `/${relType}` - return mreq.mreq("GET", path) -} - /** * @param {string} roomID * @param {string} type @@ -219,7 +207,6 @@ module.exports.getEvent = getEvent module.exports.getAllState = getAllState module.exports.getStateEvent = getStateEvent module.exports.getJoinedMembers = getJoinedMembers -module.exports.getRelations = getRelations module.exports.sendState = sendState module.exports.sendEvent = sendEvent module.exports.redactEvent = redactEvent diff --git a/types.d.ts b/types.d.ts index db72eb3..d4e31ef 100644 --- a/types.d.ts +++ b/types.d.ts @@ -173,12 +173,6 @@ export namespace Event { key: string // the unicode emoji, mxc uri, or reaction text } } - - export type Outer_M_Room_Redaction = Outer<{ - reason?: string - }> & { - redacts: string - } } export namespace R { @@ -215,9 +209,3 @@ export namespace R { event_id: string } } - -export type Pagination { - chunk: T[] - next_batch?: string - prev_match?: string -}