From cf39737b5a7de3df774f66b29b69c09e64e87ded Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 4 Aug 2025 18:09:39 +1200 Subject: [PATCH] Move to util --- src/d2m/converters/message-to-event.js | 14 +++----------- src/discord/utils.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/d2m/converters/message-to-event.js b/src/d2m/converters/message-to-event.js index 5af7487..527ba1d 100644 --- a/src/d2m/converters/message-to-event.js +++ b/src/d2m/converters/message-to-event.js @@ -491,19 +491,11 @@ async function messageToEvent(message, guild, options = {}, di) { } else { // repliedToUnknownEvent // This reply can't point to the Matrix event because it isn't bridged, we need to indicate this. assert(message.referenced_message) - const dateDifference = new Date(message.timestamp).getTime() - new Date(message.referenced_message.timestamp).getTime() - const oneHour = 60 * 60 * 1000 - if (dateDifference < oneHour) { - var dateDisplay = "n" - } else if (dateDifference < 25 * oneHour) { - var dateDisplay = ` ${Math.floor(dateDifference / oneHour)}-hour-old` - } else { - var dateDisplay = ` ${Math.round(dateDifference / (24 * oneHour))}-day-old` - } - html = `
In reply to a${dateDisplay} unbridged message from ${repliedToDisplayName}:` + const dateDisplay = dUtils.howOldUnbridgedMessage(message.referenced_message.timestamp, message.timestamp) + html = `
In reply to ${dateDisplay} from ${repliedToDisplayName}:` + `
${repliedToHtml}
` + html - body = (`In reply to a${dateDisplay} unbridged message:\n${repliedToDisplayName}: ` + body = (`In reply to ${dateDisplay}:\n${repliedToDisplayName}: ` + repliedToBody).split("\n").map(line => "> " + line).join("\n") + "\n\n" + body } diff --git a/src/discord/utils.js b/src/discord/utils.js index dea05ae..963f0b8 100644 --- a/src/discord/utils.js +++ b/src/discord/utils.js @@ -136,6 +136,24 @@ function getPublicUrlForCdn(url) { return `${reg.ooye.bridge_origin}/download/discord${match[1]}/${match[2]}/${match[3]}/${match[4]}` } +/** + * @param {string} oldTimestamp + * @param {string} newTimestamp + * @returns {string} "a x-day-old unbridged message" + */ +function howOldUnbridgedMessage(oldTimestamp, newTimestamp) { + const dateDifference = new Date(newTimestamp).getTime() - new Date(oldTimestamp).getTime() + const oneHour = 60 * 60 * 1000 + if (dateDifference < oneHour) { + return "an unbridged message" + } else if (dateDifference < 25 * oneHour) { + var dateDisplay = `a ${Math.floor(dateDifference / oneHour)}-hour-old unbridged message` + } else { + var dateDisplay = `a ${Math.round(dateDifference / (24 * oneHour))}-day-old unbridged message` + } + return dateDisplay +} + module.exports.getPermissions = getPermissions module.exports.hasPermission = hasPermission module.exports.hasSomePermissions = hasSomePermissions @@ -145,3 +163,4 @@ module.exports.isEphemeralMessage = isEphemeralMessage module.exports.snowflakeToTimestampExact = snowflakeToTimestampExact module.exports.timestampToSnowflakeInexact = timestampToSnowflakeInexact module.exports.getPublicUrlForCdn = getPublicUrlForCdn +module.exports.howOldUnbridgedMessage = howOldUnbridgedMessage