Compare commits
No commits in common. "4f807159ba0b9a6765e0328f06c6efb2032b668a" and "2e68c7edf50ab72084b0f8a6d5eb35e59f2dc6e8" have entirely different histories.
4f807159ba
...
2e68c7edf5
9 changed files with 3 additions and 130 deletions
|
@ -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<Ty.Event.Outer<Ty.Event.M_Reaction>>} */
|
|
||||||
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
|
|
|
@ -70,10 +70,4 @@ CREATE TABLE IF NOT EXISTS "emoji" (
|
||||||
"mxc_url" TEXT NOT NULL,
|
"mxc_url" TEXT NOT NULL,
|
||||||
PRIMARY KEY("id")
|
PRIMARY KEY("id")
|
||||||
) WITHOUT ROWID;
|
) 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;
|
COMMIT;
|
||||||
|
|
6
db/orm-utils.d.ts
vendored
6
db/orm-utils.d.ts
vendored
|
@ -69,12 +69,6 @@ export type Models = {
|
||||||
animated: number
|
animated: number
|
||||||
mxc_url: string
|
mxc_url: string
|
||||||
}
|
}
|
||||||
|
|
||||||
reaction: {
|
|
||||||
hashed_event_id: number
|
|
||||||
message_id: string
|
|
||||||
encoded_emoji: string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Prepared<Row> = {
|
export type Prepared<Row> = {
|
||||||
|
|
|
@ -5,8 +5,6 @@ const Ty = require("../../types")
|
||||||
|
|
||||||
const passthrough = require("../../passthrough")
|
const passthrough = require("../../passthrough")
|
||||||
const {discord, sync, db, select} = passthrough
|
const {discord, sync, db, select} = passthrough
|
||||||
/** @type {import("../converters/utils")} */
|
|
||||||
const utils = sync.require("../converters/utils")
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>} event
|
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>} event
|
||||||
|
@ -50,9 +48,7 @@ async function addReaction(event) {
|
||||||
console.log("add reaction from matrix:", emoji, encoded, encodedTrimmed, "chosen:", discordPreferredEncoding)
|
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
|
return 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.addReaction = addReaction
|
module.exports.addReaction = addReaction
|
||||||
|
|
|
@ -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
|
|
|
@ -38,7 +38,7 @@ function getPublicUrlForMxc(mxc) {
|
||||||
/**
|
/**
|
||||||
* Event IDs are really big and have more entropy than we need.
|
* 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.
|
* 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.
|
* 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
|
* 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.
|
* This should give very efficient storage with sufficient entropy.
|
||||||
|
|
|
@ -12,8 +12,6 @@ const {discord, db, sync, as} = require("../passthrough")
|
||||||
const sendEvent = sync.require("./actions/send-event")
|
const sendEvent = sync.require("./actions/send-event")
|
||||||
/** @type {import("./actions/add-reaction")} */
|
/** @type {import("./actions/add-reaction")} */
|
||||||
const addReaction = sync.require("./actions/add-reaction")
|
const addReaction = sync.require("./actions/add-reaction")
|
||||||
/** @type {import("./actions/redact")} */
|
|
||||||
const redact = sync.require("./actions/redact")
|
|
||||||
/** @type {import("./converters/utils")} */
|
/** @type {import("./converters/utils")} */
|
||||||
const utils = sync.require("./converters/utils")
|
const utils = sync.require("./converters/utils")
|
||||||
/** @type {import("../matrix/api")}) */
|
/** @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",
|
sync.addTemporaryListener(as, "type:m.room.avatar", guard("m.room.avatar",
|
||||||
/**
|
/**
|
||||||
* @param {Ty.Event.StateOuter<Ty.Event.M_Room_Avatar>} event
|
* @param {Ty.Event.StateOuter<Ty.Event.M_Room_Avatar>} event
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
const Ty = require("../types")
|
const Ty = require("../types")
|
||||||
const assert = require("assert").strict
|
const assert = require("assert")
|
||||||
|
|
||||||
const passthrough = require("../passthrough")
|
const passthrough = require("../passthrough")
|
||||||
const { discord, sync, db } = passthrough
|
const { discord, sync, db } = passthrough
|
||||||
|
@ -109,18 +109,6 @@ function getJoinedMembers(roomID) {
|
||||||
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`)
|
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} roomID
|
|
||||||
* @param {string} eventID
|
|
||||||
* @param {string?} [relType]
|
|
||||||
* @returns {Promise<Ty.Pagination<Ty.Event.Outer<any>>>}
|
|
||||||
*/
|
|
||||||
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} roomID
|
||||||
* @param {string} type
|
* @param {string} type
|
||||||
|
@ -219,7 +207,6 @@ module.exports.getEvent = getEvent
|
||||||
module.exports.getAllState = getAllState
|
module.exports.getAllState = getAllState
|
||||||
module.exports.getStateEvent = getStateEvent
|
module.exports.getStateEvent = getStateEvent
|
||||||
module.exports.getJoinedMembers = getJoinedMembers
|
module.exports.getJoinedMembers = getJoinedMembers
|
||||||
module.exports.getRelations = getRelations
|
|
||||||
module.exports.sendState = sendState
|
module.exports.sendState = sendState
|
||||||
module.exports.sendEvent = sendEvent
|
module.exports.sendEvent = sendEvent
|
||||||
module.exports.redactEvent = redactEvent
|
module.exports.redactEvent = redactEvent
|
||||||
|
|
12
types.d.ts
vendored
12
types.d.ts
vendored
|
@ -173,12 +173,6 @@ export namespace Event {
|
||||||
key: string // the unicode emoji, mxc uri, or reaction text
|
key: string // the unicode emoji, mxc uri, or reaction text
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Outer_M_Room_Redaction = Outer<{
|
|
||||||
reason?: string
|
|
||||||
}> & {
|
|
||||||
redacts: string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace R {
|
export namespace R {
|
||||||
|
@ -215,9 +209,3 @@ export namespace R {
|
||||||
event_id: string
|
event_id: string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Pagination<T> {
|
|
||||||
chunk: T[]
|
|
||||||
next_batch?: string
|
|
||||||
prev_match?: string
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue