Don't delete our reaction on Discord unless we have 0 of that reaction coming from Matrix #85
3 changed files with 32 additions and 1 deletions
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
const {EventEmitter} = require("events")
|
const {EventEmitter} = require("events")
|
||||||
const passthrough = require("../../passthrough")
|
const passthrough = require("../../passthrough")
|
||||||
const {select} = passthrough
|
const {select, sync} = passthrough
|
||||||
|
/** @type {import("../../matrix/utils")} */
|
||||||
|
const utils = sync.require("../../matrix/utils")
|
||||||
|
|
||||||
const DEBUG_RETRIGGER = false
|
const DEBUG_RETRIGGER = false
|
||||||
|
|
||||||
|
|
@ -57,6 +59,30 @@ function eventNotFoundThenRetrigger(inputID, fn, ...rest) {
|
||||||
return true // event was not found, then retrigger
|
return true // event was not found, then retrigger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function reactionNotFoundThenRetrigger(reactionEventID, fn, ...rest){
|
||||||
|
const reactionEventHash = utils.getEventIDHash(reactionEventID)
|
||||||
|
const reaction = select("reaction", "encoded_emoji", {hashed_event_id: reactionEventHash})
|
||||||
|
if (reaction) {
|
||||||
|
debugRetrigger(`[retrigger] OK eid <-> reaction = ${reactionEventID} <-> ${reactionEventHash}`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
debugRetrigger(`[retrigger] WAIT id = ${reactionEventID}`)
|
||||||
|
emitter.once(reactionEventID, () => {
|
||||||
|
debugRetrigger(`[retrigger] TRIGGER id = ${reactionEventID}`)
|
||||||
|
fn(...rest)
|
||||||
|
})
|
||||||
|
// if the event never arrives, don't trigger the callback, just clean up
|
||||||
|
setTimeout(() => {
|
||||||
|
if (emitter.listeners(reactionEventID).length) {
|
||||||
|
debugRetrigger(`[retrigger] EXPIRE id = ${reactionEventID}`)
|
||||||
|
}
|
||||||
|
emitter.removeAllListeners(reactionEventID)
|
||||||
|
}, 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.
|
* Anything calling retrigger during the callback will be paused and retriggered after the callback resolves.
|
||||||
* @template T
|
* @template T
|
||||||
|
|
@ -88,5 +114,6 @@ function messageFinishedBridging(messageID) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.eventNotFoundThenRetrigger = eventNotFoundThenRetrigger
|
module.exports.eventNotFoundThenRetrigger = eventNotFoundThenRetrigger
|
||||||
|
module.exports.reactionNotFoundThenRetrigger = reactionNotFoundThenRetrigger
|
||||||
module.exports.messageFinishedBridging = messageFinishedBridging
|
module.exports.messageFinishedBridging = messageFinishedBridging
|
||||||
module.exports.pauseChanges = pauseChanges
|
module.exports.pauseChanges = pauseChanges
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,8 @@ 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)
|
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.messageFinishedBridging(event.event_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.addReaction = addReaction
|
module.exports.addReaction = addReaction
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,8 @@ async function suppressEmbeds(event) {
|
||||||
* @param {Ty.Event.Outer_M_Room_Redaction} event
|
* @param {Ty.Event.Outer_M_Room_Redaction} event
|
||||||
*/
|
*/
|
||||||
async function removeReaction(event) {
|
async function removeReaction(event) {
|
||||||
|
if (retrigger.reactionNotFoundThenRetrigger(event.redacts, () => as.emit("type:m.room.redaction", event))) return
|
||||||
|
|
||||||
const hash = utils.getEventIDHash(event.redacts)
|
const hash = utils.getEventIDHash(event.redacts)
|
||||||
const row = from("reaction").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")
|
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()
|
.select("reference_channel_id", "message_id", "encoded_emoji").where({hashed_event_id: hash}).get()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue