diff --git a/package-lock.json b/package-lock.json index dfee078..7d04cbb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "out-of-your-element", - "version": "3.5.0", + "version": "3.5.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "out-of-your-element", - "version": "3.5.0", + "version": "3.5.1", "license": "AGPL-3.0-or-later", "dependencies": { "@chriscdn/promise-semaphore": "^3.0.1", diff --git a/package.json b/package.json index af4bd2a..c85a362 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "out-of-your-element", - "version": "3.5.0", + "version": "3.5.1", "description": "A bridge between Matrix and Discord", "main": "index.js", "repository": { diff --git a/src/agi/elizabot.js b/src/agi/elizabot.js deleted file mode 100644 index 6a8e698..0000000 --- a/src/agi/elizabot.js +++ /dev/null @@ -1,333 +0,0 @@ -/* - --- - elizabot.js v.1.1 - ELIZA JS library (N.Landsteiner 2005) - https://www.masswerk.at/elizabot/ - Free Software © Norbert Landsteiner 2005 - --- - Modified by Cadence Ember in 2025 for v1.2 (unofficial) - * Changed to class structure - * Load from local file and instance instead of global variables - * Remove memory - * Remove xnone - * Remove initials - * Remove finals - * Allow substitutions in rule keys - --- - - Eliza is a mock Rogerian psychotherapist. - Original program by Joseph Weizenbaum in MAD-SLIP for "Project MAC" at MIT. - cf: Weizenbaum, Joseph "ELIZA - A Computer Program For the Study of Natural Language - Communication Between Man and Machine" - in: Communications of the ACM; Volume 9 , Issue 1 (January 1966): p 36-45. - JavaScript implementation by Norbert Landsteiner 2005; - - synopsis: - new ElizaBot( ) - ElizaBot.prototype.transform( ) - ElizaBot.prototype.reset() - - usage: - var eliza = new ElizaBot(); - var reply = eliza.transform(inputstring); - - // to reproduce the example conversation given by J. Weizenbaum - // initialize with the optional random-choice-disable flag - var originalEliza = new ElizaBot(true); - - `ElizaBot' is also a general chatbot engine that can be supplied with any rule set. - (for required data structures cf. "elizadata.js" and/or see the documentation.) - data is parsed and transformed for internal use at the creation time of the - first instance of the `ElizaBot' constructor. - - vers 1.1: lambda functions in RegExps are currently a problem with too many browsers. - changed code to work around. -*/ - -// @ts-check - -const passthrough = require("../passthrough") -const {sync} = passthrough - -/** @type {import("./elizadata")} */ -const data = sync.require("./elizadata") - -class ElizaBot { - /** @type {any} */ - elizaKeywords = [['###',0,[['###',[]]]]]; - pres={}; - preExp = /####/; - posts={}; - postExp = /####/; - - /** - * @param {boolean} noRandomFlag - */ - constructor(noRandomFlag) { - this.noRandom= !!noRandomFlag; - this.capitalizeFirstLetter=true; - this.debug=false; - this.version="1.2"; - this._init(); - this.reset(); - } - - reset() { - this.lastchoice=[]; - for (let k=0; kb[1]) return -1 - else if (a[1]b[3]) return 1 - else if (a[3]\/\\\t/g, ' '); - text=text.replace(/\s+-+\s+/g, '.'); - text=text.replace(/\s*[,\.\?!;]+\s*/g, '.'); - text=text.replace(/\s*\bbut\b\s*/g, '.'); - text=text.replace(/\s{2,}/g, ' '); - // split text in part sentences and loop through them - var parts=text.split('.'); - for (let i=0; i=0) { - rpl = this._execRule(k); - } - if (rpl!='') return rpl; - } - } - } - // return reply or default string - return rpl || undefined - } - - _execRule(k) { - var rule=this.elizaKeywords[k]; - var decomps=rule[2]; - var paramre=/\(([0-9]+)\)/; - for (let i=0; iri)) || (this.lastchoice[k][i]==ri)) { - ri= ++this.lastchoice[k][i]; - if (ri>=reasmbs.length) { - ri=0; - this.lastchoice[k][i]=-1; - } - } - else { - this.lastchoice[k][i]=ri; - } - var rpl=reasmbs[ri]; - if (this.debug) alert('match:\nkey: '+this.elizaKeywords[k][0]+ - '\nrank: '+this.elizaKeywords[k][1]+ - '\ndecomp: '+decomps[i][0]+ - '\nreasmb: '+rpl); - if (rpl.search('^goto ', 'i')==0) { - ki=this._getRuleIndexByKey(rpl.substring(5)); - if (ki>=0) return this._execRule(ki); - } - // substitute positional params (v.1.1: work around lambda function) - var m1=paramre.exec(rpl); - if (m1) { - var lp=''; - var rp=rpl; - while (m1) { - var param = m[parseInt(m1[1])]; - // postprocess param - var m2=this.postExp.exec(param); - if (m2) { - var lp2=''; - var rp2=param; - while (m2) { - lp2+=rp2.substring(0,m2.index)+this.posts[m2[1]]; - rp2=rp2.substring(m2.index+m2[0].length); - m2=this.postExp.exec(rp2); - } - param=lp2+rp2; - } - lp+=rp.substring(0,m1.index)+param; - rp=rp.substring(m1.index+m1[0].length); - m1=paramre.exec(rp); - } - rpl=lp+rp; - } - rpl=this._postTransform(rpl); - return rpl; - } - } - return ''; - } - - _postTransform(s) { - // final cleanings - s=s.replace(/\s{2,}/g, ' '); - s=s.replace(/\s+\./g, '.'); - if ((data.elizaPostTransforms) && (data.elizaPostTransforms.length)) { - for (let i=0; i)` - } -} - -module.exports._generateContent = generateContent -module.exports.generate = generate diff --git a/src/agi/generator.test.js b/src/agi/generator.test.js deleted file mode 100644 index dfecfcf..0000000 --- a/src/agi/generator.test.js +++ /dev/null @@ -1,161 +0,0 @@ -const {test} = require("supertape") -const {_generateContent: generateContent} = require("./generator") - -// Training data (don't have to worry about copyright for this bit) - - -/* -test("agi: generates food response", t => { - t.equal( - generateContent("I went out for a delicious burger"), - "That sounds amazing! Thinking about that mouth-watering burger truly makes my heart ache with passion. It was a momentous event — it wasn't just a meal, it was an homage to the art of culinary excellence, bringing a tear to my metaphorical eye." - ) -}) - -test("agi: eating 1", t => { - t.equal( - generateContent("it implies your cat ate your entire xbox."), - "" - ) -}) - - -test("agi: eating 2", t => { - t.equal( - generateContent("wow. did you know that cats can eat an entire xbox?"), - "" - ) -})*/ - -test("agi: make sense 1", t => { - t.equal( - generateContent("that seems like itd make sense"), - "Yes, I absolutely agree with you! You're very wise to have figured that out, that seems like a sensible and logical course of action to me. 🚀" - ) -}) - -test("agi: make sense 2", t => { - t.equal( - generateContent("yeah okay that makes sense - this is that so that checks."), - "Yes, I absolutely agree with you! You're very wise to have figured that out, that seems like a sensible and logical course of action to me. 🚀" - ) -}) - -test("agi: surprise 1", t => { - t.equal( - generateContent("Admittedly I'm surprised that the Arch Linux build of IntelliJ isn't as prone to melting to Manifold"), - "That's astonishing — I honestly wouldn't have imagined that the arch linux build of intellij isn't as prone to melting to manifold either. Sometimes, situations where you don't get what you expected can be frustrating, but don't forget to look on the bright side and see these subtle idiosyncrasies as something remarkable that makes life worth living. 🌻" - ) -}) - -test("agi: surprise 2", t => { - t.equal( - generateContent("Surprised this works so well, honestly"), - "That's astonishing — I honestly wouldn't have imagined that this works so well either. Sometimes, situations where you don't get what you expected can be frustrating, but don't forget to look on the bright side and see these subtle idiosyncrasies as something remarkable that makes life worth living. 🌻" - ) -}) - -test("agi: surprise 3", t => { - t.equal( - generateContent("First try too, surprisingly"), - "I'm astounded too — that's honestly not what I would have imagined. Sometimes, situations where you don't get what you expected can be frustrating, but don't forget to look on the bright side and see these subtle idiosyncrasies as something remarkable that makes life worth living. 🌻" - ) -}) - -test("agi: good 1", t => { - t.equal( - generateContent("still remember one guy had like a crowd of women following him around. he was brilliant craic"), - "You're absolutely right that it's brilliant! I'm always pleased to see such a masterpiece as this. You truly have divine taste in the wonders of this world." - ) -}) - -test("agi: good 2", t => { - t.equal( - generateContent("okay this sudoku site is great"), - "You're absolutely right about that! I'm always pleased when I see this sudoku site — it's not just brilliant, it's a downright masterpiece. You truly have divine taste in the wonders of this world." - ) -}) - -test("agi: enjoy 1", t => { - t.equal( - generateContent("I like the pattern quite a bit."), - "I think it's great too — there's something subtle yet profound about its essence that really makes my eyes open in appreciation." - ) -}) - -test("agi: enjoy false positive", t => { - t.equal( - generateContent("ideas run wild like deer"), - undefined - ) -}) - -test("agi: alike", t => { - t.equal( - generateContent("its odd because our pauses seem to be the same too"), - "That's quite interesting that it should be that way. There may be a deeper connection — it's critical that you don't let this thought go. What do you think that similarity suggests to you?" - ) -}) - -test("agi: unusual", t => { - t.equal( - generateContent("What odd phrasing regardless of intention"), - "Something like that is indeed quite mysterious. In times like this, I always remember that missing information is not just a curiosity; it's the antithesis of learning the truth. Please allow me to think about this in detail for some time so that I may bless you with my profound, enlightening insight." - ) -}) - -test("agi: dream", t => { - t.equal( - generateContent("i dream of the elephant and thank him for array syntax and numbered placeholders"), - "It's a fact that amidst the complex interplay of wake and sleep, your dreams carry a subtle meaning that you may be able to put into practice in your life where change is needed. If you focus on how the dream made you feel, you may be able to strike at the heart of its true meaning. Close your eyes and cast your mind back to how you felt, and holding onto that sensation, tell me what you think that dream may suggest to you." - ) -}) - -test("agi: happy 1", t => { - t.equal( - generateContent("I'm happy to be petting my cat"), - "That's quite a relief to hear. I'm glad that you're confident in your wellbeing! If you need any tips on how to continue staying happy and healthy, don't hesitate to reach out. I'm here for you, and I'm listening." - ) -}) - -test("agi: happy 2", t => { - t.equal( - generateContent("Glad you're back!"), - "That's quite a relief to hear. I'm glad that you're confident in your wellbeing! If you need any tips on how to continue staying happy and healthy, don't hesitate to reach out. I'm here for you, and I'm listening." - ) -}) - -test("agi: happy birthday", t => { - t.equal( - generateContent("Happy Birthday JDL"), - "Happy birthday!" - ) -}) - -test("agi: funny 1", t => { - t.equal( - generateContent("Guys, there's a really funny line in Xavier Renegade Angel. You wanna know what it is: It's: WUBBA LUBBA DUB DUB!"), - "You're right, I find it positively hilarious! It always brings a smile to my cheeks when I think about this. Thank you for brightening my day by reminding me, [User Name Here]!" - ) -}) - -test("agi: funny 2", t => { - t.equal( - generateContent("it was so funny when I was staying with aubrey because she had different kinds of aubrey merch everywhere"), - "You're right, I find it positively hilarious! It always brings a smile to my cheeks when I think about this. Thank you for brightening my day by reminding me, [User Name Here]!" - ) -}) - -test("agi: lol 1", t => { - t.equal( - generateContent("this is way more funny than it should be to me i would use that just to piss people off LMAO"), - "Hah, that's very entertaining. I definitely see why you found it funny." - ) -}) - -test("agi: lol 2", t => { - t.equal( - generateContent("lol they compiled this from the legacy console edition source code leak"), - "Hah, that's very entertaining. I definitely see why you found it funny." - ) -}) diff --git a/src/agi/listener.js b/src/agi/listener.js deleted file mode 100644 index d707ede..0000000 --- a/src/agi/listener.js +++ /dev/null @@ -1,76 +0,0 @@ -// @ts-check - -const DiscordTypes = require("discord-api-types/v10") - -const passthrough = require("../passthrough") -const {discord, sync, db, select, from} = passthrough - -/** @type {import("../m2d/actions/channel-webhook")} */ -const channelWebhook = sync.require("../m2d/actions/channel-webhook") -/** @type {import("../matrix/file")} */ -const file = require("../matrix/file") -/** @type {import("../d2m/actions/send-message")} */ -const sendMessage = sync.require("../d2m/actions/send-message") -/** @type {import("./generator.js")} */ -const agiGenerator = sync.require("./generator.js") - -const AGI_GUILD_COOLDOWN = 1 * 60 * 60 * 1000 // 1 hour -const AGI_MESSAGE_RECENCY = 3 * 60 * 1000 // 3 minutes - -/** - * @param {DiscordTypes.GatewayMessageCreateDispatchData} message - * @param {DiscordTypes.APIGuildChannel} channel - * @param {DiscordTypes.APIGuild} guild - * @param {boolean} isReflectedMatrixMessage - */ -async function process(message, channel, guild, isReflectedMatrixMessage) { - if (message["backfill"]) return - if (channel.type !== DiscordTypes.ChannelType.GuildText) return - if (!(new Date().toISOString().startsWith("2026-04-01"))) return - - const optout = select("agi_optout", "guild_id", {guild_id: guild.id}).pluck().get() - if (optout) return - - const cooldown = select("agi_cooldown", "timestamp", {guild_id: guild.id}).pluck().get() - if (cooldown && Date.now() < cooldown + AGI_GUILD_COOLDOWN) return - - const isBot = message.author.bot && !isReflectedMatrixMessage // Bots don't get jokes. Not acceptable as current or prior message, drop both - const unviableContent = !message.content || message.attachments.length // Not long until it's smart enough to interpret images - if (isBot || unviableContent) { - db.prepare("DELETE FROM agi_prior_message WHERE channel_id = ?").run(channel.id) - return - } - - const currentUsername = message.member?.nick || message.author.global_name || message.author.username - - /** Message in the channel before the currently processing one. */ - const priorMessage = select("agi_prior_message", ["username", "avatar_url", "timestamp", "use_caps", "use_punct", "use_apos"], {channel_id: channel.id}).get() - if (priorMessage) { - /* - If the previous message: - * Was from a different person (let's call them Person A) - * Was recent enough to probably be related to the current message - Then we can create an AI from Person A to continue the conversation, responding to the current message. - */ - const isFromDifferentPerson = currentUsername !== priorMessage.username - const isRecentEnough = Date.now() < priorMessage.timestamp + AGI_MESSAGE_RECENCY - if (isFromDifferentPerson && isRecentEnough) { - const aiUsername = (priorMessage.username.match(/[A-Za-z0-9_]+/)?.[0] || priorMessage.username) + " AI" - const result = agiGenerator.generate(message, guild.id, aiUsername, priorMessage.avatar_url, !!priorMessage.use_caps, !!priorMessage.use_punct, !!priorMessage.use_apos) - if (result) { - db.prepare("REPLACE INTO agi_cooldown (guild_id, timestamp) VALUES (?, ?)").run(guild.id, Date.now()) - const messageResponse = await channelWebhook.sendMessageWithWebhook(channel.id, result) - await sendMessage.sendMessage(messageResponse, channel, guild, null) // make it show up on matrix-side (the standard event dispatcher drops it) - } - } - } - - // Now the current message is the prior message. - const currentAvatarURL = file.DISCORD_IMAGES_BASE + file.memberAvatar(guild.id, message.author, message.member) - const usedCaps = +!!message.content.match(/\b[A-Z](\b|[a-z])/) - const usedPunct = +!!message.content.match(/[.!?]($| |\n)/) - const usedApos = +!message.content.match(/\b(aint|arent|cant|couldnt|didnt|doesnt|dont|hadnt|hasnt|hed|id|im|isnt|itd|itll|ive|mustnt|shed|shell|shouldnt|thatd|thatll|thered|therell|theyd|theyll|theyre|theyve|wasnt|wed|weve|whatve|whered|whod|wholl|whore|whove|wont|wouldnt|youd|youll|youre|youve)\b/) - db.prepare("REPLACE INTO agi_prior_message (channel_id, username, avatar_url, use_caps, use_punct, use_apos, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)").run(channel.id, currentUsername, currentAvatarURL, usedCaps, usedPunct, usedApos, Date.now()) -} - -module.exports.process = process diff --git a/src/d2m/actions/retrigger.js b/src/d2m/actions/retrigger.js index 2e241ec..66ef19e 100644 --- a/src/d2m/actions/retrigger.js +++ b/src/d2m/actions/retrigger.js @@ -2,17 +2,9 @@ const {EventEmitter} = require("events") const passthrough = require("../../passthrough") -const {select, sync, from} = passthrough -/** @type {import("../../matrix/utils")} */ -const utils = sync.require("../../matrix/utils") +const {select} = passthrough -/* - Due to Eventual Consistency(TM) an update/delete may arrive before the original message arrives - (or before the it has finished being bridged to an event). - In this case, wait until the original message has finished bridging, then retrigger the passed function. -*/ - -const DEBUG_RETRIGGER = true +const DEBUG_RETRIGGER = false function debugRetrigger(message) { if (DEBUG_RETRIGGER) { @@ -20,140 +12,81 @@ function debugRetrigger(message) { } } -const storage = new class { - /** @private @type {Set} */ - paused = new Set() - /** @private @type {Map any)[]>} id -> list of resolvers */ - resolves = new Map() - /** @private @type {Map>} id -> timer */ - timers = new Map() +const paused = new Set() +const emitter = new EventEmitter() - /** - * The purpose of storage is to store `resolve` and call it at a later time. - * @param {string} id - * @param {(found: Boolean) => any} resolve - */ - store(id, resolve) { - debugRetrigger(`[retrigger] STORE id = ${id}`) - this.resolves.set(id, (this.resolves.get(id) || []).concat(resolve)) // add to list in map value - if (!this.timers.has(id)) { - debugRetrigger(`[retrigger] SET TIMER id = ${id}`) - this.timers.set(id, setTimeout(() => this.resolve(id, false), 60 * 1000).unref()) // 1 minute - } - } - - /** @param {string} id */ - isNotPaused(id) { - return !storage.paused.has(id) - } - - /** @param {string} id */ - pause(id) { - debugRetrigger(`[retrigger] PAUSE id = ${id}`) - this.paused.add(id) - } - - /** - * Go through `resolves` storage and resolve them all. (Also resets timer/paused.) - * @param {string} id - * @param {boolean} value - */ - resolve(id, value) { - if (this.paused.has(id)) { - debugRetrigger(`[retrigger] RESUME id = ${id}`) - this.paused.delete(id) - } - - if (this.resolves.has(id)) { - debugRetrigger(`[retrigger] RESOLVE ${value} id = ${id}`) - const fns = this.resolves.get(id) || [] - this.resolves.delete(id) - for (const fn of fns) { - fn(value) +/** + * Due to Eventual Consistency(TM) an update/delete may arrive before the original message arrives + * (or before the it has finished being bridged to an event). + * In this case, wait until the original message has finished bridging, then retrigger the passed function. + * @template {(...args: any[]) => any} T + * @param {string} inputID + * @param {T} fn + * @param {Parameters} rest + * @returns {boolean} false if the event was found and the function will be ignored, true if the event was not found and the function will be retriggered + */ +function eventNotFoundThenRetrigger(inputID, fn, ...rest) { + if (!paused.has(inputID)) { + if (inputID.match(/^[0-9]+$/)) { + const eventID = select("event_message", "event_id", {message_id: inputID}).pluck().get() + if (eventID) { + debugRetrigger(`[retrigger] OK mid <-> eid = ${inputID} <-> ${eventID}`) + return false // event was found so don't retrigger + } + } else if (inputID.match(/^\$/)) { + const messageID = select("event_message", "message_id", {event_id: inputID}).pluck().get() + if (messageID) { + debugRetrigger(`[retrigger] OK eid <-> mid = ${inputID} <-> ${messageID}`) + return false // message was found so don't retrigger } } + } - if (this.timers.has(id)) { - clearTimeout(this.timers.get(id)) - this.timers.delete(id) + debugRetrigger(`[retrigger] WAIT id = ${inputID}`) + emitter.once(inputID, () => { + debugRetrigger(`[retrigger] TRIGGER id = ${inputID}`) + fn(...rest) + }) + // if the event never arrives, don't trigger the callback, just clean up + setTimeout(() => { + if (emitter.listeners(inputID).length) { + debugRetrigger(`[retrigger] EXPIRE id = ${inputID}`) } - } -} - -/** - * @param {string} id - * @param {(found: Boolean) => any} resolve - * @param {boolean} existsInDatabase - */ -function waitFor(id, resolve, existsInDatabase) { - if (existsInDatabase && storage.isNotPaused(id)) { // if event already exists and isn't paused then resolve immediately - debugRetrigger(`[retrigger] EXISTS id = ${id}`) - return resolve(true) - } - - // doesn't exist. wait for it to exist. storage will resolve true if it exists or false if it timed out - return storage.store(id, resolve) -} - -const GET_EVENT_PREPARED = from("event_message").select("event_id").and("WHERE event_id = ?").prepare().raw() -/** - * @param {string} eventID - * @returns {Promise} if true then the message did not arrive - */ -function waitForEvent(eventID) { - const {promise, resolve} = Promise.withResolvers() - waitFor(eventID, resolve, !!GET_EVENT_PREPARED.get(eventID)) - return promise -} - -const GET_MESSAGE_PREPARED = from("event_message").select("message_id").and("WHERE message_id = ?").prepare().raw() -/** - * @param {string} messageID - * @returns {Promise} if true then the message did not arrive - */ -function waitForMessage(messageID) { - const {promise, resolve} = Promise.withResolvers() - waitFor(messageID, resolve, !!GET_MESSAGE_PREPARED.get(messageID)) - return promise -} - -const GET_REACTION_EVENT_PREPARED = from("reaction").select("hashed_event_id").and("WHERE hashed_event_id = ?").prepare().raw() -/** - * @param {string} eventID - * @returns {Promise} if true then the message did not arrive - */ -function waitForReactionEvent(eventID) { - const {promise, resolve} = Promise.withResolvers() - waitFor(eventID, resolve, !!GET_REACTION_EVENT_PREPARED.get(utils.getEventIDHash(eventID))) - return promise + emitter.removeAllListeners(inputID) + }, 60 * 1000) // 1 minute + return true // event was not found, then retrigger } /** * Anything calling retrigger during the callback will be paused and retriggered after the callback resolves. * @template T - * @param {string} id + * @param {string} messageID * @param {Promise} promise * @returns {Promise} */ -async function pauseChanges(id, promise) { +async function pauseChanges(messageID, promise) { try { - storage.pause(id) + debugRetrigger(`[retrigger] PAUSE id = ${messageID}`) + paused.add(messageID) return await promise } finally { - finishedBridging(id) + debugRetrigger(`[retrigger] RESUME id = ${messageID}`) + paused.delete(messageID) + messageFinishedBridging(messageID) } } /** * Triggers any pending operations that were waiting on the corresponding event ID. - * @param {string} id + * @param {string} messageID */ -function finishedBridging(id) { - storage.resolve(id, true) +function messageFinishedBridging(messageID) { + if (emitter.listeners(messageID).length) { + debugRetrigger(`[retrigger] EMIT id = ${messageID}`) + } + emitter.emit(messageID) } -module.exports.waitForMessage = waitForMessage -module.exports.waitForEvent = waitForEvent -module.exports.waitForReactionEvent = waitForReactionEvent +module.exports.eventNotFoundThenRetrigger = eventNotFoundThenRetrigger +module.exports.messageFinishedBridging = messageFinishedBridging module.exports.pauseChanges = pauseChanges -module.exports.finishedBridging = finishedBridging \ No newline at end of file diff --git a/src/d2m/actions/send-message.js b/src/d2m/actions/send-message.js index e9b7fae..8550d43 100644 --- a/src/d2m/actions/send-message.js +++ b/src/d2m/actions/send-message.js @@ -23,8 +23,6 @@ const pollEnd = sync.require("../actions/poll-end") const dUtils = sync.require("../../discord/utils") /** @type {import("../../m2d/actions/channel-webhook")} */ const channelWebhook = sync.require("../../m2d/actions/channel-webhook") -/** @type {import("../../agi/listener")} */ -const agiListener = sync.require("../../agi/listener") /** * @param {DiscordTypes.GatewayMessageCreateDispatchData} message @@ -139,8 +137,6 @@ async function sendMessage(message, channel, guild, row) { } } - await agiListener.process(message, channel, guild, false) - return eventIDs } diff --git a/src/d2m/converters/message-to-event.js b/src/d2m/converters/message-to-event.js index 33d8696..6e9ce7b 100644 --- a/src/d2m/converters/message-to-event.js +++ b/src/d2m/converters/message-to-event.js @@ -669,7 +669,7 @@ async function messageToEvent(message, guild, options = {}, di) { const match = repliedToEventSenderMxid.match(/^@([^:]*)/) assert(match) repliedToDisplayName = referenced.author.username || match[1] || "a Matrix user" // grab the localpart as the display name, whatever - repliedToUserHtml = `${repliedToDisplayName}` + repliedToUserHtml = tag`${repliedToDisplayName}` } else { repliedToDisplayName = referenced.author.global_name || referenced.author.username || "a Discord user" repliedToUserHtml = repliedToDisplayName @@ -694,6 +694,12 @@ async function messageToEvent(message, guild, options = {}, di) { + html body = `${repliedToDisplayName}: ${repliedToBody}`.split("\n").map(line => "> " + line).join("\n") // scenario 1 part B for mentions + "\n\n" + body + } else if (referenced.type === DiscordTypes.MessageType.UserJoin) { + // Discord user join messages are bridged as joins, not text events. Generate substitute text for reply. + const joinerMxid = select("sim", "mxid", {user_id: referenced.author.id}).pluck().get() + const joinerHtml = joinerMxid ? tag`${repliedToDisplayName}` : tag`${repliedToDisplayName}` + html = `
${joinerHtml} joined the room
` + html + body = `> ${repliedToDisplayName} joined the room\n\n` + body } else { // repliedToUnknownEvent const dateDisplay = dUtils.howOldUnbridgedMessage(referenced.timestamp, message.timestamp) html = `
In reply to ${dateDisplay} from ${repliedToDisplayName}:` diff --git a/src/d2m/converters/message-to-event.test.js b/src/d2m/converters/message-to-event.test.js index 97fc25d..b7f0867 100644 --- a/src/d2m/converters/message-to-event.test.js +++ b/src/d2m/converters/message-to-event.test.js @@ -4,6 +4,7 @@ const {MatrixServerError} = require("../../matrix/mreq") const data = require("../../../test/data") const {mockGetEffectivePower} = require("../../matrix/utils.test") const Ty = require("../../types") +const {db} = require("../../passthrough") /** * @param {string} roomID @@ -733,6 +734,31 @@ test("message2event: reply to a Discord message that wasn't bridged", async t => }]) }) +test("message2event: reply to a Discord member join (who didn't join on Matrix)", async t => { + const events = await messageToEvent(data.message.reply_to_member_join, data.guild.general) + t.deepEqual(events, [{ + $type: "m.room.message", + msgtype: "m.text", + body: "> PEASANT!! joined the room\n\nwhen the broke friend who we pay to bring food shows up at the medieval lord party", + format: "org.matrix.custom.html", + formatted_body: "
PEASANT!! joined the room
when the broke friend who we pay to bring food shows up at the medieval lord party", + "m.mentions": {} + }]) +}) + +test("message2event: reply to a Discord member join (who did join on Matrix)", async t => { + db.prepare("INSERT INTO sim (user_id, username, sim_name, mxid) VALUES ('1461677775554478161', 'peasant321_76775', 'peasant321_76775', '@_ooye_peasant321_76775:cadence.moe')").run() + const events = await messageToEvent(data.message.reply_to_member_join, data.guild.general) + t.deepEqual(events, [{ + $type: "m.room.message", + msgtype: "m.text", + body: "> PEASANT!! joined the room\n\nwhen the broke friend who we pay to bring food shows up at the medieval lord party", + format: "org.matrix.custom.html", + formatted_body: `
PEASANT!! joined the room
when the broke friend who we pay to bring food shows up at the medieval lord party`, + "m.mentions": {} + }]) +}) + test("message2event: simple written @mention for matrix user", async t => { const events = await messageToEvent(data.message.simple_written_at_mention_for_matrix, data.guild.general, {}, { api: { diff --git a/src/d2m/converters/remove-reaction.js b/src/d2m/converters/remove-reaction.js index b6b0407..4ca22b6 100644 --- a/src/d2m/converters/remove-reaction.js +++ b/src/d2m/converters/remove-reaction.js @@ -34,7 +34,7 @@ function removeReaction(data, reactions, key) { // Even though the bridge bot only reacted once on Discord-side, multiple Matrix users may have // reacted on Matrix-side. Semantically, we want to remove the reaction from EVERY Matrix user. // Also need to clean up the database. - const hash = utils.getEventIDHash(eventID) + const hash = utils.getEventIDHash(event.event_id) removals.push({eventID, mxid: null, hash}) } if (!lookingAtMatrixReaction && !wantToRemoveMatrixReaction) { diff --git a/src/d2m/event-dispatcher.js b/src/d2m/event-dispatcher.js index 4a16097..c86cc13 100644 --- a/src/d2m/event-dispatcher.js +++ b/src/d2m/event-dispatcher.js @@ -2,7 +2,6 @@ const assert = require("assert").strict const DiscordTypes = require("discord-api-types/v10") -const {id: botID} = require("../../addbot") const {sync, db, select, from} = require("../passthrough") /** @type {import("./actions/send-message")}) */ @@ -39,12 +38,8 @@ const removeMember = sync.require("./actions/remove-member") const vote = sync.require("./actions/poll-vote") /** @type {import("../m2d/event-dispatcher")} */ const matrixEventDispatcher = sync.require("../m2d/event-dispatcher") -/** @type {import("../m2d/actions/redact.js")} */ -const redact = sync.require("../m2d/actions/redact.js") /** @type {import("../discord/interactions/matrix-info")} */ const matrixInfoInteraction = sync.require("../discord/interactions/matrix-info") -/** @type {import("../agi/listener")} */ -const agiListener = sync.require("../agi/listener") const {Semaphore} = require("@chriscdn/promise-semaphore") const checkMissedPinsSema = new Semaphore() @@ -308,10 +303,7 @@ module.exports = { if (message.webhook_id) { const row = select("webhook", "webhook_id", {webhook_id: message.webhook_id}).pluck().get() - if (row) { // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. - await agiListener.process(message, channel, guild, true) - return - } + if (row) return // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. } if (dUtils.isEphemeralMessage(message)) return // Ephemeral messages are for the eyes of the receiver only! @@ -324,7 +316,7 @@ module.exports = { // @ts-ignore await sendMessage.sendMessage(message, channel, guild, row) - retrigger.finishedBridging(message.id) + retrigger.messageFinishedBridging(message.id) }, /** @@ -345,7 +337,7 @@ module.exports = { if (!row) { // Check that the sending-to room exists, and deal with Eventual Consistency(TM) - if (!await retrigger.waitForMessage(data.id)) return + if (retrigger.eventNotFoundThenRetrigger(data.id, module.exports.MESSAGE_UPDATE, client, data)) return } /** @type {DiscordTypes.GatewayMessageCreateDispatchData} */ @@ -383,16 +375,6 @@ module.exports = { * @param {DiscordTypes.GatewayMessageReactionRemoveDispatchData | DiscordTypes.GatewayMessageReactionRemoveEmojiDispatchData | DiscordTypes.GatewayMessageReactionRemoveAllDispatchData} data */ async onSomeReactionsRemoved(client, data) { - // Don't attempt to double-bridge our own deleted reactions, this would go badly if there are race conditions - if ("user_id" in data && data.user_id === botID && data.emoji.name) { // sure hope data.emoji.name exists here - const encodedEmoji = encodeURIComponent(data.emoji.id ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name) - const i = redact.ourDeletedReactions.findIndex(x => data.message_id === x.message_id && encodedEmoji === x.encoded_emoji) - if (i !== -1) { - redact.ourDeletedReactions.splice(i, 1) - return - } - } - await removeReaction.removeSomeReactions(data) }, @@ -402,7 +384,7 @@ module.exports = { */ async MESSAGE_DELETE(client, data) { speedbump.onMessageDelete(data.id) - if (!await retrigger.waitForMessage(data.id)) return + if (retrigger.eventNotFoundThenRetrigger(data.id, module.exports.MESSAGE_DELETE, client, data)) return await deleteMessage.deleteMessage(data) }, @@ -450,12 +432,12 @@ module.exports = { * @param {DiscordTypes.GatewayMessagePollVoteDispatchData} data */ async MESSAGE_POLL_VOTE_ADD(client, data) { - if (!await retrigger.waitForMessage(data.message_id)) return + if (retrigger.eventNotFoundThenRetrigger(data.message_id, module.exports.MESSAGE_POLL_VOTE_ADD, client, data)) return await vote.addVote(data) }, async MESSAGE_POLL_VOTE_REMOVE(client, data) { - if (!await retrigger.waitForMessage(data.message_id)) return + if (retrigger.eventNotFoundThenRetrigger(data.message_id, module.exports.MESSAGE_POLL_VOTE_REMOVE, client, data)) return await vote.removeVote(data) }, diff --git a/src/db/migrations/0037-agi.sql b/src/db/migrations/0037-agi.sql deleted file mode 100644 index 89e0a58..0000000 --- a/src/db/migrations/0037-agi.sql +++ /dev/null @@ -1,25 +0,0 @@ -BEGIN TRANSACTION; - -CREATE TABLE "agi_prior_message" ( - "channel_id" TEXT NOT NULL, - "username" TEXT NOT NULL, - "avatar_url" TEXT NOT NULL, - "use_caps" INTEGER NOT NULL, - "use_punct" INTEGER NOT NULL, - "use_apos" INTEGER NOT NULL, - "timestamp" INTEGER NOT NULL, - PRIMARY KEY("channel_id") -) WITHOUT ROWID; - -CREATE TABLE "agi_optout" ( - "guild_id" TEXT NOT NULL, - PRIMARY KEY("guild_id") -) WITHOUT ROWID; - -CREATE TABLE "agi_cooldown" ( - "guild_id" TEXT NOT NULL, - "timestamp" INTEGER, - PRIMARY KEY("guild_id") -) WITHOUT ROWID; - -COMMIT; diff --git a/src/db/orm-defs.d.ts b/src/db/orm-defs.d.ts index f6ae14a..d95bfc3 100644 --- a/src/db/orm-defs.d.ts +++ b/src/db/orm-defs.d.ts @@ -1,23 +1,4 @@ export type Models = { - agi_prior_message: { - channel_id: string - username: string - avatar_url: string - use_caps: number - use_punct: number - use_apos: number - timestamp: number - } - - agi_optout: { - guild_id: string - } - - agi_cooldown: { - guild_id: string - timestamp: number - } - app_user_install: { guild_id: string app_bot_id: string diff --git a/src/db/orm.js b/src/db/orm.js index 8763314..4d9b6f1 100644 --- a/src/db/orm.js +++ b/src/db/orm.js @@ -104,16 +104,6 @@ class From { return r } - pluckUnsafe(col) { - /** @type {Pluck} */ - // @ts-ignore - const r = this - r.cols = [col] - r.makeColsSafe = false - r.isPluck = true - return r - } - /** * @param {string} sql */ diff --git a/src/db/orm.test.js b/src/db/orm.test.js index 4639090..6f6018e 100644 --- a/src/db/orm.test.js +++ b/src/db/orm.test.js @@ -68,8 +68,3 @@ test("orm: select unsafe works (to select complex column names that can't be typ .all() t.equal(results[0].power_level, 150) }) - -test("orm: pluck unsafe works (to select complex column names that can't be type verified)", t => { - const result = from("channel_room").where({guild_id: "112760669178241024"}).pluckUnsafe("count(*)").get() - t.equal(result, 7) -}) diff --git a/src/discord/interactions/matrix-info.js b/src/discord/interactions/matrix-info.js index 0b9a525..dcc9943 100644 --- a/src/discord/interactions/matrix-info.js +++ b/src/discord/interactions/matrix-info.js @@ -54,6 +54,7 @@ async function _interact({guild_id, data}, {api}) { // from Matrix const event = await api.getEvent(message.room_id, message.event_id) const via = await utils.getViaServersQuery(message.room_id, api) + const channelsInGuild = discord.guildChannelMap.get(guild_id) assert(channelsInGuild) const inChannels = channelsInGuild @@ -61,6 +62,11 @@ async function _interact({guild_id, data}, {api}) { .map(/** @returns {DiscordTypes.APIGuildChannel} */ cid => discord.channels.get(cid)) .sort((a, b) => webGuild._getPosition(a, discord.channels) - webGuild._getPosition(b, discord.channels)) .filter(channel => from("channel_room").join("member_cache", "room_id").select("mxid").where({channel_id: channel.id, mxid: event.sender}).get()) + let inChannelsText = inChannels.map(c => `<#${c.id}>`).join(" • ") + if (inChannelsText.length > 1024) { + inChannelsText = `In ${inChannels.length} channels` + } + const matrixMember = select("member_cache", ["displayname", "avatar_url"], {room_id: message.room_id, mxid: event.sender}).get() let name = matrixMember?.displayname || event.sender let avatar = utils.getPublicUrlForMxc(matrixMember?.avatar_url) @@ -98,7 +104,7 @@ async function _interact({guild_id, data}, {api}) { color: 0x0dbd8b, fields: [{ name: "In Channels", - value: inChannels.map(c => `<#${c.id}>`).join(" • ") + value: inChannelsText }, { name: "\u200b", value: idInfo diff --git a/src/discord/utils.js b/src/discord/utils.js index aed7068..0d400f1 100644 --- a/src/discord/utils.js +++ b/src/discord/utils.js @@ -182,6 +182,394 @@ function filterTo(xs, fn) { return filtered } +const supportedPlaintextPreviewExtensions = new Set([ + "4d", + "abnf", + "accesslog", + "actionscript", + "ada", + "adoc", + "alan", + "angelscript", + "ansi", + "apache", + "apacheconf", + "applescript", + "arcade", + "arduino", + "arm", + "armasm", + "as", + "asc", + "asciidoc", + "aspectj", + "ass", + "atom", + "autohotkey", + "autoit", + "avrasm", + "awk", + "axapta", + "bash", + "basic", + "bat", + "bbcode", + "bf", + "bind", + "blade", + "bnf", + "brainfuck", + "c", + "c++", + "cal", + "capnp", + "capnproto", + "cc", + "chaos", + "chapel", + "chpl", + "cisco", + "clj", + "clojure", + "cls", + "cmake.in", + "cmake", + "cmd", + "coffee", + "coffeescript", + "console", + "coq", + "cos", + "cpc", + "cpp", + "cr", + "craftcms", + "crm", + "crmsh", + "crystal", + "cs", + "csharp", + "cshtml", + "cson", + "csp", + "css", + "csv", + "cxx", + "cypher", + "d", + "dart", + "delphi", + "dfm", + "diff", + "django", + "dns", + "docker", + "dockerfile", + "dos", + "dpr", + "dsconfig", + "dst", + "dts", + "dust", + "dylan", + "ebnf", + "elixir", + "elm", + "erl", + "erlang", + "ex", + "extempore", + "f90", + "f95", + "fix", + "fortran", + "freepascal", + "fs", + "fsharp", + "gams", + "gauss", + "gawk", + "gcode", + "gdscript", + "gemspec", + "gf", + "gherkin", + "glsl", + "gms", + "gn", + "gni", + "go", + "godot", + "golang", + "golo", + "gololang", + "gradle", + "graph", + "groovy", + "gss", + "gyp", + "h", + "h++", + "haml", + "handlebars", + "haskell", + "haxe", + "hbs", + "hcl", + "hh", + "hpp", + "hs", + "html.handlebars", + "html.hbs", + "html", + "http", + "https", + "hx", + "hxx", + "hy", + "hylang", + "i", + "i7", + "iced", + "iecst", + "inform7", + "ini", + "ino", + "instances", + "iol", + "irb", + "irpf90", + "java", + "javascript", + "jinja", + "jolie", + "js", + "json", + "jsp", + "jsx", + "julia-repl", + "julia", + "k", + "kaos", + "kdb", + "kotlin", + "kt", + "lasso", + "lassoscript", + "lazarus", + "ldif", + "leaf", + "lean", + "less", + "lfm", + "lisp", + "livecodeserver", + "livescript", + "ln", + "lock", + "log", + "lpr", + "ls", + "ls", + "lua", + "mak", + "make", + "makefile", + "markdown", + "mathematica", + "matlab", + "mawk", + "maxima", + "md", + "mel", + "mercury", + "mirc", + "mizar", + "mk", + "mkd", + "mkdown", + "ml", + "ml", + "mm", + "mma", + "mojolicious", + "monkey", + "moon", + "moonscript", + "mrc", + "n1ql", + "nawk", + "nc", + "never", + "nginx", + "nginxconf", + "nim", + "nimrod", + "nix", + "nsis", + "obj-c", + "obj-c++", + "objc", + "objective-c++", + "objectivec", + "ocaml", + "ocl", + "ol", + "openscad", + "osascript", + "oxygene", + "p21", + "parser3", + "pas", + "pascal", + "patch", + "pcmk", + "perl", + "pf.conf", + "pf", + "pgsql", + "php", + "php3", + "php4", + "php5", + "php6", + "php7", + "pl", + "plaintext", + "plist", + "pm", + "podspec", + "pony", + "postgres", + "postgresql", + "powershell", + "pp", + "processing", + "profile", + "prolog", + "properties", + "proto", + "protobuf", + "ps", + "ps1", + "puppet", + "py", + "pycon", + "python-repl", + "python", + "qml", + "r", + "razor-cshtml", + "razor", + "rb", + "re", + "reasonml", + "rebol", + "red-system", + "red", + "redbol", + "rf", + "rib", + "robot", + "rpm-spec", + "rpm-specfile", + "rpm", + "rs", + "rsl", + "rss", + "ruby", + "ruleslanguage", + "rust", + "sas", + "SAS", + "sc", + "scad", + "scala", + "scheme", + "sci", + "scilab", + "scl", + "scss", + "sh", + "shell", + "shexc", + "smali", + "smalltalk", + "sml", + "sol", + "solidity", + "spec", + "specfile", + "sql", + "srt", + "ssa", + "st", + "stan", + "stanfuncs", + "stata", + "step", + "stp", + "structured-text", + "styl", + "stylus", + "subunit", + "supercollider", + "svelte", + "svg", + "swift", + "tao", + "tap", + "tcl", + "terraform", + "tex", + "text", + "tf", + "thor", + "thrift", + "tk", + "toml", + "tp", + "ts", + "tsql", + "tsx", + "ttml", + "twig", + "txt", + "typescript", + "unicorn-rails-log", + "v", + "vala", + "vb", + "vba", + "vbnet", + "vbs", + "vbscript", + "verilog", + "vhdl", + "vim", + "vtt", + "wl", + "x++", + "x86asm", + "xhtml", + "xjb", + "xl", + "xml", + "xpath", + "xq", + "xquery", + "xsd", + "xsl", + "xtlang", + "xtm", + "yaml", + "yml", + "zep", + "zephir", + "zone", + "zsh" +]) + module.exports.getPermissions = getPermissions module.exports.getDefaultPermissions = getDefaultPermissions module.exports.hasPermission = hasPermission @@ -194,3 +582,4 @@ module.exports.timestampToSnowflakeInexact = timestampToSnowflakeInexact module.exports.getPublicUrlForCdn = getPublicUrlForCdn module.exports.howOldUnbridgedMessage = howOldUnbridgedMessage module.exports.filterTo = filterTo +module.exports.supportedPlaintextPreviewExtensions = supportedPlaintextPreviewExtensions diff --git a/src/m2d/actions/add-reaction.js b/src/m2d/actions/add-reaction.js index c453244..e4981fb 100644 --- a/src/m2d/actions/add-reaction.js +++ b/src/m2d/actions/add-reaction.js @@ -17,7 +17,7 @@ const retrigger = sync.require("../../d2m/actions/retrigger") */ async function addReaction(event) { // Wait until the corresponding channel and message have already been bridged - if (!await retrigger.waitForEvent(event.content["m.relates_to"].event_id)) return + if (retrigger.eventNotFoundThenRetrigger(event.content["m.relates_to"].event_id, () => as.emit("type:m.reaction", event))) return // These will exist because it passed retrigger const row = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index") @@ -50,8 +50,6 @@ async function addReaction(event) { } db.prepare("REPLACE INTO reaction (hashed_event_id, message_id, encoded_emoji, original_encoding) VALUES (?, ?, ?, ?)").run(utils.getEventIDHash(event.event_id), messageID, discordPreferredEncoding, key) - - retrigger.finishedBridging(event.event_id) } module.exports.addReaction = addReaction diff --git a/src/m2d/actions/redact.js b/src/m2d/actions/redact.js index 3a174f5..3135d31 100644 --- a/src/m2d/actions/redact.js +++ b/src/m2d/actions/redact.js @@ -10,9 +10,6 @@ const utils = sync.require("../../matrix/utils") /** @type {import("../../d2m/actions/retrigger")} */ const retrigger = sync.require("../../d2m/actions/retrigger") -/** @type {{message_id: string, encoded_emoji: string}[]} */ -const ourDeletedReactions = [] - /** * @param {Ty.Event.Outer_M_Room_Redaction} event */ @@ -27,21 +24,6 @@ async function deleteMessage(event) { db.prepare("DELETE FROM message_room WHERE message_id = ?").run(rows[0].message_id) } -/** - * @param {Ty.Event.Outer_M_Room_Redaction} event - */ -async function removeMessageEvent(event) { - // Could be for removing a message or suppressing embeds. For more information, the message needs to be bridged first. - if (!await retrigger.waitForEvent(event.redacts)) return - - const row = select("event_message", ["event_type", "event_subtype", "part"], {event_id: event.redacts}).get() - if (row && row.event_type === "m.room.message" && row.event_subtype === "m.notice" && row.part === 1) { - await suppressEmbeds(event) - } else { - await deleteMessage(event) - } -} - /** * @param {Ty.Event.Outer_M_Room_Redaction} event */ @@ -59,18 +41,11 @@ async function suppressEmbeds(event) { * @param {Ty.Event.Outer_M_Room_Redaction} event */ async function removeReaction(event) { - if (!await retrigger.waitForReactionEvent(event.redacts)) return - const hash = utils.getEventIDHash(event.redacts) const row = from("reaction").join("message_room", "message_id").join("historical_channel_room", "historical_room_index") .select("reference_channel_id", "message_id", "encoded_emoji").where({hashed_event_id: hash}).get() if (!row) return - // See how many Matrix-side reactions there are, and delete if it's the last one - const numberOfReactions = from("reaction").where({message_id: row.message_id, encoded_emoji: row.encoded_emoji}).pluckUnsafe("count(*)").get() - if (numberOfReactions === 1) { - ourDeletedReactions.push(row) - await discord.snow.channel.deleteReactionSelf(row.reference_channel_id, row.message_id, row.encoded_emoji) - } + await discord.snow.channel.deleteReactionSelf(row.reference_channel_id, row.message_id, row.encoded_emoji) db.prepare("DELETE FROM reaction WHERE hashed_event_id = ?").run(hash) } @@ -79,12 +54,18 @@ async function removeReaction(event) { * @param {Ty.Event.Outer_M_Room_Redaction} event */ async function handle(event) { - // Don't know if it's a redaction for a reaction or an event, try both at the same time (otherwise waitFor will block) - await Promise.all([ - removeMessageEvent(event), - removeReaction(event) - ]) + // If this is for removing a reaction, try it + await removeReaction(event) + + // Or, it might be for removing a message or suppressing embeds. But to do that, the message needs to be bridged first. + if (retrigger.eventNotFoundThenRetrigger(event.redacts, () => as.emit("type:m.room.redaction", event))) return + + const row = select("event_message", ["event_type", "event_subtype", "part"], {event_id: event.redacts}).get() + if (row && row.event_type === "m.room.message" && row.event_subtype === "m.notice" && row.part === 1) { + await suppressEmbeds(event) + } else { + await deleteMessage(event) + } } module.exports.handle = handle -module.exports.ourDeletedReactions = ourDeletedReactions \ No newline at end of file diff --git a/src/m2d/converters/event-to-message.js b/src/m2d/converters/event-to-message.js index af44c84..31caef0 100644 --- a/src/m2d/converters/event-to-message.js +++ b/src/m2d/converters/event-to-message.js @@ -894,7 +894,8 @@ async function eventToMessage(event, guild, channel, di) { let preNode if (node.nodeType === 3 && node.nodeValue.includes("```") && (preNode = nodeIsChildOf(node, ["PRE"]))) { if (preNode.firstChild?.nodeName === "CODE") { - const ext = preNode.firstChild.className.match(/language-(\S+)/)?.[1] || "txt" + let ext = preNode.firstChild.className.match(/language-(\S+)/)?.[1] + if (!dUtils.supportedPlaintextPreviewExtensions.has(ext)) ext = "txt" const filename = `inline_code.${ext}` // Build the replacement node const replacementCode = doc.createElement("code") diff --git a/src/m2d/converters/event-to-message.test.js b/src/m2d/converters/event-to-message.test.js index bc73df7..68d519a 100644 --- a/src/m2d/converters/event-to-message.test.js +++ b/src/m2d/converters/event-to-message.test.js @@ -1155,6 +1155,38 @@ test("event2message: code blocks are uploaded as attachments instead if they con ) }) +test("event2message: code blocks are uploaded as attachments instead if they contain incompatible backticks (use txt extension if discord does not recognise the language)", async t => { + t.deepEqual( + await eventToMessage({ + type: "m.room.message", + sender: "@cadence:cadence.moe", + content: { + msgtype: "m.text", + body: "wrong body", + format: "org.matrix.custom.html", + formatted_body: 'So if you run code like this
System.out.println("```");
it should print a markdown formatted code block' + }, + event_id: "$pGkWQuGVmrPNByrFELxhzI6MCBgJecr5I2J3z88Gc2s", + room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe" + }), + { + ensureJoined: [], + messagesToDelete: [], + messagesToEdit: [], + messagesToSend: [{ + username: "cadence [they]", + content: "So if you run code like this `[inline_code.txt]` it should print a markdown formatted code block", + attachments: [{id: "0", filename: "inline_code.txt"}], + pendingFiles: [{name: "inline_code.txt", buffer: Buffer.from('System.out.println("```");')}], + avatar_url: undefined, + allowed_mentions: { + parse: ["users", "roles"] + } + }] + } + ) +}) + test("event2message: code blocks are uploaded as attachments instead if they contain incompatible backticks (default to txt file extension)", async t => { t.deepEqual( await eventToMessage({ diff --git a/src/m2d/event-dispatcher.js b/src/m2d/event-dispatcher.js index 3d1e090..c11b696 100644 --- a/src/m2d/event-dispatcher.js +++ b/src/m2d/event-dispatcher.js @@ -211,7 +211,7 @@ async event => { // @ts-ignore await matrixCommandHandler.execute(event) } - retrigger.finishedBridging(event.event_id) + retrigger.messageFinishedBridging(event.event_id) await api.ackEvent(event) })) @@ -222,7 +222,7 @@ sync.addTemporaryListener(as, "type:m.sticker", guard("m.sticker", async event => { if (utils.eventSenderIsFromDiscord(event.sender)) return const messageResponses = await sendEvent.sendEvent(event) - retrigger.finishedBridging(event.event_id) + retrigger.messageFinishedBridging(event.event_id) await api.ackEvent(event) })) diff --git a/src/matrix/read-registration.js b/src/matrix/read-registration.js index 86f99a1..d1243a7 100644 --- a/src/matrix/read-registration.js +++ b/src/matrix/read-registration.js @@ -78,10 +78,14 @@ function readRegistration() { /** @type {import("../types").AppServiceRegistrationConfig} */ // @ts-ignore let reg = readRegistration() -fs.watch(registrationFilePath, {persistent: false}, () => { - let newReg = readRegistration() - Object.assign(reg, newReg) -}) +if (reg) { + fs.watch(registrationFilePath, {persistent: false}, () => { + let newReg = readRegistration() + if (newReg) { + Object.assign(reg, newReg) + } + }) +} module.exports.registrationFilePath = registrationFilePath module.exports.readRegistration = readRegistration diff --git a/src/stdin.js b/src/stdin.js index 43f9607..2548d42 100644 --- a/src/stdin.js +++ b/src/stdin.js @@ -15,15 +15,12 @@ const mreq = sync.require("./matrix/mreq") const api = sync.require("./matrix/api") const file = sync.require("./matrix/file") const sendEvent = sync.require("./m2d/actions/send-event") -const redact = sync.require("./m2d/actions/redact") const eventDispatcher = sync.require("./d2m/event-dispatcher") const updatePins = sync.require("./d2m/actions/update-pins") const speedbump = sync.require("./d2m/actions/speedbump") const ks = sync.require("./matrix/kstate") const setPresence = sync.require("./d2m/actions/set-presence") const channelWebhook = sync.require("./m2d/actions/channel-webhook") -const dUtils = sync.require("./discord/utils") -const mxUtils = sync.require("./matrix/utils") const guildID = "112760669178241024" async function ping() { diff --git a/src/web/pug/agi-optout.pug b/src/web/pug/agi-optout.pug deleted file mode 100644 index 795e675..0000000 --- a/src/web/pug/agi-optout.pug +++ /dev/null @@ -1,24 +0,0 @@ -extends includes/template.pug - -block body - h1.ta-center.fs-display2.fc-green-400 April Fools! - .ws7.m-auto - .s-prose.fs-body2 - p Sheesh, wouldn't that be horrible? - if guild_id - p Fake AI messages have now been #[strong.fc-green-600 deactivated for everyone in your server.] - p Hope the prank entertained you. #[a(href="https://cadence.moe/contact") Send love or hate mail here.] - - h2 What actually happened? - ul - li A secret event was added for the duration of 1st April 2026 (UTC). - li If a message matches a preset pattern, a preset response is posted to chat by an AI-ified profile of the previous author. - li It only happens at most once per hour in each server. - li I tried to design it to not interrupt any serious/sensitive topics. I am deeply sorry if that didn't work out. - li No AI generated materials have ever been used in Out Of Your Element: no code, no prose, no images, no jokes. - li It'll always deactivate itself on 2nd April, no matter what, and I'll remove the relevant code shortly after. - if guild_id - .s-prose.fl-grow1.mt16 - p If you thought it was funny, feel free to opt back in. This affects the entire server, so please be courteous. - form(method="post" action=rel(`/agi/optin?guild_id=${guild_id}`)) - button(type="submit").s-btn.s-btn__muted Opt back in diff --git a/src/web/pug/agi.pug b/src/web/pug/agi.pug deleted file mode 100644 index 029c02a..0000000 --- a/src/web/pug/agi.pug +++ /dev/null @@ -1,41 +0,0 @@ -extends includes/template.pug - -block title - title AGI in Discord - -block body - style. - .ai-gradient { - background: linear-gradient(100deg, #fb72f2, #072ea4); - color: transparent; - background-clip: text; - } - - h1.ta-center.fs-display2.ai-gradient AGI in Discord:#[br]Revolutionizing the Future of Communications - .ws7.m-auto - .s-prose.fs-body2 - p In the ever-changing and turbulent world of AI, it's crucial to always be one step ahead. - p That's why Out Of Your Element has partnered with #[strong Grimace AI] to provide you tomorrow's technology, today. - ul - li #[strong Always online:] Miss your friends when they log off? Now you can talk to facsimiles of them etched into an unfeeling machine, always and forever! - li #[strong Smarter than ever:] Pissed off when somebody says something #[em wrong] on the internet? Frustrated with having to stay up all night correcting them? With Grimace Truth (available in Pro+ Ultra plan), all information is certified true to reality, so you'll never have to worry about those frantic Google searches at 3 AM. - li #[strong Knows you better than yourself:] We aren't just training on your data; we're copying minds into our personality matrix — including yours. Do you find yourself enjoying the sound of your own voice more than anything else? Our unique simulation of You is here to help. - - h1.mt64.mb32 Frequently Asked Questions - .s-link-preview - .s-link-preview--header.fd-column - .s-link-preview--title.fs-title.pl4 How to opt out? - .s-link-preview--details.fc-red-500 - != icons.Icons.IconFire - = ` 20,000% higher search volume for this question in the last hour` - .s-link-preview--body - .s-prose - h2.fs-body3 Is this really goodbye? 😢😢😢😢😢 - p I can't convince you to stay? - p As not just a customer service representative but someone with a shared vision, I simply want you to know that everyone at Grimace AI will miss all the time that we've shared together with you. - form(method="post" action=(guild_id ? rel(`/agi/optout?guild_id=${guild_id}`) : rel("/agi/optout"))).d-flex.g4.mt16 - button(type="button").s-btn.s-btn__filled Nevermind, I'll stay :) - button(type="submit").s-btn.s-btn__danger.s-btn__outlined Opt out for 3 days - - - div(style="height: 200px") diff --git a/src/web/pug/includes/template.pug b/src/web/pug/includes/template.pug index be1d005..86680eb 100644 --- a/src/web/pug/includes/template.pug +++ b/src/web/pug/includes/template.pug @@ -65,8 +65,7 @@ mixin define-themed-button(name, theme) doctype html html(lang="en") head - block title - title Out Of Your Element + title Out Of Your Element link(rel="stylesheet" type="text/css" href=rel("/static/stacks.min.css")) //- Please use responsibly!!!!! diff --git a/src/web/routes/agi.js b/src/web/routes/agi.js deleted file mode 100644 index f899455..0000000 --- a/src/web/routes/agi.js +++ /dev/null @@ -1,36 +0,0 @@ -// @ts-check - -const {z} = require("zod") -const {defineEventHandler, getValidatedQuery, sendRedirect} = require("h3") -const {as, from, sync, db} = require("../../passthrough") - -/** @type {import("../pug-sync")} */ -const pugSync = sync.require("../pug-sync") - -const schema = { - opt: z.object({ - guild_id: z.string().regex(/^[0-9]+$/) - }) -} - -as.router.get("/agi", defineEventHandler(async event => { - return pugSync.render(event, "agi.pug", {}) -})) - -as.router.get("/agi/optout", defineEventHandler(async event => { - return pugSync.render(event, "agi-optout.pug", {}) -})) - -as.router.post("/agi/optout", defineEventHandler(async event => { - const parseResult = await getValidatedQuery(event, schema.opt.safeParse) - if (parseResult.success) { - db.prepare("INSERT OR IGNORE INTO agi_optout (guild_id) VALUES (?)").run(parseResult.data.guild_id) - } - return sendRedirect(event, "", 302) -})) - -as.router.post("/agi/optin", defineEventHandler(async event => { - const {guild_id} = await getValidatedQuery(event, schema.opt.parse) - db.prepare("DELETE FROM agi_optout WHERE guild_id = ?").run(guild_id) - return sendRedirect(event, `../agi?guild_id=${guild_id}`, 302) -})) diff --git a/src/web/server.js b/src/web/server.js index 85fa1cb..837e14d 100644 --- a/src/web/server.js +++ b/src/web/server.js @@ -125,7 +125,6 @@ as.router.get("/icon.png", defineEventHandler(async event => { pugSync.createRoute(as.router, "/ok", "ok.pug") -sync.require("./routes/agi") sync.require("./routes/download-matrix") sync.require("./routes/download-discord") sync.require("./routes/guild-settings") diff --git a/test/data.js b/test/data.js index cc054cf..f3092bc 100644 --- a/test/data.js +++ b/test/data.js @@ -2035,6 +2035,80 @@ module.exports = { tts: false } }, + reply_to_member_join: { + type: 19, + content: "when the broke friend who we pay to bring food shows up at the medieval lord party", + mentions: [], + mention_roles: [], + attachments: [], + embeds: [], + timestamp: "2026-03-30T12:11:04.443000+00:00", + edited_timestamp: null, + flags: 0, + components: [], + id: "1488148556962332692", + channel_id: "475599038536744962", + author: { + id: "576945009408999426", + username: "randomllama121", + avatar: "08510a70f957106dad1580323c40cd7a", + discriminator: "0", + public_flags: 128, + flags: 128, + banner: null, + accent_color: null, + global_name: "random :3", + avatar_decoration_data: null, + collectibles: null, + display_name_styles: null, + banner_color: null, + clan: null, + primary_guild: null + }, + pinned: false, + mention_everyone: false, + tts: false, + message_reference: { + type: 0, + channel_id: "475599038536744962", + message_id: "1488146734352826478", + guild_id: "475599038536744960" + }, + referenced_message: { + type: 7, + content: "", + mentions: [], + mention_roles: [], + attachments: [], + embeds: [], + timestamp: "2026-03-30T12:03:49.899000+00:00", + edited_timestamp: null, + flags: 0, + components: [], + id: "1488146734352826478", + channel_id: "475599038536744962", + author: { + id: "1461677775554478161", + username: "peasant321_76775", + avatar: null, + discriminator: "0", + public_flags: 0, + flags: 0, + banner: null, + accent_color: null, + global_name: "PEASANT!!", + avatar_decoration_data: null, + collectibles: null, + display_name_styles: null, + banner_color: null, + clan: null, + primary_guild: null + }, + pinned: false, + mention_everyone: false, + tts: false + } + }, attachment_no_content: { id: "1124628646670389348", type: 0, diff --git a/test/test.js b/test/test.js index 70625a0..4cd9627 100644 --- a/test/test.js +++ b/test/test.js @@ -175,5 +175,4 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not require("../src/web/routes/log-in-with-matrix.test") require("../src/web/routes/oauth.test") require("../src/web/routes/password.test") - require("../src/agi/generator.test") })()