From 8439512f1aaf619e5e007e11c02128e53b909c16 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 23 Nov 2023 15:51:25 +1300 Subject: [PATCH] Add snowflake timestamp converter functions --- .../message-to-event.embeds.test.js | 26 ------------------- discord/utils.js | 14 ++++++++++ discord/utils.test.js | 23 ++++++++++++++++ m2d/converters/event-to-message.test.js | 3 ++- 4 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 discord/utils.test.js diff --git a/d2m/converters/message-to-event.embeds.test.js b/d2m/converters/message-to-event.embeds.test.js index 173e016..b339687 100644 --- a/d2m/converters/message-to-event.embeds.test.js +++ b/d2m/converters/message-to-event.embeds.test.js @@ -3,32 +3,6 @@ const {messageToEvent} = require("./message-to-event") const data = require("../../test/data") const Ty = require("../../types") -/** - * @param {string} roomID - * @param {string} eventID - * @returns {(roomID: string, eventID: string) => Promise>} - */ -function mockGetEvent(t, roomID_in, eventID_in, outer) { - return async function(roomID, eventID) { - t.equal(roomID, roomID_in) - t.equal(eventID, eventID_in) - return new Promise(resolve => { - setTimeout(() => { - resolve({ - event_id: eventID_in, - room_id: roomID_in, - origin_server_ts: 1680000000000, - unsigned: { - age: 2245, - transaction_id: "$local.whatever" - }, - ...outer - }) - }) - }) - } -} - test("message2event embeds: nothing but a field", async t => { const events = await messageToEvent(data.message_with_embeds.nothing_but_a_field, data.guild.general, {}) t.deepEqual(events, [{ diff --git a/discord/utils.js b/discord/utils.js index 8856aa7..ceb4468 100644 --- a/discord/utils.js +++ b/discord/utils.js @@ -2,6 +2,8 @@ const DiscordTypes = require("discord-api-types/v10") +const EPOCH = 1420070400000 + /** * @param {string[]} userRoles * @param {DiscordTypes.APIGuild["roles"]} guildRoles @@ -56,5 +58,17 @@ function isWebhookMessage(message) { return message.webhook_id && !isInteractionResponse } +/** @param {string} snowflake */ +function snowflakeToTimestampExact(snowflake) { + return Number(BigInt(snowflake) >> 22n) + EPOCH +} + +/** @param {number} timestamp */ +function timestampToSnowflakeInexact(timestamp) { + return String((timestamp - EPOCH) * 2**22) +} + module.exports.getPermissions = getPermissions module.exports.isWebhookMessage = isWebhookMessage +module.exports.snowflakeToTimestampExact = snowflakeToTimestampExact +module.exports.timestampToSnowflakeInexact = timestampToSnowflakeInexact diff --git a/discord/utils.test.js b/discord/utils.test.js new file mode 100644 index 0000000..fd064ef --- /dev/null +++ b/discord/utils.test.js @@ -0,0 +1,23 @@ +const {test} = require("supertape") +const data = require("../test/data") +const utils = require("./utils") + +test("is webhook message: identifies bot interaction response as not a message", t => { + t.equal(utils.isWebhookMessage(data.interaction_message.thinking_interaction), false) +}) + +test("is webhook message: identifies webhook interaction response as not a message", t => { + t.equal(utils.isWebhookMessage(data.interaction_message.thinking_interaction_without_bot_user), false) +}) + +test("is webhook message: identifies webhook message as a message", t => { + t.equal(utils.isWebhookMessage(data.special_message.bridge_echo_webhook), true) +}) + +test("discord utils: converts snowflake to timestamp", t => { + t.equal(utils.snowflakeToTimestampExact("86913608335773696"), 1440792219004) +}) + +test("discerd utils: converts timestamp to snowflake", t => { + t.match(utils.timestampToSnowflakeInexact(1440792219004), /^869136083357.....$/) +}) diff --git a/m2d/converters/event-to-message.test.js b/m2d/converters/event-to-message.test.js index 8bd4223..1af5e42 100644 --- a/m2d/converters/event-to-message.test.js +++ b/m2d/converters/event-to-message.test.js @@ -2168,7 +2168,8 @@ test("event2message: guessed @mentions may join members to mention", async t => const subtext = { user: { id: "321876634777218072", - username: "subtext", + username: "subtextual", + global_name: "subtext", discriminator: "0" } }